private MAttributeSetInstance saveSelection0() { log.info(""); final MAttributeSet as = asiTemplate.getMAttributeSet(); Check.assumeNotNull(as, "as not null"); // Create a new ASI which is copying the existing one final MAttributeSetInstance asi = new MAttributeSetInstance(getCtx(), 0, ITrx.TRXNAME_ThreadInherited); InterfaceWrapperHelper.copyValues( asiTemplate, asi, false); // honorIsCalculated=false => copy everything asi.setM_AttributeSet(as); // make sure we have the right AttributeSet model set // boolean changed = false; final Set<String> mandatory = new LinkedHashSet<>(); // Lot if (!m_productWindow && as.isLot()) { String text = fieldLotString.getText(); asi.setLot(text); if (as.isLotMandatory() && (text == null || text.length() == 0)) { mandatory.add(msgBL.translate(getCtx(), "Lot")); } changed = true; } // Lot // Serial No if (!m_productWindow && as.isSerNo()) { final String serNo = fieldSerNo.getText(); asi.setSerNo(serNo); if (as.isSerNoMandatory() && Check.isEmpty(serNo, true)) { mandatory.add(msgBL.translate(getCtx(), "SerNo")); } changed = true; } // SerNo // // Guarantee Date (if required) if (fieldGuaranteeDateDisplayed) { final Timestamp guaranteeDate = fieldGuaranteeDate.getValue(); asi.setGuaranteeDate(guaranteeDate); if (as.isGuaranteeDate() && as.isGuaranteeDateMandatory() && guaranteeDate == null) { mandatory.add(msgBL.translate(getCtx(), I_M_AttributeSetInstance.COLUMNNAME_GuaranteeDate)); } changed = true; } // // New Instance if (changed || asi.getM_AttributeSetInstance_ID() <= 0) { InterfaceWrapperHelper.save(asi); changed = true; } final int attributeSetInstanceId = asi.getM_AttributeSetInstance_ID(); // // Save Instance Attributes for (final MAttribute attribute : m_attributes) { final CEditor editor = attributeId2editor.get(attribute.getM_Attribute_ID()); if (MAttribute.ATTRIBUTEVALUETYPE_List.equals(attribute.getAttributeValueType())) { @SuppressWarnings("unchecked") final CComboBox<I_M_AttributeValue> editorCombo = (CComboBox<I_M_AttributeValue>) editor; final I_M_AttributeValue attributeValue = editorCombo.getSelectedItem(); if (attribute.isMandatory() && attributeValue == null) { mandatory.add(attribute.getName()); } attribute.setMAttributeInstance(attributeSetInstanceId, attributeValue); } else if (MAttribute.ATTRIBUTEVALUETYPE_Number.equals(attribute.getAttributeValueType())) { final VNumber editorNumber = (VNumber) editor; final BigDecimal value = (BigDecimal) editorNumber.getValue(); if (attribute.isMandatory() && value == null) { mandatory.add(attribute.getName()); } attribute.setMAttributeInstance(attributeSetInstanceId, value); } else if (MAttribute.ATTRIBUTEVALUETYPE_Date.equals(attribute.getAttributeValueType())) { final VDate editorDate = (VDate) editor; final Timestamp value = editorDate.getValue(); if (attribute.isMandatory() && value == null) { mandatory.add(attribute.getName()); } attribute.setMAttributeInstance(attributeSetInstanceId, value); } else { final VString editorString = (VString) editor; final String value = editorString.getText(); if (attribute.isMandatory() && Check.isEmpty(value, false)) { mandatory.add(attribute.getName()); } attribute.setMAttributeInstance(attributeSetInstanceId, value); } changed = true; } // for all attributes // // Throw exception if there are some mandatory fields which were not set if (!mandatory.isEmpty()) { throw new AdempiereException("@FillMandatory@ " + StringUtils.toString(mandatory, ", ")); } // Save Model if (changed) { asi.setMAttributeSet(as); // NOTE: this is workaround for the case when M_AttributeSet_ID=0 attributeSetInstanceBL.setDescription(asi); InterfaceWrapperHelper.save(asi); } return asi; }
/** * Add Attribute Line * * @param attribute attribute * @param product product level attribute * @param readOnly value is read only */ private void addAttributeLine(final MAttribute attribute) { final boolean product = m_productWindow; final boolean readOnly = false; log.fine(attribute + ", Product=" + product + ", R/O=" + readOnly); CLabel label = new CLabel(attribute.getName()); if (product) { label.setFont(new Font(label.getFont().getFontName(), Font.BOLD, label.getFont().getSize())); } if (attribute.getDescription() != null) { label.setToolTipText(attribute.getDescription()); } centerPanel.add(label, new ALayoutConstraint(m_row++, 0)); // final int attributeSetInstanceId = asiTemplate.getM_AttributeSetInstance_ID(); final int attributeId = attribute.getM_Attribute_ID(); final MAttributeInstance instance = attribute.getMAttributeInstance(attributeSetInstanceId); if (MAttribute.ATTRIBUTEVALUETYPE_List.equals(attribute.getAttributeValueType())) { InterfaceWrapperHelper.setDynAttribute( attribute, Env.DYNATTR_WindowNo, attributeContext.getWindowNo()); InterfaceWrapperHelper.setDynAttribute( attribute, Env.DYNATTR_TabNo, attributeContext.getTabNo()); // tabNo final I_M_AttributeValue[] values = attribute.getMAttributeValues(getSOTrx()); // optional = null final CComboBox<I_M_AttributeValue> editor = new CComboBox<>(values); boolean found = false; if (instance != null && instance.getM_AttributeValue_ID() > 0) { for (int i = 0; i < values.length; i++) { if (values[i] != null && values[i].getM_AttributeValue_ID() == instance.getM_AttributeValue_ID()) { editor.setSelectedIndex(i); found = true; break; } } if (found) log.fine( "Attribute=" + attribute.getName() + " #" + values.length + " - found: " + instance); else log.warning( "Attribute=" + attribute.getName() + " #" + values.length + " - NOT found: " + instance); } // setComboBox else { log.fine("Attribute=" + attribute.getName() + " #" + values.length + " no instance"); } label.setLabelFor(editor); centerPanel.add(editor, null); if (readOnly) editor.setEnabled(false); else attributeId2editor.put(attributeId, editor); } else if (MAttribute.ATTRIBUTEVALUETYPE_Number.equals(attribute.getAttributeValueType())) { final VNumber editor = new VNumber( attribute.getName(), // ColumnName attribute.isMandatory(), // mandatory false, // IsReadOnly true, // IsUpdateable DisplayType.Number, // DisplayType attribute.getName() // Title ); if (instance != null) { if (InterfaceWrapperHelper.isNull(instance, I_M_AttributeInstance.COLUMNNAME_ValueNumber)) { editor.setValue(null); } else { editor.setValue(instance.getValueNumber()); } } else { // better don't set a default value // editor.setValue(Env.ZERO); } label.setLabelFor(editor); centerPanel.add(editor, null); if (readOnly) editor.setEnabled(false); else attributeId2editor.put(attributeId, editor); } else if (MAttribute.ATTRIBUTEVALUETYPE_Date.equals(attribute.getAttributeValueType())) { final VDate editor = new VDate( attribute.getName(), // columnName attribute.isMandatory(), // mandatory false, // isReadOnly true, // isUpdateable DisplayType.Date, // displayType attribute.getName() // title ); if (instance != null) editor.setValue(instance.getValueDate()); label.setLabelFor(editor); centerPanel.add(editor, null); if (readOnly) editor.setEnabled(false); else attributeId2editor.put(attributeId, editor); } else // Text Field { VString editor = new VString( attribute.getName(), // ColumnName attribute.isMandatory(), // mandatory false, // isReadOnly true, // isUpdateable 20, // displayLength INSTANCE_VALUE_LENGTH, // fieldLength null, // VFormat null // ObscureType ); if (instance != null) editor.setText(instance.getValue()); label.setLabelFor(editor); centerPanel.add(editor, null); if (readOnly) editor.setEnabled(false); else attributeId2editor.put(attributeId, editor); } // // Add our attribute to the list of attributes m_attributes.add(attribute); } // addAttributeLine