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