001package org.kuali.ole.describe.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.OLEParameterConstants;
005import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
006import org.kuali.ole.describe.bo.OleItemAvailableStatus;
007import org.kuali.rice.core.api.config.property.ConfigContext;
008import org.kuali.rice.core.api.criteria.QueryByCriteria;
009import org.kuali.rice.kns.service.KNSServiceLocator;
010import org.kuali.rice.krad.maintenance.MaintenanceDocument;
011import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
012import org.kuali.rice.krad.service.KRADServiceLocator;
013
014import java.util.HashMap;
015import java.util.List;
016import java.util.Map;
017
018/**
019 * OleItemAvailableStatusRule validates maintenance object for Instance Item Available Status Maintenance Document
020 */
021public class OleItemAvailableStatusRule extends MaintenanceDocumentRuleBase {
022
023    private InstanceEditorFormDataHandler instanceEditorFormDataHandler;
024
025    /**
026     * Gets the InstanceEditorFormDataHandler attribute.
027     *
028     * @return Returns InstanceEditorFormDataHandler.
029     */
030    private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
031        if (null == instanceEditorFormDataHandler) {
032            instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
033        }
034        return instanceEditorFormDataHandler;
035    }
036
037    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
038        boolean isValid = true;
039        OleItemAvailableStatus oleItemAvailableStatus = (OleItemAvailableStatus) document.getNewMaintainableObject().getDataObject();
040
041        isValid &= validateItemAvailableStatusCode(oleItemAvailableStatus);
042        return isValid;
043    }
044
045    /**
046     * This method  validates duplicate Item Available Status Id and return boolean value.
047     *
048     * @param oleItemAvailableStatus
049     * @return boolean
050     */
051    private boolean validateItemAvailableStatusCode(OleItemAvailableStatus oleItemAvailableStatus) {
052
053        String itemProperty = getInstanceEditorFormDataHandler()
054                .getParameter(OLEConstants.DESC_NMSPC, OLEConstants.DESCRIBE_COMPONENT, OLEParameterConstants.ITEM_STATUS_READONLY);
055        String[] itemArray = itemProperty.split(",");
056        for (String ItemStatus : itemArray) {
057            if (!(oleItemAvailableStatus.isActive())) {
058                if (ItemStatus.equalsIgnoreCase(oleItemAvailableStatus.getItemAvailableStatusCode())) {
059                    this.putFieldError(OLEConstants.OleItemAvailableStatus.ITEM_ACTIVE_INDICATOR, "error.activeIndicator.false");
060                }
061            }
062        }
063        if (oleItemAvailableStatus.getItemAvailableStatusCode() != null) {
064
065            Map<String, String> criteria = new HashMap<String, String>();
066
067            criteria.put(OLEConstants.OleItemAvailableStatus.ITEM_AVAILABLE_STATUS_CD, oleItemAvailableStatus.getItemAvailableStatusCode());
068
069            List<OleItemAvailableStatus> itemAvailableStatusCodeInDatabase = (List<OleItemAvailableStatus>) KRADServiceLocator.getDataObjectService().findMatching(OleItemAvailableStatus.class, QueryByCriteria.Builder.andAttributes(criteria).build()).getResults();
070
071            if ((itemAvailableStatusCodeInDatabase.size() > 0)) {
072                for (OleItemAvailableStatus oleItemAvailableStatusObj : itemAvailableStatusCodeInDatabase) {
073                    String itemAvailableStatusId = oleItemAvailableStatusObj.getItemAvailableStatusId();
074
075                    if (null == oleItemAvailableStatus.getItemAvailableStatusId() ||
076                            !itemAvailableStatusId.equalsIgnoreCase(oleItemAvailableStatus.getItemAvailableStatusId())) {
077                        this.putFieldError(OLEConstants.OleItemAvailableStatus.ITEM_AVAILABLE_STATUS_CODE, "error.duplicate.code");
078                        return false;
079                    }
080                }
081            }
082        }
083
084        return true;
085    }
086}