Ejemplo n.º 1
0
        public CustomFieldDataEntityBase GetObjectCustomFieldData(ObjectCustomField objectCustomField)
        {
            try
            {
                CustomFieldDataEntityBase ret = null;

                if (objectCustomField == null)
                {
                    return(null);
                }

                //Determine CustomFieldCategoryEnum
                string customFieldTypeString =
                    objectCustomField.ObjectTypeCustomFieldId.CustomFieldId.CustomFieldTypeId.Code;

                CustomFieldTypeEnum customFieldTypeEnum;

                bool isParseSuccess =
                    Enum.TryParse <CustomFieldTypeEnum>(customFieldTypeString, out customFieldTypeEnum);

                if (!isParseSuccess)
                {
                    throw new Exception(String.Format("Not supported the CustomFieldType '{0}'", customFieldTypeString));
                }

                var customFieldData =
                    objectCustomField.ObjectCustomFieldDatas.Select(r => r.CustomFieldDataId);

                if (customFieldData == null || customFieldData.Count() == 0)
                {
                    return(null);
                }

                ObjectCustomFieldData objectCustomFieldData = customFieldData.First().ObjectCustomFieldDatas.First();

                CustomFieldTypeFlag customFieldTypeFlag =
                    CustomFieldTypeFlag.Parse(objectCustomField.CustomFieldType);

                #region User defined list data type
                if (customFieldTypeEnum == CustomFieldTypeEnum.MULTI_CHOICE_LIST ||
                    customFieldTypeEnum == CustomFieldTypeEnum.SINGLE_CHOICE_LIST)
                {
                    ret = new UserDefinedListCustomFieldDataEntity(
                        objectCustomFieldData.ObjectCustomFieldId.ObjectCustomFieldId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectTypeCustomFieldId.ObjectTypeCustomFieldId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectId.ObjectId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectTypeCustomFieldId.CustomFieldId.CustomFieldId,
                        customFieldTypeFlag,
                        CustomFieldCategoryEnum.LIST,
                        customFieldData.Select(r => r.CustomFieldDataId)
                        );
                }
                #endregion

                #region Basic data type
                else if (customFieldTypeEnum == CustomFieldTypeEnum.DATETIME ||
                         customFieldTypeEnum == CustomFieldTypeEnum.FLOAT ||
                         customFieldTypeEnum == CustomFieldTypeEnum.INTEGER ||
                         customFieldTypeEnum == CustomFieldTypeEnum.STRING)
                {
                    BasicCustomFieldTypeEnum basicType =
                        (BasicCustomFieldTypeEnum)Enum.Parse(typeof(BasicCustomFieldTypeEnum),
                                                             Enum.GetName(typeof(CustomFieldTypeEnum), customFieldTypeEnum));
                    object dataValue = null;

                    switch (customFieldTypeEnum)
                    {
                    case CustomFieldTypeEnum.STRING:
                        basicType = BasicCustomFieldTypeEnum.STRING;
                        dataValue = ((CustomFieldDataString)customFieldData.First()).StringValue;
                        break;

                    case CustomFieldTypeEnum.DATETIME:
                        basicType = BasicCustomFieldTypeEnum.DATETIME;
                        dataValue = ((CustomFieldDataDateTime)customFieldData.First()).DateTimeValue;
                        break;

                    case CustomFieldTypeEnum.FLOAT:
                        basicType = BasicCustomFieldTypeEnum.FLOAT;
                        dataValue = ((CustomFieldDataFloat)customFieldData.First()).FloatValue;
                        break;

                    case CustomFieldTypeEnum.INTEGER:
                        basicType = BasicCustomFieldTypeEnum.INTEGER;
                        dataValue = ((CustomFieldDataInt)customFieldData.First()).IntValue;
                        break;
                    }

                    ret = new BasicCustomFieldDataEntity(
                        objectCustomFieldData.ObjectCustomFieldId.ObjectCustomFieldId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectTypeCustomFieldId.ObjectTypeCustomFieldId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectId.ObjectId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectTypeCustomFieldId.CustomFieldId.CustomFieldId,
                        customFieldTypeFlag,
                        CustomFieldCategoryEnum.BASIC,
                        dataValue,
                        basicType);
                }
                #endregion Basic data type

                #region Predefinition data type
                //Predefinition data type
                else
                {
                    string predefinitionTypeString = ((PredefinitionData)customFieldData.First()).PredefinitionType;
                    ret = new PredefinitionCustomFieldEntity(
                        objectCustomFieldData.ObjectCustomFieldId.ObjectCustomFieldId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectTypeCustomFieldId.ObjectTypeCustomFieldId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectId.ObjectId,
                        objectCustomFieldData.ObjectCustomFieldId.ObjectTypeCustomFieldId.CustomFieldId.CustomFieldId,
                        customFieldTypeFlag,
                        CustomFieldCategoryEnum.BUILT_IN,
                        customFieldData.Select(r => ((PredefinitionData)r).RefId),
                        predefinitionTypeString
                        );
                }
                #endregion Predefinition data type

                return(ret);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        public void CopyCustomFieldData(Guid sourceCMSObjectId, Guid targetCMSObjectId, bool isOnlyGetReadOnly)
        {
            ObjectCustomFieldBO objectCustomFieldBO = null;
            Session             session             = null;

            try
            {
                session = XpoHelper.GetNewSession();
                //Get source object
                NAS.DAL.CMS.ObjectDocument.Object sourceCMSObject =
                    session.GetObjectByKey <NAS.DAL.CMS.ObjectDocument.Object>(sourceCMSObjectId);

                if (sourceCMSObject == null)
                {
                    throw new Exception("Source object cannot be null");
                }

                NAS.DAL.CMS.ObjectDocument.Object targetCMSObject =
                    session.GetObjectByKey <NAS.DAL.CMS.ObjectDocument.Object>(targetCMSObjectId);

                if (targetCMSObject == null)
                {
                    throw new Exception("Target object cannot be null");
                }

                if (!sourceCMSObject.ObjectTypeId.Equals(targetCMSObject.ObjectTypeId))
                {
                    throw new Exception("Object type of the source and the target is different");
                }

                if (isOnlyGetReadOnly)
                {
                    int countReadOnlyData =
                        sourceCMSObject.ObjectCustomFields.Count(r =>
                                                                 !r.CustomFieldType.Equals(Utility.CustomFieldTypeConstant.CUSTOM_FIELD_TYPE_DEFAULT));
                    if (countReadOnlyData == 0)
                    {
                        return;
                    }
                }

                IEnumerable <CustomFieldDataEntityBase> sourceCMSObjectData =
                    GetCustomFieldData(sourceCMSObjectId, isOnlyGetReadOnly);

                objectCustomFieldBO = new ObjectCustomFieldBO();

                //Update target's custom fields before copy data from source
                UpdateCustomFields(targetCMSObject.ObjectId);

                foreach (var sourceData in sourceCMSObjectData)
                {
                    ObjectCustomField targetObjectCustomField = targetCMSObject.ObjectCustomFields
                                                                .Where(r => r.ObjectTypeCustomFieldId.ObjectTypeCustomFieldId
                                                                       == sourceData.ObjectTypeCustomFieldId)
                                                                .First();

                    BasicCustomFieldDataEntity           tempBasicDataEntity         = null;
                    UserDefinedListCustomFieldDataEntity tempUserDefinedListEntity   = null;
                    PredefinitionCustomFieldEntity       tempPredefinitionDataEntity = null;

                    switch (sourceData.CustomFieldCategory)
                    {
                    case CustomFieldCategoryEnum.BASIC:
                        tempBasicDataEntity = (BasicCustomFieldDataEntity)sourceData;
                        //Update basic data
                        objectCustomFieldBO.UpdateBasicData(
                            targetObjectCustomField.ObjectCustomFieldId,
                            tempBasicDataEntity.BasicDataValue,
                            tempBasicDataEntity.BasicCustomFieldType,
                            tempBasicDataEntity.ObjectCustomFieldFlag);
                        break;

                    case CustomFieldCategoryEnum.LIST:
                        tempUserDefinedListEntity = (UserDefinedListCustomFieldDataEntity)sourceData;
                        //Update user defined list data
                        objectCustomFieldBO.UpdateUserDefinedListData(
                            targetObjectCustomField.ObjectCustomFieldId,
                            tempUserDefinedListEntity.UserDefinedItemIds,
                            tempUserDefinedListEntity.ObjectCustomFieldFlag);
                        break;

                    case CustomFieldCategoryEnum.BUILT_IN:
                        tempPredefinitionDataEntity = (PredefinitionCustomFieldEntity)sourceData;
                        //Update predefinition data
                        PredefinitionCustomFieldTypeEnum predefinitionCustomFieldTypeEnum;
                        bool isValidPredefinitionType =
                            Enum.TryParse <PredefinitionCustomFieldTypeEnum>(
                                tempPredefinitionDataEntity.PredefinitionType,
                                out predefinitionCustomFieldTypeEnum);
                        if (!isValidPredefinitionType)
                        {
                            throw new Exception("Invalid predeninition type");
                        }

                        objectCustomFieldBO.UpdatePredefinitionData(
                            targetObjectCustomField.ObjectCustomFieldId,
                            tempPredefinitionDataEntity.PredefinitionRefIds,
                            predefinitionCustomFieldTypeEnum,
                            tempPredefinitionDataEntity.ObjectCustomFieldFlag);
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }