001package org.kuali.ole.describe.rule;
002
003
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.describe.bo.OleAcquisitionMethod;
006import org.kuali.rice.krad.maintenance.MaintenanceDocument;
007import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
008
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012
013/**
014 * OleAcquisitionMethodsRule validates maintenance object for Acquisition Method Maintenance Document
015 */
016public class OleAcquisitionMethodsRule extends MaintenanceDocumentRuleBase {
017
018
019    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
020        boolean isValid = true;
021        OleAcquisitionMethod acquisitionMethod = (OleAcquisitionMethod) document.getNewMaintainableObject().getDataObject();
022
023        isValid &= validateAcquisitionCodeMethod(acquisitionMethod);
024        return isValid;
025    }
026
027    /**
028     * This method  validates duplicate acquisitionMethod Id and return boolean value.
029     *
030     * @param acquisitionMethod
031     * @return boolean
032     */
033    private boolean validateAcquisitionCodeMethod(OleAcquisitionMethod acquisitionMethod) {
034
035        if (acquisitionMethod.getAcquisitionMethodCode() != null) {
036            Map<String, String> criteria = new HashMap<String, String>();
037            criteria.put(OLEConstants.OleAcquisitionMethod.ACQUISITION_METHOD_CD, acquisitionMethod.getAcquisitionMethodCode());
038            List<OleAcquisitionMethod> acquisitionMethodInDatabase = (List<OleAcquisitionMethod>) getBoService().findMatching(OleAcquisitionMethod.class, criteria);
039            if ((acquisitionMethodInDatabase.size() > 0)) {
040                for (OleAcquisitionMethod acquisitionObj : acquisitionMethodInDatabase) {
041                    Integer acquisitionMethodId = acquisitionObj.getAcquisitionMethodId();
042                    if (null == acquisitionMethod.getAcquisitionMethodId() || (acquisitionMethod.getAcquisitionMethodId().intValue() != acquisitionMethodId.intValue())) {
043                        this.putFieldError(OLEConstants.OleAcquisitionMethod.ACQUISITION_METHOD_CODE, "error.duplicate.code");
044                        return false;
045                    }
046                }
047            }
048        }
049        return true;
050    }
051}