001    /**
002     * Copyright 2005-2013 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.rice.kew.actions;
017    
018    import org.kuali.rice.kew.api.WorkflowDocument;
019    import org.kuali.rice.kew.api.WorkflowDocumentFactory;
020    import org.kuali.rice.kew.api.WorkflowRuntimeException;
021    import org.kuali.rice.kew.api.action.ActionType;
022    import org.kuali.rice.kew.api.exception.WorkflowException;
023    import org.kuali.rice.kew.framework.postprocessor.*;
024    import org.kuali.rice.kew.framework.postprocessor.AfterProcessEvent;
025    import org.kuali.rice.kew.framework.postprocessor.BeforeProcessEvent;
026    import org.kuali.rice.kew.framework.postprocessor.DeleteEvent;
027    import org.kuali.rice.kew.framework.postprocessor.DocumentLockingEvent;
028    import org.kuali.rice.kew.framework.postprocessor.DocumentRouteLevelChange;
029    import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
030    import org.kuali.rice.kew.framework.postprocessor.ProcessDocReport;
031    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
032    
033    import java.util.ArrayList;
034    import java.util.Arrays;
035    import java.util.List;
036    import java.util.Set;
037    
038    
039    /**
040     * This is a post processor class used for a Super User Test 
041     * 
042     * @author Kuali Rice Team (rice.collab@kuali.org)
043     *
044     */
045    public class SuperUserActionInvalidPostProcessor implements PostProcessor {
046    
047        private static final String USER_AUTH_ID = "rkirkend";
048        
049        /**
050         * THIS METHOD WILL THROW AN EXCEPTION IF OLD ROUTE NODE IS 'WorkflowTemplate'
051         */
052        public ProcessDocReport doActionTaken(org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent event) throws Exception {
053            if (isDocumentPostProcessable(WorkflowDocumentFactory.loadDocument(getPrincipalId(USER_AUTH_ID), event.getDocumentId()))) {
054                return new ProcessDocReport(true, "");
055            }
056            throw new WorkflowRuntimeException("Post Processor should never be called in this instance");
057        }
058    
059        /**
060         * THIS METHOD WILL THROW AN EXCEPTION IF OLD ROUTE NODE IS 'WorkflowTemplate'
061         */
062        public ProcessDocReport afterActionTaken(ActionType performed, org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent event) throws Exception {
063            if (isDocumentPostProcessable(WorkflowDocumentFactory.loadDocument(getPrincipalId(USER_AUTH_ID), event.getDocumentId()))) {
064                return new ProcessDocReport(true, "");
065            }
066            throw new WorkflowRuntimeException("Post Processor should never be called in this instance");
067        }
068    
069        /**
070         * THIS METHOD WILL THROW AN EXCEPTION IF OLD ROUTE NODE IS 'WorkflowTemplate'
071         */
072        public ProcessDocReport doDeleteRouteHeader(DeleteEvent event) throws Exception {
073            if (isDocumentPostProcessable(WorkflowDocumentFactory.loadDocument(getPrincipalId(USER_AUTH_ID), event.getDocumentId()))) {
074                return new ProcessDocReport(true, "");
075            }
076            throw new WorkflowRuntimeException("Post Processor should never be called in this instance");
077        }
078    
079        /**
080         * THIS METHOD WILL THROW AN EXCEPTION IF OLD ROUTE NODE IS 'WorkflowTemplate'
081         */
082        public ProcessDocReport doRouteLevelChange(DocumentRouteLevelChange levelChangeEvent) throws Exception {
083            if (isDocumentPostProcessable(WorkflowDocumentFactory.loadDocument(getPrincipalId(USER_AUTH_ID), levelChangeEvent.getDocumentId()))) {
084                return new ProcessDocReport(true, "");
085            }
086            if ("WorkflowDocument2".equals(levelChangeEvent.getNewNodeName())) {
087                return new ProcessDocReport(true, "");
088            }
089            throw new WorkflowRuntimeException("Post Processor should never be called in this instance");
090        }
091    
092        /**
093         * THIS METHOD WILL THROW AN EXCEPTION IF OLD ROUTE NODE IS 'WorkflowTemplate'
094         */
095        public ProcessDocReport doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) throws Exception {
096            if (isDocumentPostProcessable(WorkflowDocumentFactory.loadDocument(getPrincipalId(USER_AUTH_ID), statusChangeEvent.getDocumentId()))) {
097                return new ProcessDocReport(true, "");
098            }
099            throw new WorkflowRuntimeException("Post Processor should never be called in this instance");
100        }
101        
102        /**
103         * THIS METHOD WILL THROW AN EXCEPTION IF OLD ROUTE NODE IS 'WorkflowTemplate'
104         */
105        public ProcessDocReport beforeProcess(BeforeProcessEvent beforeProcessEvent) throws Exception {
106            if (isDocumentPostProcessable(WorkflowDocumentFactory.loadDocument(getPrincipalId(USER_AUTH_ID), beforeProcessEvent.getDocumentId()))) {
107                return new ProcessDocReport(true, "");
108            }
109            throw new WorkflowRuntimeException("Post Processor should never be called in this instance");
110        }
111        
112        /**
113         * THIS METHOD WILL THROW AN EXCEPTION IF OLD ROUTE NODE IS 'WorkflowTemplate'
114         */
115        public ProcessDocReport afterProcess(AfterProcessEvent afterProcessEvent) throws Exception {
116            if (isDocumentPostProcessable(WorkflowDocumentFactory.loadDocument(getPrincipalId(USER_AUTH_ID), afterProcessEvent.getDocumentId()), Arrays.asList(new String[]{"WorkflowDocument2"}))) {
117                return new ProcessDocReport(true, "");
118            }
119            throw new WorkflowRuntimeException("Post Processor should never be called in this instance");
120        }
121        
122        /**
123         * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#getDocumentIdsToLock(org.kuali.rice.kew.framework.postprocessor.DocumentLockingEvent)
124         */
125        public List<String> getDocumentIdsToLock(DocumentLockingEvent lockingEvent) throws Exception {
126                    return null;
127            }
128    
129            private boolean isDocumentPostProcessable(WorkflowDocument doc) throws WorkflowException {
130            return isDocumentPostProcessable(doc, new ArrayList<String>());
131        }
132        
133        private boolean isDocumentPostProcessable(WorkflowDocument doc, List<String> validNodeNames) throws WorkflowException {
134            Set<String> nodeNames = doc.getNodeNames();
135            if (nodeNames != null && nodeNames.size() > 0) {
136                    String nodeName = nodeNames.iterator().next();
137                    return validNodeNames.contains(nodeName) || (nodeName.equals("AdHoc")) || (nodeName.equals("WorkflowDocument"));
138            }
139            return false;
140        }
141    
142        private String getPrincipalId(String principalName) {
143            return KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName).getPrincipalId();
144        }
145    }