001package org.kuali.ole.describe.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.describe.bo.OleAction;
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 * OleActionRule validates maintenance object for Action Maintenance Document
014 */
015public class OleActionRule extends MaintenanceDocumentRuleBase {
016
017
018    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
019        boolean isValid = true;
020        OleAction oleAction = (OleAction) document.getNewMaintainableObject().getDataObject();
021        isValid &= validateActionCode(oleAction);
022        return isValid;
023    }
024
025    /**
026     * This method  validates duplicate action Id and return boolean value.
027     *
028     * @param oleNewAction
029     * @return boolean
030     */
031    private boolean validateActionCode(OleAction oleNewAction) {
032
033        if (oleNewAction.getActionCode() != null) {
034            Map<String, String> criteria = new HashMap<String, String>();
035            criteria.put(OLEConstants.OleAction.ACTION_CD, oleNewAction.getActionCode());
036            List<OleAction> actionCodeInDatabase = (List<OleAction>) getBoService().findMatching(OleAction.class, criteria);
037            if ((actionCodeInDatabase.size() > 0)) {
038                for (OleAction actionObj : actionCodeInDatabase) {
039                    Integer actionId = actionObj.getActionId();
040                    if (null == oleNewAction.getActionId() || (oleNewAction.getActionId().intValue() != actionId.intValue())) {
041                        this.putFieldError(OLEConstants.OleAction.ACTION_CODE, "error.duplicate.code");
042                        return false;
043                    }
044                }
045            }
046        }
047        return true;
048    }
049}