/** Create/Save Account */ private void action_Save() { log.info(""); /** Check completeness (mandatory fields) ... and for duplicates */ StringBuffer sb = new StringBuffer(); StringBuffer sql = new StringBuffer("SELECT C_ValidCombination_ID, Alias FROM C_ValidCombination WHERE "); Object value = null; if (s_AcctSchema.isHasAlias()) { value = f_Alias.getValue().toString(); if (value == null) sb.append(Msg.translate(Env.getCtx(), "Alias")).append(", "); } MAcctSchemaElement[] elements = s_AcctSchema.getAcctSchemaElements(); for (int i = 0; i < elements.length; i++) { MAcctSchemaElement ase = elements[i]; String type = ase.getElementType(); // if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Organization)) { value = f_AD_Org_ID.getValue(); sql.append("AD_Org_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Account)) { value = f_Account_ID.getValue(); sql.append("Account_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_SubAccount)) { value = f_SubAcct_ID.getValue(); sql.append("C_SubAcct_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Product)) { value = f_M_Product_ID.getValue(); sql.append("M_Product_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_BPartner)) { value = f_C_BPartner_ID.getValue(); sql.append("C_BPartner_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Campaign)) { value = f_C_Campaign_ID.getValue(); sql.append("C_Campaign_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_LocationFrom)) { value = f_C_LocFrom_ID.getValue(); sql.append("C_LocFrom_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_LocationTo)) { value = f_C_LocTo_ID.getValue(); sql.append("C_LocTo_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Project)) { value = f_C_Project_ID.getValue(); sql.append("C_Project_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_SalesRegion)) { value = f_C_SalesRegion_ID.getValue(); sql.append("C_SalesRegion_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_OrgTrx)) { value = f_AD_OrgTrx_ID.getValue(); sql.append("AD_OrgTrx_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Activity)) { value = f_C_Activity_ID.getValue(); sql.append("C_Activity_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_UserList1)) { value = f_User1_ID.getValue(); sql.append("User1_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_UserList2)) { value = f_User2_ID.getValue(); sql.append("User2_ID"); if (value == null) sql.append(" IS NULL AND "); else sql.append("=").append(value).append(" AND "); } // if (ase.isMandatory() && value == null) sb.append(ase.getName()).append(", "); } // Fields in Element Order if (sb.length() != 0) { ADialog.error(m_WindowNo, this, "FillMandatory", sb.substring(0, sb.length() - 2)); return; } if (f_AD_Org_ID == null || f_AD_Org_ID.getValue() == null) { ADialog.error(m_WindowNo, this, "FillMandatory", Msg.getElement(Env.getCtx(), "AD_Org_ID")); return; } if (f_Account_ID == null || f_Account_ID.getValue() == null) { ADialog.error(m_WindowNo, this, "FillMandatory", Msg.getElement(Env.getCtx(), "Account_ID")); return; } /** Check if already exists */ sql.append("AD_Client_ID=? AND C_AcctSchema_ID=?"); log.fine("Check = " + sql.toString()); int IDvalue = 0; String Alias = null; try { PreparedStatement pstmt = DB.prepareStatement(sql.toString(), null); pstmt.setInt(1, m_AD_Client_ID); pstmt.setInt(2, s_AcctSchema.getC_AcctSchema_ID()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { IDvalue = rs.getInt(1); Alias = rs.getString(2); } rs.close(); pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql.toString(), e); IDvalue = 0; } log.fine("ID=" + IDvalue + ", Alias=" + Alias); if (Alias == null) Alias = ""; // We have an account like this already - check alias if (IDvalue != 0 && s_AcctSchema.isHasAlias() && !f_Alias.getValue().toString().equals(Alias)) { sql = new StringBuffer("UPDATE C_ValidCombination SET Alias="); if (f_Alias.getValue().toString().length() == 0) sql.append("NULL"); else sql.append("'").append(f_Alias.getValue()).append("'"); sql.append(" WHERE C_ValidCombination_ID=").append(IDvalue); int i = 0; try { java.sql.PreparedStatement stmt = DB.prepareStatement( sql.toString(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE, null); i = stmt.executeUpdate(); stmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql.toString(), e); } if (i == 0) ADialog.error(m_WindowNo, this, "AccountNotUpdated"); } // load and display if (IDvalue != 0) { loadInfo(IDvalue, s_AcctSchema.getC_AcctSchema_ID()); action_Find(false); return; } log.config("New"); Alias = null; if (f_Alias != null) Alias = f_Alias.getValue().toString(); int C_SubAcct_ID = 0; if (f_SubAcct_ID != null && f_SubAcct_ID.getValue() != null) C_SubAcct_ID = ((Integer) f_SubAcct_ID.getValue()).intValue(); int M_Product_ID = 0; if (f_M_Product_ID != null && f_M_Product_ID.getValue() != null) M_Product_ID = ((Integer) f_M_Product_ID.getValue()).intValue(); int C_BPartner_ID = 0; if (f_C_BPartner_ID != null && f_C_BPartner_ID.getValue() != null) C_BPartner_ID = ((Integer) f_C_BPartner_ID.getValue()).intValue(); int AD_OrgTrx_ID = 0; if (f_AD_OrgTrx_ID != null && f_AD_OrgTrx_ID.getValue() != null) AD_OrgTrx_ID = ((Integer) f_AD_OrgTrx_ID.getValue()).intValue(); int C_LocFrom_ID = 0; if (f_C_LocFrom_ID != null && f_C_LocFrom_ID.getValue() != null) C_LocFrom_ID = ((Integer) f_C_LocFrom_ID.getValue()).intValue(); int C_LocTo_ID = 0; if (f_C_LocTo_ID != null && f_C_LocTo_ID.getValue() != null) C_LocTo_ID = ((Integer) f_C_LocTo_ID.getValue()).intValue(); int C_SRegion_ID = 0; if (f_C_SalesRegion_ID != null && f_C_SalesRegion_ID.getValue() != null) C_SRegion_ID = ((Integer) f_C_SalesRegion_ID.getValue()).intValue(); int C_Project_ID = 0; if (f_C_Project_ID != null && f_C_Project_ID.getValue() != null) C_Project_ID = ((Integer) f_C_Project_ID.getValue()).intValue(); int C_Campaign_ID = 0; if (f_C_Campaign_ID != null && f_C_Campaign_ID.getValue() != null) C_Campaign_ID = ((Integer) f_C_Campaign_ID.getValue()).intValue(); int C_Activity_ID = 0; if (f_C_Activity_ID != null && f_C_Activity_ID.getValue() != null) C_Activity_ID = ((Integer) f_C_Activity_ID.getValue()).intValue(); int User1_ID = 0; if (f_User1_ID != null && f_User1_ID.getValue() != null) User1_ID = ((Integer) f_User1_ID.getValue()).intValue(); int User2_ID = 0; if (f_User2_ID != null && f_User2_ID.getValue() != null) User2_ID = ((Integer) f_User2_ID.getValue()).intValue(); MAccount acct = MAccount.get( Env.getCtx(), m_AD_Client_ID, ((Integer) f_AD_Org_ID.getValue()).intValue(), s_AcctSchema.getC_AcctSchema_ID(), ((Integer) f_Account_ID.getValue()).intValue(), C_SubAcct_ID, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SRegion_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID, 0, 0); if (acct != null && acct.get_ID() == 0) acct.save(); // Show Info if (acct == null || acct.get_ID() == 0) loadInfo(0, 0); else { // Update Account with optional Alias if (Alias != null && Alias.length() > 0) { acct.setAlias(Alias); acct.save(); } loadInfo(acct.get_ID(), s_AcctSchema.getC_AcctSchema_ID()); } action_Find(false); } // action_Save
/** * Action Find. - create where clause - query database * * @param includeAliasCombination include alias combination */ private void action_Find(boolean includeAliasCombination) { log.info(""); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); // Create where Clause MQuery query = null; if (m_query != null) query = m_query.deepCopy(); else query = new MQuery(); // Alias if (includeAliasCombination && f_Alias != null && f_Alias.getValue().toString().length() > 0) { String value = f_Alias.getValue().toString().toUpperCase(); if (!value.endsWith("%")) value += "%"; query.addRestriction("UPPER(Alias)", MQuery.LIKE, value); } // Combination (mandatory) if (includeAliasCombination && f_Combination.getValue().toString().length() > 0) { String value = f_Combination.getValue().toString().toUpperCase(); if (!value.endsWith("%")) value += "%"; query.addRestriction("UPPER(Combination)", MQuery.LIKE, value); } // Org (mandatory) if (f_AD_Org_ID != null && f_AD_Org_ID.getValue() != null) query.addRestriction("AD_Org_ID", MQuery.EQUAL, f_AD_Org_ID.getValue()); // Account (mandatory) if (f_Account_ID != null && f_Account_ID.getValue() != null) query.addRestriction("Account_ID", MQuery.EQUAL, f_Account_ID.getValue()); if (f_SubAcct_ID != null && f_SubAcct_ID.getValue() != null) query.addRestriction("C_SubAcct_ID", MQuery.EQUAL, f_SubAcct_ID.getValue()); // Product if (f_M_Product_ID != null && f_M_Product_ID.getValue() != null) query.addRestriction("M_Product_ID", MQuery.EQUAL, f_M_Product_ID.getValue()); // BPartner if (f_C_BPartner_ID != null && f_C_BPartner_ID.getValue() != null) query.addRestriction("C_BPartner_ID", MQuery.EQUAL, f_C_BPartner_ID.getValue()); // Campaign if (f_C_Campaign_ID != null && f_C_Campaign_ID.getValue() != null) query.addRestriction("C_Campaign_ID", MQuery.EQUAL, f_C_Campaign_ID.getValue()); // Loc From if (f_C_LocFrom_ID != null && f_C_LocFrom_ID.getValue() != null) query.addRestriction("C_LocFrom_ID", MQuery.EQUAL, f_C_LocFrom_ID.getValue()); // Loc To if (f_C_LocTo_ID != null && f_C_LocTo_ID.getValue() != null) query.addRestriction("C_LocTo_ID", MQuery.EQUAL, f_C_LocTo_ID.getValue()); // Project if (f_C_Project_ID != null && f_C_Project_ID.getValue() != null) query.addRestriction("C_Project_ID", MQuery.EQUAL, f_C_Project_ID.getValue()); // SRegion if (f_C_SalesRegion_ID != null && f_C_SalesRegion_ID.getValue() != null) query.addRestriction("C_SalesRegion_ID", MQuery.EQUAL, f_C_SalesRegion_ID.getValue()); // Org Trx if (f_AD_OrgTrx_ID != null && f_AD_OrgTrx_ID.getValue() != null) query.addRestriction("AD_OrgTrx_ID", MQuery.EQUAL, f_AD_OrgTrx_ID.getValue()); // Activity if (f_C_Activity_ID != null && f_C_Activity_ID.getValue() != null) query.addRestriction("C_Activity_ID", MQuery.EQUAL, f_C_Activity_ID.getValue()); // User 1 if (f_User1_ID != null && f_User1_ID.getValue() != null) query.addRestriction("User1_ID", MQuery.EQUAL, f_User1_ID.getValue()); // User 2 if (f_User2_ID != null && f_User2_ID.getValue() != null) query.addRestriction("User2_ID", MQuery.EQUAL, f_User2_ID.getValue()); // Query m_mTab.setQuery(query); m_mTab.query(false); statusBar.setStatusDB(String.valueOf(m_mTab.getRowCount())); setCursor(Cursor.getDefaultCursor()); } // action_Find