public IHttpActionResult Post(EntityPropertyModel requestModel) { var entityName = requestModel.EntityName; var propertyName = requestModel.PropertyName; var propertyValue = requestModel.Value; var entityId = requestModel.EntityId; var currentUser = ApplicationContext.Current.CurrentUser; Media media = null; if (PropertyNames.IsMediaPropertyName(propertyName)) { //the property value must be an integer var valueAsInteger = propertyValue.GetInteger(false); if (valueAsInteger == 0) { return(BadRequest()); } //get media since this is media property, let's get the media first media = _mediaService.Get(valueAsInteger); //is the person trying to mess around actually is a capable person if (!currentUser.CanEditResource(media)) { return(RespondFailure("Unauthorized", "post_entityproperty")); } } //get valid system property name if available propertyName = PropertyNames.ParseToValidSystemPropertyName(propertyName) ?? propertyName; switch (entityName.ToLower()) { case "user": //somebody is trying to set the user's properties. He must be the user himself or administrator var user = _userService.Get(entityId); if (user == null || !currentUser.CanEditUser(user)) { return(NotFound()); } user.SetPropertyValue(propertyName, propertyValue); if (media != null) { //also attach the media to user so we can show them all at one place //this way if user wants to delete the media, we'll simply detach them immediately to postpone till a service performs deletion _mediaService.AttachMediaToEntity(user, media); } break; } return(RespondSuccess()); }
/// <summary> /// /// </summary> /// <param name="rModel">current EntityRelationModel which is evaluated.</param> /// <returns></returns> private void CreateConstraintQuery(EntityPropertyModel rModel, sysBpmsEntityDef entityDef, List <string> listQueries) { sysBpmsEntityDef foreignEntity = this.GetInfo(rModel.RelationToEntityID.Value); string constaintName = this.GetConstraintName(rModel.Name, entityDef.FormattedTableName, foreignEntity.FormattedTableName); //generate foreign key query listQueries.Add($@"ALTER TABLE [{entityDef.FormattedTableName}] WITH CHECK ADD CONSTRAINT [{constaintName}] FOREIGN KEY([{rModel.Name}]) REFERENCES [{foreignEntity.FormattedTableName}] ([ID])"); listQueries.Add($@"ALTER TABLE [{entityDef.FormattedTableName}] CHECK CONSTRAINT [{constaintName}]"); rModel.RelationConstaintName = constaintName; }
public ResultOperation UpdateTable(sysBpmsEntityDef newEntityDef) { DataBaseQueryService dataBaseQueryService = new DataBaseQueryService(base.UnitOfWork); sysBpmsEntityDef currentEntityDef = this.GetInfo(newEntityDef.ID); ResultOperation resultOperation = new ResultOperation(); List <string> addQuery = new List <string>(); if (newEntityDef.IsActive && newEntityDef.Properties != null && newEntityDef.Properties.Any()) { foreach (EntityPropertyModel newProperty in newEntityDef.Properties) { //change Property if (currentEntityDef.Properties.Any(c => c.ID == newProperty.ID)) { EntityPropertyModel currentProperty = currentEntityDef.Properties.FirstOrDefault(c => c.ID == newProperty.ID); //change property name. if (currentProperty.Name != newProperty.Name) { addQuery.Add($" EXEC sp_rename '{currentEntityDef.FormattedTableName}.{currentProperty.Name}', '{newProperty.Name}', 'COLUMN'; {Environment.NewLine} "); } if (currentProperty.DbType != newProperty.DbType || currentProperty.SqlTypeName != newProperty.SqlTypeName || currentProperty.Required != newProperty.Required) { addQuery.Add($@" ALTER TABLE {currentEntityDef.FormattedTableName} ALTER COLUMN {newProperty.Name} {newProperty.SqlTypeName} {newProperty.SqlDefaultValue} {(newProperty.Required ? "NOT NULL" : "NULL")} ; "); } if (currentProperty.DefaultValue != newProperty.DefaultValue) { if (!string.IsNullOrWhiteSpace(currentProperty.DefaultValue)) { addQuery.Add($@" ALTER TABLE {currentEntityDef.FormattedTableName} DROP CONSTRAINT def_{currentEntityDef.FormattedTableName}_{currentProperty.Name} ; "); } addQuery.Add($@" ALTER TABLE {currentEntityDef.FormattedTableName} ADD CONSTRAINT def_{currentEntityDef.FormattedTableName}_{newProperty.Name} {newProperty.SqlDefaultValue} FOR {newProperty.Name} ;"); } //if property is no longer a entity type drop relation constraint if (currentProperty.RelationToEntityID.HasValue && !newProperty.RelationToEntityID.HasValue) { addQuery.Add($@"ALTER TABLE {currentEntityDef.FormattedTableName} DROP CONSTRAINT {currentProperty.RelationConstaintName};"); } else { if (currentProperty.RelationToEntityID != newProperty.RelationToEntityID) { //if it had relation add drop constraint relation query if (currentProperty.RelationToEntityID.HasValue) { addQuery.Add($@"ALTER TABLE {currentEntityDef.FormattedTableName} DROP CONSTRAINT {currentProperty.RelationConstaintName};"); } this.CreateConstraintQuery(newProperty, newEntityDef, addQuery); } } } else { addQuery.Add($@"ALTER TABLE {currentEntityDef.FormattedTableName} ADD {newProperty.Name} {newProperty.SqlTypeName} {newProperty.SqlDefaultValue} {(newProperty.Required ? "NOT NULL" : "NULL")} ;"); //Create Constraint for new properties if (newProperty.RelationToEntityID.HasValue) { this.CreateConstraintQuery(newProperty, newEntityDef, addQuery); } } } //deleted properties foreach (EntityPropertyModel currentProperty in currentEntityDef.Properties.Where(c => !newEntityDef.Properties.Any(d => d.ID == c.ID))) { //drop default CONSTRAINT if (!string.IsNullOrWhiteSpace(currentProperty.DefaultValue)) { addQuery.Add($@" ALTER TABLE {currentEntityDef.FormattedTableName} DROP CONSTRAINT def_{currentEntityDef.FormattedTableName}_{currentProperty.Name} ; "); } //if it has relation add drop constraint relation query if (currentProperty.DbType == EntityPropertyModel.e_dbType.Entity) { addQuery.Add($@"ALTER TABLE {currentEntityDef.FormattedTableName} DROP CONSTRAINT {currentProperty.RelationConstaintName};"); } //drop property query addQuery.Add($@"ALTER TABLE {currentEntityDef.FormattedTableName} DROP COLUMN {currentProperty.Name};"); } } foreach (string query in addQuery) { dataBaseQueryService.ExecuteBySqlQuery(query, false, null); } return(resultOperation); }
private List <SqlParameter> GetColumsAsParams(sysBpmsEntityDef sysBpmsEntityDef, Dictionary <string, object> dataList, sysBpmsVariable sysBpmsVariable) { List <EntityPropertyModel> Properties = sysBpmsEntityDef.Properties; List <SqlParameter> columnParams = new List <SqlParameter>(); for (int index = 0; index < dataList.Count; index++) { var item = dataList.ToList()[index]; string propertyName = (sysBpmsVariable == null || sysBpmsVariable.VarTypeLU == (int)sysBpmsVariable.e_VarTypeLU.Object) ? item.Key : sysBpmsVariable.FieldName; EntityPropertyModel _Property = Properties.FirstOrDefault(c => c.Name == propertyName); if (Properties.Any(c => c.Name == propertyName)) { switch (_Property.DbType) { case EntityPropertyModel.e_dbType.Decimal: if (_Property.Required) { dataList[item.Key] = item.Value.ToDecimalObj(); } else { dataList[item.Key] = item.Value != null?item.Value.ToDecimalObjNull() : (decimal?)null; } break; case EntityPropertyModel.e_dbType.Integer: if (_Property.Required) { dataList[item.Key] = item.Value.ToIntObj(); } else { dataList[item.Key] = item.Value != null?item.Value.ToIntObjNull() : (int?)null; } break; case EntityPropertyModel.e_dbType.Long: if (_Property.Required) { dataList[item.Key] = Convert.ToInt64(item.Value); } else { dataList[item.Key] = item.Value != null?Convert.ToInt64(item.Value) : (long?)null; } break; case EntityPropertyModel.e_dbType.String: dataList[item.Key] = item.Value.ToStringObj(); break; case EntityPropertyModel.e_dbType.DateTime: if (string.IsNullOrWhiteSpace(item.Value.ToStringObj())) { dataList[item.Key] = DBNull.Value; } else { dataList[item.Key] = Convert.ToDateTime(dataList[item.Key]); } break; case EntityPropertyModel.e_dbType.Uniqueidentifier: if (string.IsNullOrWhiteSpace(item.Value.ToStringObj())) { dataList[item.Key] = DBNull.Value; } break; } columnParams.Add(new SqlParameter("@" + propertyName, dataList[item.Key])); } } return(columnParams); }