001/*
002 * Copyright 2008-2009 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.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.module.purap.PurapKeyConstants;
020import org.kuali.ole.module.purap.document.PaymentRequestDocument;
021import org.kuali.ole.sys.document.validation.GenericValidation;
022import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
023import org.kuali.rice.kew.api.WorkflowDocument;
024import org.kuali.rice.krad.bo.Note;
025import org.kuali.rice.krad.util.GlobalVariables;
026import org.kuali.rice.krad.util.KRADConstants;
027import org.kuali.rice.krad.util.ObjectUtils;
028
029import java.util.List;
030import java.util.Set;
031
032public class PaymentRequestInvoiceImageAttachmentValidation extends GenericValidation {
033
034    public boolean validate(AttributedDocumentEvent event) {
035        boolean valid = true;
036        PaymentRequestDocument document = (PaymentRequestDocument) event.getDocument();
037        GlobalVariables.getMessageMap().clearErrorPath();
038
039        if (isDocumentStoppedInRouteNode(document, "ImageAttachment")) {
040            //assume false if we're in the correct node
041            valid = false;
042
043            //loop through notes looking for a invoice image
044            List boNotes = document.getNotes();
045            if (ObjectUtils.isNotNull(boNotes)) {
046                for (Object obj : boNotes) {
047                    Note note = (Note) obj;
048
049                    note.refreshReferenceObject("attachment");
050                    if (ObjectUtils.isNotNull(note.getAttachment()) && PurapConstants.AttachmentTypeCodes.ATTACHMENT_TYPE_INVOICE_IMAGE.equals(note.getAttachment().getAttachmentTypeCode())) {
051                        valid = true;
052                        break;
053                    }
054                }
055            }
056
057            if (valid == false) {
058                GlobalVariables.getMessageMap().putError(KRADConstants.NEW_DOCUMENT_NOTE_PROPERTY_NAME, PurapKeyConstants.ERROR_PAYMENT_REQUEST_INVOICE_REQUIRED);
059            }
060        }
061
062        GlobalVariables.getMessageMap().clearErrorPath();
063
064        return valid;
065    }
066
067    /**
068     * @param document
069     * @param nodeName
070     * @return
071     */
072    protected boolean isDocumentStoppedInRouteNode(PaymentRequestDocument document, String nodeName) {
073        WorkflowDocument workflowDoc = document.getDocumentHeader().getWorkflowDocument();
074        Set<String> currentRouteLevels = workflowDoc.getCurrentNodeNames();
075        if (currentRouteLevels.contains(nodeName) && workflowDoc.isApprovalRequested()) {
076            return true;
077        }
078        return false;
079    }
080}