/// <summary> /// Function to Create Version Fields (Defaults for Version tab) against version tab /// </summary> /// <param name="ver_AD_Tab_ID"> Version TabID</param> /// <param name="verFieldDS">Dataset of Version Fields</param> /// <param name="hasVerFlds">check of Version Field</param> /// <param name="verColumnsDS"> Dataset of Version Columns</param> /// <param name="Ver_TableID"> Version TableID</param> /// <returns>String (Message)</returns> private string CreateDefaultFields(int ver_AD_Tab_ID, DataSet verFieldDS, bool hasVerFlds, DataSet verColumnsDS, int Ver_TableID) { // Get system elements against Version Table's Columns string VerTableName = Util.GetValueOfString(DB.ExecuteScalar("SELECT TableName FROM AD_Table WHERE AD_Table_ID = " + Ver_TableID, null, Get_Trx())); GetSystemElements(VerTableName); // Get field Group ID for Versioning int AD_FieldGroup_ID = Util.GetValueOfInt(DB.ExecuteScalar("SELECT AD_FieldGroup_ID FROM AD_FieldGroup WHERE Name = 'Versioning'", null, Get_Trx())); // get max seqno + 500 to set seqnumbers for new fields int SeqNo = Util.GetValueOfInt(DB.ExecuteScalar("SELECT MAX(SeqNo) + 500 FROM AD_Field WHERE AD_Tab_ID = " + ver_AD_Tab_ID, null, Get_Trx())); for (int i = 0; i < listDefVerCols.Count; i++) { // check if system element exist DataRow[] drVerFld = verFieldDS.Tables[0].Select("AD_Element_ID = " + _listDefVerElements[i]); if (drVerFld.Length <= 0) { int VerColID = 0; MField verFld = new MField(GetCtx(), 0, Get_TrxName()); verFld.SetAD_Tab_ID(ver_AD_Tab_ID); DataRow[] drVerCol = verColumnsDS.Tables[0].Select("ColumnName = '" + listDefVerCols[i].ToString() + "'"); if (drVerCol.Length > 0) { VerColID = Util.GetValueOfInt(drVerCol[0]["AD_Column_ID"]); } else { log.Log(Level.SEVERE, "Version Column Not Found :: " + listDefVerCols[i].ToString()); Get_TrxName().Rollback(); return(Msg.GetMsg(GetCtx(), "ColumnNameNotFound") + " :: " + listDefVerCols[i].ToString()); } verFld.SetAD_Column_ID(VerColID); if (listDefVerCols[i].ToString() == "Processing" || listDefVerCols[i].ToString() == "Processed" || listDefVerCols[i].ToString() == VerTableName + "_ID") { verFld.SetIsDisplayed(false); } if (listDefVerCols[i].ToString() == "VersionLog") { verFld.SetDisplayLength(100); } //if (listDefVerCols[i].ToString() == "ProcessedVersion" || listDefVerCols[i].ToString() == "RecordVersion") verFld.SetIsSameLine(true); // Set Field Group for Versioning field if (AD_FieldGroup_ID > 0) { verFld.SetAD_FieldGroup_ID(AD_FieldGroup_ID); } verFld.SetSeqNo(SeqNo); //"VersionValidFrom", "IsVersionApproved", "ProcessedVersion", "RecordVersion", "Processed", "Processing", "VersionLog", "OldVersion" if (listDefVerCols[i].ToString() == "VersionValidFrom") { verFld.SetMRIsDisplayed("Y"); verFld.SetMRSeqNo(5); } if (listDefVerCols[i].ToString() == "RecordVersion") { verFld.SetMRIsDisplayed("Y"); verFld.SetMRSeqNo(2); } if (!verFld.Save()) { log.Log(Level.SEVERE, "Version Field not saved :: " + verFld.GetName()); Get_TrxName().Rollback(); return(Msg.GetMsg(GetCtx(), "FieldNotSaved") + " :: " + verFld.GetName()); } else { SeqNo = SeqNo + 10; } } } return(""); }
/// <summary> /// Process /// throws Exception /// </summary> /// <returns>info</returns> override protected string DoIt() { //IDbTransaction trx = ExecuteQuery.GerServerTransaction(); MTab tab = new MTab(GetCtx(), p_AD_Tab_ID, Get_Trx()); //MTab tab = new MTab(GetCtx(), p_AD_Tab_ID, trx.ToString()); if (p_AD_Tab_ID == 0 || tab == null || tab.Get_ID() == 0) { throw new Exception("@NotFound@: @AD_Tab_ID@ " + p_AD_Tab_ID); } //log.info(tab.toString()); int count = 0; string sql = "SELECT * FROM AD_Column c " + "WHERE NOT EXISTS (SELECT * FROM AD_Field f " + "WHERE c.AD_Column_ID=f.AD_Column_ID" + " AND c.AD_Table_ID=@AD_Table_Id" // #1 + " AND f.AD_Tab_ID=@AD_Tab_Id)" // #2 + " AND AD_Table_ID=@AD_Table_Id1" // #3 + " AND NOT (Name LIKE 'Created%' OR Name LIKE 'Updated%')" + " AND IsActive='Y' " + "ORDER BY Name desc"; try { SqlParameter[] param = new SqlParameter[3]; param[0] = new SqlParameter("@AD_Table_Id", tab.GetAD_Table_ID()); param[1] = new SqlParameter("@AD_Tab_Id", tab.GetAD_Tab_ID()); param[2] = new SqlParameter("@AD_Table_Id1", tab.GetAD_Table_ID()); DataSet ds = DataBase.DB.ExecuteDataset(sql, param, Get_Trx()); //DataSet ds1 = ExecuteQuery.ExecuteDataset(sql); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DataRow dr = ds.Tables[0].Rows[i]; //MColumn column = new MColumn (getCtx(),dr, Get_Trx()); //MColumn column = new MColumn(GetCtx(), dr, null); MColumn column = new MColumn(GetCtx(), dr, Get_Trx()); // MField field = new MField(tab); field.SetColumn(column); if (column.IsKey()) { field.SetIsDisplayed(false); } if (column.GetColumnName().ToString() == "AD_Client_ID") { field.SetSeqNo(10); } if (column.GetColumnName().ToString() == "AD_Org_ID") { field.SetIsSameLine(true); field.SetSeqNo(20); } //Export_ID Check [Hide Export Field] if (column.GetColumnName().ToString() == "Export_ID") { field.SetIsDisplayed(false); } if (field.Save()) { AddLog(0, DateTime.MinValue, Decimal.Parse(count.ToString()), column.GetName()); //AddLog(0, DateTime.MinValue, null, ); count++; } } ds = null; } catch (Exception e) { log.Log(Level.SEVERE, sql, e); } return("@Created@ #" + count); }
/// <summary> /// Create version fields against version tab /// </summary> /// <param name="tab"> Object of MTab </param> /// <param name="Ver_AD_Tab_ID"> Tab ID of Version window </param> /// <param name="Ver_AD_Table_ID"> Table ID of Version </param> /// <returns>string (Message)</returns> private string CreateVerFields(MTab tab, int Ver_AD_Tab_ID, int Ver_AD_Table_ID) { // Get all fields from Master Tab int[] fields = MTable.GetAllIDs("AD_Field", "AD_Tab_ID = " + tab.GetAD_Tab_ID(), Get_TrxName()); bool hasOrigCols = false; bool hasVerFields = false; bool hasVerCols = false; // Get columns from Master table DataSet origColDS = DB.ExecuteDataset("SELECT AD_Column_ID, Name,ColumnSql, ColumnName FROM AD_Column WHERE AD_Table_ID = " + tab.GetAD_Table_ID(), null, Get_TrxName()); if (origColDS != null && origColDS.Tables[0].Rows.Count > 0) { hasOrigCols = true; } // Get fields from Version Tab DataSet verFieldDS = DB.ExecuteDataset("SELECT f.AD_Column_ID, f.Name, f.AD_Field_ID, (SELECT c.AD_Element_ID FROM AD_Column c WHERE c.AD_Column_ID = f.AD_Column_ID) AS AD_Element_ID FROM AD_Field f WHERE f.AD_Tab_ID = " + Ver_AD_Tab_ID, null, Get_TrxName()); if (verFieldDS != null && verFieldDS.Tables[0].Rows.Count > 0) { hasVerFields = true; } // Get Columns from Version Table DataSet verColumnsDS = DB.ExecuteDataset("SELECT AD_Column_ID, Name, ColumnName FROM AD_Column WHERE AD_Table_ID = " + Ver_AD_Table_ID, null, Get_TrxName()); if (verColumnsDS != null && verColumnsDS.Tables[0].Rows.Count > 0) { hasVerCols = true; } StringBuilder sbColName = new StringBuilder(""); foreach (int fld in fields) { bool createNew = true; MField origFld = new MField(GetCtx(), fld, Get_TrxName()); // check if Field already exist for Version Tab else create new if (hasVerFields) { DataRow[] drFld = verFieldDS.Tables[0].Select("Name = '" + origFld.GetName() + "'"); if (drFld.Length > 0) { createNew = false; } } // if Field do not exist on Version tab if (createNew) { sbColName.Clear(); int VerColID = 0; // Get column Info from Column ID of Master Table DataRow[] drOrigColName = origColDS.Tables[0].Select("AD_Column_ID = " + origFld.GetAD_Column_ID()); if (drOrigColName.Length > 0) { if (Util.GetValueOfString(drOrigColName[0]["ColumnSQL"]).Trim() != "") { continue; } sbColName.Append(Util.GetValueOfString(drOrigColName[0]["ColumnName"])); // check whether Column exist in Version table with column name of Master Table // if column not found return with Message DataRow[] drVerCol = verColumnsDS.Tables[0].Select("ColumnName = '" + sbColName.ToString() + "'"); if (drVerCol.Length > 0) { VerColID = Util.GetValueOfInt(drVerCol[0]["AD_Column_ID"]); } else { log.Log(Level.SEVERE, "Version Column Not Found :: " + sbColName.ToString()); Get_TrxName().Rollback(); return(Msg.GetMsg(GetCtx(), "ColumnNameNotFound") + " :: " + sbColName.ToString()); } } else { log.Log(Level.SEVERE, "Column ID not found in Original table :: " + origFld.GetAD_Column_ID()); Get_TrxName().Rollback(); return(Msg.GetMsg(GetCtx(), "ColumnNameNotFound") + " :: " + origFld.GetAD_Column_ID()); } // Only create Version Field if Version Column found if (VerColID > 0) { // check if field is already created with column // else skip creating field int fldID = Util.GetValueOfInt(DB.ExecuteScalar("SELECT AD_Field_ID FROM AD_Field WHERE AD_Column_ID = " + VerColID + " AND AD_Tab_ID = " + Ver_AD_Tab_ID, null, Get_TrxName())); if (fldID <= 0) { // Create Field for Column, copy field from Master tables Field tab on Version Field against Version Tab MField verFld = new MField(GetCtx(), 0, Get_TrxName()); origFld.CopyTo(verFld); verFld.SetAD_Tab_ID(Ver_AD_Tab_ID); verFld.SetAD_Column_ID(VerColID); verFld.SetExport_ID(null); if (!verFld.Save()) { ValueNamePair vnp = VLogger.RetrieveError(); string error = ""; if (vnp != null) { error = vnp.GetName(); if (error == "" && vnp.GetValue() != null) { error = vnp.GetValue(); } } if (error == "") { error = "Error in creating Version Field"; } log.Log(Level.SEVERE, "Version Field not saved :: " + verFld.GetName() + " :: " + error); Get_TrxName().Rollback(); return(Msg.GetMsg(GetCtx(), "FieldNotSaved") + " :: " + verFld.GetName()); } } } } } // Fill Dataset again from Version Field verFieldDS = DB.ExecuteDataset("SELECT f.AD_Column_ID, f.Name, f.AD_Field_ID, (SELECT c.AD_Element_ID FROM AD_Column c WHERE c.AD_Column_ID = f.AD_Column_ID) AS AD_Element_ID FROM AD_Field f WHERE f.AD_Tab_ID = " + Ver_AD_Tab_ID, null, Get_TrxName()); // Create Default Fields against default Columns for Version tab string retMsg = CreateDefaultFields(Ver_AD_Tab_ID, verFieldDS, hasVerFields, verColumnsDS, Ver_AD_Table_ID); if (retMsg != "") { Get_TrxName().Rollback(); return(retMsg); } return(""); }