001package org.kuali.ole.select.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.select.bo.OLEPlatformStatus;
005import org.kuali.rice.krad.maintenance.MaintenanceDocument;
006import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
007import org.kuali.rice.krad.service.KRADServiceLocator;
008import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
009
010import java.util.HashMap;
011import java.util.List;
012import java.util.Map;
013
014/**
015 * Created by chenchulakshmig on 9/16/14.
016 * OLEPlatformStatusRule validates maintenance object for Platform Status Maintenance Document
017 */
018public class OLEPlatformStatusRule extends MaintenanceDocumentRuleBase {
019
020    /**
021     * @param document
022     * @return boolean
023     */
024    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
025        boolean isValid = true;
026        OLEPlatformStatus olePlatformStatus = (OLEPlatformStatus) document.getNewMaintainableObject().getDataObject();
027        isValid &= validatePlatformStatusName(olePlatformStatus);
028        return isValid;
029    }
030
031    /**
032     * * This method  validates duplicate platform status name and return boolean value.
033     *
034     * @param olePlatformStatus
035     * @return boolean
036     */
037    private boolean validatePlatformStatusName(OLEPlatformStatus olePlatformStatus) {
038        if (olePlatformStatus.getPlatformStatusName() != null) {
039            Map<String, String> criteria = new HashMap<String, String>();
040            criteria.put(OLEConstants.PLATFORM_STATUS_NAME, olePlatformStatus.getPlatformStatusName());
041            List<OLEPlatformStatus> authenticationMethodInDatabase = (List<OLEPlatformStatus>) KRADServiceLocator.getBusinessObjectService().findMatching(OLEPlatformStatus.class, criteria);
042            if ((authenticationMethodInDatabase.size() > 0)) {
043                for (OLEPlatformStatus authenticationMethodObj : authenticationMethodInDatabase) {
044                    String platformStatusId = authenticationMethodObj.getPlatformStatusId();
045                    if (null == olePlatformStatus.getPlatformStatusId() || (!olePlatformStatus.getPlatformStatusId().equals(platformStatusId))) {
046                        this.putFieldError(OLEConstants.PLATFORM_STATUS_NAM_FIELD, OLEConstants.DUPLICATE);
047                        return false;
048                    }
049                }
050            }
051        }
052        return true;
053    }
054}