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.cam.util;
017    
018    import org.apache.commons.lang.math.NumberUtils;
019    import org.kuali.kfs.sys.context.SpringContext;
020    import org.kuali.rice.kew.exception.WorkflowException;
021    import org.kuali.rice.kim.bo.Person;
022    import org.kuali.rice.kim.service.PersonService;
023    import org.kuali.rice.kns.util.GlobalVariables;
024    import org.kuali.rice.kns.util.KNSConstants;
025    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
026    import org.kuali.rice.kns.workflow.service.WorkflowDocumentService;
027    
028    /**
029     * In situation where the Maintainable does not have access to the document,  this class is a utility which 
030     * will retrieve the workflow document 
031     * 
032     */
033    public final class MaintainableWorkflowUtils {
034    
035        private MaintainableWorkflowUtils() {
036        }
037    
038        /**
039         * This method checks if the Workflow document is in Saved or Enroute status
040         * 
041         * @param documentNumber
042         * @return
043         */
044        public static boolean isDocumentSavedOrEnroute(String documentNumber) {
045            boolean isSaveOrEnroute = false;
046            KualiWorkflowDocument workflowDocument = getKualiWorkflowDocument(documentNumber);
047            
048            if (workflowDocument != null) {
049                isSaveOrEnroute = workflowDocument.stateIsSaved() || workflowDocument.stateIsEnroute();
050            }
051            return isSaveOrEnroute;
052        }
053        
054        /**
055         * Retrieve the KualiWorkflowDocument base on documentNumber
056         * 
057         * @param documentNumber
058         * @return
059         */
060        private static KualiWorkflowDocument getKualiWorkflowDocument(String documentNumber) {
061    
062            KualiWorkflowDocument workflowDocument = null;
063            WorkflowDocumentService workflowDocumentService = SpringContext.getBean(WorkflowDocumentService.class);
064            // we need to use the system user here, since this code could be called within the
065            // context of workflow, where there is no user session
066            Person person = SpringContext.getBean(PersonService.class).getPersonByPrincipalName(KNSConstants.SYSTEM_USER);
067            try {
068                workflowDocument = workflowDocumentService.createWorkflowDocument(NumberUtils.createLong(documentNumber), person);
069            }
070            catch (WorkflowException ex) {
071                throw new RuntimeException("Error to retrieve workflow document: " + documentNumber, ex);
072            }
073            return workflowDocument ;
074        }
075    
076    
077    
078    }