Exemplo n.º 1
0
        public IActionResult UpdateEntityRelationRecord([FromBody]InputEntityRelationRecordUpdateModel model)
        {
            var recMan = new RecordManager(service);
            var entMan = new EntityManager(service.StorageService);
            BaseResponseModel response = new BaseResponseModel { Timestamp = DateTime.UtcNow, Success = true, Errors = new List<ErrorModel>() };

            if (model == null)
            {
                response.Errors.Add(new ErrorModel { Message = "Invalid model." });
                response.Success = false;
                return DoResponse(response);
            }

            EntityRelation relation = null;
            if (string.IsNullOrWhiteSpace(model.RelationName))
            {
                response.Errors.Add(new ErrorModel { Message = "Invalid relation name.", Key = "relationName" });
                response.Success = false;
                return DoResponse(response);
            }
            else
            {
                relation = new EntityRelationManager(service.StorageService).Read(model.RelationName).Object;
                if (relation == null)
                {
                    response.Errors.Add(new ErrorModel { Message = "Invalid relation name. No relation with that name.", Key = "relationName" });
                    response.Success = false;
                    return DoResponse(response);
                }
            }

            var originEntity = entMan.ReadEntity(relation.OriginEntityId).Object;
            var targetEntity = entMan.ReadEntity(relation.TargetEntityId).Object;
            var originField = originEntity.Fields.Single(x => x.Id == relation.OriginFieldId);
            var targetField = targetEntity.Fields.Single(x => x.Id == relation.TargetFieldId);

            if (model.DetachTargetFieldRecordIds != null && model.DetachTargetFieldRecordIds.Any() && targetField.Required && relation.RelationType != EntityRelationType.ManyToMany)
            {
                response.Errors.Add(new ErrorModel { Message = "Cannot detach records, when target field is required.", Key = "originFieldRecordId" });
                response.Success = false;
                return DoResponse(response);
            }

            EntityQuery query = new EntityQuery(originEntity.Name, "*", EntityQuery.QueryEQ("id", model.OriginFieldRecordId), null, null, null);
            QueryResponse result = recMan.Find(query);
            if (result.Object.Data.Count == 0)
            {
                response.Errors.Add(new ErrorModel { Message = "Origin record was not found. Id=[" + model.OriginFieldRecordId + "]", Key = "originFieldRecordId" });
                response.Success = false;
                return DoResponse(response);
            }

            var originRecord = result.Object.Data[0];
            object originValue = originRecord[originField.Name];

            List<EntityRecord> attachTargetRecords = new List<EntityRecord>();
            List<EntityRecord> detachTargetRecords = new List<EntityRecord>();

            foreach (var targetId in model.AttachTargetFieldRecordIds)
            {
                query = new EntityQuery(targetEntity.Name, "*", EntityQuery.QueryEQ("id", targetId), null, null, null);
                result = recMan.Find(query);
                if (result.Object.Data.Count == 0)
                {
                    response.Errors.Add(new ErrorModel { Message = "Attach target record was not found. Id=[" + targetEntity + "]", Key = "targetRecordId" });
                    response.Success = false;
                    return DoResponse(response);
                }
                else if (attachTargetRecords.Any(x => (Guid)x["id"] == targetId))
                {
                    response.Errors.Add(new ErrorModel { Message = "Attach target id was duplicated. Id=[" + targetEntity + "]", Key = "targetRecordId" });
                    response.Success = false;
                    return DoResponse(response);
                }
                attachTargetRecords.Add(result.Object.Data[0]);
            }

            foreach (var targetId in model.DetachTargetFieldRecordIds)
            {
                query = new EntityQuery(targetEntity.Name, "*", EntityQuery.QueryEQ("id", targetId), null, null, null);
                result = recMan.Find(query);
                if (result.Object.Data.Count == 0)
                {
                    response.Errors.Add(new ErrorModel { Message = "Detach target record was not found. Id=[" + targetEntity + "]", Key = "targetRecordId" });
                    response.Success = false;
                    return DoResponse(response);
                }
                else if (attachTargetRecords.Any(x => (Guid)x["id"] == targetId))
                {
                    response.Errors.Add(new ErrorModel { Message = "Detach target id was duplicated. Id=[" + targetEntity + "]", Key = "targetRecordId" });
                    response.Success = false;
                    return DoResponse(response);
                }
                detachTargetRecords.Add(result.Object.Data[0]);
            }

            var transaction = recMan.CreateTransaction();
            try
            {

                transaction.Begin();

                switch (relation.RelationType)
                {
                    case EntityRelationType.OneToOne:
                    case EntityRelationType.OneToMany:
                        {
                            foreach (var record in detachTargetRecords)
                            {
                                record[targetField.Name] = null;

                                var updResult = recMan.UpdateRecord(targetEntity, record);
                                if (!updResult.Success)
                                {
                                    transaction.Rollback();
                                    response.Errors = updResult.Errors;
                                    response.Message = "Target record id=[" + record["id"] + "] detach operation failed.";
                                    response.Success = false;
                                    return DoResponse(response);
                                }
                            }

                            foreach (var record in attachTargetRecords)
                            {
                                record[targetField.Name] = originValue;

                                var updResult = recMan.UpdateRecord(targetEntity, record);
                                if (!updResult.Success)
                                {
                                    transaction.Rollback();
                                    response.Errors = updResult.Errors;
                                    response.Message = "Target record id=[" + record["id"] + "] attach operation failed.";
                                    response.Success = false;
                                    return DoResponse(response);
                                }
                            }
                        }
                        break;
                    case EntityRelationType.ManyToMany:
                        {
                            foreach (var record in detachTargetRecords)
                            {
                                QueryResponse updResult = recMan.RemoveRelationManyToManyRecord(relation.Id, (Guid)originValue, (Guid)record[targetField.Name]);

                                if (!updResult.Success)
                                {
                                    transaction.Rollback();
                                    response.Errors = updResult.Errors;
                                    response.Message = "Target record id=[" + record["id"] + "] detach operation failed.";
                                    response.Success = false;
                                    return DoResponse(response);
                                }
                            }

                            foreach (var record in attachTargetRecords)
                            {
                                QueryResponse updResult = recMan.CreateRelationManyToManyRecord(relation.Id, (Guid)originValue, (Guid)record[targetField.Name]);

                                if (!updResult.Success)
                                {
                                    transaction.Rollback();
                                    response.Errors = updResult.Errors;
                                    response.Message = "Target record id=[" + record["id"] + "] attach  operation failed.";
                                    response.Success = false;
                                    return DoResponse(response);
                                }
                            }
                        }
                        break;
                    default:
                        {
                            transaction.Rollback();
                            throw new Exception("Not supported relation type");
                        }
                }

                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null)
                    transaction.Rollback();

                response.Success = false;
                response.Message = ex.Message;
                return DoResponse(response);
            }

            return DoResponse(response);
        }
Exemplo n.º 2
0
        public void InitializeSystemEntities()
        {
            EntityResponse response = null;
            FieldResponse fieldResponse = null;
            EntityManager em = new EntityManager(StorageService);
            EntityRelationManager rm = new EntityRelationManager(StorageService);
            RecordManager recMan = new RecordManager(this);

            var transaction = recMan.CreateTransaction();

            try
            {

                transaction.Begin();

                IStorageSystemSettingsRepository systemSettingsRepository = StorageService.GetSystemSettingsRepository();
                IStorageSystemSettings storeSystemSettings = systemSettingsRepository.Read();

                Guid systemSettingsId = new Guid("F3223177-B2FF-43F5-9A4B-FF16FC67D186");
                SystemSettings systemSettings = new SystemSettings();
                systemSettings.Id = systemSettingsId;

                int currentVersion = 0;
                if (storeSystemSettings != null)
                {
                    systemSettings = new SystemSettings(storeSystemSettings);
                    currentVersion = systemSettings.Version;
                }

                EntityManager entityManager = new EntityManager(StorageService);

                //tmp code - during debug only
                //em.DeleteEntity(SystemIds.UserEntityId);
                //em.DeleteEntity(SystemIds.RoleEntityId);
                //rm.Delete(SystemIds.UserRoleRelationId);
                //currentVersion = 0;

                if (currentVersion < 150508)
                {
                    systemSettings.Version = 150508;

                    List<Guid> allowedRoles = new List<Guid>();
                    allowedRoles.Add(SystemIds.AdministratorRoleId);

                    #region << create role entity >>

                    {
                        InputEntity roleEntity = new InputEntity();
                        roleEntity.Id = SystemIds.RoleEntityId;
                        roleEntity.Name = "role";
                        roleEntity.Label = "Role";
                        roleEntity.LabelPlural = "Roles";
                        roleEntity.System = true;
                        roleEntity.RecordPermissions = new RecordPermissions();
                        roleEntity.RecordPermissions.CanRead = allowedRoles;
                        roleEntity.RecordPermissions.CanCreate = allowedRoles;
                        roleEntity.RecordPermissions.CanUpdate = allowedRoles;
                        roleEntity.RecordPermissions.CanDelete = allowedRoles;

                        response = entityManager.CreateEntity(roleEntity);

                        InputTextField nameRoleField = new InputTextField();

                        nameRoleField.Id = new Guid("36F91EBD-5A02-4032-8498-B7F716F6A349");
                        nameRoleField.Name = "name";
                        nameRoleField.Label = "Name";
                        nameRoleField.PlaceholderText = "";
                        nameRoleField.Description = "The name of the role";
                        nameRoleField.HelpText = "";
                        nameRoleField.Required = true;
                        nameRoleField.Unique = false;
                        nameRoleField.Searchable = false;
                        nameRoleField.Auditable = false;
                        nameRoleField.System = true;
                        nameRoleField.DefaultValue = "";

                        nameRoleField.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(roleEntity.Id.Value, nameRoleField, false);

                        InputTextField descriptionRoleField = new InputTextField();

                        descriptionRoleField.Id = new Guid("4A8B9E0A-1C36-40C6-972B-B19E2B5D265B");
                        descriptionRoleField.Name = "description";
                        descriptionRoleField.Label = "Description";
                        descriptionRoleField.PlaceholderText = "";
                        descriptionRoleField.Description = "";
                        descriptionRoleField.HelpText = "";
                        descriptionRoleField.Required = true;
                        descriptionRoleField.Unique = false;
                        descriptionRoleField.Searchable = false;
                        descriptionRoleField.Auditable = false;
                        descriptionRoleField.System = true;
                        descriptionRoleField.DefaultValue = "";

                        descriptionRoleField.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(roleEntity.Id.Value, descriptionRoleField, false);
                    }

                    #endregion

                    #region << create user entity >>
                    {

                        InputEntity userEntity = new InputEntity();
                        userEntity.Id = SystemIds.UserEntityId;
                        userEntity.Name = "user";
                        userEntity.Label = "User";
                        userEntity.LabelPlural = "Users";
                        userEntity.System = true;
                        userEntity.RecordPermissions = new RecordPermissions();
                        userEntity.RecordPermissions.CanRead = allowedRoles;
                        userEntity.RecordPermissions.CanCreate = allowedRoles;
                        userEntity.RecordPermissions.CanUpdate = allowedRoles;
                        userEntity.RecordPermissions.CanDelete = allowedRoles;

                        response = entityManager.CreateEntity(userEntity);

                        InputTextField firstName = new InputTextField();

                        firstName.Id = new Guid("DF211549-41CC-4D11-BB43-DACA4C164411");
                        firstName.Name = "first_name";
                        firstName.Label = "First Name";
                        firstName.PlaceholderText = "";
                        firstName.Description = "First name of the user";
                        firstName.HelpText = "";
                        firstName.Required = true;
                        firstName.Unique = false;
                        firstName.Searchable = false;
                        firstName.Auditable = false;
                        firstName.System = true;
                        firstName.DefaultValue = "";

                        firstName.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, firstName, false);

                        InputTextField lastName = new InputTextField();

                        lastName.Id = new Guid("63E685B1-B2C6-4961-B393-2B6723EBD1BF");
                        lastName.Name = "last_name";
                        lastName.Label = "Last Name";
                        lastName.PlaceholderText = "";
                        lastName.Description = "Last name of the user";
                        lastName.HelpText = "";
                        lastName.Required = true;
                        lastName.Unique = false;
                        lastName.Searchable = false;
                        lastName.Auditable = false;
                        lastName.System = true;
                        lastName.DefaultValue = "";

                        lastName.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, lastName, false);

                        InputEmailField email = new InputEmailField();

                        email.Id = new Guid("9FC75C8F-CE80-4A64-81D7-E2BEFA5E4815");
                        email.Name = "email";
                        email.Label = "Email";
                        email.PlaceholderText = "";
                        email.Description = "Email address of the user";
                        email.HelpText = "";
                        email.Required = true;
                        email.Unique = true;
                        email.Searchable = false;
                        email.Auditable = false;
                        email.System = true;
                        email.DefaultValue = "";

                        email.MaxLength = 255;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, email, false);

                        InputPasswordField password = new InputPasswordField();

                        password.Id = new Guid("4EDE88D9-217A-4462-9300-EA0D6AFCDCEA");
                        password.Name = "password";
                        password.Label = "Password";
                        password.PlaceholderText = "";
                        password.Description = "Password for the user account";
                        password.HelpText = "";
                        password.Required = true;
                        password.Unique = true;
                        password.Searchable = false;
                        password.Auditable = false;
                        password.System = true;
                        password.MinLength = 6;
                        password.MaxLength = 24;
                        password.Encrypted = true;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, password, false);

                        InputDateTimeField lastLoggedIn = new InputDateTimeField();

                        lastLoggedIn.Id = new Guid("3C85CCEC-D526-4E47-887F-EE169D1F508D");
                        lastLoggedIn.Name = "last_logged_in";
                        lastLoggedIn.Label = "Last Logged In";
                        lastLoggedIn.PlaceholderText = "";
                        lastLoggedIn.Description = "";
                        lastLoggedIn.HelpText = "";
                        lastLoggedIn.Required = false;
                        lastLoggedIn.Unique = true;
                        lastLoggedIn.Searchable = false;
                        lastLoggedIn.Auditable = true;
                        lastLoggedIn.System = true;
                        lastLoggedIn.DefaultValue = null;

                        lastLoggedIn.Format = "dd MMM yyyy HH:mm:ss";
                        lastLoggedIn.UseCurrentTimeAsDefaultValue = true;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, lastLoggedIn, false);

                        InputCheckboxField enabledField = new InputCheckboxField();

                        enabledField.Id = new Guid("C0C63650-7572-4252-8E4B-4E25C94897A6");
                        enabledField.Name = "enabled";
                        enabledField.Label = "Enabled";
                        enabledField.PlaceholderText = "";
                        enabledField.Description = "Shows if the user account is enabled";
                        enabledField.HelpText = "";
                        enabledField.Required = true;
                        enabledField.Unique = false;
                        enabledField.Searchable = false;
                        enabledField.Auditable = false;
                        enabledField.System = true;
                        enabledField.DefaultValue = false;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, enabledField, false);

                        InputCheckboxField verifiedUserField = new InputCheckboxField();

                        verifiedUserField.Id = new Guid("F1BA5069-8CC9-4E66-BCC3-60E33C79C265");
                        verifiedUserField.Name = "verified";
                        verifiedUserField.Label = "Verified";
                        verifiedUserField.PlaceholderText = "";
                        verifiedUserField.Description = "Shows if the user email is verified";
                        verifiedUserField.HelpText = "";
                        verifiedUserField.Required = true;
                        verifiedUserField.Unique = false;
                        verifiedUserField.Searchable = false;
                        verifiedUserField.Auditable = false;
                        verifiedUserField.System = true;
                        verifiedUserField.DefaultValue = false;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, verifiedUserField, false);

                    }

                    #endregion

                    #region << create user - role relation >>
                    {
                        var userEntity = em.ReadEntity(SystemIds.UserEntityId).Object;
                        var roleEntity = em.ReadEntity(SystemIds.RoleEntityId).Object;

                        EntityRelation userRoleRelation = new EntityRelation();
                        userRoleRelation.Id = SystemIds.UserRoleRelationId;
                        userRoleRelation.Name = "user_role";
                        userRoleRelation.Label = "User-Role";
                        userRoleRelation.System = true;
                        userRoleRelation.RelationType = EntityRelationType.ManyToMany;
                        userRoleRelation.TargetEntityId = userEntity.Id;
                        userRoleRelation.TargetFieldId = userEntity.Fields.Single(x => x.Name == "id").Id;
                        userRoleRelation.OriginEntityId = roleEntity.Id;
                        userRoleRelation.OriginFieldId = roleEntity.Fields.Single(x => x.Name == "id").Id;
                        {
                            var result = rm.Create(userRoleRelation);
                            if (!result.Success)
                                throw new Exception("CREATE USER-ROLE RELATION:" + result.Message);
                        }
                    }
                    #endregion

                    #region << create system records >>

                    {
                        EntityRecord user = new EntityRecord();
                        user["id"] = SystemIds.FirstUserId;
                        user["first_name"] = "WebVella";
                        user["last_name"] = "Erp";
                        user["password"] = "******";
                        user["email"] = "*****@*****.**";
                        user["created_by"] = SystemIds.FirstUserId;
                        user["last_modified_by"] = SystemIds.FirstUserId;
                        user["created_on"] = DateTime.UtcNow;
                        user["enabled"] = true;

                        QueryResponse result = recMan.CreateRecord("user", user);
                        if (!result.Success)
                            throw new Exception("CREATE FIRST USER RECORD:" + result.Message);
                    }

                    {
                        EntityRecord adminRole = new EntityRecord();
                        adminRole["id"] = SystemIds.AdministratorRoleId;
                        adminRole["name"] = "Administrator";
                        adminRole["description"] = "";
                        adminRole["created_by"] = SystemIds.FirstUserId;
                        adminRole["last_modified_by"] = SystemIds.FirstUserId;
                        adminRole["created_on"] = DateTime.UtcNow;

                        QueryResponse result = recMan.CreateRecord("role", adminRole);
                        if (!result.Success)
                            throw new Exception("CREATE ADMINITRATOR ROLE RECORD:" + result.Message);
                    }

                    {
                        EntityRecord regularRole = new EntityRecord();
                        regularRole["id"] = SystemIds.RegularRoleId;
                        regularRole["name"] = "Regular";
                        regularRole["description"] = "";
                        regularRole["created_by"] = SystemIds.FirstUserId;
                        regularRole["last_modified_by"] = SystemIds.FirstUserId;
                        regularRole["created_on"] = DateTime.UtcNow;

                        QueryResponse result = recMan.CreateRecord("role", regularRole);
                        if (!result.Success)
                            throw new Exception("CREATE REGULAR ROLE RECORD:" + result.Message);
                    }

                    {
                        EntityRecord guestRole = new EntityRecord();
                        guestRole["id"] = SystemIds.GuestRoleId;
                        guestRole["name"] = "Guest";
                        guestRole["description"] = "";
                        guestRole["created_by"] = SystemIds.FirstUserId;
                        guestRole["last_modified_by"] = SystemIds.FirstUserId;
                        guestRole["created_on"] = DateTime.UtcNow;

                        QueryResponse result = recMan.CreateRecord("role", guestRole);
                        if (!result.Success)
                            throw new Exception("CREATE GUEST ROLE RECORD:" + result.Message);
                    }

                    {
                        QueryResponse result = recMan.CreateRelationManyToManyRecord(SystemIds.UserRoleRelationId, SystemIds.AdministratorRoleId, SystemIds.FirstUserId);
                        if (!result.Success)
                            throw new Exception("CREATE FIRST-USER <-> ADMINISTRATOR ROLE RELATION RECORD:" + result.Message);

                        result = recMan.CreateRelationManyToManyRecord(SystemIds.UserRoleRelationId, SystemIds.RegularRoleId, SystemIds.FirstUserId);
                        if (!result.Success)
                            throw new Exception("CREATE FIRST-USER <-> REGULAR ROLE RELATION RECORD:" + result.Message);

                    }

                    #endregion

                    #region << create Area entity >>
                    {
                        InputEntity areaEntity = new InputEntity();
                        areaEntity.Id = SystemIds.AreaEntityId;
                        areaEntity.Name = "area";
                        areaEntity.Label = "Area";
                        areaEntity.LabelPlural = "areas";
                        areaEntity.System = true;
                        areaEntity.IconName = "folder";
                        areaEntity.Weight = 10;
                        areaEntity.RecordPermissions = new RecordPermissions();
                        areaEntity.RecordPermissions.CanRead = allowedRoles;
                        areaEntity.RecordPermissions.CanCreate = allowedRoles;
                        areaEntity.RecordPermissions.CanUpdate = allowedRoles;
                        areaEntity.RecordPermissions.CanDelete = allowedRoles;
                        {
                            var createResponse = entityManager.CreateEntity(areaEntity);
                            if (!createResponse.Success)
                                throw new Exception("System error 10330. Message:" + createResponse.Message);
                        }

                        InputTextField color = new InputTextField();
                        color.Id = new Guid("2B4AACD9-3C34-4C44-B3A3-8AFF1520CFF6");
                        color.Name = "color";
                        color.Label = "Color";
                        color.PlaceholderText = "";
                        color.Description = "";
                        color.HelpText = "";
                        color.Required = true;
                        color.Unique = false;
                        color.Searchable = false;
                        color.Auditable = false;
                        color.System = true;
                        color.DefaultValue = "teal";
                        color.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, color, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField label = new InputTextField();
                        label.Id = new Guid("F050E7A1-AFB7-4346-B57B-1F12B2BD5AE5");
                        label.Name = "label";
                        label.Label = "Label";
                        label.PlaceholderText = "";
                        label.Description = "";
                        label.HelpText = "";
                        label.Required = true;
                        label.Unique = false;
                        label.Searchable = false;
                        label.Auditable = false;
                        label.System = true;
                        label.DefaultValue = "Default";
                        label.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, label, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField iconName = new InputTextField();
                        iconName.Id = new Guid("5EA0C872-D219-4D94-9EFA-C5DA978D316B");
                        iconName.Name = "icon_name";
                        iconName.Label = "Icon Name";
                        iconName.PlaceholderText = "";
                        iconName.Description = "";
                        iconName.HelpText = "";
                        iconName.Required = true;
                        iconName.Unique = false;
                        iconName.Searchable = false;
                        iconName.Auditable = false;
                        iconName.System = true;
                        iconName.DefaultValue = "database";
                        iconName.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, iconName, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputNumberField weight = new InputNumberField();
                        weight.Id = new Guid("9B169431-6C31-4141-80EB-5844B8333E63");
                        weight.Name = "weight";
                        weight.Label = "Weight";
                        weight.PlaceholderText = "";
                        weight.Description = "";
                        weight.HelpText = "";
                        weight.Required = true;
                        weight.Unique = false;
                        weight.Searchable = false;
                        weight.Auditable = false;
                        weight.System = true;
                        weight.DefaultValue = 10;
                        weight.MinValue = 0;
                        weight.DecimalPlaces = 2;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, weight, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField subscriptions = new InputTextField();
                        subscriptions.Id = new Guid("288EA657-C12C-4AC1-B701-81D6F9F39363");
                        subscriptions.Name = "subscriptions";
                        subscriptions.Label = "Subscriptions JSON String";
                        subscriptions.PlaceholderText = "";
                        subscriptions.Description = "Stringified Array of subscription objects";
                        subscriptions.HelpText = "";
                        subscriptions.Required = false;
                        subscriptions.Unique = false;
                        subscriptions.Searchable = false;
                        subscriptions.Auditable = false;
                        subscriptions.System = true;
                        subscriptions.DefaultValue = null;
                        subscriptions.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, subscriptions, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField name = new InputTextField();
                        name.Id = new Guid("F297577B-073E-4D18-81F3-675C1AFB466D");
                        name.Name = "name";
                        name.Label = "Name";
                        name.PlaceholderText = "";
                        name.Description = "";
                        name.HelpText = "";
                        name.Required = true;
                        name.Unique = false;
                        name.Searchable = false;
                        name.Auditable = false;
                        name.System = true;
                        name.DefaultValue = "default";
                        name.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, name, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField roles = new InputTextField();
                        roles.Id = new Guid("8E486F76-D0C1-4D0E-8617-9EF868BF1C55");
                        roles.Name = "roles";
                        roles.Label = "Subscriptions JSON String";
                        roles.PlaceholderText = "";
                        roles.Description = "Stringified Array of roles that have access to this area";
                        roles.HelpText = "";
                        roles.Required = false;
                        roles.Unique = false;
                        roles.Searchable = false;
                        roles.Auditable = false;
                        roles.System = true;
                        roles.DefaultValue = null;
                        roles.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, roles, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                    }
                    #endregion
                }

                if (currentVersion < 150919)
                {
                    systemSettings.Version = 150919;
                    List<Guid> allowedRoles = new List<Guid>();
                    allowedRoles.Add(SystemIds.AdministratorRoleId);
                    Guid filterEntityId = new Guid("2a20e8b1-5713-4257-a860-b964a5cfb317");

                    #region << create filter >>
                    {
                        InputEntity filterEntity = new InputEntity();
                        filterEntity.Id = filterEntityId;
                        filterEntity.Name = "filter";
                        filterEntity.Label = "Filter";
                        filterEntity.LabelPlural = "Filters";
                        filterEntity.System = true;
                        filterEntity.IconName = "filter";
                        filterEntity.Weight = 100;
                        filterEntity.RecordPermissions = new RecordPermissions();
                        filterEntity.RecordPermissions.CanRead = allowedRoles;
                        filterEntity.RecordPermissions.CanCreate = allowedRoles;
                        filterEntity.RecordPermissions.CanUpdate = allowedRoles;
                        filterEntity.RecordPermissions.CanDelete = allowedRoles;
                        {
                            var createResponse = entityManager.CreateEntity(filterEntity);
                            if (!createResponse.Success)
                                throw new Exception("System error 10330. Message:" + createResponse.Message);
                        }

                        InputTextField filterId = new InputTextField();
                        filterId.Id = new Guid("597bd9f4-c534-4244-af88-b6970c6abb21");
                        filterId.Name = "filter_id";
                        filterId.Label = "Filter id";
                        filterId.PlaceholderText = "";
                        filterId.Description = "";
                        filterId.HelpText = "";
                        filterId.Required = true;
                        filterId.Unique = false;
                        filterId.Searchable = false;
                        filterId.Auditable = false;
                        filterId.System = true;
                        filterId.DefaultValue = "";
                        filterId.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(filterEntityId, filterId, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField fieldName = new InputTextField();
                        fieldName.Id = new Guid("e4094c4d-0fd3-44ef-9cc0-190ea48f7bca");
                        fieldName.Name = "field_name";
                        fieldName.Label = "Field name";
                        fieldName.PlaceholderText = "";
                        fieldName.Description = "";
                        fieldName.HelpText = "";
                        fieldName.Required = true;
                        fieldName.Unique = false;
                        fieldName.Searchable = false;
                        fieldName.Auditable = false;
                        fieldName.System = true;
                        fieldName.DefaultValue = "";
                        fieldName.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(filterEntityId, fieldName, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField relationName = new InputTextField();
                        relationName.Id = new Guid("2859082c-5356-4d13-8b7b-e8fe65417279");
                        relationName.Name = "relation_name";
                        relationName.Label = "Relation name";
                        relationName.PlaceholderText = "";
                        relationName.Description = "";
                        relationName.HelpText = "";
                        relationName.Required = true;
                        relationName.Unique = false;
                        relationName.Searchable = false;
                        relationName.Auditable = false;
                        relationName.System = true;
                        relationName.DefaultValue = "";
                        relationName.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(filterEntityId, relationName, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField values = new InputTextField();
                        values.Id = new Guid("ff7511ae-0117-41c6-9a03-08e3c6568c39");
                        values.Name = "values";
                        values.Label = "Values";
                        values.PlaceholderText = "";
                        values.Description = "array of encoded strings";
                        values.HelpText = "";
                        values.Required = true;
                        values.Unique = false;
                        values.Searchable = false;
                        values.Auditable = false;
                        values.System = true;
                        values.DefaultValue = "";
                        values.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(filterEntityId, values, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField helper = new InputTextField();
                        helper.Id = new Guid("9056b9fb-4a4a-456f-9c64-539eaf70b6b2");
                        helper.Name = "helper";
                        helper.Label = "Helper";
                        helper.PlaceholderText = "";
                        helper.Description = "stringified object that helps constructing the filter on the interface";
                        helper.HelpText = "";
                        helper.Required = true;
                        helper.Unique = false;
                        helper.Searchable = false;
                        helper.Auditable = false;
                        helper.System = true;
                        helper.DefaultValue = "";
                        helper.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(filterEntityId, helper, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField entityName = new InputTextField();
                        entityName.Id = new Guid("22f58779-4c91-4278-8412-88e1a55784e1");
                        entityName.Name = "entity_name";
                        entityName.Label = "Entity Name";
                        entityName.PlaceholderText = "";
                        entityName.Description = "";
                        entityName.HelpText = "";
                        entityName.Required = true;
                        entityName.Unique = false;
                        entityName.Searchable = false;
                        entityName.Auditable = false;
                        entityName.System = true;
                        entityName.DefaultValue = "";
                        entityName.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(filterEntityId, entityName, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField listName = new InputTextField();
                        listName.Id = new Guid("a2cb0965-fe51-42b0-a07d-765507da8865");
                        listName.Name = "list_name";
                        listName.Label = "List Name";
                        listName.PlaceholderText = "";
                        listName.Description = "";
                        listName.HelpText = "";
                        listName.Required = true;
                        listName.Unique = false;
                        listName.Searchable = false;
                        listName.Auditable = false;
                        listName.System = true;
                        listName.DefaultValue = "";
                        listName.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(filterEntityId, listName, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputDateTimeField lastUsedOn = new InputDateTimeField();

                        lastUsedOn.Id = new Guid("207cf27b-3595-41fd-b028-bcd47845ad25");
                        lastUsedOn.Name = "last_used_on";
                        lastUsedOn.Label = "Last used on";
                        lastUsedOn.PlaceholderText = "";
                        lastUsedOn.Description = "";
                        lastUsedOn.HelpText = "";
                        lastUsedOn.Required = true;
                        lastUsedOn.Unique = false;
                        lastUsedOn.Searchable = false;
                        lastUsedOn.Auditable = false;
                        lastUsedOn.System = true;
                        lastUsedOn.DefaultValue = null;
                        lastUsedOn.Format = "dd  MMM yyyy HH:mm:ss";
                        lastUsedOn.UseCurrentTimeAsDefaultValue = true;

                        {
                            var createResponse = entityManager.CreateField(filterEntityId, lastUsedOn, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        #region << match_type >>
                        InputSelectField matchType = new InputSelectField();
                        matchType.Id = new Guid("15416751-e66d-4b85-ae2f-974255f9c987");
                        matchType.Name = "match_type";
                        matchType.Label = "Match type";
                        matchType.PlaceholderText = "";
                        matchType.Description = "";
                        matchType.HelpText = "";
                        matchType.Required = true;
                        matchType.Unique = false;
                        matchType.Searchable = false;
                        matchType.Auditable = false;
                        matchType.System = true;
                        matchType.DefaultValue = "exact";
                        matchType.Options = null;
                        matchType.Options = new List<SelectFieldOption>
                            {
                                new SelectFieldOption(){ Key = "exact", Value = "Exact match" },
                                new SelectFieldOption(){ Key = "range", Value = "Range" },
                                new SelectFieldOption(){ Key = "period", Value = "Period" },
                                new SelectFieldOption(){ Key = "regex", Value = "Regex" }
                            };

                        {
                            var createResponse = entityManager.CreateField(filterEntityId, matchType, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10060. Entity: filter. Field: match_type" + " Message:" + createResponse.Message);
                        }
                        #endregion

                    }
                    #endregion

                }

                if (currentVersion < 150924)
                {
                    systemSettings.Version = 150924;
                    //User
                    #region << image >>
                    {
                        InputImageField imageField = new InputImageField();
                        imageField.Id = new Guid("bf199b74-4448-4f58-93f5-6b86d888843b");
                        imageField.Name = "image";
                        imageField.Label = "Image";
                        imageField.PlaceholderText = "";
                        imageField.Description = "";
                        imageField.HelpText = "";
                        imageField.Required = false;
                        imageField.Unique = false;
                        imageField.Searchable = false;
                        imageField.Auditable = false;
                        imageField.System = true;
                        imageField.DefaultValue = string.Empty;
                        imageField.EnableSecurity = false;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.UserEntityId, imageField, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10060. Entity: user. Field: image" + " Message:" + createResponse.Message);
                        }
                    }
                    #endregion
                }

                storeSystemSettings = systemSettingsRepository.Convert(systemSettings);
                systemSettingsRepository.Save(storeSystemSettings);

                //if (currentVersion == 150508) //update to 150510
                //{
                //    systemSettings.Version = 150510;

                //    storeSystemSettings = systemSettingsRepository.Convert(systemSettings);
                //    systemSettingsRepository.Save(storeSystemSettings);
                //}

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
        }
Exemplo n.º 3
0
        public void InitializeSystemEntities()
        {
            EntityResponse response = null;
            FieldResponse fieldResponse = null;
            EntityManager em = new EntityManager(StorageService);
            EntityRelationManager rm = new EntityRelationManager(StorageService);
            RecordManager recMan = new RecordManager(this);

            var transaction = recMan.CreateTransaction();

            try {

                transaction.Begin();

                IStorageSystemSettingsRepository systemSettingsRepository = StorageService.GetSystemSettingsRepository();
                IStorageSystemSettings storeSystemSettings = systemSettingsRepository.Read();

                Guid systemSettingsId = new Guid("F3223177-B2FF-43F5-9A4B-FF16FC67D186");
                SystemSettings systemSettings = new SystemSettings();
                systemSettings.Id = systemSettingsId;

                int currentVersion = 0;
                if (storeSystemSettings != null)
                {
                    systemSettings = new SystemSettings(storeSystemSettings);
                    currentVersion = systemSettings.Version;
                }

                EntityManager entityManager = new EntityManager(StorageService);

                //tmp code - during debug only
                //em.DeleteEntity(SystemIds.UserEntityId);
                //em.DeleteEntity(SystemIds.RoleEntityId);
                //rm.Delete(SystemIds.UserRoleRelationId);
                //currentVersion = 0;

                if (currentVersion < 150508)
                {
                    systemSettings.Version = 150508;

                    List<Guid> allowedRoles = new List<Guid>();
                    allowedRoles.Add(new Guid("F42EBA3B-6433-752B-6C34-B322A7B4CE7D"));

                    #region << create role entity >>

                    {
                        InputEntity roleEntity = new InputEntity();
                        roleEntity.Id = SystemIds.RoleEntityId;
                        roleEntity.Name = "role";
                        roleEntity.Label = "Role";
                        roleEntity.LabelPlural = "Roles";
                        roleEntity.System = true;
                        roleEntity.RecordPermissions = new RecordPermissions();
                        roleEntity.RecordPermissions.CanRead = allowedRoles;
                        roleEntity.RecordPermissions.CanCreate = allowedRoles;
                        roleEntity.RecordPermissions.CanUpdate = allowedRoles;
                        roleEntity.RecordPermissions.CanDelete = allowedRoles;

                        response = entityManager.CreateEntity(roleEntity);

                        InputTextField nameRoleField = new InputTextField();

                        nameRoleField.Id = new Guid("36F91EBD-5A02-4032-8498-B7F716F6A349");
                        nameRoleField.Name = "name";
                        nameRoleField.Label = "Name";
                        nameRoleField.PlaceholderText = "";
                        nameRoleField.Description = "The name of the role";
                        nameRoleField.HelpText = "";
                        nameRoleField.Required = true;
                        nameRoleField.Unique = false;
                        nameRoleField.Searchable = false;
                        nameRoleField.Auditable = false;
                        nameRoleField.System = true;
                        nameRoleField.DefaultValue = "";

                        nameRoleField.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(roleEntity.Id.Value, nameRoleField,false);

                        InputTextField descriptionRoleField = new InputTextField();

                        descriptionRoleField.Id = new Guid("4A8B9E0A-1C36-40C6-972B-B19E2B5D265B");
                        descriptionRoleField.Name = "description";
                        descriptionRoleField.Label = "Description";
                        descriptionRoleField.PlaceholderText = "";
                        descriptionRoleField.Description = "";
                        descriptionRoleField.HelpText = "";
                        descriptionRoleField.Required = true;
                        descriptionRoleField.Unique = false;
                        descriptionRoleField.Searchable = false;
                        descriptionRoleField.Auditable = false;
                        descriptionRoleField.System = true;
                        descriptionRoleField.DefaultValue = "";

                        descriptionRoleField.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(roleEntity.Id.Value, descriptionRoleField,false);
                    }

                    #endregion

                    #region << create user entity >>
                    {

                        InputEntity userEntity = new InputEntity();
                        userEntity.Id = SystemIds.UserEntityId;
                        userEntity.Name = "user";
                        userEntity.Label = "User";
                        userEntity.LabelPlural = "Users";
                        userEntity.System = true;
                        userEntity.RecordPermissions = new RecordPermissions();
                        userEntity.RecordPermissions.CanRead = allowedRoles;
                        userEntity.RecordPermissions.CanCreate = allowedRoles;
                        userEntity.RecordPermissions.CanUpdate = allowedRoles;
                        userEntity.RecordPermissions.CanDelete = allowedRoles;

                        response = entityManager.CreateEntity(userEntity);

                        InputTextField firstName = new InputTextField();

                        firstName.Id = new Guid("DF211549-41CC-4D11-BB43-DACA4C164411");
                        firstName.Name = "first_name";
                        firstName.Label = "First Name";
                        firstName.PlaceholderText = "";
                        firstName.Description = "First name of the user";
                        firstName.HelpText = "";
                        firstName.Required = true;
                        firstName.Unique = false;
                        firstName.Searchable = false;
                        firstName.Auditable = false;
                        firstName.System = true;
                        firstName.DefaultValue = "";

                        firstName.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, firstName,false);

                        InputTextField lastName = new InputTextField();

                        lastName.Id = new Guid("63E685B1-B2C6-4961-B393-2B6723EBD1BF");
                        lastName.Name = "last_name";
                        lastName.Label = "Last Name";
                        lastName.PlaceholderText = "";
                        lastName.Description = "Last name of the user";
                        lastName.HelpText = "";
                        lastName.Required = true;
                        lastName.Unique = false;
                        lastName.Searchable = false;
                        lastName.Auditable = false;
                        lastName.System = true;
                        lastName.DefaultValue = "";

                        lastName.MaxLength = 200;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, lastName,false);

                        InputEmailField email = new InputEmailField();

                        email.Id = new Guid("9FC75C8F-CE80-4A64-81D7-E2BEFA5E4815");
                        email.Name = "email";
                        email.Label = "Email";
                        email.PlaceholderText = "";
                        email.Description = "Email address of the user";
                        email.HelpText = "";
                        email.Required = true;
                        email.Unique = true;
                        email.Searchable = false;
                        email.Auditable = false;
                        email.System = true;
                        email.DefaultValue = "";

                        email.MaxLength = 255;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, email,false);

                        InputPasswordField password = new InputPasswordField();

                        password.Id = new Guid("4EDE88D9-217A-4462-9300-EA0D6AFCDCEA");
                        password.Name = "password";
                        password.Label = "Password";
                        password.PlaceholderText = "";
                        password.Description = "Password for the user account";
                        password.HelpText = "";
                        password.Required = true;
                        password.Unique = true;
                        password.Searchable = false;
                        password.Auditable = false;
                        password.System = true;
                        password.MinLength = 6;
                        password.MaxLength = 24;
                        password.Encrypted = true;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, password, false);

                        InputDateTimeField lastLoggedIn = new InputDateTimeField();

                        lastLoggedIn.Id = new Guid("3C85CCEC-D526-4E47-887F-EE169D1F508D");
                        lastLoggedIn.Name = "last_logged_in";
                        lastLoggedIn.Label = "Last Logged In";
                        lastLoggedIn.PlaceholderText = "";
                        lastLoggedIn.Description = "";
                        lastLoggedIn.HelpText = "";
                        lastLoggedIn.Required = false;
                        lastLoggedIn.Unique = true;
                        lastLoggedIn.Searchable = false;
                        lastLoggedIn.Auditable = true;
                        lastLoggedIn.System = true;
                        lastLoggedIn.DefaultValue = null;

                        lastLoggedIn.Format = "MM/dd/YYYY";
                        lastLoggedIn.UseCurrentTimeAsDefaultValue = true;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, lastLoggedIn, false);

                        InputCheckboxField enabledField = new InputCheckboxField();

                        enabledField.Id = new Guid("C0C63650-7572-4252-8E4B-4E25C94897A6");
                        enabledField.Name = "enabled";
                        enabledField.Label = "Enabled";
                        enabledField.PlaceholderText = "";
                        enabledField.Description = "Shows if the user account is enabled";
                        enabledField.HelpText = "";
                        enabledField.Required = true;
                        enabledField.Unique = false;
                        enabledField.Searchable = false;
                        enabledField.Auditable = false;
                        enabledField.System = true;
                        enabledField.DefaultValue = false;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, enabledField,false);

                        InputCheckboxField verifiedUserField = new InputCheckboxField();

                        verifiedUserField.Id = new Guid("F1BA5069-8CC9-4E66-BCC3-60E33C79C265");
                        verifiedUserField.Name = "verified";
                        verifiedUserField.Label = "Verified";
                        verifiedUserField.PlaceholderText = "";
                        verifiedUserField.Description = "Shows if the user email is verified";
                        verifiedUserField.HelpText = "";
                        verifiedUserField.Required = true;
                        verifiedUserField.Unique = false;
                        verifiedUserField.Searchable = false;
                        verifiedUserField.Auditable = false;
                        verifiedUserField.System = true;
                        verifiedUserField.DefaultValue = false;

                        fieldResponse = entityManager.CreateField(userEntity.Id.Value, verifiedUserField, false);
                    }

                    #endregion

                    #region << create user - role relation >>
                    {
                        var userEntity = em.ReadEntity(SystemIds.UserEntityId).Object;
                        var roleEntity = em.ReadEntity(SystemIds.RoleEntityId).Object;

                        EntityRelation userRoleRelation = new EntityRelation();
                        userRoleRelation.Id = SystemIds.UserRoleRelationId; ;
                        userRoleRelation.Name = "user_role";
                        userRoleRelation.Label = "User-Role";
                        userRoleRelation.System = true;
                        userRoleRelation.RelationType = EntityRelationType.ManyToMany;
                        userRoleRelation.TargetEntityId = userEntity.Id;
                        userRoleRelation.TargetFieldId = userEntity.Fields.Single(x => x.Name == "id").Id;
                        userRoleRelation.OriginEntityId = roleEntity.Id;
                        userRoleRelation.OriginFieldId = roleEntity.Fields.Single(x => x.Name == "id").Id;
                        {
                            var result = rm.Create(userRoleRelation);
                            if (!result.Success)
                                throw new Exception("CREATE USER-ROLE RELATION:" + result.Message);
                        }
                    }
                    #endregion

                    #region << create system records >>

                    {
                        EntityRecord user = new EntityRecord();
                        user["id"] = SystemIds.FirstUserId;
                        user["first_name"] = "WebVella";
                        user["last_name"] = "Erp";
                        user["password"] = "******";
                        user["email"] = "*****@*****.**";
                        user["created_by"] = SystemIds.FirstUserId;
                        user["last_modified_by"] = SystemIds.FirstUserId;
                        user["created_on"] = DateTime.UtcNow;

                        QueryResponse result = recMan.CreateRecord("user", user);
                        if (!result.Success)
                            throw new Exception("CREATE FIRST USER RECORD:" + result.Message);
                    }

                    {
                        EntityRecord adminRole = new EntityRecord();
                        adminRole["id"] = SystemIds.AdministratorRoleId;
                        adminRole["name"] = "Administrator";
                        adminRole["description"] = "";
                        adminRole["created_by"] = SystemIds.FirstUserId;
                        adminRole["last_modified_by"] = SystemIds.FirstUserId;
                        adminRole["created_on"] = DateTime.UtcNow;

                        QueryResponse result = recMan.CreateRecord("role", adminRole);
                        if (!result.Success)
                            throw new Exception("CREATE ADMINITRATOR ROLE RECORD:" + result.Message);
                    }

                    {
                        EntityRecord regularRole = new EntityRecord();
                        regularRole["id"] = SystemIds.RegularRoleId;
                        regularRole["name"] = "Regular";
                        regularRole["description"] = "";
                        regularRole["created_by"] = SystemIds.FirstUserId;
                        regularRole["last_modified_by"] = SystemIds.FirstUserId;
                        regularRole["created_on"] = DateTime.UtcNow;

                        QueryResponse result = recMan.CreateRecord("role", regularRole);
                        if (!result.Success)
                            throw new Exception("CREATE REGULAR ROLE RECORD:" + result.Message);
                    }

                    {
                        EntityRecord guestRole = new EntityRecord();
                        guestRole["id"] = SystemIds.GuestRoleId;
                        guestRole["name"] = "Guest";
                        guestRole["description"] = "";
                        guestRole["created_by"] = SystemIds.FirstUserId;
                        guestRole["last_modified_by"] = SystemIds.FirstUserId;
                        guestRole["created_on"] = DateTime.UtcNow;

                        QueryResponse result = recMan.CreateRecord("role", guestRole);
                        if (!result.Success)
                            throw new Exception("CREATE GUEST ROLE RECORD:" + result.Message);
                    }

                    {
                        QueryResponse result = recMan.CreateRelationManyToManyRecord(SystemIds.UserRoleRelationId, SystemIds.FirstUserId, SystemIds.AdministratorRoleId);
                        if (!result.Success)
                            throw new Exception("CREATE FIRST-USER <-> ADMINISTRATOR ROLE RELATION RECORD:" + result.Message);

                        result = recMan.CreateRelationManyToManyRecord(SystemIds.UserRoleRelationId, SystemIds.FirstUserId, SystemIds.RegularRoleId);
                        if (!result.Success)
                            throw new Exception("CREATE FIRST-USER <-> REGULAR ROLE RELATION RECORD:" + result.Message);

                        result = recMan.CreateRelationManyToManyRecord(SystemIds.UserRoleRelationId, SystemIds.FirstUserId, SystemIds.GuestRoleId);
                        if (!result.Success)
                            throw new Exception("CREATE FIRST-USER <-> GUEST ROLE RELATION RECORD:" + result.Message);
                    }

                    #endregion

                    #region << create Area entity >>
                    {
                        InputEntity areaEntity = new InputEntity();
                        areaEntity.Id = SystemIds.AreaEntityId;
                        areaEntity.Name = "area";
                        areaEntity.Label = "Area";
                        areaEntity.LabelPlural = "areas";
                        areaEntity.System = true;
                        areaEntity.IconName = "folder";
                        areaEntity.Weight = 10;
                        areaEntity.RecordPermissions = new RecordPermissions();
                        areaEntity.RecordPermissions.CanRead = allowedRoles;
                        areaEntity.RecordPermissions.CanCreate = allowedRoles;
                        areaEntity.RecordPermissions.CanUpdate = allowedRoles;
                        areaEntity.RecordPermissions.CanDelete = allowedRoles;
                        {
                            var createResponse = entityManager.CreateEntity(areaEntity);
                            if (!createResponse.Success)
                                throw new Exception("System error 10330. Message:" + createResponse.Message);
                        }

                        InputTextField color = new InputTextField();
                        color.Id = new Guid("2B4AACD9-3C34-4C44-B3A3-8AFF1520CFF6");
                        color.Name = "color";
                        color.Label = "Color";
                        color.PlaceholderText = "";
                        color.Description = "";
                        color.HelpText = "";
                        color.Required = true;
                        color.Unique = false;
                        color.Searchable = false;
                        color.Auditable = false;
                        color.System = true;
                        color.DefaultValue = "teal";
                        color.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, color, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField label = new InputTextField();
                        label.Id = new Guid("F050E7A1-AFB7-4346-B57B-1F12B2BD5AE5");
                        label.Name = "label";
                        label.Label = "Label";
                        label.PlaceholderText = "";
                        label.Description = "";
                        label.HelpText = "";
                        label.Required = true;
                        label.Unique = false;
                        label.Searchable = false;
                        label.Auditable = false;
                        label.System = true;
                        label.DefaultValue = "Default";
                        label.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, label, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField iconName = new InputTextField();
                        iconName.Id = new Guid("5EA0C872-D219-4D94-9EFA-C5DA978D316B");
                        iconName.Name = "icon_name";
                        iconName.Label = "Icon Name";
                        iconName.PlaceholderText = "";
                        iconName.Description = "";
                        iconName.HelpText = "";
                        iconName.Required = true;
                        iconName.Unique = false;
                        iconName.Searchable = false;
                        iconName.Auditable = false;
                        iconName.System = true;
                        iconName.DefaultValue = "database";
                        iconName.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, iconName, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputNumberField weight = new InputNumberField();
                        weight.Id = new Guid("9B169431-6C31-4141-80EB-5844B8333E63");
                        weight.Name = "weight";
                        weight.Label = "Weight";
                        weight.PlaceholderText = "";
                        weight.Description = "";
                        weight.HelpText = "";
                        weight.Required = true;
                        weight.Unique = false;
                        weight.Searchable = false;
                        weight.Auditable = false;
                        weight.System = true;
                        weight.DefaultValue = 10;
                        weight.MinValue = 0;
                        weight.DecimalPlaces = 2;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, weight, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField subscriptions = new InputTextField();
                        subscriptions.Id = new Guid("288EA657-C12C-4AC1-B701-81D6F9F39363");
                        subscriptions.Name = "subscriptions";
                        subscriptions.Label = "Subscriptions JSON String";
                        subscriptions.PlaceholderText = "";
                        subscriptions.Description = "Stringified Array of subscription objects";
                        subscriptions.HelpText = "";
                        subscriptions.Required = false;
                        subscriptions.Unique = false;
                        subscriptions.Searchable = false;
                        subscriptions.Auditable = false;
                        subscriptions.System = true;
                        subscriptions.DefaultValue = null;
                        subscriptions.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, subscriptions, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField name = new InputTextField();
                        name.Id = new Guid("F297577B-073E-4D18-81F3-675C1AFB466D");
                        name.Name = "name";
                        name.Label = "Name";
                        name.PlaceholderText = "";
                        name.Description = "";
                        name.HelpText = "";
                        name.Required = true;
                        name.Unique = false;
                        name.Searchable = false;
                        name.Auditable = false;
                        name.System = true;
                        name.DefaultValue = "default";
                        name.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, name, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                        InputTextField roles = new InputTextField();
                        roles.Id = new Guid("8E486F76-D0C1-4D0E-8617-9EF868BF1C55");
                        roles.Name = "roles";
                        roles.Label = "Subscriptions JSON String";
                        roles.PlaceholderText = "";
                        roles.Description = "Stringified Array of roles that have access to this area";
                        roles.HelpText = "";
                        roles.Required = false;
                        roles.Unique = false;
                        roles.Searchable = false;
                        roles.Auditable = false;
                        roles.System = true;
                        roles.DefaultValue = null;
                        roles.MaxLength = null;
                        {
                            var createResponse = entityManager.CreateField(SystemIds.AreaEntityId, roles, false);
                            if (!createResponse.Success)
                                throw new Exception("System error 10340. Message:" + createResponse.Message);
                        }

                    }
                    #endregion
                }

                storeSystemSettings = systemSettingsRepository.Convert(systemSettings);
                systemSettingsRepository.Save(storeSystemSettings);

                //if (currentVersion == 150508) //update to 150510
                //{
                //    systemSettings.Version = 150510;

                //    storeSystemSettings = systemSettingsRepository.Convert(systemSettings);
                //    systemSettingsRepository.Save(storeSystemSettings);
                //}

                transaction.Commit();
            }
            catch( Exception )
            {
                transaction.Rollback();
                throw;
            }
        }