public Subquery PersistenceParamMap(IMapping mapToUse, object fromObject)
        {
            // TODO: add mapping direction to all param functions, dont' assume outbound
            MappingContext context = mapToUse.CreateContext(this.PersistenceAction.GetAdapter(PersistenceAdapterPurpose.Parameters, MappingDirection.Outbound), this);

            mapToUse.Apply(context);

            return(this);
        }
Beispiel #2
0
        public void Import(TextReader reader)
        {
            var csv = new CsvReader(reader);

            mapping.Apply(csv.Configuration);

            var records = csv.GetRecords <Record>();

            int n        = 1;
            var keywords = new List <MetadataKeyword>();

            try
            {
                foreach (var record in records)
                {
                    var result = recordService.Insert(record, userInfo);

                    if (!result.Success)
                    {
                        if (!SkipBadRecords)
                        {
                            throw new ImportException(String.Format("Import failed due to validation errors at record {0}: {1}",
                                                                    n, result.Validation.Errors.ToConcatenatedString(e => e.Message, "; ")));
                        }
                    }

                    n++;
                    Results.Add(result);

                    keywords.AddRange(result.Record.Gemini.Keywords);
                }
            }
            catch (CsvHelperException ex)
            {
                string info = (string)ex.Data["CsvHelper"];
                throw new ImportException("CsvHelper exception: " + info, ex);
            }

            // import new vocabs and keywords
            foreach (var vocab in mapping.RequiredVocabularies)
            {
                // if vocab already exists, Insert returns a VocabularyServiceResult with Success=false, which we just ignore
                vocabularyService.Insert(vocab);
            }
            vocabularyService.AddKeywordsToExistingControlledVocabs(keywords);
        }
        void IMapping.InnerApply(MappingContext c)
        {
            var context = (MappingContext <T>)c;

            // .......................
            // GET ALL MAPPINGS

            List <IMapping> mappingsToApply = null;

            foreach (IMapping mapping in ((IMapping)this).GetAllMappings(includeBase: true))
            {
                if (mappingsToApply == null)
                {
                    mappingsToApply = new List <IMapping>();
                }

                mappingsToApply.Add(mapping);
            }

            // .......................
            // APPLY CHILD MAPPINGS

            Dictionary <IEntityProperty, object> propertyValues = null;
            bool hasChildMappings = false;

            // Apply current mappings
            for (int i = 0; i < mappingsToApply.Count; i++)
            {
                IMapping mapping = mappingsToApply[i];

                // Ignore subquery mappings that aren't the current mapping
                if (mapping is ISubqueryMapping && context.CurrentSubquery.Mapping != mapping)
                {
                    continue;
                }

                MappingContext currentContext = mapping is IChildMapping?
                                                mapping.CreateContext(context) :
                                                    context;

                mapping.Apply(currentContext);
                if (context.DoBreak)
                {
                    break;
                }

                if (mapping is IChildMapping)
                {
                    // If a target has been specified, save it
                    if (currentContext.IsTargetSet)
                    {
                        hasChildMappings = true;

                        if (mapping is IPropertyMapping)
                        {
                            if (propertyValues == null)
                            {
                                propertyValues = new Dictionary <IEntityProperty, object>();
                            }

                            var propertyMapping = (IPropertyMapping)mapping;
                            propertyValues.Add(propertyMapping.Property, currentContext.Target);
                        }
                        else if (mapping is IVariableMapping)
                        {
                            var variableMapping = (IVariableMapping)mapping;
                            context.SetVariable(variableMapping.Variable, currentContext.Target);
                        }
                    }
                }

                // There might be derived mappings at this point, find them
                if (i == mappingsToApply.Count - 1 && context.TargetType != null)
                {
                    IEntityDefinition definition = this.EntitySpace.GetDefinition(context.TargetType);
                    if (definition != null)
                    {
                        // TODO: better way to indicate which derived mapping to use - possibly by name
                        IMapping derived = null;
                        foreach (IMapping d in definition.Mappings.Where(m => m.BaseMapping == this))
                        {
                            derived = d;
                            break;
                        }

                        // Get all inherit
                        if (derived != null)
                        {
                            mappingsToApply.AddRange(derived.GetAllMappings(includeBase: true, untilBase: typeof(T)));
                        }
                    }
                }
            }

            // .......................
            // GET FROM CACHE

            bool isEntity    = this.EntitySpace.GetDefinition <T>() != null;
            bool shouldCache = isEntity && this.CacheIdentity != null && propertyValues != null && this.CacheIdentity.HasValidValues(propertyValues);

            if (shouldCache)
            {
                Identity cacheId = this.CacheIdentity.IdentityFromValues(propertyValues);

                object val = context.Cache.Get <T>(cacheId);
                if (val != null)
                {
                    // Update cache
                    context.Target = (T)val;
                    context.Cache.Update(this.CacheIdentity, propertyValues);
                    shouldCache = false;
                }
            }

            // .......................
            // INSTANTIATION

            // Instantiate target if it hasn't been specifically applied and there are sub mappings that have executed
            if (!context.IsTargetSet && hasChildMappings)
            {
                // Instantiate a new target
                context.Target = context.TargetType != null ?
                                 (T)Activator.CreateInstance(context.TargetType) :
                                 Activator.CreateInstance <T>();
            }

            // .......................
            // PUT IN CACHE

            if (context.IsTargetSet && propertyValues != null)
            {
                // Apply properties
                foreach (var propVal in propertyValues)
                {
                    propVal.Key.SetValue(context.Target, propVal.Value);
                }

                // Add to cache
                if (shouldCache)
                {
                    context.Cache.Put <T>(this.CacheIdentity, context.Target, propertyValues.Keys.ToArray());
                }
            }
        }