} // 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
/// <summary> /// Check Existence of Std columns and create them in AD and DB /// </summary> /// <param name="table">name of the table</param> /// <param name="EntityType">Entity Type</param> public static void CheckStandardColumns(MTable table, String EntityType) { if (table == null) { return; } if (Utility.Util.IsEmpty(EntityType)) { EntityType = table.GetEntityType(); } table.GetColumns(true); // get new columns // Key Column String colName = table.GetTableName() + "_ID"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.ID); col.SetIsKey(true); col.SetIsUpdateable(false); col.SetIsMandatory(true); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "AD_Client_ID"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.TableDir); col.SetIsUpdateable(false); col.SetIsMandatory(true); col.SetAD_Val_Rule_ID(116); // Client Login col.SetDefaultValue("@#AD_Client_ID@"); col.SetConstraintType(X_AD_Column.CONSTRAINTTYPE_Restrict); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "AD_Org_ID"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.TableDir); col.SetIsUpdateable(false); col.SetIsMandatory(true); col.SetAD_Val_Rule_ID(104); // Org Security col.SetDefaultValue("@#AD_Org_ID@"); col.SetConstraintType(X_AD_Column.CONSTRAINTTYPE_Restrict); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "Created"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.DateTime); col.SetIsUpdateable(false); col.SetIsMandatory(true); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "Updated"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.DateTime); col.SetIsUpdateable(false); col.SetIsMandatory(true); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "CreatedBy"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.Table); col.SetAD_Reference_Value_ID(110); col.SetConstraintType(X_AD_Column.CONSTRAINTTYPE_DoNOTCreate); col.SetIsUpdateable(false); col.SetIsMandatory(true); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "UpdatedBy"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.Table); col.SetAD_Reference_Value_ID(110); col.SetConstraintType(X_AD_Column.CONSTRAINTTYPE_DoNOTCreate); col.SetIsUpdateable(false); col.SetIsMandatory(true); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "IsActive"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.YesNo); col.SetDefaultValue("Y"); col.SetIsUpdateable(true); col.SetIsMandatory(true); col.SetEntityType(EntityType); CreateColumn(col, table, true); } colName = "Export_ID"; if (table.GetColumn(colName) == null) { MColumn col = new MColumn(table); col.SetColumnName(colName); col.SetAD_Reference_ID(DisplayType.String); col.SetIsUpdateable(true); col.SetIsMandatory(false); col.SetEntityType(EntityType); col.SetFieldLength(50); CreateColumn(col, table, true); } } // checkStandardColumns
} // 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