/**
  * Check availability and insert record
  *
  * @return true if saved/updated
  */
 private boolean cmd_save() {
   log.config("");
   //	Set AssignDateTo
   Calendar date = new GregorianCalendar();
   getDateAndTimeFrom(date);
   Timestamp assignDateFrom = new Timestamp(date.getTimeInMillis());
   BigDecimal qty = fQty.getValue();
   KeyNamePair uom = (KeyNamePair) m_lookup.get(fResource.getSelectedItem());
   int minutes = MUOMConversion.convertToMinutes(Env.getCtx(), uom.getKey(), qty);
   Timestamp assignDateTo = TimeUtil.addMinutess(assignDateFrom, minutes);
   m_mAssignment.setAssignDateTo(assignDateTo);
   //
   //	m_mAssignment.dump();
   return m_mAssignment.save();
 } //	cmdSave
  /**
   * Assignment Dialog.
   *
   * <pre>
   * 		Creates a new assignment oor displays an assignment
   * 		Create new:	(ID == 0)
   * 			check availability & create assignment
   * 			(confirmed when order/incoice/timeExpense is processed)
   * 			alternatively let InfoResource do the assignment
   * 			return ID
   * 		Existing assignment: (ID != 0)
   * 			if confirmed - no change.
   * 			ability to delete or change assignment
   * 			return ID
   * 	</pre>
   *
   * @param mAssignment Assignment
   * @param allowZoom allow to zoom to schedule
   * @param allowDelete allow to delete recorde
   */
  public WAssignmentDialog(
      MResourceAssignment mAssignment, boolean allowZoom, boolean allowDelete) {
    super();
    this.setTitle(Msg.getMsg(Env.getCtx(), "VAssignmentDialog"));
    this.setAttribute("mode", "modal");
    this.setBorder("normal");

    log.config(mAssignment.toString());
    m_mAssignment = mAssignment;
    try {
      init();
      if (!allowZoom) confirmPanel.getButton("Zoom").setVisible(false);
      delete.setVisible(allowDelete);
    } catch (Exception e) {
      log.log(Level.SEVERE, "", e);
    }
    setDisplay(); //	from mAssignment
    //
    AEnv.showWindow(this);
  } //	VAssignmentDialog
  /** Initialize component & values from m_mAssignment */
  private void setDisplay() {
    m_setting = true;

    //	Set Resource
    int S_Resource_ID = m_mAssignment.getS_Resource_ID();
    KeyNamePair[] resources = new KeyNamePair[m_lookup.size()];
    m_lookup.keySet().toArray(resources);
    for (int i = 0; i < resources.length; i++) {
      if (resources[i].getKey() == S_Resource_ID) {
        fResource.setSelectedIndex(i);
        break;
      }
    }
    ListItem listItem = fResource.getSelectedItem();
    KeyNamePair check = new KeyNamePair((Integer) listItem.getValue(), listItem.getLabel());
    if (check == null || check.getKey() != S_Resource_ID) {
      if (m_mAssignment.getS_ResourceAssignment_ID() == 0) // 	new record select first
      fResource.setSelectedItem(fResource.getSelectedItem()); // 	initiates UOM display
      else log.log(Level.SEVERE, "Resource not found ID=" + S_Resource_ID);
    }

    //	Set Date, Qty
    fDateFrom.setValue(m_mAssignment.getAssignDateFrom());
    fTimeFrom.setValue(m_mAssignment.getAssignDateFrom());
    fQty.setValue(m_mAssignment.getQty());

    //	Name, Description
    fName.setValue(m_mAssignment.getName());
    fDescription.setValue(m_mAssignment.getDescription());

    //	Set Editor to R/O if confirmed
    boolean readWrite = true;
    if (m_mAssignment.isConfirmed()) readWrite = false;
    confirmPanel.getButton("Cancel").setVisible(readWrite);
    fResource.setEnabled(readWrite);
    fDateFrom.setReadonly(!readWrite);
    fQty.setEnabled(readWrite);

    m_setting = false;
  } //	dynInit
  public void onEvent(Event e) throws Exception {
    if (m_setting) return;
    //	Update Assignment
    ListItem listItem = fResource.getSelectedItem();
    KeyNamePair resource =
        listItem != null
            ? new KeyNamePair((Integer) listItem.getValue(), listItem.getLabel())
            : null;
    if (resource != null) {
      int S_Resource_ID = resource.getKey();
      m_mAssignment.setS_Resource_ID(S_Resource_ID);
    }

    Calendar date = new GregorianCalendar();
    getDateAndTimeFrom(date);

    Timestamp assignDateFrom = new Timestamp(date.getTimeInMillis());
    if (assignDateFrom != null) m_mAssignment.setAssignDateFrom(assignDateFrom);
    if (fQty.getValue() != null) {
      BigDecimal qty = fQty.getValue();
      m_mAssignment.setQty(qty);
    }
    m_mAssignment.setName((String) fName.getValue());
    m_mAssignment.setDescription((String) fDescription.getValue());

    //	Resource - Look up UOM
    if (e.getTarget() == fResource) {
      Object o = m_lookup.get(fResource.getSelectedItem());
      if (o == null) lUOM.setValue(" ? ");
      else lUOM.setValue(o.toString());
    }

    //	Zoom - InfoResource
    else if (e.getTarget().getId().equals("Zoom")) {
      InfoSchedule is = new InfoSchedule(m_mAssignment, true);
      if (is.getMResourceAssignment() != null) {
        m_mAssignment = is.getMResourceAssignment();
        //	setDisplay();
        detach();
      }
      is = null;
    }

    //	cancel - return
    else if (e.getTarget().getId().equals("Cancel")) {
      m_cancel = true;
      detach();
    }

    //	delete - delete and return
    else if (e.getTarget().getId().equals("Delete")) {
      if (m_mAssignment.delete(true)) {
        m_mAssignment = null;
        detach();
      } else FDialog.error(0, this, "ResourceAssignmentNotDeleted");
    }

    //	OK - Save
    else if (e.getTarget().getId().equals("Ok")) {
      if (cmd_save()) detach();
    }
  }