Exemplo n.º 1
0
        Field IFieldRepository.CreateNew(Field item, bool explicitOrder)
        {
            using (var scope = new QPConnectionScope())
            {
                try
                {
                    if (QPContext.DatabaseType == DatabaseType.SqlServer)
                    {
                        ChangeMaxOrderTriggerState(scope.DbConnection, false);
                        ChangeInsertFieldTriggerState(scope.DbConnection, false);
                        ChangeM2MDefaultTriggerState(scope.DbConnection, false);
                    }

                    if (explicitOrder)
                    {
                        item.ReorderContentFields();
                    }
                    else
                    {
                        var fieldsExists = QPContext.EFContext.FieldSet.Any(n => n.ContentId == item.ContentId);
                        var maxOrder     = fieldsExists ?
                                           (int)QPContext.EFContext.FieldSet.Where(n => n.ContentId == item.ContentId)
                                           .Select(n => n.Order).Max() : 0;
                        item.Order = maxOrder + 1;
                    }

                    var constraint   = item.Constraint;
                    var dynamicImage = item.DynamicImage;
                    var fieldValues  = item.QpPluginFieldValues;

                    DefaultRepository.TurnIdentityInsertOn(EntityTypeCode.Field, item);
                    var newItem = DefaultRepository.Save <Field, FieldDAL>(item);
                    var field   = GetDal(newItem);
                    Common.AddColumn(scope.DbConnection, field);

                    DefaultRepository.TurnIdentityInsertOff(EntityTypeCode.Field);

                    SaveConstraint(constraint, newItem);
                    SaveDynamicImage(dynamicImage, newItem);
                    UpdatePluginValues(fieldValues, newItem.Id);

                    return(GetById(newItem.Id));
                }
                finally
                {
                    if (QPContext.DatabaseType == DatabaseType.SqlServer)
                    {
                        ChangeMaxOrderTriggerState(scope.DbConnection, true);
                        ChangeInsertFieldTriggerState(scope.DbConnection, true);
                        ChangeM2MDefaultTriggerState(scope.DbConnection, true);
                    }
                }
            }
        }