示例#1
0
        private ResponceApplyChanges ApplyDbChanges(ResponceApplyChanges response, EntitySchema entitySchema, bool isBild = false)
        {
            try {
                DBMetaScript           dbMetaScript = UserConnection.DBMetaScript;
                DBMetaActionCollection actions      = GetDbMetaActions(entitySchema);
                dbMetaScript.CheckActions(actions);
                DBMetaScriptValidationMessageCollection validationMessages = dbMetaScript.ValidationMessages;
                if (validationMessages.HasErrors())
                {
                    response.Code    = "Error.DbMetaActions.Get";
                    response.success = false;
                    return(response);
                }
                dbMetaScript.ExecuteActions(actions);
                if (validationMessages.HasErrors())
                {
                    response.Code    = "Error.DbMetaActions.Execute";
                    response.success = false;
                    return(response);
                }
            } catch (Exception e) {
                response.success = false;
                response.Code    = "Error.DbMetaActions.Apply";
                response.Message = e.Message;
                return(response);
            }
            if (response.success && isBild)
            {
                try {
#if !NETSTANDARD2_0 // TODO CRM-46783
                    var workspaceBuilder = WorkspaceBuilderUtility.CreateWorkspaceBuilder(AppConnection);
                    CompilerErrorCollection compilerErrors = workspaceBuilder.Build(AppConnection.Workspace);
                    if (compilerErrors.HasErrors)
                    {
                        response.success = false;
                        response.Code    = "Error.DbMetaActions.BuildError";
                        return(response);
                    }
#endif
                } catch (Exception) {
                    response.success = false;
                    response.Code    = "Error.DbMetaActions.Build";
                    return(response);
                }
            }
            return(response);
        }
示例#2
0
        private ResponceApplyChanges ApplyColumnsChanges(Guid entitySchemaId, List <ChangedColumn> changedColumns)
        {
            ResponceApplyChanges responce = new ResponceApplyChanges();

            responce.success = true;

            var entitySchemaManager = UserConnection.EntitySchemaManager;

            string baseCaption      = string.Empty;
            var    baseEntitySchema = entitySchemaManager.FindItemByUId(entitySchemaId);

            if (baseEntitySchema != null)
            {
                baseCaption = baseEntitySchema.Caption;
            }
            ISchemaManagerItem designSchemaItem = entitySchemaManager.DesignItemInCustomPackage(UserConnection, entitySchemaId);

            if (baseCaption != string.Empty)
            {
                designSchemaItem.Caption = baseCaption;
            }
            var entitySchema = designSchemaItem.Instance as EntitySchema;

            if (baseCaption != string.Empty)
            {
                entitySchema.Caption = baseCaption;
            }

            Dictionary <string, string>       newLookupsCollection         = new Dictionary <string, string>();
            Dictionary <string, EntitySchema> newLookupsEntitiesCollection = new Dictionary <string, EntitySchema>();

            var newLookups = changedColumns.Where(c => c.IsNewLookup && entitySchema.Columns.FindByName(c.Name) == null);

            if (newLookups != null)
            {
                foreach (ChangedColumn item in newLookups)
                {
                    var dictionaryEntitySchema = entitySchemaManager.FindItemByName(item.ReferenceSchemaName);
                    if (dictionaryEntitySchema != null)
                    {
                        responce.Code    = "Error.Dictionary.Exists";
                        responce.Message = item.ReferenceSchemaName;
                        responce.success = false;
                        return(responce);
                    }
                    newLookupsCollection.Add(item.ReferenceSchemaName, item.ReferenceSchemaCaption);
                }
            }
            if (newLookupsCollection.Count > 0)
            {
                var sysFolderId = (new Select(UserConnection)
                                   .Column("Id")
                                   .From("SysSchemaFolder")
                                   .Where("ParentId").IsNull() as Select)
                                  .ExecuteScalar <Guid>();
                var baseLookupEntitySchema = entitySchemaManager.GetInstanceByName("BaseLookup");
                var sysLookup       = entitySchemaManager.GetInstanceByName("SysLookup");
                var customPackageId = WorkspaceUtilities.ForceGetCustomPackageUId(UserConnection);
                foreach (var item in newLookupsCollection)
                {
                    var    lookupEntitySchemaItemUId = Guid.NewGuid();
                    object schemaNamePrefix;
                    var    lookupSchemaName = item.Key;
                    if (SysSettings.TryGetValue(UserConnection, "SchemaNamePrefix", out schemaNamePrefix) &&
                        !schemaNamePrefix.Equals(string.Empty))
                    {
                        lookupSchemaName = string.Concat(schemaNamePrefix, lookupSchemaName);
                    }
                    ISchemaManagerItem <EntitySchema> lookupEntitySchemaItem = entitySchemaManager.CreateSchema(lookupSchemaName,
                                                                                                                baseLookupEntitySchema, UserConnection, lookupEntitySchemaItemUId, false);
                    lookupEntitySchemaItem.Caption = item.Value;
                    var lookupEntitySchema = (EntitySchema)lookupEntitySchemaItem.Instance;
                    lookupEntitySchema.Caption = item.Value;
                    entitySchemaManager.SaveDesignedItemInSessionData(lookupEntitySchemaItem);
                    entitySchemaManager.SaveDesignedItemFolderIdInSessionData(UserConnection, lookupEntitySchemaItemUId, sysFolderId);
                    entitySchemaManager.SaveDesignedItemPackageUIdInSessionData(UserConnection, lookupEntitySchemaItemUId, customPackageId);
                    entitySchemaManager.SaveSchema(lookupEntitySchemaItemUId, UserConnection);
                    try {
                        responce = ApplyDbChanges(responce, lookupEntitySchema);
                    } catch (Exception) {
                        responce.success = false;
                        responce.Code    = "Error.DbMetaActions.LookupApply";
                        return(responce);
                    }
                    newLookupsEntitiesCollection.Add(item.Key, lookupEntitySchema);
                    Entity sysLookupEntity = sysLookup.CreateEntity(UserConnection);
                    sysLookupEntity.SetColumnValue("Id", Guid.NewGuid());
                    sysLookupEntity.SetColumnValue("Name", item.Value);
                    sysLookupEntity.SetColumnValue("Description", string.Empty);
                    sysLookupEntity.SetColumnValue("SysFolderId", new Guid("80AB12C2-F36B-1410-A883-16D83CAB0980"));                     //// SysLookupFolder - General
                    sysLookupEntity.SetColumnValue("SysEntitySchemaUId", lookupEntitySchema.UId);
                    sysLookupEntity.SetColumnValue("ProcessListeners", 0);
                    sysLookupEntity.Save();
                }
            }

            foreach (ChangedColumn item in changedColumns)
            {
                EntitySchemaColumn column = entitySchema.Columns.FindByName(item.Name);
                if (column == null)
                {
                    DataValueType dataValueType = GetDataValueTypeByTypeName(item.TypeGroupName, item.TypeName);
                    column                      = entitySchema.Columns.CreateItem();
                    column.UId                  = Guid.NewGuid();
                    column.UsageType            = EntitySchemaColumnUsageType.General;
                    column.Name                 = item.Name;
                    column.DataValueType        = dataValueType;
                    column.DataValueTypeManager = dataValueType.DataValueTypeManager;
                    column.DataValueTypeUId     = dataValueType.UId;
                    if (dataValueType.IsLookup || dataValueType.IsMultiLookup)
                    {
                        EntitySchema referenceEntitySchema;
                        if (item.IsNewLookup)
                        {
                            referenceEntitySchema = newLookupsEntitiesCollection.First(l => l.Key == item.ReferenceSchemaName).Value;
                        }
                        else
                        {
                            referenceEntitySchema = entitySchemaManager.GetInstanceByName(item.ReferenceSchemaName);
                        }
                        column.ReferenceSchema    = referenceEntitySchema;
                        column.ReferenceSchemaUId = referenceEntitySchema.UId;
                    }
                    entitySchema.Columns.Add(column);
                }
                column.Caption         = item.Caption;
                column.IsSimpleLookup  = item.IsEnum;
                column.RequirementType = item.IsRequired ?
                                         EntitySchemaColumnRequirementType.ApplicationLevel : EntitySchemaColumnRequirementType.None;
                column.IsMultiLineText = item.IsMultiline;
            }

            entitySchemaManager.SaveDesignedItemInSessionData(UserConnection, entitySchema as MetaSchema, entitySchema.UId);

            try {
                entitySchemaManager.SaveSchema(entitySchema.UId, UserConnection, false);
                responce         = ApplyDbChanges(responce, entitySchema, true);
                responce.success = true;
            } catch (Exception) {
                responce.success = false;
                responce.Code    = "Error.DbMetaActions.EntitySchemaApply";
            }
            return(responce);
        }