001package org.kuali.ole.describe.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.describe.bo.OleReceiptStatus;
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 * OleReceiptStatusRule validates maintenance object for Receipt Status Maintenance Document
014 */
015public class OleReceiptStatusRule extends MaintenanceDocumentRuleBase {
016
017
018    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
019        boolean isValid = true;
020        OleReceiptStatus oleReceiptStatus = (OleReceiptStatus) document.getNewMaintainableObject().getDataObject();
021
022        isValid &= validateReceiptStatusCode(oleReceiptStatus);
023        return isValid;
024    }
025
026    /**
027     * This method  validates duplicate receiptStatus Id and return boolean value.
028     *
029     * @param oleReceiptStatus
030     * @return boolean
031     */
032    private boolean validateReceiptStatusCode(OleReceiptStatus oleReceiptStatus) {
033
034        if (oleReceiptStatus.getReceiptStatusCode() != null) {
035
036            Map<String, String> criteria = new HashMap<String, String>();
037
038            criteria.put(OLEConstants.OleReceiptStatus.RECEIPT_STATUS_CD, oleReceiptStatus.getReceiptStatusCode());
039
040            List<OleReceiptStatus> receiptStatusCodeInDatabase = (List<OleReceiptStatus>) getBoService().findMatching(OleReceiptStatus.class, criteria);
041
042            if ((receiptStatusCodeInDatabase.size() > 0)) {
043
044                for (OleReceiptStatus receiptStatusObj : receiptStatusCodeInDatabase) {
045                    String receiptStatusId = receiptStatusObj.getReceiptStatusId();
046                    if (null == oleReceiptStatus.getReceiptStatusId() ||
047                            !(receiptStatusId.equalsIgnoreCase(oleReceiptStatus.getReceiptStatusId()))) {
048                        this.putFieldError(OLEConstants.OleReceiptStatus.RECEIPT_STATUS_CODE, "error.duplicate.code");
049                        return false;
050                    }
051                }
052            }
053            return true;
054        }
055        return false;
056    }
057}