示例#1
0
        /// <summary>
        /// Generates a XPCollection with Criteria Operator and create a TreeView Model, from a parameter XPGuidObject Class Type. Used to generate TreeView Models to GenericTreeViewModel Class.
        /// </summary>
        /// <param name="XPCollection"> used to generate TreeView Model</param>
        /// <param name="ColumnProperties"> TreeView Column Properties, used to select which XPOCollection fields are used to generate TreeView ListStore Model.</param>
        /// <param name="TreeViewMode"> TreeView Mode Default or CheckBox</param>
        /// <returns>Returns ListStore, a model ready to be used in GenericTreeViewModel.</returns>
        public override void InitDataModel(XPCollection pDataSource, List <GenericTreeViewColumnProperty> pColumnProperties, GenericTreeViewMode pGenericTreeViewMode)
        {
            //Log4Net
            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            //Parameters
            XPCollection _XpCollection = pDataSource;
            List <GenericTreeViewColumnProperty> _columnProperties = pColumnProperties;
            GenericTreeViewMode _treeViewMode = pGenericTreeViewMode;

            //Initialize Model and Column Properties
            ListStore model = GenericTreeViewModel.InitModel(_columnProperties, pGenericTreeViewMode);

            //Init ColumnValues Object Array
            System.Object[] columnValues = new System.Object[_columnProperties.Count];

            //Start Render Model Values from Collection
            String fieldName;

            //Loop Records
            Int32 rowIndex = -1;

            foreach (XPGuidObject dataRow in (_XpCollection as XPCollection))
            {
                // Increment RownIndex
                rowIndex++;
                // reset undefinedRecord
                bool undefinedRecord = false;

                // This will reload current dataRow, without this XPO wil use cached values, and never be in sync when we change data outside
                dataRow.Reload();

                //Loop Fields : Generate Abstract Values to use in Model
                for (int i = 0; i < _columnProperties.Count; i++)
                {
                    //Default FieldName
                    fieldName = _columnProperties[i].Name;

                    //if (fieldName == "Ord" || fieldName == "Code")
                    //{
                    //    _log.Debug("BREAK");
                    //}

                    try
                    {
                        //Dont extract value from Collection if is the CheckBox, its not in Collection, and is always Disabled
                        if (_columnProperties[i].PropertyType == GenericTreeViewColumnPropertyType.CheckBox)
                        {
                            //Initial Checked Value
                            columnValues[i] = false;
                        }
                        else if (fieldName == "RowIndex")
                        {
                            columnValues[i] = rowIndex;
                        }
                        //Query
                        else if (_columnProperties[i].Query != null && _columnProperties[i].Query != string.Empty)
                        {
                            columnValues[i] = ColumnPropertyGetQuery(_columnProperties[i].Query, dataRow.GetMemberValue("Oid"));
                            // Decrypt Before use Format
                            if (_columnProperties[i].DecryptValue)
                            {
                                columnValues[i] = XPGuidObject.DecryptIfNeeded(columnValues[i]);
                            }
                            //Format String using Column FormatProvider
                            if (_columnProperties[i].FormatProvider != null && Convert.ToString(columnValues[i]) != string.Empty)
                            {
                                columnValues[i] = string.Format(_columnProperties[i].FormatProvider, "{0}", columnValues[i]);
                            }
                        }
                        //If detect XPGuidObject Value Type (Value is a XPObject, Child Object), Get Value from its Chield Field (Related Table)
                        else if (dataRow.GetMemberValue(_columnProperties[i].Name) != null &&
                                 dataRow.GetMemberValue(_columnProperties[i].Name).GetType().BaseType == typeof(XPGuidObject))
                        {
                            columnValues[i] = GetXPGuidObjectChildValue(dataRow.GetMemberValue(_columnProperties[i].Name), fieldName, _columnProperties[i].ChildName);
                        }
                        //Get Default Value from Field Name
                        else
                        {
                            // Check/Detect if is a undefinedRecord
                            if (fieldName == "Oid")
                            {
                                undefinedRecord = ((new Guid(dataRow.GetMemberValue(_columnProperties[i].Name).ToString())).Equals(SettingsApp.XpoOidUndefinedRecord));
                            }

                            //TODO (Muga): melhorar isto para ser generico e contemplar os outros campos
                            if (dataRow.GetMemberValue(_columnProperties[i].Name) != null)
                            {
                                // Detect UndefinedRecord and if is a Int Field, and Replace with string.Empty, this ways we Dont Show 0 in Null Undefined Records
                                if (undefinedRecord && dataRow.GetMemberValue(_columnProperties[i].Name).GetType().Name.Equals("UInt32"))
                                {
                                    columnValues[i] = string.Empty;
                                }
                                //Boolean Fields
                                else if (dataRow.GetMemberValue(_columnProperties[i].Name).GetType().Name.Equals("Boolean"))
                                {
                                    bool booleanValue = Convert.ToBoolean(dataRow.GetMemberValue(_columnProperties[i].Name));
                                    columnValues[i] = (booleanValue) ? resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_treeview_true") : resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_treeview_false");
                                }
                                //Other Fields
                                else
                                {
                                    columnValues[i] = Convert.ToString(dataRow.GetMemberValue(_columnProperties[i].Name));
                                    //Format String using Column FormatProvider
                                    if (_columnProperties[i].FormatProvider != null)
                                    {
                                        columnValues[i] = string.Format(_columnProperties[i].FormatProvider, "{0}", columnValues[i]);
                                    }
                                }
                            }
                            //If Field is NULL or Empty always assign string.Empty to prevent re-use last record value
                            else
                            {
                                columnValues[i] = string.Empty;
                            }
                        }

                        // ResourceString : Extract ResourceManager from Final Value
                        if (_columnProperties[i].ResourceString == true && columnValues[i] != null)
                        {
                            // Try to get ResourceString Value, this is required to replace value, but only if it a valid resourceString (not replaced yet after update)
                            // After an Update and Refresh it turns into a string(non resource token), this protection prevents the replace double with a null resourceString,
                            // leaving tree cell value with an empty value
                            bool checkIfResourceStringExist = (resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], columnValues[i].ToString()) != null) ? true : false;
                            // Only Replace resourceString if value is resourceString is not Yet been replaced, ex after an update
                            //_log.Debug(string.Format("columnValues[i]#1: [{0}]", columnValues[i]));
                            if (checkIfResourceStringExist)
                            {
                                columnValues[i] = resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], columnValues[i].ToString());
                            }
                            //_log.Debug(string.Format("columnValues[i]#2: [{0}]", columnValues[i]));
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(string.Format("XPCollectionToModel(): {0}", ex.Message), ex);
                        columnValues[i] = string.Format("Invalid Field {0}", fieldName);
                    }
                }
                ;
                //Add Column Value to Model
                model.AppendValues(columnValues);
            }
            _listStoreModel = model;
        }
        public override void InitDataModel(DataTable pDataSource, List <GenericTreeViewColumnProperty> pColumnProperties, GenericTreeViewMode pGenericTreeViewMode)
        {
            //Log4Net
            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            //Parameters
            DataTable dataTable = pDataSource;
            List <GenericTreeViewColumnProperty> _columnProperties = pColumnProperties;
            GenericTreeViewMode _treeViewMode = pGenericTreeViewMode;

            //Initialize Model and Column Properties
            ListStore model = GenericTreeViewModel.InitModel(_columnProperties, pGenericTreeViewMode);

            //Init ColumnValues Object Array
            System.Object[] columnValues = new System.Object[_columnProperties.Count];

            //Start Render Model Values from Collection
            String fieldName;

            //Always add Index to Rows, NOT USED: Check if existe before Add to prevent Refresh Add Again
            dataTable.Columns.Add("Index", typeof(Int32)).SetOrdinal(0);//Put in Index 0, First Column

            //If Detect CheckBox Mode, Insert CheckBox Column in DataTable
            if (_treeViewMode == GenericTreeViewMode.CheckBox)
            {
                //NOT USED: Check if existe before Add to prevent Refresh Add Again "if (dataTable.Columns.IndexOf("CheckBox") < 0)"
                dataTable.Columns.Add("CheckBox", typeof(Boolean)).SetOrdinal(1);//Put in Index 1, After Index
            }

            //Loop Records
            Int32 rowIndex = -1;

            foreach (DataRow dataRow in dataTable.Rows)
            {
                //Increase RowIndex
                rowIndex++;

                //Loop Fields : Generate Abstract Values to use in Model
                for (int i = 0; i < _columnProperties.Count; i++)
                {
                    fieldName = _columnProperties[i].Name;

                    //If is in first Field (Index) assign rowIndex to Model and DataRow
                    if (fieldName == "RowIndex")
                    {
                        columnValues[i] = rowIndex;
                        dataRow[i]      = rowIndex;
                    }
                    //Query
                    else if (_columnProperties[i].Query != null && _columnProperties[i].Query != string.Empty)
                    {
                        columnValues[i] = ColumnPropertyGetQuery(_columnProperties[i].Query, dataRow.ItemArray[i]);
                    }
                    //If detect XPGuidObject Value Type (Value is a XPObject, Child Object), Get Value from its Chield Field (Related Table)
                    else if (dataRow.ItemArray[i] != null && dataRow.ItemArray[i].GetType().BaseType == typeof(XPGuidObject))
                    {
                        columnValues[i] = GetXPGuidObjectChildValue(dataRow.ItemArray[i], fieldName, _columnProperties[i].ChildName);
                    }
                    //If is second field Assign false to checkbox to Model and DataRow
                    else if (fieldName == "RowCheckBox")
                    {
                        columnValues[i] = false;
                        dataRow[i]      = false;
                    }
                    //Else All others Fields assign Value from dataRow
                    else
                    {
                        columnValues[i] = FrameworkUtils.FormatDataTableFieldFromType(dataRow.ItemArray[i].ToString(), dataRow.ItemArray[i].GetType().Name);
                        //Format String using Column FormatProvider
                        if (_columnProperties[i].FormatProvider != null)
                        {
                            columnValues[i] = string.Format(_columnProperties[i].FormatProvider, "{0}", columnValues[i]);
                        }
                    }
                }

                //Add Column Value to Model
                model.AppendValues(columnValues);
            }

            _listStoreModel = model;
        }