Ejemplo n.º 1
0
 public MColumn(MTable parent)
     : this(parent.GetCtx(), 0, parent.Get_TrxName())
 {
     SetClientOrg(parent);
     SetAD_Table_ID(parent.GetAD_Table_ID());
     SetEntityType(parent.GetEntityType());
 }       //	M_Column
Ejemplo n.º 2
0
        /// <summary>
        /// Get Table from Cache
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="tableName">case insensitive table name</param>
        /// <returns>able or null</returns>
        /// <author>Raghunandan</author>
        public static MTable Get(Ctx ctx, string tableName)
        {
            //Uesd in translationTable Class
            if (tableName == null)
            {
                return(null);
            }
            //	Check cache
            //iterator<MTable> it = s_cache.Values().iterator();
            IEnumerator <MTable> it = s_cache.Values.GetEnumerator();

            it.Reset();
            while (it.MoveNext())
            //foreach(MTable mtable in it)
            {
                //MTable retValue = (MTable)it.next();
                MTable retValue1 = (MTable)it.Current;
                //if (tableName.equalsIgnoreCase(retValue.getTableName()))
                if (tableName.ToLower().Equals(retValue1.GetTableName().ToLower()))
                {
                    return(retValue1);
                }
            }
            //	Get direct
            MTable  retValue = null;
            String  sql      = "SELECT * FROM AD_Table WHERE UPPER(TableName)=UPPER('" + tableName + "')";
            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 MTable(ctx, rs, null);
                }
            }
            catch (Exception e)
            {
                //
                s_log.Log(Level.SEVERE, sql, e);
            }
            ds = null;
            if (retValue != null)
            {
                int key = retValue.GetAD_Table_ID();
                s_cache.Add(key, retValue);
            }
            return(retValue);
        }
Ejemplo n.º 3
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
Ejemplo n.º 4
0
        }       //	generate

        /// <summary>
        /// Generate Delta and History Table
        /// </summary>
        /// <returns></returns>
        private bool GenerateTable()
        {
            //	Table
            m_derivedTable = MTable.Get(m_ctx, m_dTableName);
            if (m_derivedTable == null)
            {
                m_derivedTable = new MTable(m_ctx, 0, null);
            }

            PO.CopyValues(m_baseTable, m_derivedTable);
            m_derivedTable.SetTableName(m_dTableName);
            m_derivedTable.SetName(m_derivedTable.GetName() + " SubTable");
            m_derivedTable.SetSubTableType(m_derivedTableType);
            m_derivedTable.SetBase_Table_ID(m_baseTable.GetAD_Table_ID());
            if (!m_derivedTable.Save())
            {
                throw new Exception("Cannot save " + m_dTableName);
            }

            MColumn[] dCols = SyncMColumns(true);

            //	Sync Columns in Database
            List <MColumn> list = new List <MColumn>(dCols.Length);

            foreach (MColumn element in dCols)
            {
                list.Add(element);
            }
            Trx trx = Trx.Get("getDatabaseMetaData");
            //

            String catalog   = "";
            String schema    = DataBase.DB.GetSchema();
            String tableName = m_dTableName;

            tableName = tableName.ToUpper();
            int noColumns = 0;
            //
            DataSet rs = null;

            using (DatabaseMetaData md = new DatabaseMetaData())
            {
                rs = md.GetColumns(catalog, schema, tableName);
            }
            for (int rscount = 0; rscount <= rs.Tables[0].Rows.Count - 1; rscount++)
            {
                noColumns++;
                String columnName = rs.Tables[0].Rows[rscount]["COLUMN_NAME"].ToString();
                bool   found      = false;
                for (int i = 0; i < list.Count; i++)
                {
                    MColumn dCol = list[i];
                    if (columnName.Equals(dCol.GetColumnName(), StringComparison.OrdinalIgnoreCase))
                    {
                        String sql = dCol.GetSQLModify(m_derivedTable, false);
                        DataBase.DB.ExecuteUpdateMultiple(sql, false, null);
                        found = true;
                        list.Remove(list[i]);
                        break;
                    }
                }
                if (!found)
                {
                    String sql = "ALTER TABLE " + m_dTableName + " DROP COLUMN " + columnName;
                    DataBase.DB.ExecuteQuery(sql, null);
                }
            }
            //rs.close();
            trx.Close();

            //	No Columns
            if (noColumns == 0)
            {
                String sql = m_derivedTable.GetSQLCreate();
                return(DataBase.DB.ExecuteUpdateMultiple(sql, false, null) >= 0);
            }
            //	New Columns
            for (int i = 0; i < list.Count(); i++)
            {
                MColumn dCol = list[i];
                if (dCol.IsVirtualColumn())
                {
                    continue;
                }
                String sql = dCol.GetSQLAdd(m_derivedTable);
                DataBase.DB.ExecuteUpdateMultiple(sql, false, null);
            }
            return(true);
        }       //	generateTable
Ejemplo n.º 5
0
 /// <summary>
 /// Parent Constructor
 /// </summary>
 /// <param name="parent">parent</param>
 public MViewComponent(MTable parent) : this(parent.GetCtx(), 0, parent.Get_TrxName())
 {
     SetClientOrg(parent);
     SetAD_Table_ID(parent.GetAD_Table_ID());
 }       //	MViewComponent