001package org.kuali.ole.select.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.select.bo.OleAgreementMethod;
005import org.kuali.rice.krad.maintenance.MaintenanceDocument;
006import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
007
008import java.util.HashMap;
009import java.util.List;
010import java.util.Map;
011
012/**
013 * Created with IntelliJ IDEA.
014 * User: ?
015 * Date: 3/21/13
016 * Time: 1:16 PM
017 * To change this template use File | Settings | File Templates.
018 */
019public class OleAgreementMethodRule extends MaintenanceDocumentRuleBase {
020
021    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
022        boolean isValid = true;
023        OleAgreementMethod oleAgreementMethod = (OleAgreementMethod) document.getNewMaintainableObject().getDataObject();
024
025        isValid &= validateOleAgreementMethodName(oleAgreementMethod);
026        return isValid;
027    }
028
029    /**
030     * This method  validates duplicate Agreement Method Id and return boolean value.
031     *
032     * @param oleAgreementMethod
033     * @return boolean
034     */
035    private boolean validateOleAgreementMethodName(OleAgreementMethod oleAgreementMethod) {
036
037        if (oleAgreementMethod.getAgreementMethodName() != null) {
038            Map<String, String> criteria = new HashMap<String, String>();
039            criteria.put(OLEConstants.OleAgreementMethodRule.AGR_MTHD_NAME, oleAgreementMethod.getAgreementMethodName());
040            List<OleAgreementMethod> savedOleAgreementMethod = (List<OleAgreementMethod>) getBoService().findMatching(OleAgreementMethod.class, criteria);
041            if ((savedOleAgreementMethod.size() > 0)) {
042                for (OleAgreementMethod agreementMethod : savedOleAgreementMethod) {
043                    String agreementMethodId = agreementMethod.getAgreementMethodId();
044                    if (null == oleAgreementMethod.getAgreementMethodId() || (!oleAgreementMethod.getAgreementMethodId().equalsIgnoreCase(agreementMethodId))) {
045                        this.putFieldError(OLEConstants.OleAgreementMethodRule.AGR_MTHD_NAME_FIELD, OLEConstants.ERROR_DUPLICATE_CODE);
046                        return false;
047                    }
048                }
049            }
050        }
051        return true;
052    }
053}