/**
   * ************************************************************************ 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);
    }
  }
Example #2
0
  public ProcessHelper setPO(Object o) {
    PO po = InterfaceWrapperHelper.getStrictPO(o);
    if (po != null) {
      setTableId(po.get_Table_ID());
      setRecordId(po.get_ID());
      return this;
    }

    GridTab gridTab = GridTabWrapper.getGridTab(o);
    if (gridTab != null) {
      setTableId(gridTab.get_TableName());
      setRecordId(gridTab.getKeyID(gridTab.getCurrentRow()));
      return this;
    }

    fail("Object " + o + " is not supported in setPO");
    return this;
  }
 @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;
 }
  /** @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