001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.purap.document.validation.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderSensitiveData;
022    import org.kuali.kfs.module.purap.businessobject.SensitiveData;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.vnd.businessobject.CommodityCode;
026    import org.kuali.rice.kns.document.MaintenanceDocument;
027    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
028    import org.kuali.rice.kns.service.BusinessObjectService;
029    import org.kuali.rice.kns.service.DataDictionaryService;
030    
031    /**
032     * Validates the SensitiveData maintenance document.
033     */
034    public class SensitiveDataRule extends MaintenanceDocumentRuleBase {
035    
036        /**
037         * This method performs custom route business rule checks on the document being routed.  The rules include confirming the 
038         * validity of the work group.
039         * 
040         * @param document The document being routed.
041         * @return True if all the business rules pass, false otherwise.
042         * 
043         * @see MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
044         */
045        @Override
046        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
047            boolean valid = true;
048            valid &= validateInactivationBlocking();
049            return valid;
050        }
051    
052        protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
053            boolean valid = true;
054            valid &= validateInactivationBlocking();
055            return valid;
056        }
057    
058        protected boolean validateInactivationBlocking() {
059            SensitiveData oldSensitiveData = (SensitiveData)getOldBo();
060            SensitiveData newSensitiveData = (SensitiveData)getNewBo();
061            if (oldSensitiveData.isActive() && !newSensitiveData.isActive()) {
062                if (hasABlockingRecord(newSensitiveData.getSensitiveDataCode())) {
063                    String documentLabel = "SensitiveData"; //SpringContext.getBean(DataDictionaryService.class).getDocumentLabelByClass(newSensitiveData.getClass());
064                    putGlobalError(KFSKeyConstants.ERROR_CANNOT_INACTIVATE_USED_BY_ACTIVE_RECORDS, documentLabel);
065                    return false;
066                }
067            }
068            return true;
069        }
070        
071        protected boolean hasABlockingRecord(String sensitiveDataCode) {
072            Map<String, Object> queryMap = new HashMap<String, Object>();
073            queryMap.put("sensitiveDataCode", sensitiveDataCode);
074    
075            //Check whether there are any PurchaseOrderSensitiveData whose sensitiveDataCode match with this SensitiveData's code
076            boolean hasPurchaseOrderSensitiveDataBlockingRecord = SpringContext.getBean(BusinessObjectService.class).countMatching(PurchaseOrderSensitiveData.class, queryMap) > 0;
077            
078            queryMap.put("active", true);
079            //Check whether there are any active CommodityCode whose sensitiveDataCode match with this SensitiveData's code
080            boolean hasCommodityCodeBlockingRecord = SpringContext.getBean(BusinessObjectService.class).countMatching(CommodityCode.class, queryMap) > 0;
081            
082            return hasPurchaseOrderSensitiveDataBlockingRecord || hasCommodityCodeBlockingRecord;
083        }
084    
085    }