001package org.kuali.ole.describe.rule;
002
003
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.describe.bo.OleAccessMethod;
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 * OleAccessMethodsRule validates maintenance object for Access Method Maintenance Document
015 */
016public class OleAccessMethodsRule extends MaintenanceDocumentRuleBase {
017
018    /**
019     * @param document
020     * @return boolean
021     */
022
023    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
024        boolean isValid = true;
025        OleAccessMethod accessMethod = (OleAccessMethod) document.getNewMaintainableObject().getDataObject();
026        isValid &= validateAccessMethodCode(accessMethod);
027        return isValid;
028    }
029
030    /**
031     * This method  validates duplicate accessMethod Id and return boolean value.
032     *
033     * @param accessMethod
034     * @return boolean
035     */
036    private boolean validateAccessMethodCode(OleAccessMethod accessMethod) {
037        if (accessMethod.getAccessMethodCode() != null) {
038            Map<String, String> criteria = new HashMap<String, String>();
039            criteria.put(OLEConstants.OleAccessMethod.ACCESS_METHOD_CD, accessMethod.getAccessMethodCode());
040            List<OleAccessMethod> accessMethodInDatabase = (List<OleAccessMethod>) getBoService().findMatching(OleAccessMethod.class, criteria);
041            if ((accessMethodInDatabase.size() > 0)) {
042                for (OleAccessMethod accessMethodObj : accessMethodInDatabase) {
043                    Integer accessMethodId = accessMethodObj.getAccessMethodId();
044                    if (null == accessMethod.getAccessMethodId() || (accessMethod.getAccessMethodId().intValue() != accessMethodId.intValue())) {
045                        this.putFieldError(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "error.duplicate.code");
046                        return false;
047                    }
048                }
049            }
050        }
051        return true;
052    }
053}
054