Example #1
0
        public static M_Element Get(Ctx ctx, String columnName, Trx trxName)
        {
            if (columnName == null || columnName.Length == 0)
            {
                return(null);
            }
            M_Element retValue = null;
            String    sql      = "SELECT * FROM AD_Element WHERE UPPER(ColumnName)='" + columnName.ToUpper() + "'";
            DataSet   ds       = null;

            try
            {
                ds = DataBase.DB.ExecuteDataset(sql, null, trxName);
            }
            catch
            {
                ds = DataBase.DB.ExecuteDataset(sql, null, null);            //in case the transaction error comes, try again without trx
            }
            try
            {
                //ds = DataBase.DB.ExecuteDataset(sql, null, trxName);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow rs = ds.Tables[0].Rows[i];
                    retValue = new M_Element(ctx, rs, trxName);
                    _log.Warning("Not unique: " + columnName + " -> " + retValue + " - " + rs["ColumnName"].ToString());
                }
                ds = null;
            }
            catch (Exception e)
            {
                _log.Log(Level.SEVERE, sql, e);
            }
            return(retValue);
        }
Example #2
0
        /// <summary>
        ///Get Element
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="AD_Column_ID">columnName case insensitive column name</param>
        /// <returns>case sensitive column name</returns>
        public static M_Element GetOfColumn(Ctx ctx, int AD_Column_ID)
        {
            if (AD_Column_ID == 0)
            {
                return(null);
            }
            M_Element retValue = null;
            String    sql      = "SELECT * FROM AD_Element e "
                                 + "WHERE EXISTS (SELECT * FROM AD_Column c "
                                 + "WHERE c.AD_Element_ID=e.AD_Element_ID AND c.AD_Column_ID=" + AD_Column_ID + ")";
            DataSet ds = null;

            try
            {
                ds = DataBase.DB.ExecuteDataset(sql, null, null);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow rs = ds.Tables[0].Rows[i];
                    retValue = new M_Element(ctx, rs, null);
                }
                ds = null;
            }
            catch (Exception e)
            {
                _log.Log(Level.SEVERE, sql, e);
            }
            return(retValue);
        }
Example #3
0
 /// <summary>
 /// Get Element
 /// </summary>
 /// <returns>element</returns>
 public M_Element GetElement()
 {
     if (_element == null || _element.GetAD_Element_ID() != GetAD_Element_ID())
     {
         _element = new M_Element(GetCtx(), GetAD_Element_ID(), Get_TrxName());
     }
     return(_element);
 }
Example #4
0
        }       //	checkStandardColumns

        /// <summary>
        /// Create Column in AD and DB
        /// </summary>
        /// <param name="col">Column Object</param>
        /// <param name="table">Table Object</param>
        /// <param name="alsoInDB">Also in DB</param>
        /// <returns></returns>
        private static bool CreateColumn(MColumn col, MTable table, bool alsoInDB)
        {
            //	Element
            M_Element element = M_Element.Get(col.GetCtx(), col.GetColumnName(), col.Get_TrxName());

            if (element == null)
            {
                element = new M_Element(col.GetCtx(), col.GetColumnName(), col.GetEntityType(), null);
                if (!element.Save())
                {
                    return(false);
                }
                log.Info("Created Element: " + element.GetColumnName());
            }
            //	Column Sync
            col.SetColumnName(element.GetColumnName());
            col.SetName(element.GetName());
            col.SetDescription(element.GetDescription());
            col.SetHelp(element.GetHelp());
            col.SetAD_Element_ID(element.GetAD_Element_ID());
            //

            if (!col.Save())
            {
                return(false);
            }

            //	DB
            if (!alsoInDB)
            {
                return(true);
            }
            //
            String sql     = col.GetSQLAdd(table);
            bool   success = DataBase.DB.ExecuteUpdateMultiple(sql, false, table.Get_TrxName()) >= 0;

            if (success)
            {
                log.Info("Created: " + table.GetTableName()
                         + "." + col.GetColumnName());
            }
            else
            {
                log.Warning("NOT Created: " + table.GetTableName()
                            + "." + col.GetColumnName());
            }
            return(success);
        }       //	createColumn
Example #5
0
        /// <summary>
        ///	Before Save
        /// </summary>
        /// <param name="newRecord">newRecord new</param>
        /// <returns>true</returns>
        protected override bool BeforeSave(bool newRecord)
        {
            if (DB.UseMigratedConnection)
            {
                return(true);
            }

            //	Sync Terminology
            if ((newRecord || Is_ValueChanged("AD_Column_ID")) &&
                IsCentrallyMaintained())
            {
                M_Element element = M_Element.GetOfColumn(GetCtx(), GetAD_Column_ID());
                SetName(element.GetName());
                SetDescription(element.GetDescription());
                SetHelp(element.GetHelp());
            }
            return(true);
        }
Example #6
0
        /// <summary>
        /// Before Save
        /// </summary>
        /// <param name="newRecord">new</param>
        /// <returns>true</returns>
        protected override Boolean BeforeSave(Boolean newRecord)
        {
            //	Sync Terminology
            if ((newRecord || Is_ValueChanged("AD_Element_ID")) &&
                GetAD_Element_ID() != 0 &&
                IsCentrallyMaintained())
            {
                M_Element element = new M_Element(GetCtx(), GetAD_Element_ID(), null);
                SetName(element.GetName());
                SetDescription(element.GetDescription());
                SetHelp(element.GetHelp());
            }
            //	Auto Numbering
            if (newRecord && GetSeqNo() == 0)
            {
                String sql = "SELECT COALESCE(MAX(SeqNo),0)+10 FROM AD_InfoColumn WHERE AD_InfoWindow_ID=" + GetAD_InfoWindow_ID();
                int    no  = DataBase.DB.GetSQLValue(null, sql);
                SetSeqNo(no);
            }

            return(true);
        }
Example #7
0
        /// <summary>
        /// Before Save
        /// </summary>
        /// <param name="newRecord"></param>
        /// <returns></returns>
        protected override bool BeforeSave(bool newRecord)
        {
            if (!CheckVersions(false))
            {
                return(false);
            }

            int displayType = GetAD_Reference_ID();

            //	Length
            if (DisplayType.IsLOB(displayType)) //	LOBs are 0
            {
                if (GetFieldLength() != 0)
                {
                    SetFieldLength(0);
                }
            }
            else if (GetFieldLength() == 0)
            {
                if (DisplayType.IsID(displayType))
                {
                    SetFieldLength(10);
                }
                else if (DisplayType.IsNumeric(displayType))
                {
                    SetFieldLength(14);
                }
                else if (DisplayType.IsDate(displayType))
                {
                    SetFieldLength(7);
                }
                else if (DisplayType.YesNo == displayType)
                {
                    SetFieldLength(1);
                }
                else
                {
                    log.SaveError("FillMandatory", Utility.Msg.GetElement(GetCtx(), "FieldLength"));
                    return(false);
                }
            }

            /** Views are not updateable
             * UPDATE AD_Column c
             * SET IsUpdateable='N', IsAlwaysUpdateable='N'
             * WHERE AD_Table_ID IN (SELECT AD_Table_ID FROM AD_Table WHERE IsView='Y')
             **/

            //	Virtual Column
            if (IsVirtualColumn())
            {
                if (IsMandatory())
                {
                    SetIsMandatory(false);
                }
                if (IsMandatoryUI())
                {
                    SetIsMandatoryUI(false);
                }
                if (IsUpdateable())
                {
                    SetIsUpdateable(false);
                }
            }
            //	Updateable/Mandatory
            if (IsParent() || IsKey())
            {
                SetIsUpdateable(false);
                SetIsMandatory(true);
            }
            if (IsAlwaysUpdateable() && !IsUpdateable())
            {
                SetIsAlwaysUpdateable(false);
            }
            //	Encrypted
            if (IsEncrypted())
            {
                int dt = GetAD_Reference_ID();
                if (IsKey() || IsParent() || IsStandardColumn() ||
                    IsVirtualColumn() || IsIdentifier() || IsTranslated() ||
                    DisplayType.IsLookup(dt) || DisplayType.IsLOB(dt) ||
                    "DocumentNo".ToLower().Equals(GetColumnName().ToLower()) ||
                    "Value".ToLower().Equals(GetColumnName().ToLower()) ||
                    "Name".ToLower().Equals(GetColumnName().ToLower()))
                {
                    log.Warning("Encryption not sensible - " + GetColumnName());
                    SetIsEncrypted(false);
                }
            }

            //	Sync Terminology
            if ((newRecord || Is_ValueChanged("AD_Element_ID")) &&
                GetAD_Element_ID() != 0)
            {
                _element = new M_Element(GetCtx(), GetAD_Element_ID(), Get_TrxName());
                SetColumnName(_element.GetColumnName());
                SetName(_element.GetName());
                SetDescription(_element.GetDescription());
                SetHelp(_element.GetHelp());
            }

            if (IsKey() && (newRecord || Is_ValueChanged("IsKey")))
            {
                SetConstraintType(null);
            }

            return(true);
        }
Example #8
0
        }       //	generateView

        /// <summary>
        /// Synchronize target table with base table in dictionary
        /// </summary>
        /// <param name="derived">is derived</param>
        /// <returns></returns>
        private MColumn[] SyncMColumns(bool derived)
        {
            MTable target = derived ? m_derivedTable : m_viewTable;
            MTable source = derived ? m_baseTable : m_derivedTable;

            MColumn[] sCols = source.GetColumns(false);
            MColumn[] tCols = target.GetColumns(false);
            //	Base Columns
            foreach (MColumn sCol in sCols)
            {
                MColumn tCol = null;
                foreach (MColumn column in tCols)
                {
                    if (sCol.GetColumnName().Equals(column.GetColumnName()))
                    {
                        tCol = column;
                        break;
                    }
                }
                if (tCol == null)
                {
                    tCol = new MColumn(target);
                }
                PO.CopyValues(sCol, tCol);
                tCol.SetAD_Table_ID(target.GetAD_Table_ID());   //	reset parent
                tCol.SetIsCallout(false);
                tCol.SetCallout(null);
                tCol.SetIsMandatory(false);
                tCol.SetIsMandatoryUI(false);
                tCol.SetIsTranslated(false);
                //	tCol.SetIsUpdateable(true);
                if (tCol.IsKey())
                {
                    tCol.SetIsKey(false);
                    tCol.SetAD_Reference_ID(DisplayType.TableDir);
                }
                if (tCol.Save())
                {
                    throw new Exception("Cannot save column " + sCol.GetColumnName());
                }
            }
            //
            tCols = target.GetColumns(true);
            List <String> addlColumns = new List <String>();

            if (derived && !m_history)  //	delta only
            {
                //	KeyColumn
                String  keyColumnName = target.GetTableName() + "_ID";
                MColumn key           = target.GetColumn(keyColumnName);
                if (key == null)
                {
                    key = new MColumn(target);
                    M_Element ele = M_Element.Get(m_ctx, keyColumnName, target.Get_TrxName());
                    if (ele == null)
                    {
                        ele = new M_Element(m_ctx, keyColumnName, target.GetEntityType(), null);
                        ele.Save();
                    }
                    key.SetAD_Element_ID(ele.GetAD_Element_ID());
                    key.SetAD_Reference_ID(DisplayType.ID);
                    key.Save();
                }
                addlColumns.Add(keyColumnName);
                //	Addl References
                if (m_userDef)
                {
                    String colName = "AD_Role_ID";
                    addlColumns.Add(colName);
                    if (target.GetColumn(colName) == null)
                    {
                        MColumn col = new MColumn(target);
                        col.SetColumnName(colName);
                        col.SetAD_Reference_ID(DisplayType.TableDir);
                        CreateColumn(col, target, false);
                        col.SetIsUpdateable(false);
                        col.SetIsMandatory(false);
                    }
                    colName = "AD_User_ID";
                    addlColumns.Add(colName);
                    if (target.GetColumn(colName) == null)
                    {
                        MColumn col = new MColumn(target);
                        col.SetColumnName(colName);
                        col.SetAD_Reference_ID(DisplayType.TableDir);
                        col.SetIsUpdateable(false);
                        col.SetIsMandatory(false);
                        CreateColumn(col, target, false);
                    }
                }
                else    //	System
                {
                    String colName = "IsSystemDefault";
                    addlColumns.Add(colName);
                    if (target.GetColumn(colName) == null)
                    {
                        MColumn col = new MColumn(target);
                        col.SetColumnName(colName);
                        col.SetAD_Reference_ID(DisplayType.YesNo);
                        col.SetDefaultValue("N");
                        col.SetIsUpdateable(false);
                        col.SetIsMandatory(true);
                        CreateColumn(col, target, false);
                    }
                }
            }


            //	Delete
            foreach (MColumn tCol in tCols)
            {
                MColumn sCol = null;
                foreach (MColumn column in sCols)
                {
                    if (tCol.GetColumnName().Equals(column.GetColumnName()))
                    {
                        sCol = column;
                        break;
                    }
                }
                if (sCol == null)
                {
                    if (!addlColumns.Contains(tCol.GetColumnName()))
                    {
                        if (!tCol.Delete(true))
                        {
                            throw new Exception("Cannot delete column "
                                                + tCol.GetColumnName());
                        }
                    }
                }
            }
            return(tCols);
        }       //	SyncMColumns