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.HashSet;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.module.purap.PurapConstants;
023    import org.kuali.kfs.module.purap.PurapKeyConstants;
024    import org.kuali.kfs.module.purap.PurapPropertyConstants;
025    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderVendorQuote;
026    import org.kuali.kfs.module.purap.businessobject.SensitiveData;
027    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
028    import org.kuali.kfs.sys.KFSKeyConstants;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.kfs.vnd.businessobject.VendorDetail;
032    import org.kuali.kfs.vnd.document.service.VendorService;
033    import org.kuali.rice.kns.util.GlobalVariables;
034    
035    /**
036     * A validation that checks whether the given accounting line is accessible to the given user or not
037     */
038    public class PurchaseOrderAssignSensitiveDataValidation extends GenericValidation {
039    
040        private PurchaseOrderDocument accountingDocumentForValidation;
041        private String sensitiveDataAssignmentReason;
042        private List<SensitiveData> sensitiveDatasAssigned;
043        
044        /**
045         * Applies rules for validation of sensitive data assignment to the PurchaseOrder document:
046         * The assignment reason must not be empty; 
047         * The assigned sensitive data entries must be active and not redundant.
048         * 
049         * @param document  A PurchaseOrderDocument (or one of its children)
050         * @return      True if all relevant validation rules are passed.
051         */
052        public boolean validate(AttributedDocumentEvent event) {
053            boolean valid = true;
054            GlobalVariables.getMessageMap().clearErrorPath();        
055            HashSet<String> sdset = new HashSet<String>();
056            
057            if (StringUtils.isEmpty(sensitiveDataAssignmentReason)) {
058                GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_SENSITIVE_DATA_TAB_ERRORS, PurapKeyConstants.ERROR_ASSIGN_SENSITIVE_DATA_REASON_EMPTY);
059                valid = false;                            
060            }
061            
062            for (Object sdobj : sensitiveDatasAssigned) {
063                SensitiveData sd = (SensitiveData)sdobj;
064                if (!sd.isActive()) {
065                    GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_SENSITIVE_DATA_TAB_ERRORS, PurapKeyConstants.ERROR_ASSIGN_SENSITIVE_DATA_INACTIVE, sd.getSensitiveDataDescription());
066                    valid = false;                
067                }
068                else if (!sdset.add(sd.getSensitiveDataCode())) {
069                    GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_SENSITIVE_DATA_TAB_ERRORS, PurapKeyConstants.ERROR_ASSIGN_SENSITIVE_DATA_REDUNDANT, sd.getSensitiveDataDescription());
070                    valid = false;                                    
071                }            
072            }
073    
074            GlobalVariables.getMessageMap().clearErrorPath();
075            return valid;
076        }
077     
078        public PurchaseOrderDocument getAccountingDocumentForValidation() {
079            return accountingDocumentForValidation;
080        }
081    
082        public void setAccountingDocumentForValidation(PurchaseOrderDocument accountingDocumentForValidation) {
083            this.accountingDocumentForValidation = accountingDocumentForValidation;
084        }
085        
086        public String getSensitiveDataAssignmentReason() {
087            return sensitiveDataAssignmentReason;
088        }
089    
090        public void setSensitiveDataAssignmentReason(String sensitiveDataAssignmentReason) {
091            this.sensitiveDataAssignmentReason = sensitiveDataAssignmentReason;
092        }
093    
094        public List<SensitiveData> getSensitiveDatasAssigned() {
095            return sensitiveDatasAssigned;
096        }
097    
098        public void setSensitiveDatasAssigned(List<SensitiveData> sensitiveDatasAssigned) {
099            this.sensitiveDatasAssigned = sensitiveDatasAssigned;
100        }
101    
102    }
103