001package org.kuali.ole.select.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.select.bo.OLERequestPriority;
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: divyaj
015 * Date: 6/21/13
016 * Time: 4:56 PM
017 * To change this template use File | Settings | File Templates.
018 */
019public class OLERequestPriorityRule extends MaintenanceDocumentRuleBase {
020
021
022    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
023        boolean isValid = true;
024        OLERequestPriority OLERequestPriority = (OLERequestPriority) document.getNewMaintainableObject().getDataObject();
025
026        isValid &= validateOleMaterialTypeCode(OLERequestPriority);
027        return isValid;
028    }
029
030
031    private boolean validateOleMaterialTypeCode(OLERequestPriority OLERequestPriority) {
032
033        if (OLERequestPriority.getOleRequestPriorityName() != null) {
034            Map<String, String> criteria = new HashMap<String, String>();
035            criteria.put(OLEConstants.OleRequestPriority.REQUEST_PRIORITY_NAME, OLERequestPriority.getOleRequestPriorityName());
036            List<OLERequestPriority> savedOLERequestPriority = (List<OLERequestPriority>) getBoService().findMatching(OLERequestPriority.class, criteria);
037            if ((savedOLERequestPriority.size() > 0)) {
038                for (OLERequestPriority requestPriority : savedOLERequestPriority) {
039                    String requestPriorityId = requestPriority.getOleRequestPriorityId();
040                    if (null == OLERequestPriority.getOleRequestPriorityId() || (!OLERequestPriority.getOleRequestPriorityId().equalsIgnoreCase(requestPriorityId))) {
041                        this.putFieldError(OLEConstants.OleRequestPriority.REQUEST_PRIORITY_FIELD, OLEConstants.OleRequestPriority.ERROR_REQUEST_PRIORITY_NAME);
042                        return false;
043                    }
044                }
045            }
046        }
047        return true;
048    }
049}