001package org.kuali.ole.select.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.select.bo.OleLicenseRequestLocation;
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/22/13
016 * Time: 12:36 PM
017 * To change this template use File | Settings | File Templates.
018 */
019public class OleLicenseRequestLocationRule extends MaintenanceDocumentRuleBase {
020    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
021        boolean isValid = true;
022        OleLicenseRequestLocation oleLicenseRequestLocation = (OleLicenseRequestLocation) document.getNewMaintainableObject().getDataObject();
023
024        isValid &= validateOleLicenseRequestLocation(oleLicenseRequestLocation);
025        return isValid;
026    }
027
028    /**
029     * This method  validates duplicate Agreement Type Id and return boolean value.
030     *
031     * @param oleLicenseRequestLocation
032     * @return boolean
033     */
034    private boolean validateOleLicenseRequestLocation(OleLicenseRequestLocation oleLicenseRequestLocation) {
035
036        if (oleLicenseRequestLocation.getName() != null) {
037            Map<String, String> criteria = new HashMap<String, String>();
038            criteria.put(OLEConstants.NAME, oleLicenseRequestLocation.getName());
039            List<OleLicenseRequestLocation> savedOleLicenseRequestLocation = (List<OleLicenseRequestLocation>) getBoService().findMatching(OleLicenseRequestLocation.class, criteria);
040            if ((savedOleLicenseRequestLocation.size() > 0)) {
041                for (OleLicenseRequestLocation location : savedOleLicenseRequestLocation) {
042                    String licenseId = location.getId();
043                    if (null == oleLicenseRequestLocation.getId() || (!oleLicenseRequestLocation.getId().equalsIgnoreCase(licenseId))) {
044                        this.putFieldError(OLEConstants.NAME_FIELD, OLEConstants.ERROR_DUPLICATE_CODE);
045                        return false;
046                    }
047                }
048            }
049        }
050        return true;
051    }
052}