Exemple #1
0
        /// <summary>
        /// Should be called after the object has all it's properties (if applicable).
        /// It creates the default criteria classes depending on the object type.
        /// </summary>
        public void AddDefaultCriteriaAndParameters()
        {
            if (_currentCslaObject.CriteriaObjects.Count != 0)
            {
                return;
            }

            if (_currentCslaObject.ObjectType == CslaObjectType.NameValueList ||
                _currentCslaObject.ObjectType == CslaObjectType.ReadOnlyCollection ||
                _currentCslaObject.ObjectType == CslaObjectType.EditableRootCollection ||
                _currentCslaObject.ObjectType == CslaObjectType.DynamicEditableRootCollection)
            {
                if (_currentUnit.Params.AutoCriteria)
                {
                    Criteria crit = CreateEmptyFetchCriteria();
                    _currentCslaObject.CriteriaObjects.Add(crit);
                    crit.SetSprocNames();
                }
                //no need to go through the properties here.
                return;
            }

            List <ValueProperty> primaryKeyProperties = new List <ValueProperty>();
            ValueProperty        timestampProperty    = null;
            bool UseForCreate = false;

            // retrieve all primary key and timestamp properties
            foreach (ValueProperty prop in _currentCslaObject.ValueProperties)
            {
                if (prop.PrimaryKey != ValueProperty.UserDefinedKeyBehaviour.Default)
                {
                    primaryKeyProperties.Add(prop);
                    if (!(prop.DbBindColumn.IsIdentity || prop.PropertyType == TypeCodeEx.Guid))
                    {
                        UseForCreate = true;
                    }
                }
                else if (prop.DbBindColumn.NativeType == "timestamp")
                {
                    timestampProperty = prop;
                }
            }

            if (primaryKeyProperties.Count > 0 || timestampProperty != null)
            {
                // Try to find default Criteria object
                Criteria defaultCriteria   = _currentCslaObject.CriteriaObjects.Find("Criteria");
                Criteria timestampCriteria = _currentCslaObject.CriteriaObjects.Find("CriteriaTS");

                // If criteria objects are not set
                if (_currentCslaObject.CriteriaObjects.Count == 0)
                {
                    // If default criteria doesn't exists, create a new criteria
                    if (defaultCriteria == null)
                    {
                        if (_currentCslaObject.ObjectType != CslaObjectType.ReadOnlyObject)
                        {
                            defaultCriteria      = new Criteria(_currentCslaObject);
                            defaultCriteria.Name = "Criteria";
                            defaultCriteria.GetOptions.Enable();
                            if (_currentCslaObject.ObjectType == CslaObjectType.EditableRoot ||
                                _currentCslaObject.ObjectType == CslaObjectType.EditableSwitchable ||
                                _currentCslaObject.ObjectType == CslaObjectType.EditableChild ||
                                _currentCslaObject.ObjectType == CslaObjectType.DynamicEditableRoot)
                            {
                                defaultCriteria.DeleteOptions.Enable();
                                if (defaultCriteria.Properties.Count > 0 && UseForCreate)
                                {
                                    defaultCriteria.CreateOptions.Factory    = true;
                                    defaultCriteria.CreateOptions.DataPortal = true;
                                    defaultCriteria.CreateOptions.RunLocal   = true;
                                }
                                else
                                {
                                    Criteria createCriteria = _currentCslaObject.CriteriaObjects.Find("CriteriaNew");
                                    if (createCriteria == null)
                                    {
                                        createCriteria      = new Criteria(_currentCslaObject);
                                        createCriteria.Name = "CriteriaNew";
                                        createCriteria.CreateOptions.DataPortal = true;
                                        createCriteria.CreateOptions.Factory    = true;
                                        createCriteria.CreateOptions.RunLocal   = true;
                                        _currentCslaObject.CriteriaObjects.Add(createCriteria);
                                    }
                                }
                            }

                            defaultCriteria.SetSprocNames();

                            if (_currentUnit.GenerationParams.TargetFramework == TargetFramework.CSLA40 &&
                                _currentCslaObject.ObjectType != CslaObjectType.EditableRoot &&
                                _currentCslaObject.ObjectType != CslaObjectType.EditableSwitchable)
                            {
                                defaultCriteria.Name = "CriteriaDelete";
                                defaultCriteria.GetOptions.Factory       = false;
                                defaultCriteria.GetOptions.DataPortal    = false;
                                defaultCriteria.GetOptions.Procedure     = false;
                                defaultCriteria.GetOptions.ProcedureName = string.Empty;
                                defaultCriteria.DeleteOptions.Factory    = false;
                                defaultCriteria.DeleteOptions.DataPortal = false;
                            }

                            _currentCslaObject.CriteriaObjects.Add(defaultCriteria);
                            AddPropertiesToCriteria(primaryKeyProperties, defaultCriteria);

                            if (_currentUnit.Params.AutoTimestampCriteria && timestampProperty != null && timestampCriteria == null)
                            {
                                AddTimestampProperty(defaultCriteria, timestampProperty);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void SetValuePropertyInfo(IDataBaseObject obj, IResultSet rs, IColumnInfo prop, ValueProperty destination)
        {
            IColumnInfo p = prop;

            SetDbBindColumn(obj, rs, p, destination.DbBindColumn);
            destination.Nullable = (p.IsNullable);

            if (p.NativeTypeName == "timestamp")
            {
                destination.ReadOnly          = true;
                destination.MarkDirtyOnChange = false;
                destination.Undoable          = false;
                destination.DeclarationMode   = _currentUnit.Params.CreateTimestampPropertyMode;
            }

            if (_currentCslaObject.ObjectType == CslaObjectType.ReadOnlyObject)
            {
                destination.DeclarationMode = _currentUnit.Params.CreateReadOnlyObjectsPropertyMode;
            }

            if (p.IsPrimaryKey)
            {
                if (p.IsIdentity)
                {
                    destination.PrimaryKey           = ValueProperty.UserDefinedKeyBehaviour.DBProvidedPK;
                    destination.ReadOnly             = true;
                    destination.PropSetAccessibility = AccessorVisibility.Default;
                }
                else
                {
                    destination.PrimaryKey = ValueProperty.UserDefinedKeyBehaviour.UserProvidedPK;
                }

                if (destination.PropertyType == TypeCodeEx.Guid)
                {
                    destination.DefaultValue = _currentUnit.Params.IDGuidDefaultValue;
                }
                else if (p.IsIdentity)
                {
                    switch (p.DbType)
                    {
                    case DbType.Int16:
                        destination.DefaultValue = _currentUnit.Params.IDInt16DefaultValue;
                        break;

                    case DbType.Int32:
                        destination.DefaultValue = _currentUnit.Params.IDInt32DefaultValue;
                        break;

                    case DbType.Int64:
                        destination.DefaultValue = _currentUnit.Params.IDInt64DefaultValue;
                        break;
                    }
                }
            }

            if (_currentUnit.Params.CreationDateColumn == p.ColumnName)
            {
                destination.ReadOnly             = true;
                destination.PropSetAccessibility = AccessorVisibility.Default;
                destination.DataAccess           = ValueProperty.DataAccessBehaviour.CreateOnly;
                if (destination.PropertyType == TypeCodeEx.SmartDate)
                {
                    destination.DefaultValue = _currentUnit.Params.LogDateAndTime
                                                   ? "new SmartDate(DateTime.Now)"
                                                   : "new SmartDate(DateTime.Today)";
                }
                else
                {
                    destination.DefaultValue = _currentUnit.Params.LogDateAndTime
                                                   ? "DateTime.Now"
                                                   : "DateTime.Today";
                }
            }
            else if (_currentUnit.Params.CreationUserColumn == p.ColumnName)
            {
                destination.ReadOnly             = true;
                destination.PropSetAccessibility = AccessorVisibility.Default;
                destination.DataAccess           = ValueProperty.DataAccessBehaviour.CreateOnly;
                destination.DefaultValue         = _currentUnit.Params.GetUserMethod;
            }
            else if (_currentUnit.Params.ChangedDateColumn == p.ColumnName)
            {
                destination.ReadOnly             = true;
                destination.PropSetAccessibility = AccessorVisibility.Default;
                if (CslaTemplateHelper.IsCreationDateColumnPresent(_currentCslaObject))
                {
                    destination.DefaultValue = "$" + _currentUnit.Params.CreationDateColumn;
                }
                else
                {
                    destination.DefaultValue = _currentUnit.Params.LogDateAndTime
                                                   ? "new SmartDate(DateTime.Now)"
                                                   : "new SmartDate(DateTime.Today)";
                }
            }
            else if (_currentUnit.Params.ChangedUserColumn == p.ColumnName)
            {
                destination.ReadOnly             = true;
                destination.PropSetAccessibility = AccessorVisibility.Default;
                if (CslaTemplateHelper.IsCreationUserColumnPresent(_currentCslaObject))
                {
                    destination.DefaultValue = "$" + _currentUnit.Params.CreationUserColumn;
                }
                else
                {
                    destination.DefaultValue = _currentUnit.Params.GetUserMethod;
                }
            }
            else if (_currentUnit.Params.DatesDefaultStringWithTypeConversion &&
                     (destination.PropertyType == TypeCodeEx.SmartDate ||
                      destination.PropertyType == TypeCodeEx.DateTime))
            {
                destination.BackingFieldType = destination.PropertyType;
                destination.PropertyType     = TypeCodeEx.String;
                destination.DeclarationMode  = PropertyDeclaration.ManagedWithTypeConversion;
            }

            return;
        }
Exemple #3
0
 public void SetValuePropertyInfo(ITableInfo table, IColumnInfo p, ValueProperty destination)
 {
     SetValuePropertyInfo(table, table, p, destination);
 }
Exemple #4
0
 public void SetValuePropertyInfo(IViewInfo view, IColumnInfo p, ValueProperty destination)
 {
     SetValuePropertyInfo(view, view, p, destination);
 }
Exemple #5
0
        /// <summary>
        /// Adds the specified list of columns from the specified resultset and database object to the current object.
        /// </summary>
        /// <param name="obj">The database object.</param>
        /// <param name="rs">The result set.</param>
        /// <param name="selectedColumns"></param>
        /// <param name="createDefaultCriteria">If true, it calls AddDefaultCriteriaAndParameters() automatically</param>
        public void AddProperties(IDataBaseObject obj, IResultSet rs, IList <IColumnInfo> selectedColumns, bool createDefaultCriteria, bool askConfirmation)
        {
            if (_currentCslaObject == null || selectedColumns.Count == 0)
            {
                return;
            }

            var added = false;
            List <ValueProperty> addedProps    = new List <ValueProperty>();
            StringCollection     notaddedProps = new StringCollection();
            ColumnOriginType     origin        = ColumnOriginType.Table;
            IColumnInfo          col;

            for (int i = 0; i < selectedColumns.Count; i++)
            {
                col = selectedColumns[i];
                // use name of column to see if a property of the same name exists
                string propertyName = col.ColumnName;
                if (PropertyExists(propertyName))
                {
                    int count = 1;
                    while (PropertyExists(propertyName + count))
                    {
                        count += 1;
                    }
                    propertyName += count.ToString();
                }

                ValueProperty newProp = new ValueProperty();

                newProp.Name         = propertyName;
                newProp.PropertyType = TypeHelper.GetTypeCodeEx(col.ManagedType);
                SetValuePropertyInfo(obj, rs, col, newProp);
                if (newProp.DbBindColumn.ColumnOriginType != ColumnOriginType.Table)
                {
                    origin = newProp.DbBindColumn.ColumnOriginType;
                }
                if (!askConfirmation)
                {
                    _currentCslaObject.ValueProperties.Add(newProp);
                    added = true;
                }
                addedProps.Add(newProp);
            }

            if (addedProps.Count > 0 && askConfirmation)
            {
                var msg = new StringBuilder();
                msg.Append(
                    "The properties listed below are missing.\r\n\r\n" +
                    "Do you want to add the following properties:" +
                    Environment.NewLine);
                foreach (var valueProperty in addedProps)
                {
                    msg.AppendFormat(" - {0}.{1}\r\n", _currentCslaObject.ObjectName, valueProperty.Name);
                }

                var response = MessageBox.Show(msg.ToString(),
                                               @"Missing ValueProperty found.",
                                               MessageBoxButtons.YesNo,
                                               MessageBoxIcon.Error);
                if (response == DialogResult.Yes)
                {
                    foreach (var valueProperty in addedProps)
                    {
                        _currentCslaObject.ValueProperties.Add(valueProperty);
                    }
                    added = true;
                }
            }

            // Add Get-, New- and DeleteObjectCriteria and linked parameters
            if (createDefaultCriteria)
            {
                AddDefaultCriteriaAndParameters();
            }

            // Display message to the user
            StringBuilder sb = new StringBuilder();

            if (addedProps.Count > 0 && added)
            {
                sb.Append("Successfully added the following properties:" + Environment.NewLine);
                foreach (var propName in addedProps)
                {
                    sb.AppendFormat("\t{0}.{1}\r\n", _currentCslaObject.ObjectName, propName.Name);
                }
            }

            if (notaddedProps.Count > 0)
            {
                sb.Append("The following properties already exist:" + Environment.NewLine);
                foreach (string propName in notaddedProps)
                {
                    sb.Append("\t" + propName + Environment.NewLine);
                }
            }

            if (origin != ColumnOriginType.Table)
            {
                _currentCslaObject.GenerateSprocs = false;
                sb.Append(Environment.NewLine);
                sb.Append("Note: \"Generate stored procedures\" was set to false because the origins of the columns are not tables.");
            }

            if (sb.ToString().Length > 0)
            {
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }
        }