예제 #1
0
        internal virtual bool TryMapConfiguredFields(ImportEntityPipelineArgs args)
        {
            if (args.EntityMappingItem == null)
            {
                args.EntityMappingItem = this._cmpHelper.GetEntityMappingItem(args);
            }

            Assert.IsNotNull((object)args.EntityMappingItem,
                             "Could not find any Entity Mapping item for the Entity Type (Schema): " + args.ContentTypeIdentifier);
            bool flag = false;

            foreach (Item obj in args.EntityMappingItem.Children.Where <Item>((Func <Item, bool>)(i =>
                                                                                                  i.TemplateID == Sitecore.Connector.CMP.Constants.RelationFieldMappingTemplateId)))
            {
                string fieldName = obj[Sitecore.Connector.CMP.Constants.FieldMappingSitecoreFieldNameFieldId];
                string str       = obj[Sitecore.Connector.CMP.Constants.FieldMappingCmpFieldNameFieldId];

                if (!string.IsNullOrEmpty(fieldName))
                {
                    if (!string.IsNullOrEmpty(str))
                    {
                        try
                        {
                            if (obj.TemplateID == Sitecore.Connector.CMP.Constants.RelationFieldMappingTemplateId)
                            {
                                string cmpRelationName =
                                    obj[
                                        Sitecore.Connector.CMP.Constants
                                        .RelationFieldMappingCmpRelationFieldNameFieldId];

                                if (string.IsNullOrEmpty(cmpRelationName))
                                {
                                    this.Logger.Error(
                                        BaseHelper.GetLogMessageText(
                                            DemoCmpFieldMapping._settings.LogMessageTitle,
                                            string.Format(
                                                "Configuration of the field mapping '{0}' is incorrect. Required fields are not specified.",
                                                (object)obj.ID)), (object)this);
                                    flag = true;
                                    continue;
                                }

                                if (args.Item.Fields[fieldName].Type == "CmpMultiList")
                                {
                                    Log.Debug("DEMO CUSTOMIZATION: CmpMultiList field '" + args.Item.Fields[fieldName].Name + "' initial value: " + args.Item[fieldName], this);
                                    args.Item[fieldName] = GetListfieldValue(args.Item[fieldName],
                                                                             args.Item.Fields[fieldName].Source, args.Item.Database);
                                    Log.Debug("DEMO CUSTOMIZATION: CmpMultiList field '" + args.Item.Fields[fieldName].Name + "' edited with: " + args.Item[fieldName], this);
                                }
                                else
                                {
                                    List <string> stringList =
                                        this._cmpHelper.TryMapRelationPropertyValues(args, cmpRelationName, str);
                                    args.Item[fieldName] = stringList.Count != 0
                                        ? string.Join(
                                        DemoCmpFieldMapping._settings.RelationFieldMappingSeparator,
                                        (IEnumerable <string>)stringList)
                                        : string.Empty;
                                }
                                continue;
                            }

                            args.Item[fieldName] = this._mapper.Convert(args.EntityDefinition, str,
                                                                        args.Entity.GetPropertyValue(str));
                            continue;
                        }
                        catch (Exception ex)
                        {
                            this.Logger.Error(BaseHelper.GetLogMessageText(
                                                  DemoCmpFieldMapping._settings.LogMessageTitle,
                                                  $"An error occurred during converting '{(object)str}' field to '{(object)fieldName}' field. Field mapping ID: '{(object)obj.ID}'."),
                                              ex, (object)this);
                            flag           = true;
                            args.Exception = ex;
                            continue;
                        }
                    }
                }

                this.Logger.Error(
                    BaseHelper.GetLogMessageText(DemoCmpFieldMapping._settings.LogMessageTitle,
                                                 $"Configuration of the field mapping '{(object)obj.ID}' is incorrect. Required fields are not specified."), (object)this);
                flag = true;
            }

            return(!flag);
        }