/**
   * ************************************************************************ Save Multiple records
   * - Clone a record and assign new values to each clone for a specific column.
   *
   * @param ctx context
   * @param tableName Table Name
   * @param columnName Column for which value need to be changed
   * @param recordId Record to clone
   * @param values Values to be assigned to clones for the specified column
   * @param trxName Transaction
   * @throws Exception If error is occured when loading the PO or saving clones
   * @author ashley
   */
  protected void saveMultipleRecords(
      Properties ctx,
      String tableName,
      String columnName,
      int recordId,
      Integer[] values,
      String trxName)
      throws Exception {
    if (values == null) {
      return;
    }

    int oldRow = gridTab.getCurrentRow();
    GridField lineField = gridTab.getField("Line");

    for (int i = 0; i < values.length; i++) {
      if (!gridTab.dataNew(true)) {
        throw new IllegalStateException("Could not clone tab");
      }

      gridTab.setValue(columnName, values[i]);

      if (lineField != null) {
        gridTab.setValue(lineField, 0);
      }

      if (!gridTab.dataSave(false)) {
        throw new IllegalStateException("Could not update tab");
      }

      gridTab.setCurrentRow(oldRow);
    }
  }
Exemple #2
0
 public static void bind(DataModel data, GridTab curTab) {
   Set<String> keys = data.getData().keySet();
   for (String key : keys) {
     if (curTab.getField(key) != null) {
       // ?? wat abt callouts?
       curTab.setValue(key, data.get(key));
     }
   }
 }
 @Override
 public synchronized Object put(Object key, Object value) {
   if (gridTab == null) throw new IllegalStateException("Method not supported (gridTab is null)");
   if (gridTab.getCurrentRow() != row) {
     return ctx.put(key, value);
   }
   String columnName = getColumnName(key);
   if (columnName == null) {
     return ctx.put(key, value);
   }
   GridField field = gridTab.getField(columnName);
   if (field == null) {
     return ctx.put(key, value);
   }
   Object valueOld = field.getValue();
   field.setValue(value, false); // inserting=false
   return valueOld;
 }
  /**
   * Dyanmic Init. When a row is selected, the editor values are set (editors do not change grid)
   *
   * @return true if initialized
   */
  private boolean initAccount() {
    m_AD_Client_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "AD_Client_ID");
    //	Get AcctSchema Info
    if (s_AcctSchema == null || s_AcctSchema.getC_AcctSchema_ID() != m_C_AcctSchema_ID)
      s_AcctSchema = new MAcctSchema(Env.getCtx(), m_C_AcctSchema_ID, null);
    log.config(s_AcctSchema.toString() + ", #" + s_AcctSchema.getAcctSchemaElements().length);
    Env.setContext(Env.getCtx(), m_WindowNo, "C_AcctSchema_ID", m_C_AcctSchema_ID);

    //  Model
    int AD_Window_ID = 153; // 	Maintain Account Combinations
    GridWindowVO wVO = AEnv.getMWindowVO(m_WindowNo, AD_Window_ID, 0);
    if (wVO == null) return false;
    m_mWindow = new GridWindow(wVO);
    m_mTab = m_mWindow.getTab(0);
    // Make sure is the tab is loaded - teo_sarca [ 1659124 ]
    if (!m_mTab.isLoadComplete()) m_mWindow.initTab(0);

    //  ParameterPanel restrictions
    m_mTab.getField("Alias").setDisplayLength(15);
    m_mTab.getField("Combination").setDisplayLength(15);
    //  Grid restrictions
    m_mTab.getField("AD_Client_ID").setDisplayed(false);
    m_mTab.getField("C_AcctSchema_ID").setDisplayed(false);
    m_mTab.getField("IsActive").setDisplayed(false);
    m_mTab.getField("IsFullyQualified").setDisplayed(false);
    //  don't show fields not being displayed in this environment
    for (int i = 0; i < m_mTab.getFieldCount(); i++) {
      GridField field = m_mTab.getField(i);
      if (!field.isDisplayed(true)) //  check context
      field.setDisplayed(false);
    }

    //  GridController
    m_gridController = new GridController();
    m_gridController.initGrid(m_mTab, true, m_WindowNo, null, null);
    m_gridController.setPreferredSize(new Dimension(300, 100));
    panel.add(m_gridController, BorderLayout.CENTER);

    //  Prepare Parameter
    m_gbc.anchor = GridBagConstraints.NORTHWEST;
    m_gbc.gridy = 0; // 	line
    m_gbc.gridx = 0;
    m_gbc.gridwidth = 1;
    m_gbc.insets = m_fieldInsets;
    m_gbc.fill = GridBagConstraints.HORIZONTAL;
    m_gbc.weightx = 0;
    m_gbc.weighty = 0;

    int TabNo = 0;

    //	Alias
    if (s_AcctSchema.isHasAlias()) {
      GridField alias = m_mTab.getField("Alias");
      f_Alias = VEditorFactory.getEditor(m_mTab, alias, false);
      addLine(alias, f_Alias, false);
    } //	Alias

    //	Combination
    GridField combination = m_mTab.getField("Combination");
    f_Combination = VEditorFactory.getEditor(m_mTab, combination, false);
    addLine(combination, f_Combination, false);
    m_newRow = true;

    /** Create Fields in Element Order */
    MAcctSchemaElement[] elements = s_AcctSchema.getAcctSchemaElements();
    for (int i = 0; i < elements.length; i++) {
      MAcctSchemaElement ase = elements[i];
      String type = ase.getElementType();
      boolean isMandatory = ase.isMandatory();
      //
      if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Organization)) {
        GridField field = m_mTab.getField("AD_Org_ID");
        f_AD_Org_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_AD_Org_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Account)) {
        GridField field = m_mTab.getField("Account_ID");
        f_Account_ID = VEditorFactory.getEditor(m_mTab, field, false);
        //	((VLookup)f_Account_ID).setWidth(400);
        addLine(field, f_Account_ID, isMandatory);
        f_Account_ID.addVetoableChangeListener(this);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_SubAccount)) {
        GridField field = m_mTab.getField("C_SubAcct_ID");
        f_SubAcct_ID = VEditorFactory.getEditor(m_mTab, field, false);
        //	((VLookup)f_SubAcct_ID).setWidth(400);
        addLine(field, f_SubAcct_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Product)) {
        GridField field = m_mTab.getField("M_Product_ID");
        f_M_Product_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_M_Product_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_BPartner)) {
        GridField field = m_mTab.getField("C_BPartner_ID");
        f_C_BPartner_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_C_BPartner_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Campaign)) {
        GridField field = m_mTab.getField("C_Campaign_ID");
        f_C_Campaign_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_C_Campaign_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_LocationFrom)) {
        GridField field = m_mTab.getField("C_LocFrom_ID");
        f_C_LocFrom_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_C_LocFrom_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_LocationTo)) {
        GridField field = m_mTab.getField("C_LocTo_ID");
        f_C_LocTo_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_C_LocTo_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Project)) {
        GridField field = m_mTab.getField("C_Project_ID");
        f_C_Project_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_C_Project_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_SalesRegion)) {
        GridField field = m_mTab.getField("C_SalesRegion_ID");
        f_C_SalesRegion_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_C_SalesRegion_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_OrgTrx)) {
        GridField field = m_mTab.getField("AD_OrgTrx_ID");
        f_AD_OrgTrx_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_AD_OrgTrx_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_Activity)) {
        GridField field = m_mTab.getField("C_Activity_ID");
        f_C_Activity_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_C_Activity_ID, isMandatory);
      }
      //	User1
      else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_UserList1)) {
        GridField field = m_mTab.getField("User1_ID");
        f_User1_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_User1_ID, isMandatory);
      } else if (type.equals(MAcctSchemaElement.ELEMENTTYPE_UserList2)) {
        GridField field = m_mTab.getField("User2_ID");
        f_User2_ID = VEditorFactory.getEditor(m_mTab, field, false);
        addLine(field, f_User2_ID, isMandatory);
      }
    } //	Create Fields in Element Order

    //	Add description
    m_newRow = true;
    m_gbc.gridy = m_line++;
    m_gbc.gridx = 0;
    m_gbc.gridwidth = 4;
    m_gbc.insets = new Insets(5, 15, 2, 0); // 	top,left,bottom,right
    m_gbc.fill = GridBagConstraints.HORIZONTAL;
    f_Description.setFont(f_Description.getFont().deriveFont(Font.ITALIC));
    parameterPanel.add(f_Description, m_gbc);

    //	Finish
    m_query = new MQuery();
    m_query.addRestriction("C_AcctSchema_ID", MQuery.EQUAL, m_C_AcctSchema_ID);
    m_query.addRestriction("IsFullyQualified", MQuery.EQUAL, "Y");
    if (m_mAccount.C_ValidCombination_ID == 0) m_mTab.setQuery(MQuery.getEqualQuery("1", "2"));
    else {
      MQuery query = new MQuery();
      query.addRestriction("C_AcctSchema_ID", MQuery.EQUAL, m_C_AcctSchema_ID);
      query.addRestriction("C_ValidCombination_ID", MQuery.EQUAL, m_mAccount.C_ValidCombination_ID);
      m_mTab.setQuery(query);
    }
    m_mTab.query(false);
    m_gridController.getTable().addMouseListener(new VAccountDialog_mouseAdapter(this));
    m_gridController.addDataStatusListener(this);

    statusBar.setStatusLine(s_AcctSchema.toString());
    statusBar.setStatusDB("?");

    //	Initial value
    if (m_mAccount.C_ValidCombination_ID != 0) m_mTab.navigate(0);

    log.config("fini");
    return true;
  } //	initAccount
  /** @param e */
  public void valueChange(ValueChangeEvent e) {
    if (gridTab.isProcessed()) //  only active records
    {
      Object source = e.getSource();
      if (source instanceof WEditor) {
        // Elaine 2009/05/06
        WEditor editor = (WEditor) source;
        GridField gridField = editor.getGridField();

        if (gridField != null) {
          if (!gridField.isEditable(true)) {
            logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
            return;
          }
        } else if (!editor.isReadWrite()) {
          logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
          return;
        }
      } else {
        logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
        return;
      }
    } //  processed
    logger.config(
        "("
            + gridTab.toString()
            + ") "
            + e.getPropertyName()
            + "="
            + e.getNewValue()
            + " ("
            + e.getOldValue()
            + ") "
            + (e.getOldValue() == null ? "" : e.getOldValue().getClass().getName()));

    //  Get Row/Col Info
    GridTable mTable = gridTab.getTableModel();
    int row = gridTab.getCurrentRow();
    int col = mTable.findColumn(e.getPropertyName());
    //
    if (e.getNewValue() == null
        && e.getOldValue() != null
        && e.getOldValue().toString().length() > 0) //  some editors return "" instead of null
      //        	  this is the original code from GridController, don't know what it does there but
      // it breaks ignore button for web ui
      //            mTable.setChanged (true);
      mTable.setValueAt(e.getNewValue(), row, col);
    else {

      Object newValue = e.getNewValue();
      Integer newValues[] = null;

      if (newValue instanceof Integer[]) {
        newValues = ((Integer[]) newValue);
        newValue = newValues[0];

        if (newValues.length > 1) {
          Integer valuesCopy[] = new Integer[newValues.length - 1];
          System.arraycopy(newValues, 1, valuesCopy, 0, valuesCopy.length);
          newValues = valuesCopy;
        } else {
          newValues = null;
        }
      } else if (newValue instanceof Object[]) {
        logger.severe("Multiple values can only be processed for IDs (Integer)");
        throw new IllegalArgumentException(
            "Multiple Selection values not available for this field. " + e.getPropertyName());
      }

      mTable.setValueAt(newValue, row, col);
      //  Force Callout
      if (e.getPropertyName().equals("S_ResourceAssignment_ID")) {
        GridField mField = gridTab.getField(col);
        if (mField != null && mField.getCallout().length() > 0) {
          gridTab.processFieldChange(mField); //  Dependencies & Callout
        }
      }

      if (newValues != null && newValues.length > 0) {
        // Save data, since record need to be used for generating clones.
        if (!gridTab.dataSave(false)) {
          throw new AdempiereException("SaveError");
        }

        // Retrieve the current record ID
        int recordId = gridTab.getKeyID(gridTab.getCurrentRow());

        Trx trx = Trx.get(Trx.createTrxName(), true);
        trx.start();
        try {
          saveMultipleRecords(
              Env.getCtx(),
              gridTab.getTableName(),
              e.getPropertyName(),
              recordId,
              newValues,
              trx.getTrxName());
          trx.commit();
          gridTab.dataRefreshAll();
        } catch (Exception ex) {
          trx.rollback();
          logger.severe(ex.getMessage());
          throw new AdempiereException("SaveError");
        } finally {
          trx.close();
        }
      }
    }
  } // ValueChange