001package org.kuali.ole.select.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.select.bo.OLECancellationReason;
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: meenau
015 * Date: 12/9/13
016 * Time: 4:42 PM
017 * To change this template use File | Settings | File Templates.
018 */
019public class OLECancellationReasonRule extends MaintenanceDocumentRuleBase {
020
021    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
022        boolean isValid = true;
023        OLECancellationReason oleCancellationReason = (OLECancellationReason) document.getNewMaintainableObject().getDataObject();
024
025        isValid &= validateOLECancellationReasonName(oleCancellationReason);
026        return isValid;
027    }
028
029    /**
030     * This method  validates duplicate Cancel Reason Name and return boolean value.
031     *
032     * @param oleCancellationReason
033     * @return boolean
034     */
035    private boolean validateOLECancellationReasonName(OLECancellationReason oleCancellationReason) {
036
037        if (oleCancellationReason.getCancelReasonName() != null) {
038            Map<String, String> criteria = new HashMap<String, String>();
039            criteria.put(OLEConstants.OLECancellationReasonRule.CNCL_RSN_NAME, oleCancellationReason.getCancelReasonName());
040            List<OLECancellationReason> savedOLECancellationReason = (List<OLECancellationReason>) getBoService().findMatching(OLECancellationReason.class, criteria);
041            if ((savedOLECancellationReason.size() > 0)) {
042                for (OLECancellationReason cancellationReason : savedOLECancellationReason) {
043                    String cancelReasonId = cancellationReason.getCancelReasonId();
044                    if (null == oleCancellationReason.getCancelReasonId() || (!oleCancellationReason.getCancelReasonId().equalsIgnoreCase(cancelReasonId))) {
045                        this.putFieldError(OLEConstants.OLECancellationReasonRule.CNCL_RSN_NAME_FIELD, "error.duplicate.name");
046                        return false;
047                    }
048                }
049            }
050        }
051        return true;
052    }
053}