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.ArrayList;
019    import java.util.Arrays;
020    import java.util.List;
021    
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.PurapWorkflowConstants.NodeDetails;
026    import org.kuali.kfs.module.purap.PurapWorkflowConstants.RequisitionDocument.NodeDetailEnum;
027    import org.kuali.kfs.module.purap.document.PaymentRequestDocument;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.KFSPropertyConstants;
030    import org.kuali.kfs.sys.document.validation.GenericValidation;
031    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
032    import org.kuali.rice.kew.exception.WorkflowException;
033    import org.kuali.rice.kns.bo.Note;
034    import org.kuali.rice.kns.util.GlobalVariables;
035    import org.kuali.rice.kns.util.KNSConstants;
036    import org.kuali.rice.kns.util.KualiDecimal;
037    import org.kuali.rice.kns.util.ObjectUtils;
038    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
039    
040    public class PaymentRequestInvoiceImageAttachmentValidation extends GenericValidation {
041    
042        public boolean validate(AttributedDocumentEvent event) {
043            boolean valid = true;
044            PaymentRequestDocument document = (PaymentRequestDocument)event.getDocument();
045            GlobalVariables.getMessageMap().clearErrorPath();
046            
047            if(isDocumentStoppedInRouteNode(document, "ImageAttachment")){
048                //assume false if we're in the correct node
049                valid = false;
050                
051                //loop through notes looking for a invoice image
052                List boNotes = document.getBoNotes();
053                if (ObjectUtils.isNotNull(boNotes)) {
054                    for (Object obj : boNotes) {
055                        Note note = (Note) obj;
056                        
057                        note.refreshReferenceObject("attachment");
058                        if (ObjectUtils.isNotNull(note.getAttachment()) && PurapConstants.AttachmentTypeCodes.ATTACHMENT_TYPE_INVOICE_IMAGE.equals(note.getAttachment().getAttachmentTypeCode())) {
059                            valid = true;
060                            break;
061                        }
062                    }
063                }
064                
065                if(valid == false){
066                    GlobalVariables.getMessageMap().putError(KNSConstants.NEW_DOCUMENT_NOTE_PROPERTY_NAME, PurapKeyConstants.ERROR_PAYMENT_REQUEST_INVOICE_REQUIRED);
067                }
068            }
069            
070            GlobalVariables.getMessageMap().clearErrorPath();
071                    
072            return valid;
073        }
074    
075        protected boolean isDocumentStoppedInRouteNode(PaymentRequestDocument document, String nodeName) {
076            List<String> currentRouteLevels = new ArrayList<String>();
077            try {
078                KualiWorkflowDocument workflowDoc = document.getDocumentHeader().getWorkflowDocument();
079                currentRouteLevels = Arrays.asList(document.getDocumentHeader().getWorkflowDocument().getNodeNames());
080                if (currentRouteLevels.contains(nodeName) && workflowDoc.isApprovalRequested()) {
081                    return true;
082                }
083                return false;
084            }
085            catch (WorkflowException e) {
086                throw new RuntimeException(e);
087            }
088        }
089    }