Coverage Report - org.kuali.rice.kew.dto.DTOConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
DTOConverter
0%
0/815
0%
0/374
5.22
DTOConverter$RouteNodeInstanceLoader
N/A
N/A
5.22
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.dto;
 18  
 
 19  
 import org.apache.commons.lang.ArrayUtils;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 23  
 import org.kuali.rice.core.api.reflect.DataDefinition;
 24  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 25  
 import org.kuali.rice.core.api.reflect.PropertyDefinition;
 26  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 27  
 import org.kuali.rice.core.api.util.ConcreteKeyValue;
 28  
 import org.kuali.rice.core.api.util.KeyValue;
 29  
 import org.kuali.rice.core.api.util.xml.XmlHelper;
 30  
 import org.kuali.rice.core.api.util.xml.XmlJotter;
 31  
 import org.kuali.rice.core.framework.persistence.jdbc.sql.SQLUtils;
 32  
 import org.kuali.rice.kew.actionitem.ActionItem;
 33  
 import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
 34  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 35  
 import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
 36  
 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
 37  
 import org.kuali.rice.kew.actionrequest.Recipient;
 38  
 import org.kuali.rice.kew.actions.ValidActions;
 39  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 40  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 41  
 import org.kuali.rice.kew.api.action.ActionRequest;
 42  
 import org.kuali.rice.kew.api.action.ActionTaken;
 43  
 import org.kuali.rice.kew.api.action.AdHocRevoke;
 44  
 import org.kuali.rice.kew.api.action.MovePoint;
 45  
 import org.kuali.rice.kew.api.document.DocumentContentUpdate;
 46  
 import org.kuali.rice.kew.api.document.DocumentDetail;
 47  
 import org.kuali.rice.kew.api.document.InvalidDocumentContentException;
 48  
 import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
 49  
 import org.kuali.rice.kew.definition.AttributeDefinition;
 50  
 import org.kuali.rice.kew.docsearch.DocSearchCriteriaDTO;
 51  
 import org.kuali.rice.kew.docsearch.DocSearchUtils;
 52  
 import org.kuali.rice.kew.docsearch.DocumentSearchResult;
 53  
 import org.kuali.rice.kew.docsearch.DocumentSearchResultComponents;
 54  
 import org.kuali.rice.kew.docsearch.SearchableAttributeOld;
 55  
 import org.kuali.rice.kew.docsearch.web.SearchAttributeFormContainer;
 56  
 import org.kuali.rice.kew.docsearch.xml.GenericXMLSearchableAttribute;
 57  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 58  
 import org.kuali.rice.kew.documentlink.DocumentLink;
 59  
 import org.kuali.rice.kew.engine.node.Branch;
 60  
 import org.kuali.rice.kew.engine.node.BranchState;
 61  
 import org.kuali.rice.kew.engine.node.RouteNode;
 62  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 63  
 import org.kuali.rice.kew.engine.node.State;
 64  
 import org.kuali.rice.kew.engine.simulation.SimulationActionToTake;
 65  
 import org.kuali.rice.kew.engine.simulation.SimulationCriteria;
 66  
 import org.kuali.rice.kew.exception.WorkflowException;
 67  
 import org.kuali.rice.kew.framework.docsearch.SearchableAttribute;
 68  
 import org.kuali.rice.kew.notes.Note;
 69  
 import org.kuali.rice.kew.notes.service.NoteService;
 70  
 import org.kuali.rice.kew.postprocessor.ActionTakenEvent;
 71  
 import org.kuali.rice.kew.postprocessor.AfterProcessEvent;
 72  
 import org.kuali.rice.kew.postprocessor.BeforeProcessEvent;
 73  
 import org.kuali.rice.kew.postprocessor.DeleteEvent;
 74  
 import org.kuali.rice.kew.postprocessor.DocumentLockingEvent;
 75  
 import org.kuali.rice.kew.postprocessor.DocumentRouteLevelChange;
 76  
 import org.kuali.rice.kew.postprocessor.DocumentRouteStatusChange;
 77  
 import org.kuali.rice.kew.routeheader.DocumentContent;
 78  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 79  
 import org.kuali.rice.kew.routeheader.DocumentStatusTransition;
 80  
 import org.kuali.rice.kew.routeheader.StandardDocumentContent;
 81  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 82  
 import org.kuali.rice.kew.rule.RuleDelegation;
 83  
 import org.kuali.rice.kew.rule.RuleExtension;
 84  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 85  
 import org.kuali.rice.kew.rule.RuleResponsibility;
 86  
 import org.kuali.rice.kew.rule.WorkflowAttribute;
 87  
 import org.kuali.rice.kew.rule.WorkflowAttributeValidationError;
 88  
 import org.kuali.rice.kew.rule.WorkflowAttributeXmlValidator;
 89  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 90  
 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
 91  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 92  
 import org.kuali.rice.kew.user.RoleRecipient;
 93  
 import org.kuali.rice.kew.util.KEWConstants;
 94  
 import org.kuali.rice.kew.util.ResponsibleParty;
 95  
 import org.kuali.rice.kew.web.KeyValueSort;
 96  
 import org.kuali.rice.kim.api.group.Group;
 97  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 98  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 99  
 import org.kuali.rice.kim.bo.Person;
 100  
 import org.w3c.dom.Document;
 101  
 import org.w3c.dom.Element;
 102  
 import org.w3c.dom.NodeList;
 103  
 import org.xml.sax.SAXException;
 104  
 
 105  
 import javax.xml.parsers.DocumentBuilder;
 106  
 import javax.xml.parsers.DocumentBuilderFactory;
 107  
 import javax.xml.parsers.ParserConfigurationException;
 108  
 import javax.xml.transform.TransformerException;
 109  
 import java.io.IOException;
 110  
 import java.sql.Timestamp;
 111  
 import java.util.ArrayList;
 112  
 import java.util.Arrays;
 113  
 import java.util.Collection;
 114  
 import java.util.Date;
 115  
 import java.util.HashMap;
 116  
 import java.util.HashSet;
 117  
 import java.util.Iterator;
 118  
 import java.util.List;
 119  
 import java.util.Map;
 120  
 import java.util.Set;
 121  
 
 122  
 
 123  
 /**
 124  
  * Translates Workflow server side beans into client side VO beans.
 125  
  *
 126  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 127  
  */
 128  0
 public class DTOConverter {
 129  0
     private static final Logger LOG = Logger.getLogger(DTOConverter.class);
 130  
     
 131  
     public static RouteHeaderDTO convertRouteHeader(DocumentRouteHeaderValue routeHeader, String principalId) throws WorkflowException {
 132  0
         RouteHeaderDTO routeHeaderVO = new RouteHeaderDTO();
 133  0
         if (routeHeader == null) {
 134  0
             return null;
 135  
         }
 136  0
         populateRouteHeaderVO(routeHeaderVO, routeHeader);
 137  
 
 138  0
         if (principalId != null) {
 139  0
             routeHeaderVO.setUserBlanketApprover(false); // default to false
 140  0
             if (routeHeader.getDocumentType() != null) {
 141  0
                     boolean isBlanketApprover = KEWServiceLocator.getDocumentTypePermissionService().canBlanketApprove(principalId, routeHeader.getDocumentType(), routeHeader.getDocRouteStatus(), routeHeader.getInitiatorWorkflowId());
 142  0
                 routeHeaderVO.setUserBlanketApprover(isBlanketApprover);
 143  
             }
 144  0
             Map<String, String> actionsRequested = KEWServiceLocator.getActionRequestService().getActionsRequested(routeHeader, principalId, true);
 145  0
             for (String actionRequestCode : actionsRequested.keySet()) {
 146  0
                                 if (KEWConstants.ACTION_REQUEST_FYI_REQ.equals(actionRequestCode)) {
 147  0
                     routeHeaderVO.setFyiRequested(Boolean.parseBoolean(actionsRequested.get(actionRequestCode)));                                        
 148  
                                 }
 149  0
                                 else if (KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ.equals(actionRequestCode)) {
 150  0
                     routeHeaderVO.setAckRequested(Boolean.parseBoolean(actionsRequested.get(actionRequestCode)));
 151  
                                 }
 152  0
                                 else if (KEWConstants.ACTION_REQUEST_APPROVE_REQ.equals(actionRequestCode)) {
 153  0
                     routeHeaderVO.setApproveRequested(Boolean.parseBoolean(actionsRequested.get(actionRequestCode)));                                        
 154  
                                 }
 155  
                                 else {
 156  0
                     routeHeaderVO.setCompleteRequested(Boolean.parseBoolean(actionsRequested.get(actionRequestCode)));
 157  
                                 }
 158  
                         }
 159  
             // Update notes and notesToDelete arrays in routeHeaderVO
 160  0
             routeHeaderVO.setNotesToDelete(null);
 161  0
             routeHeaderVO.setNotes(convertNotesArrayListToNoteVOArray(routeHeader.getNotes()));
 162  
         }
 163  
 
 164  
 
 165  0
         if (principalId != null) {
 166  0
                 Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
 167  0
             routeHeaderVO.setValidActions(convertValidActions(KEWServiceLocator.getActionRegistry().getValidActions(principal, routeHeader)));
 168  
         }
 169  0
         return routeHeaderVO;
 170  
     }
 171  
 
 172  
     public static ValidActionsDTO convertValidActions(ValidActions validActions) {
 173  0
         ValidActionsDTO validActionsVO = new ValidActionsDTO();
 174  0
         for (String actionTakenCode : validActions.getActionTakenCodes()) {
 175  0
             validActionsVO.addValidActionsAllowed(actionTakenCode);
 176  
         }
 177  0
         return validActionsVO;
 178  
     }
 179  
 
 180  
     private static void populateRouteHeaderVO(RouteHeaderDTO routeHeaderVO, DocumentRouteHeaderValue routeHeader) throws WorkflowException {
 181  0
         routeHeaderVO.setDocumentId(routeHeader.getDocumentId());
 182  0
         routeHeaderVO.setAppDocId(routeHeader.getAppDocId());
 183  0
         routeHeaderVO.setDateApproved(SQLUtils.convertTimestamp(routeHeader.getApprovedDate()));
 184  0
         routeHeaderVO.setDateCreated(SQLUtils.convertTimestamp(routeHeader.getCreateDate()));
 185  0
         routeHeaderVO.setDateFinalized(SQLUtils.convertTimestamp(routeHeader.getFinalizedDate()));
 186  0
         routeHeaderVO.setDateLastModified(SQLUtils.convertTimestamp(routeHeader.getStatusModDate()));
 187  0
         routeHeaderVO.setAppDocStatus(routeHeader.getAppDocStatus());
 188  0
         routeHeaderVO.setAppDocStatusDate(SQLUtils.convertTimestamp(routeHeader.getAppDocStatusDate()));
 189  
         
 190  
         /**
 191  
          * This is the original code which set everything up for lazy loading of document content
 192  
          */
 193  
         // by default, a non-initialized document content object will be sent so that it can be fetched lazily
 194  
         // DocumentContentVO documentContentVO = new DocumentContentVO();
 195  
         // documentContentVO.setDocumentId(routeHeader.getDocumentId());
 196  
         // routeHeaderVO.setDocumentContent(documentContentVO);
 197  
         /**
 198  
          * Since we removed the lazy loading in the 2.3 release, this is the code which bypasses lazy loading
 199  
          */
 200  
         // routeHeaderVO.setDocumentContent(convertDocumentContent(routeHeader.getDocContent(),
 201  
         // routeHeader.getDocumentId()));
 202  0
         routeHeaderVO.setDocRouteLevel(routeHeader.getDocRouteLevel());
 203  0
         routeHeaderVO.setCurrentRouteNodeNames(routeHeader.getCurrentRouteLevelName());
 204  
 
 205  
         /*
 206  
          * Collection activeNodes =
 207  
          * SpringServiceLocator.getRouteNodeService().getActiveNodeInstances(routeHeaderVO.getDocumentId());
 208  
          * routeHeaderVO.setNodeNames(new String[activeNodes.size()]); int index = 0; for (Iterator iterator =
 209  
          * activeNodes.iterator(); iterator.hasNext();) { RouteNodeInstance nodeInstance = (RouteNodeInstance)
 210  
          * iterator.next(); routeHeaderVO.getNodeNames()[index++] = nodeInstance.getRouteNode().getRouteNodeName(); }
 211  
          */
 212  
 
 213  0
         routeHeaderVO.setDocRouteStatus(routeHeader.getDocRouteStatus());
 214  0
         routeHeaderVO.setDocTitle(routeHeader.getDocTitle());
 215  0
         if (routeHeader.getDocumentType() != null) {
 216  0
             routeHeaderVO.setDocTypeName(routeHeader.getDocumentType().getName());
 217  0
             routeHeaderVO.setDocumentUrl(routeHeader.getDocumentType().getDocHandlerUrl());
 218  0
             routeHeaderVO.setDocTypeId(routeHeader.getDocumentTypeId());
 219  
         }
 220  0
         routeHeaderVO.setDocVersion(routeHeader.getDocVersion());
 221  0
         routeHeaderVO.setInitiatorPrincipalId(routeHeader.getInitiatorWorkflowId());
 222  0
         routeHeaderVO.setRoutedByPrincipalId(routeHeader.getRoutedByUserWorkflowId());
 223  
 
 224  
         /* populate the routeHeaderVO with the document variables */
 225  
         // FIXME: we assume there is only one for now
 226  0
         Branch routeNodeInstanceBranch = routeHeader.getRootBranch();
 227  
         // Ok, we are using the "branch state" as the arbitrary convenient repository for flow/process/edoc variables
 228  
         // so we need to stuff them into the VO
 229  0
         if (routeNodeInstanceBranch != null) {
 230  0
             List listOfBranchStates = routeNodeInstanceBranch.getBranchState();
 231  0
             Iterator it = listOfBranchStates.iterator();
 232  0
             while (it.hasNext()) {
 233  0
                 BranchState bs = (BranchState) it.next();
 234  0
                 if (bs.getKey() != null && bs.getKey().startsWith(BranchState.VARIABLE_PREFIX)) {
 235  0
                     LOG.debug("Setting branch state variable on vo: " + bs.getKey() + "=" + bs.getValue());
 236  0
                     routeHeaderVO.setVariable(bs.getKey().substring(BranchState.VARIABLE_PREFIX.length()), bs.getValue());
 237  
                 }
 238  0
             }
 239  
         }
 240  0
     }
 241  
 
 242  
     public static DocumentRouteHeaderValue convertRouteHeaderVO(RouteHeaderDTO routeHeaderVO) throws WorkflowException {
 243  0
         DocumentRouteHeaderValue routeHeader = new DocumentRouteHeaderValue();
 244  0
         routeHeader.setAppDocId(routeHeaderVO.getAppDocId());
 245  0
         routeHeader.setApprovedDate(SQLUtils.convertCalendar(routeHeaderVO.getDateApproved()));
 246  0
         routeHeader.setCreateDate(SQLUtils.convertCalendar(routeHeaderVO.getDateCreated()));
 247  0
         if (StringUtils.isEmpty(routeHeader.getDocContent())) {
 248  0
             routeHeader.setDocContent(KEWConstants.DEFAULT_DOCUMENT_CONTENT);
 249  
         }
 250  0
         routeHeader.setDocRouteLevel(routeHeaderVO.getDocRouteLevel());
 251  0
         routeHeader.setDocRouteStatus(routeHeaderVO.getDocRouteStatus());
 252  0
         routeHeader.setDocTitle(routeHeaderVO.getDocTitle());
 253  0
         if (routeHeaderVO.getDocTypeName() != null) {
 254  0
             DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(routeHeaderVO.getDocTypeName());
 255  0
             if (documentType == null) {
 256  0
                 throw new RiceRuntimeException("Could not locate the given document type name: " + routeHeaderVO.getDocTypeName());
 257  
             }
 258  0
             routeHeader.setDocumentTypeId(documentType.getDocumentTypeId());
 259  
         }
 260  0
         routeHeader.setDocVersion(routeHeaderVO.getDocVersion());
 261  0
         routeHeader.setFinalizedDate(SQLUtils.convertCalendar(routeHeaderVO.getDateFinalized()));
 262  0
         routeHeader.setInitiatorWorkflowId(routeHeaderVO.getInitiatorPrincipalId());
 263  0
         routeHeader.setRoutedByUserWorkflowId(routeHeaderVO.getRoutedByPrincipalId());
 264  0
         routeHeader.setDocumentId(routeHeaderVO.getDocumentId());
 265  0
         routeHeader.setStatusModDate(SQLUtils.convertCalendar(routeHeaderVO.getDateLastModified()));
 266  0
         routeHeader.setAppDocStatus(routeHeaderVO.getAppDocStatus());
 267  0
         routeHeader.setAppDocStatusDate(SQLUtils.convertCalendar(routeHeaderVO.getAppDocStatusDate()));
 268  
 
 269  
         
 270  
         // Convert the variables
 271  0
         List<KeyValue> variables = routeHeaderVO.getVariables();
 272  0
         if( variables != null && !variables.isEmpty()){
 273  0
                 for(KeyValue kvp : variables){
 274  0
                         routeHeader.setVariable(kvp.getKey(), kvp.getValue());
 275  
                 }
 276  
         }
 277  
         
 278  0
         return routeHeader;
 279  
     }
 280  
 
 281  
     public static ActionItemDTO convertActionItem(ActionItem actionItem) {
 282  0
         ActionItemDTO actionItemVO = new ActionItemDTO();
 283  0
         actionItemVO.setActionItemId(actionItem.getActionItemId());
 284  0
         actionItemVO.setActionItemIndex(actionItem.getActionItemIndex());
 285  0
         actionItemVO.setActionRequestCd(actionItem.getActionRequestCd());
 286  0
         actionItemVO.setActionRequestId(actionItem.getActionRequestId());
 287  0
         actionItemVO.setActionToTake(actionItem.getActionToTake());
 288  0
         actionItemVO.setDateAssigned(actionItem.getDateAssigned());
 289  0
         actionItemVO.setDateAssignedString(actionItem.getDateAssignedString());
 290  0
         actionItemVO.setDelegationType(actionItem.getDelegationType());
 291  0
         actionItemVO.setDelegatorPrincipalId(actionItem.getDelegatorWorkflowId());
 292  0
         actionItemVO.setDelegatorGroupId(actionItem.getDelegatorGroupId());
 293  0
         actionItemVO.setDocHandlerURL(actionItem.getDocHandlerURL());
 294  0
         actionItemVO.setDocLabel(actionItem.getDocLabel());
 295  0
         actionItemVO.setDocName(actionItem.getDocName());
 296  0
         actionItemVO.setDocTitle(actionItem.getDocTitle());
 297  0
         actionItemVO.setResponsibilityId(actionItem.getResponsibilityId());
 298  0
         actionItemVO.setRoleName(actionItem.getRoleName());
 299  0
         actionItemVO.setDocumentId(actionItem.getDocumentId());
 300  0
         actionItemVO.setPrincipalId(actionItem.getPrincipalId());
 301  0
         actionItemVO.setGroupId(actionItem.getGroupId());
 302  0
         return actionItemVO;
 303  
     }
 304  
 
 305  
     public static String buildUpdatedDocumentContent(String existingDocContent, DocumentContentUpdate documentContentUpdate, String documentTypeName) {
 306  0
             if (existingDocContent == null) {
 307  0
                     existingDocContent = KEWConstants.DEFAULT_DOCUMENT_CONTENT;
 308  
             }
 309  0
         String documentContent = KEWConstants.DEFAULT_DOCUMENT_CONTENT;
 310  0
         StandardDocumentContent standardDocContent = new StandardDocumentContent(existingDocContent);
 311  
         try {
 312  0
                 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 313  0
                 Document document = builder.newDocument();
 314  0
                 Element root = document.createElement(KEWConstants.DOCUMENT_CONTENT_ELEMENT);
 315  0
                 document.appendChild(root);
 316  0
                 Element applicationContentElement = standardDocContent.getApplicationContent();
 317  0
                 if (documentContentUpdate.getApplicationContent() != null) {
 318  
                         // application content has changed
 319  0
                         if (!StringUtils.isEmpty(documentContentUpdate.getApplicationContent())) {
 320  0
                                 applicationContentElement = document.createElement(KEWConstants.APPLICATION_CONTENT_ELEMENT);
 321  0
                                 XmlHelper.appendXml(applicationContentElement, documentContentUpdate.getApplicationContent());
 322  
                         } else {
 323  
                                 // they've cleared the application content
 324  0
                                 applicationContentElement = null;
 325  
                         }
 326  
                 }
 327  0
                 Element attributeContentElement = createDocumentContentSection(document, standardDocContent.getAttributeContent(), documentContentUpdate.getAttributeDefinitions(), documentContentUpdate.getAttributeContent(), KEWConstants.ATTRIBUTE_CONTENT_ELEMENT, documentTypeName);
 328  0
                 Element searchableContentElement = createDocumentContentSection(document, standardDocContent.getSearchableContent(), documentContentUpdate.getSearchableDefinitions(), documentContentUpdate.getSearchableContent(), KEWConstants.SEARCHABLE_CONTENT_ELEMENT, documentTypeName);
 329  0
                 if (applicationContentElement != null) {
 330  0
                         root.appendChild(applicationContentElement);
 331  
                 }
 332  0
                 if (attributeContentElement != null) {
 333  0
                         root.appendChild(attributeContentElement);
 334  
                 }
 335  0
                 if (searchableContentElement != null) {
 336  0
                         root.appendChild(searchableContentElement);
 337  
                 }
 338  0
                 documentContent = XmlJotter.jotNode(document);
 339  0
         } catch (ParserConfigurationException e) {
 340  0
                 throw new RiceRuntimeException("Failed to initialize XML parser.", e);
 341  0
         } catch (SAXException e) {
 342  0
                 throw new InvalidDocumentContentException("Failed to parse XML.", e);
 343  0
                 } catch (IOException e) {
 344  0
                         throw new InvalidDocumentContentException("Failed to parse XML.", e);
 345  0
                 } catch (TransformerException e) {
 346  0
                         throw new InvalidDocumentContentException("Failed to parse XML.", e);
 347  0
                 }
 348  0
         return documentContent;
 349  
     }
 350  
 
 351  
     private static Element createDocumentContentSection(Document document, Element existingAttributeElement, List<WorkflowAttributeDefinition> definitions, String content, String elementName, String documentTypeName) throws TransformerException, SAXException, IOException, ParserConfigurationException {
 352  0
         Element contentSectionElement = existingAttributeElement;
 353  
         // if they've updated the content, we're going to re-build the content section element from scratch
 354  0
         if (content != null) {
 355  0
             if (!org.apache.commons.lang.StringUtils.isEmpty(content)) {
 356  0
                 contentSectionElement = document.createElement(elementName);
 357  
                 // if they didn't merely clear the content, let's build the content section element by combining the children
 358  
                 // of the incoming XML content
 359  0
                 Element incomingAttributeElement = XmlHelper.readXml(content).getDocumentElement();
 360  0
                 NodeList children = incomingAttributeElement.getChildNodes();
 361  0
                 for (int index = 0; index < children.getLength(); index++) {
 362  0
                     contentSectionElement.appendChild(document.importNode(children.item(index), true));
 363  
                 }
 364  0
             } else {
 365  0
                 contentSectionElement = null;
 366  
             }
 367  
         }
 368  
         // if they have new definitions we're going to append those to the existing content section
 369  0
         if (definitions != null && !definitions.isEmpty()) {
 370  0
             String errorMessage = "";
 371  0
             boolean inError = false;
 372  0
             if (contentSectionElement == null) {
 373  0
                 contentSectionElement = document.createElement(elementName);
 374  
             }
 375  0
             for (WorkflowAttributeDefinition definitionVO : definitions) {
 376  0
                 AttributeDefinition definition = convertWorkflowAttributeDefinition(definitionVO);
 377  0
                 RuleAttribute ruleAttribute = definition.getRuleAttribute();
 378  0
                 Object attribute = KEWServiceLocator.getRuleAttributeService().loadRuleAttributeService(ruleAttribute,
 379  
                         null);
 380  0
                 boolean propertiesAsMap = false;
 381  0
                 if (KEWConstants.RULE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType())) {
 382  0
                     ((GenericXMLRuleAttribute) attribute).setRuleAttribute(ruleAttribute);
 383  0
                     propertiesAsMap = true;
 384  0
                 } else if (KEWConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType())) {
 385  0
                     ((GenericXMLSearchableAttribute) attribute).setRuleAttribute(ruleAttribute);
 386  0
                     propertiesAsMap = true;
 387  
                 }
 388  0
                 if (propertiesAsMap) {
 389  0
                     for (org.kuali.rice.kew.api.document.PropertyDefinition propertyDefinitionVO : definitionVO.getPropertyDefinitions()) {
 390  0
                         if (attribute instanceof GenericXMLRuleAttribute) {
 391  0
                             ((GenericXMLRuleAttribute) attribute).getParamMap().put(propertyDefinitionVO.getName(), propertyDefinitionVO.getValue());
 392  0
                         } else if (attribute instanceof GenericXMLSearchableAttribute) {
 393  0
                             ((GenericXMLSearchableAttribute) attribute).getParamMap().put(propertyDefinitionVO.getName(), propertyDefinitionVO.getValue());
 394  
                         }
 395  
                     }
 396  
                 }
 397  
 
 398  
                 // validate inputs from client application if the attribute is capable
 399  0
                 if (attribute instanceof WorkflowAttributeXmlValidator) {
 400  0
                     List<WorkflowAttributeValidationError> errors = ((WorkflowAttributeXmlValidator) attribute).validateClientRoutingData();
 401  0
                     if (!errors.isEmpty()) {
 402  0
                         inError = true;
 403  0
                         errorMessage += "Error validating attribute " + definitionVO.getAttributeName() + " ";
 404  0
                         for (WorkflowAttributeValidationError error : errors) {
 405  0
                             errorMessage += error.getMessage() + " ";
 406  
                         }
 407  
                     }
 408  
                 }
 409  
                 // dont add to xml if attribute is in error
 410  0
                 if (!inError) {
 411  0
                     if (attribute instanceof WorkflowAttribute) {
 412  0
                         String attributeDocContent = ((WorkflowAttribute) attribute).getDocContent();
 413  0
                         if (!StringUtils.isEmpty(attributeDocContent)) {
 414  0
                             XmlHelper.appendXml(contentSectionElement, attributeDocContent);
 415  
                         }
 416  0
                     } else if (attribute instanceof SearchableAttributeOld) {
 417  
                         // TODO remove this section once we remove SearchableAttributeOld
 418  0
                         String searcheAttributeContent =
 419  
                                 ((SearchableAttributeOld) attribute).getSearchContent(DocSearchUtils.getDocumentSearchContext("", documentTypeName, ""));
 420  0
                         if (!StringUtils.isEmpty(searcheAttributeContent)) {
 421  0
                             XmlHelper.appendXml(contentSectionElement, searcheAttributeContent);
 422  
                         }
 423  0
                     } else if (attribute instanceof SearchableAttribute) {
 424  0
                         SearchableAttribute searchableAttribute = (SearchableAttribute)attribute;
 425  0
                         String searchableAttributeContent = searchableAttribute.generateSearchContent(documentTypeName, definitionVO);
 426  0
                         if (!StringUtils.isBlank(searchableAttributeContent)) {
 427  0
                             XmlHelper.appendXml(contentSectionElement, searchableAttributeContent);
 428  
                         }
 429  
                     }
 430  
                 }
 431  0
             }
 432  0
             if (inError) {
 433  0
                 throw new WorkflowRuntimeException(errorMessage);
 434  
             }
 435  
 
 436  
         }
 437  0
         if (contentSectionElement != null) {
 438  
             // always be sure and import the element into the new document, if it originated from the existing doc content
 439  
             // and
 440  
             // appended to it, it will need to be imported
 441  0
             contentSectionElement = (Element) document.importNode(contentSectionElement, true);
 442  
         }
 443  0
         return contentSectionElement;
 444  
     }
 445  
     
 446  
 
 447  
     /**
 448  
      * New for Rice 2.0
 449  
      */
 450  
     public static AttributeDefinition convertWorkflowAttributeDefinition(WorkflowAttributeDefinition definition) {
 451  0
         if (definition == null) {
 452  0
             return null;
 453  
         }
 454  
         // get the rule attribute so we can get's it's message antity and not blow up if it's remote
 455  0
         RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByClassName(definition.getAttributeName());
 456  0
         if (ruleAttribute == null) {
 457  0
             ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(definition.getAttributeName());
 458  
         }
 459  0
         if (ruleAttribute == null) {
 460  0
             throw new WorkflowRuntimeException("Attribute " + definition.getAttributeName() + " not found");
 461  
         }
 462  
 
 463  0
         ObjectDefinition objectDefinition = new ObjectDefinition(ruleAttribute.getClassName());
 464  0
         if (definition.getParameters() != null) {
 465  0
                 for (String parameter : definition.getParameters()) {
 466  0
                         objectDefinition.addConstructorParameter(new DataDefinition(parameter, String.class));
 467  
                 }
 468  
         }
 469  0
         boolean propertiesAsMap = KEWConstants.RULE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType()) || KEWConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType());
 470  0
         if (!propertiesAsMap && definition.getPropertyDefinitions() != null) {
 471  0
                 for (org.kuali.rice.kew.api.document.PropertyDefinition propertyDefinition : definition.getPropertyDefinitions()) {
 472  0
                 objectDefinition.addProperty(new PropertyDefinition(propertyDefinition.getName(), new DataDefinition(propertyDefinition.getValue(), String.class)));
 473  
             }
 474  
         }
 475  
 
 476  
 //        // this is likely from an EDL validate call and ME may needed to be added to the AttDefinitionVO.
 477  
 //        if (ruleAttribute.getApplicationId() != null) {
 478  
 //            definition.setApplicationId(ruleAttribute.getApplicationId());
 479  
 //        } else {
 480  
 //            // get the me from the document type if it's been passed in - the document is having action taken on it.
 481  
 //            if (documentType != null) {
 482  
 //                definition.setApplicationId(documentType.getApplicationId());
 483  
 //            }
 484  
 //        }
 485  
 
 486  0
         return new AttributeDefinition(ruleAttribute, objectDefinition);
 487  
     }
 488  
     
 489  
     public static DocumentContentDTO convertDocumentContent(String documentContentValue, String documentId) throws WorkflowException {
 490  0
         if (documentContentValue == null) {
 491  0
             return null;
 492  
         }
 493  0
         DocumentContentDTO documentContentVO = new DocumentContentDTO();
 494  
         // initialize the content fields
 495  0
         documentContentVO.setApplicationContent("");
 496  0
         documentContentVO.setAttributeContent("");
 497  0
         documentContentVO.setSearchableContent("");
 498  0
         documentContentVO.setDocumentId(documentId);
 499  
         try {
 500  0
             DocumentContent documentContent = new StandardDocumentContent(documentContentValue);
 501  0
             if (documentContent.getApplicationContent() != null) {
 502  0
                 documentContentVO.setApplicationContent(XmlJotter.jotNode(documentContent.getApplicationContent()));
 503  
             }
 504  0
             if (documentContent.getAttributeContent() != null) {
 505  0
                 documentContentVO.setAttributeContent(XmlJotter.jotNode(documentContent.getAttributeContent()));
 506  
             }
 507  0
             if (documentContent.getSearchableContent() != null) {
 508  0
                 documentContentVO.setSearchableContent(XmlJotter.jotNode(documentContent.getSearchableContent()));
 509  
             }
 510  0
         } catch (Exception e) {
 511  0
             handleException("Error parsing document content.", e);
 512  0
         }
 513  0
         return documentContentVO;
 514  
     }
 515  
 
 516  
     public static ActionRequestDTO convertActionRequest(ActionRequestValue actionRequest) {
 517  0
             return convertActionRequest(actionRequest, true);
 518  
     }
 519  
 
 520  
     protected static ActionRequestDTO convertActionRequest(ActionRequestValue actionRequest, boolean includeActionTaken) {
 521  0
         ActionRequestDTO actionRequestVO = new ActionRequestDTO();
 522  0
         actionRequestVO.setActionRequested(actionRequest.getActionRequested());
 523  0
         actionRequestVO.setActionRequestId(actionRequest.getActionRequestId());
 524  
 
 525  0
         if (includeActionTaken && (actionRequest.getActionTaken() != null)) {
 526  0
             actionRequestVO.setActionTakenId(actionRequest.getActionTakenId());
 527  0
             actionRequestVO.setActionTaken(convertActionTaken(actionRequest.getActionTaken()));
 528  
         }
 529  
 
 530  0
         actionRequestVO.setAnnotation(actionRequest.getAnnotation());
 531  0
         actionRequestVO.setDateCreated(SQLUtils.convertTimestamp(actionRequest.getCreateDate()));
 532  0
         actionRequestVO.setDocVersion(actionRequest.getDocVersion());
 533  0
         actionRequestVO.setPrincipalId(actionRequest.getPrincipalId());
 534  0
         actionRequestVO.setForceAction(actionRequest.getForceAction());
 535  0
         actionRequestVO.setPriority(actionRequest.getPriority());
 536  0
         actionRequestVO.setRecipientTypeCd(actionRequest.getRecipientTypeCd());
 537  0
         actionRequestVO.setResponsibilityDesc(actionRequest.getResponsibilityDesc());
 538  0
         actionRequestVO.setResponsibilityId(actionRequest.getResponsibilityId());
 539  0
         actionRequestVO.setDocumentId(actionRequest.getDocumentId());
 540  0
         actionRequestVO.setRouteLevel(actionRequest.getRouteLevel());
 541  0
         actionRequestVO.setNodeName(actionRequest.getPotentialNodeName());
 542  0
         actionRequestVO.setNodeInstanceId((actionRequest.getNodeInstance() == null ? null : actionRequest.getNodeInstance().getRouteNodeInstanceId()));
 543  
         // TODO delyea - should below be using actionRequest.getRoleName()?
 544  0
         actionRequestVO.setRoleName(actionRequest.getQualifiedRoleName());
 545  0
         actionRequestVO.setQualifiedRoleName(actionRequest.getQualifiedRoleName());
 546  0
         actionRequestVO.setQualifiedRoleNameLabel(actionRequest.getQualifiedRoleNameLabel());
 547  0
         actionRequestVO.setStatus(actionRequest.getStatus());
 548  0
         actionRequestVO.setGroupId(actionRequest.getGroupId());
 549  0
         actionRequestVO.setDelegationType(actionRequest.getDelegationType());
 550  0
         actionRequestVO.setParentActionRequestId(actionRequest.getParentActionRequestId());
 551  0
         actionRequestVO.setRequestLabel(actionRequest.getRequestLabel());
 552  0
         ActionRequestDTO[] childRequestVOs = new ActionRequestDTO[actionRequest.getChildrenRequests().size()];
 553  0
         int index = 0;
 554  0
         for (ActionRequestValue childRequest : actionRequest.getChildrenRequests()) {
 555  0
             ActionRequestDTO childRequestVO = convertActionRequest(childRequest);
 556  0
             childRequestVOs[index++] = childRequestVO;
 557  0
         }
 558  0
         actionRequestVO.setChildrenRequests(childRequestVOs);
 559  0
         return actionRequestVO;
 560  
     }
 561  
 
 562  
     public static ActionTakenDTO convertActionTakenWithActionRequests(ActionTakenValue actionTaken) {
 563  0
             return convertActionTaken(actionTaken, true);
 564  
     }
 565  
 
 566  
     public static ActionTakenDTO convertActionTaken(ActionTakenValue actionTaken) {
 567  0
             return convertActionTaken(actionTaken, false);
 568  
     }
 569  
 
 570  
     protected static ActionTakenDTO convertActionTaken(ActionTakenValue actionTaken, boolean fetchActionRequests) {
 571  0
         if (actionTaken == null) {
 572  0
             return null;
 573  
         }
 574  0
         ActionTakenDTO actionTakenVO = new ActionTakenDTO();
 575  0
         actionTakenVO.setActionDate(SQLUtils.convertTimestamp(actionTaken.getActionDate()));
 576  0
         actionTakenVO.setActionTaken(actionTaken.getActionTaken());
 577  0
         actionTakenVO.setActionTakenId(actionTaken.getActionTakenId());
 578  0
         actionTakenVO.setAnnotation(actionTaken.getAnnotation());
 579  0
         actionTakenVO.setDocVersion(actionTaken.getDocVersion());
 580  0
         actionTakenVO.setDocumentId(actionTaken.getDocumentId());
 581  0
         actionTakenVO.setPrincipalId(actionTaken.getPrincipalId());
 582  0
         actionTakenVO.setDelegatorPrincpalId(actionTaken.getDelegatorPrincipalId());
 583  0
         actionTakenVO.setDelegatorGroupId(actionTaken.getDelegatorGroupId());
 584  0
         if (fetchActionRequests) {
 585  0
                 ActionRequestDTO[] actionRequests = new ActionRequestDTO[actionTaken.getActionRequests().size()];
 586  0
                 int index = 0;
 587  0
                 for (Object element : actionTaken.getActionRequests()) {
 588  0
                     ActionRequestValue actionRequest = (ActionRequestValue) element;
 589  0
                     actionRequests[index++] = convertActionRequest(actionRequest, false);
 590  0
                 }
 591  0
                 actionTakenVO.setActionRequests(actionRequests);
 592  
         }
 593  0
         return actionTakenVO;
 594  
     }
 595  
 
 596  
     public static ResponsiblePartyDTO convertResponsibleParty(ResponsibleParty responsibleParty) {
 597  0
         if (responsibleParty == null) {
 598  0
             return null;
 599  
         }
 600  0
         ResponsiblePartyDTO responsiblePartyVO = new ResponsiblePartyDTO();
 601  0
         responsiblePartyVO.setGroupId(responsibleParty.getGroupId());
 602  0
         responsiblePartyVO.setPrincipalId(responsibleParty.getPrincipalId());
 603  0
         responsiblePartyVO.setRoleName(responsibleParty.getRoleName());
 604  0
         return responsiblePartyVO;
 605  
     }
 606  
 
 607  
     public static ResponsibleParty convertResponsiblePartyVO(ResponsiblePartyDTO responsiblePartyVO) {
 608  0
         if (responsiblePartyVO == null) {
 609  0
             return null;
 610  
         }
 611  0
         ResponsibleParty responsibleParty = new ResponsibleParty();
 612  0
         responsibleParty.setGroupId(responsiblePartyVO.getGroupId());
 613  0
         responsibleParty.setPrincipalId(responsiblePartyVO.getPrincipalId());
 614  0
         responsibleParty.setRoleName(responsiblePartyVO.getRoleName());
 615  0
         return responsibleParty;
 616  
     }
 617  
 
 618  
     /**
 619  
      * refactor name to convertResponsiblePartyVO when ResponsibleParty object is gone
 620  
      *
 621  
      * @param responsiblePartyVO
 622  
      * @return
 623  
      */
 624  
     public static Recipient convertResponsiblePartyVOtoRecipient(ResponsiblePartyDTO responsiblePartyVO) {
 625  0
         if (responsiblePartyVO == null) {
 626  0
             return null;
 627  
         }
 628  0
         if (responsiblePartyVO.getRoleName() != null) {
 629  0
             return new RoleRecipient(responsiblePartyVO.getRoleName());
 630  
         }
 631  0
         String groupId = responsiblePartyVO.getGroupId();
 632  0
         if (groupId != null) {
 633  0
                 Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
 634  0
                 if (group == null) {
 635  0
                         throw new RiceRuntimeException("Failed to locate group with ID: " + groupId);
 636  
                 }
 637  0
             return new KimGroupRecipient(group);
 638  
         }
 639  0
         String principalId = responsiblePartyVO.getPrincipalId();
 640  0
         if (principalId != null) {
 641  0
             return new KimPrincipalRecipient(principalId);
 642  
         }
 643  0
         throw new WorkflowRuntimeException("ResponsibleParty of unknown type");
 644  
     }
 645  
 
 646  
     /**
 647  
      * Interface for a simple service providing RouteNodeInstanceS based on their IDs 
 648  
      */
 649  0
     public static interface RouteNodeInstanceLoader {
 650  
             RouteNodeInstance load(String routeNodeInstanceID);
 651  
     }
 652  
     
 653  
     /**
 654  
      * Converts an ActionRequestVO to an ActionRequest. The ActionRequestDTO passed in must be the root action request in the
 655  
      * graph, otherwise an IllegalArgumentException is thrown. This is to avoid potentially sticky issues with circular
 656  
      * references in the conversion. NOTE: This method's primary purpose is to convert ActionRequestVOs returned from a
 657  
      * RouteModule. Incidentally, the DTO's returned from the route module will be lacking some information (like the node
 658  
      * instance) so no attempts are made to convert this data since further initialization is handled by a higher level
 659  
      * component (namely ActionRequestService.initializeActionRequestGraph).
 660  
      */
 661  
     public static ActionRequestValue convertActionRequestDTO(ActionRequestDTO actionRequestDTO) {
 662  0
             return convertActionRequestDTO(actionRequestDTO, null);
 663  
     }
 664  
     
 665  
     /**
 666  
      * Converts an ActionRequestVO to an ActionRequest. The ActionRequestDTO passed in must be the root action request in the
 667  
      * graph, otherwise an IllegalArgumentException is thrown. This is to avoid potentially sticky issues with circular
 668  
      * references in the conversion. 
 669  
      * @param routeNodeInstanceLoader a service that will provide routeNodeInstanceS based on their IDs.
 670  
      */
 671  
     public static ActionRequestValue convertActionRequestDTO(ActionRequestDTO actionRequestDTO, 
 672  
                     RouteNodeInstanceLoader routeNodeInstanceLoader) {
 673  
             
 674  0
         if (actionRequestDTO == null) {
 675  0
             return null;
 676  
         }
 677  0
         if (actionRequestDTO.getParentActionRequestId() != null) {
 678  0
             throw new IllegalArgumentException("Cannot convert a non-root ActionRequestVO");
 679  
         }
 680  0
         ActionRequestValue actionRequest = new ActionRequestFactory().createBlankActionRequest();
 681  0
         populateActionRequest(actionRequest, actionRequestDTO, routeNodeInstanceLoader);
 682  0
         if (actionRequestDTO.getChildrenRequests() != null) {
 683  0
             for (int i = 0; i < actionRequestDTO.getChildrenRequests().length; i++) {
 684  0
                 ActionRequestDTO childVO = actionRequestDTO.getChildrenRequests()[i];
 685  0
                 actionRequest.getChildrenRequests().add(convertActionRequestVO(childVO, actionRequest, routeNodeInstanceLoader));
 686  
             }
 687  
         }
 688  0
         return actionRequest;
 689  
     }
 690  
 
 691  
     // TODO: should this be private?  If so, rename to convertActionRequestDTO for consistency.
 692  
     protected static ActionRequestValue convertActionRequestVO(ActionRequestDTO actionRequestDTO, ActionRequestValue parentActionRequest,
 693  
                     RouteNodeInstanceLoader routeNodeInstanceLoader) {
 694  0
         if (actionRequestDTO == null) {
 695  0
             return null;
 696  
         }
 697  0
         ActionRequestValue actionRequest = new ActionRequestFactory().createBlankActionRequest();
 698  0
         populateActionRequest(actionRequest, actionRequestDTO, routeNodeInstanceLoader);
 699  0
         actionRequest.setParentActionRequest(parentActionRequest);
 700  0
         actionRequest.setParentActionRequestId(parentActionRequest.getActionRequestId());
 701  0
         if (actionRequestDTO.getChildrenRequests() != null) {
 702  0
             for (int i = 0; i < actionRequestDTO.getChildrenRequests().length; i++) {
 703  0
                 ActionRequestDTO childVO = actionRequestDTO.getChildrenRequests()[i];
 704  0
                 actionRequest.getChildrenRequests().add(convertActionRequestVO(childVO, actionRequest, routeNodeInstanceLoader));
 705  
             }
 706  
         }
 707  0
         return actionRequest;
 708  
     }
 709  
 
 710  
     /**
 711  
      * This method converts everything except for the parent and child requests
 712  
      */
 713  
     private static void populateActionRequest(ActionRequestValue actionRequest, ActionRequestDTO actionRequestDTO, 
 714  
                     RouteNodeInstanceLoader routeNodeInstanceLoader) {
 715  
 
 716  0
         actionRequest.setActionRequested(actionRequestDTO.getActionRequested());
 717  0
         actionRequest.setActionRequestId(actionRequestDTO.getActionRequestId());
 718  0
         actionRequest.setActionTakenId(actionRequestDTO.getActionTakenId());
 719  0
         actionRequest.setAnnotation(actionRequestDTO.getAnnotation());
 720  0
         actionRequest.setApprovePolicy(actionRequestDTO.getApprovePolicy());
 721  0
         actionRequest.setCreateDate(new Timestamp(new Date().getTime()));
 722  0
         actionRequest.setCurrentIndicator(actionRequestDTO.getCurrentIndicator());
 723  0
         actionRequest.setDelegationType(actionRequestDTO.getDelegationType());
 724  0
         actionRequest.setDocVersion(actionRequestDTO.getDocVersion());
 725  0
         actionRequest.setForceAction(actionRequestDTO.getForceAction());
 726  0
         actionRequest.setPriority(actionRequestDTO.getPriority());
 727  0
         actionRequest.setQualifiedRoleName(actionRequestDTO.getQualifiedRoleName());
 728  0
         actionRequest.setQualifiedRoleNameLabel(actionRequestDTO.getQualifiedRoleNameLabel());
 729  0
         actionRequest.setRecipientTypeCd(actionRequestDTO.getRecipientTypeCd());
 730  0
         actionRequest.setResponsibilityDesc(actionRequestDTO.getResponsibilityDesc());
 731  0
         actionRequest.setResponsibilityId(actionRequestDTO.getResponsibilityId());
 732  0
         actionRequest.setRoleName(actionRequestDTO.getRoleName());
 733  0
         String documentId = actionRequestDTO.getDocumentId();
 734  0
         if (documentId != null) {
 735  0
             actionRequest.setDocumentId(documentId);
 736  
             //actionRequest.setRouteHeader(KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId));
 737  
         }
 738  0
         actionRequest.setRouteLevel(actionRequestDTO.getRouteLevel());
 739  
 
 740  0
         actionRequest.setStatus(actionRequestDTO.getStatus());
 741  0
         actionRequest.setPrincipalId(actionRequestDTO.getPrincipalId());
 742  0
         actionRequest.setGroupId(actionRequestDTO.getGroupId());
 743  
         
 744  0
         if (routeNodeInstanceLoader != null && actionRequestDTO.getNodeInstanceId() != null) {
 745  0
                 actionRequest.setNodeInstance(routeNodeInstanceLoader.load(actionRequestDTO.getNodeInstanceId()));
 746  
         }
 747  0
     }
 748  
 
 749  
     public static ActionTakenValue convertActionTakenVO(ActionTakenDTO actionTakenVO) {
 750  0
         if (actionTakenVO == null) {
 751  0
             return null;
 752  
         }
 753  0
         ActionTakenValue actionTaken = new ActionTakenValue();
 754  0
         actionTaken.setActionDate(new Timestamp(actionTakenVO.getActionDate().getTimeInMillis()));
 755  0
         actionTaken.setActionTaken(actionTakenVO.getActionTaken());
 756  0
         actionTaken.setActionTakenId(actionTakenVO.getActionTakenId());
 757  0
         actionTaken.setAnnotation(actionTakenVO.getAnnotation());
 758  0
         actionTaken.setCurrentIndicator(Boolean.TRUE);
 759  0
         actionTaken.setPrincipalId(actionTakenVO.getPrincipalId());
 760  0
         actionTaken.setDelegatorPrincipalId(actionTakenVO.getDelegatorPrincpalId());
 761  0
         actionTaken.setDelegatorGroupId(actionTakenVO.getDelegatorGroupId());
 762  0
         actionTaken.setDocVersion(actionTakenVO.getDocVersion());
 763  0
         KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionTakenVO.getDocumentId());
 764  
         //actionTaken.setRouteHeader(routeHeader);
 765  0
         actionTaken.setDocumentId(actionTaken.getDocumentId());
 766  0
         return actionTaken;
 767  
     }
 768  
 
 769  
     public static DocumentRouteStatusChangeDTO convertDocumentRouteStatusChange(DocumentRouteStatusChange statusChange) {
 770  0
         if (statusChange == null) {
 771  0
             return null;
 772  
         }
 773  0
         DocumentRouteStatusChangeDTO statusChangeVO = new DocumentRouteStatusChangeDTO();
 774  0
         statusChangeVO.setDocumentId(statusChange.getDocumentId());
 775  0
         statusChangeVO.setAppDocId(statusChange.getAppDocId());
 776  0
         statusChangeVO.setOldRouteStatus(statusChange.getOldRouteStatus());
 777  0
         statusChangeVO.setNewRouteStatus(statusChange.getNewRouteStatus());
 778  0
         return statusChangeVO;
 779  
     }
 780  
 
 781  
     public static DocumentRouteLevelChangeDTO convertDocumentRouteLevelChange(DocumentRouteLevelChange routeLevelChange) {
 782  0
         if (routeLevelChange == null) {
 783  0
             return null;
 784  
         }
 785  0
         DocumentRouteLevelChangeDTO routeLevelChangeVO = new DocumentRouteLevelChangeDTO();
 786  0
         routeLevelChangeVO.setDocumentId(routeLevelChange.getDocumentId());
 787  0
         routeLevelChangeVO.setAppDocId(routeLevelChange.getAppDocId());
 788  0
         routeLevelChangeVO.setOldRouteLevel(routeLevelChange.getOldRouteLevel());
 789  0
         routeLevelChangeVO.setNewRouteLevel(routeLevelChange.getNewRouteLevel());
 790  0
         routeLevelChangeVO.setOldNodeName(routeLevelChange.getOldNodeName());
 791  0
         routeLevelChangeVO.setNewNodeName(routeLevelChange.getNewNodeName());
 792  0
         routeLevelChangeVO.setOldNodeInstanceId(routeLevelChange.getOldNodeInstanceId());
 793  0
         routeLevelChangeVO.setNewNodeInstanceId(routeLevelChange.getNewNodeInstanceId());
 794  0
         return routeLevelChangeVO;
 795  
     }
 796  
 
 797  
     public static DeleteEventDTO convertDeleteEvent(DeleteEvent deleteEvent) {
 798  0
         if (deleteEvent == null) {
 799  0
             return null;
 800  
         }
 801  0
         DeleteEventDTO deleteEventVO = new DeleteEventDTO();
 802  0
         deleteEventVO.setDocumentId(deleteEvent.getDocumentId());
 803  0
         deleteEventVO.setAppDocId(deleteEvent.getAppDocId());
 804  0
         return deleteEventVO;
 805  
     }
 806  
 
 807  
     public static ActionTakenEventDTO convertActionTakenEvent(ActionTakenEvent actionTakenEvent) {
 808  0
         if (actionTakenEvent == null) {
 809  0
             return null;
 810  
         }
 811  0
         ActionTakenEventDTO actionTakenEventVO = new ActionTakenEventDTO();
 812  0
         actionTakenEventVO.setDocumentId(actionTakenEvent.getDocumentId());
 813  0
         actionTakenEventVO.setAppDocId(actionTakenEvent.getAppDocId());
 814  0
         actionTakenEventVO.setActionTaken(convertActionTaken(actionTakenEvent.getActionTaken()));
 815  0
         return actionTakenEventVO;
 816  
     }
 817  
 
 818  
     public static BeforeProcessEventDTO convertBeforeProcessEvent(BeforeProcessEvent event) {
 819  0
         if (event == null) {
 820  0
             return null;
 821  
         }
 822  0
         BeforeProcessEventDTO beforeProcessEvent = new BeforeProcessEventDTO();
 823  0
         beforeProcessEvent.setDocumentId(event.getDocumentId());
 824  0
         beforeProcessEvent.setAppDocId(event.getAppDocId());
 825  0
         beforeProcessEvent.setNodeInstanceId(event.getNodeInstanceId());
 826  0
         return beforeProcessEvent;
 827  
     }
 828  
 
 829  
     public static AfterProcessEventDTO convertAfterProcessEvent(AfterProcessEvent event) {
 830  0
         if (event == null) {
 831  0
             return null;
 832  
         }
 833  0
         AfterProcessEventDTO afterProcessEvent = new AfterProcessEventDTO();
 834  0
         afterProcessEvent.setDocumentId(event.getDocumentId());
 835  0
         afterProcessEvent.setAppDocId(event.getAppDocId());
 836  0
         afterProcessEvent.setNodeInstanceId(event.getNodeInstanceId());
 837  0
         afterProcessEvent.setSuccessfullyProcessed(event.isSuccessfullyProcessed());
 838  0
         return afterProcessEvent;
 839  
     }
 840  
 
 841  
     public static DocumentLockingEventDTO convertDocumentLockingEvent(DocumentLockingEvent event) {
 842  0
         if (event == null) {
 843  0
             return null;
 844  
         }
 845  0
         DocumentLockingEventDTO documentLockingEvent = new DocumentLockingEventDTO();
 846  0
         documentLockingEvent.setDocumentId(event.getDocumentId());
 847  0
         documentLockingEvent.setAppDocId(event.getAppDocId());
 848  0
         return documentLockingEvent;
 849  
     }
 850  
 
 851  
     
 852  
     public static AttributeDefinition convertWorkflowAttributeDefinitionVO(WorkflowAttributeDefinitionDTO definitionVO, org.kuali.rice.kew.doctype.bo.DocumentType documentType) {
 853  0
         if (definitionVO == null) {
 854  0
             return null;
 855  
         }
 856  
         // get the rule attribute so we can get's it's message antity and not blow up if it's remote
 857  0
         RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByClassName(definitionVO.getAttributeName());
 858  0
         if (ruleAttribute == null) {
 859  0
             ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(definitionVO.getAttributeName());
 860  
         }
 861  0
         if (ruleAttribute == null) {
 862  0
             throw new WorkflowRuntimeException("Attribute " + definitionVO.getAttributeName() + " not found");
 863  
         }
 864  
 
 865  0
         ObjectDefinition definition = new ObjectDefinition(ruleAttribute.getClassName());
 866  0
         for (int index = 0; index < definitionVO.getConstructorParameters().length; index++) {
 867  0
             String parameter = definitionVO.getConstructorParameters()[index];
 868  0
             definition.addConstructorParameter(new DataDefinition(parameter, String.class));
 869  
         }
 870  0
         boolean propertiesAsMap = KEWConstants.RULE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType()) || KEWConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType());
 871  0
         if (!propertiesAsMap) {
 872  0
             for (int index = 0; index < definitionVO.getProperties().length; index++) {
 873  0
                 PropertyDefinitionDTO propertyDefVO = definitionVO.getProperties()[index];
 874  0
                 definition.addProperty(new PropertyDefinition(propertyDefVO.getName(), new DataDefinition(propertyDefVO.getValue(), String.class)));
 875  
             }
 876  
         }
 877  
 
 878  
         // this is likely from an EDL validate call and ME may needed to be added to the AttDefinitionVO.
 879  0
         if (ruleAttribute.getApplicationId() != null) {
 880  0
             definition.setApplicationId(ruleAttribute.getApplicationId());
 881  
         } else {
 882  
             // get the me from the document type if it's been passed in - the document is having action taken on it.
 883  0
             if (documentType != null) {
 884  0
                 definition.setApplicationId(documentType.getApplicationId());
 885  
             }
 886  
         }
 887  
 
 888  0
         return new AttributeDefinition(ruleAttribute, definition);
 889  
     }
 890  
 
 891  
     public static DocumentDetailDTO convertDocumentDetail(DocumentRouteHeaderValue routeHeader) throws WorkflowException {
 892  0
         if (routeHeader == null) {
 893  0
             return null;
 894  
         }
 895  0
         DocumentDetailDTO detail = new DocumentDetailDTO();
 896  0
         populateRouteHeaderVO(detail, routeHeader);
 897  0
         Map nodeInstances = new HashMap();
 898  0
         List actionRequestVOs = new ArrayList();
 899  0
         List rootActionRequests = KEWServiceLocator.getActionRequestService().getRootRequests(routeHeader.getActionRequests());
 900  0
         for (Iterator iterator = rootActionRequests.iterator(); iterator.hasNext();) {
 901  0
             ActionRequestValue actionRequest = (ActionRequestValue) iterator.next();
 902  0
             actionRequestVOs.add(convertActionRequest(actionRequest));
 903  0
             RouteNodeInstance nodeInstance = actionRequest.getNodeInstance();
 904  0
             if (nodeInstance == null) {
 905  0
                 continue;
 906  
             }
 907  0
             if (nodeInstance.getRouteNodeInstanceId() == null) {
 908  0
                 throw new WorkflowException("Error creating document detail structure because of NULL node instance id.");
 909  
             }
 910  0
             nodeInstances.put(nodeInstance.getRouteNodeInstanceId(), nodeInstance);
 911  0
         }
 912  0
         detail.setActionRequests((ActionRequestDTO[]) actionRequestVOs.toArray(new ActionRequestDTO[0]));
 913  0
         List nodeInstanceVOs = new ArrayList();
 914  0
         for (Iterator iterator = nodeInstances.values().iterator(); iterator.hasNext();) {
 915  0
             RouteNodeInstance nodeInstance = (RouteNodeInstance) iterator.next();
 916  0
             nodeInstanceVOs.add(convertRouteNodeInstance(nodeInstance));
 917  0
         }
 918  0
         detail.setNodeInstances((RouteNodeInstanceDTO[]) nodeInstanceVOs.toArray(new RouteNodeInstanceDTO[0]));
 919  0
         List actionTakenVOs = new ArrayList();
 920  0
         for (Object element : routeHeader.getActionsTaken()) {
 921  0
             ActionTakenValue actionTaken = (ActionTakenValue) element;
 922  0
             actionTakenVOs.add(convertActionTaken(actionTaken));
 923  0
         }
 924  0
         detail.setActionsTaken((ActionTakenDTO[]) actionTakenVOs.toArray(new ActionTakenDTO[0]));
 925  0
         return detail;
 926  
     }
 927  
 
 928  
     public static DocumentDetail convertDocumentDetailNew(DocumentRouteHeaderValue routeHeader) {
 929  0
         if (routeHeader == null) {
 930  0
             return null;
 931  
         }
 932  0
         org.kuali.rice.kew.api.document.Document document = DocumentRouteHeaderValue.to(routeHeader);
 933  0
         DocumentDetail.Builder detail = DocumentDetail.Builder.create(document);
 934  0
         Map<String, RouteNodeInstance> nodeInstances = new HashMap<String, RouteNodeInstance>();
 935  0
         List<ActionRequest> actionRequestVOs = new ArrayList<ActionRequest>();
 936  0
         List<ActionRequestValue> rootActionRequests = KEWServiceLocator.getActionRequestService().getRootRequests(routeHeader.getActionRequests());
 937  0
         for (Iterator<ActionRequestValue> iterator = rootActionRequests.iterator(); iterator.hasNext();) {
 938  0
             ActionRequestValue actionRequest = iterator.next();
 939  0
             actionRequestVOs.add(ActionRequestValue.to(actionRequest));
 940  0
             RouteNodeInstance nodeInstance = actionRequest.getNodeInstance();
 941  0
             if (nodeInstance == null) {
 942  0
                 continue;
 943  
             }
 944  0
             if (nodeInstance.getRouteNodeInstanceId() == null) {
 945  0
                 throw new IllegalStateException("Error creating document detail structure because of NULL node instance id.");
 946  
             }
 947  0
             nodeInstances.put(nodeInstance.getRouteNodeInstanceId(), nodeInstance);
 948  0
         }
 949  0
         detail.setActionRequests(actionRequestVOs);
 950  0
         List<org.kuali.rice.kew.api.document.node.RouteNodeInstance> nodeInstanceVOs = new ArrayList<org.kuali.rice.kew.api.document.node.RouteNodeInstance>();
 951  0
         for (Iterator<RouteNodeInstance> iterator = nodeInstances.values().iterator(); iterator.hasNext();) {
 952  0
             RouteNodeInstance nodeInstance = iterator.next();
 953  0
             nodeInstanceVOs.add(RouteNodeInstance.to(nodeInstance));
 954  0
         }
 955  0
         detail.setRouteNodeInstances(nodeInstanceVOs);
 956  0
         List<ActionTaken> actionTakenVOs = new ArrayList<ActionTaken>();
 957  0
         for (Object element : routeHeader.getActionsTaken()) {
 958  0
             ActionTakenValue actionTaken = (ActionTakenValue) element;
 959  0
             actionTakenVOs.add(ActionTakenValue.to(actionTaken));
 960  0
         }
 961  0
         detail.setActionsTaken(actionTakenVOs);
 962  0
         return detail.build();
 963  
     }
 964  
 
 965  
     
 966  
     public static RouteNodeInstanceDTO convertRouteNodeInstance(RouteNodeInstance nodeInstance) throws WorkflowException {
 967  0
         if (nodeInstance == null) {
 968  0
             return null;
 969  
         }
 970  0
         RouteNodeInstanceDTO nodeInstanceVO = new RouteNodeInstanceDTO();
 971  0
         nodeInstanceVO.setActive(nodeInstance.isActive());
 972  0
         nodeInstanceVO.setBranchId(nodeInstance.getBranch().getBranchId());
 973  0
         nodeInstanceVO.setComplete(nodeInstance.isComplete());
 974  0
         nodeInstanceVO.setDocumentId(nodeInstance.getDocumentId());
 975  0
         nodeInstanceVO.setInitial(nodeInstance.isInitial());
 976  0
         nodeInstanceVO.setName(nodeInstance.getName());
 977  0
         nodeInstanceVO.setProcessId(nodeInstance.getProcess() != null ? nodeInstance.getProcess().getRouteNodeInstanceId() : null);
 978  0
         nodeInstanceVO.setRouteNodeId(nodeInstance.getRouteNode().getRouteNodeId());
 979  0
         nodeInstanceVO.setRouteNodeInstanceId(nodeInstance.getRouteNodeInstanceId());
 980  0
         nodeInstanceVO.setState(convertStates(nodeInstance.getState()));
 981  
 
 982  0
         nodeInstanceVO.setNextNodes(new RouteNodeInstanceDTO[nodeInstance.getNextNodeInstances().size()]);
 983  0
         int i = 0;
 984  0
         for (Iterator iter = nodeInstance.getNextNodeInstances().iterator(); iter.hasNext(); i++) {
 985  0
             RouteNodeInstance nextNodeInstance = (RouteNodeInstance) iter.next();
 986  0
             nodeInstanceVO.getNextNodes()[i] = convertRouteNodeInstance(nextNodeInstance);
 987  
         }
 988  
 
 989  0
         return nodeInstanceVO;
 990  
     }
 991  
 
 992  
     public static StateDTO[] convertStates(Collection states) {
 993  0
         if (states == null) {
 994  0
             return null;
 995  
         }
 996  0
         StateDTO[] stateVOs = new StateDTO[states.size()];
 997  0
         int index = 0;
 998  0
         for (Iterator iterator = states.iterator(); iterator.hasNext();) {
 999  0
             State state = (State) iterator.next();
 1000  0
             stateVOs[index++] = convertState(state);
 1001  0
         }
 1002  0
         return stateVOs;
 1003  
     }
 1004  
 
 1005  
     public static StateDTO convertState(State nodeState) {
 1006  0
         if (nodeState == null) {
 1007  0
             return null;
 1008  
         }
 1009  0
         StateDTO stateVO = new StateDTO();
 1010  0
         stateVO.setStateId(nodeState.getStateId());
 1011  0
         stateVO.setKey(nodeState.getKey());
 1012  0
         stateVO.setValue(nodeState.getValue());
 1013  0
         return stateVO;
 1014  
     }
 1015  
 
 1016  
     public static MovePoint convertMovePointVO(MovePointDTO movePointVO) {
 1017  0
         return MovePoint.create(movePointVO.getStartNodeName(), movePointVO.getStepsToMove());
 1018  
     }
 1019  
 
 1020  
     /**
 1021  
      * TODO: Temporary, just to keep compiler happy.  Remove once AdHocRevokeDTO can be removed.
 1022  
      */
 1023  
     public static AdHocRevoke convertAdHocRevokeVO(AdHocRevokeDTO revokeVO) throws WorkflowException {
 1024  0
             Set<String> nodeNames = new HashSet<String>();
 1025  0
             Set<String> principalIds = new HashSet<String>();
 1026  0
             Set<String> groupIds = new HashSet<String>();
 1027  0
             if (revokeVO.getNodeName() != null) {
 1028  0
                     nodeNames.add(revokeVO.getNodeName());
 1029  
             }
 1030  0
             if (revokeVO.getPrincipalId() != null) {
 1031  0
                     principalIds.add(revokeVO.getPrincipalId());
 1032  
             }
 1033  0
             if (revokeVO.getGroupId() != null) {
 1034  0
                     principalIds.add(revokeVO.getGroupId());
 1035  
             }
 1036  0
             return AdHocRevoke.create(nodeNames, principalIds, groupIds);
 1037  
     }
 1038  
 
 1039  
     public static WorkflowAttributeValidationErrorDTO convertWorkflowAttributeValidationError(WorkflowAttributeValidationError error) {
 1040  0
         return new WorkflowAttributeValidationErrorDTO(error.getKey(), error.getMessage());
 1041  
     }
 1042  
 
 1043  
     // Method added for updating notes on server sites based on NoteVO change. Modfy on April 7, 2006
 1044  
     public static void updateNotes(RouteHeaderDTO routeHeaderVO, String documentId) {
 1045  0
         NoteDTO[] notes = routeHeaderVO.getNotes();
 1046  0
         NoteDTO[] notesToDelete = routeHeaderVO.getNotesToDelete();
 1047  0
         Note noteToDelete = null;
 1048  0
         Note noteToSave = null;
 1049  
 
 1050  
         // Add or update notes to note table based on notes array in RouteHeaderVO
 1051  0
         if (notes != null) {
 1052  0
             for (NoteDTO note : notes) {
 1053  0
                 if (note != null) {
 1054  0
                     noteToSave = new Note();
 1055  0
                     noteToSave.setNoteId(note.getNoteId());
 1056  0
                     noteToSave.setDocumentId(documentId);
 1057  0
                     noteToSave.setNoteAuthorWorkflowId(note.getNoteAuthorWorkflowId());
 1058  0
                     noteToSave.setNoteCreateDate(SQLUtils.convertCalendar(note.getNoteCreateDate()));
 1059  0
                     noteToSave.setNoteText(note.getNoteText());
 1060  0
                     noteToSave.setLockVerNbr(note.getLockVerNbr());
 1061  
                     // if notes[i].getNoteId() == null, add note to note table, otherwise update note to note table
 1062  0
                     getNoteService().saveNote(noteToSave);
 1063  
                 }
 1064  
             }
 1065  
 
 1066  
         }
 1067  
 
 1068  
         // Delete notes from note table based on notesToDelete array in RouteHeaderVO
 1069  0
         if (notesToDelete != null) {
 1070  0
             for (NoteDTO element : notesToDelete) {
 1071  0
                 noteToDelete = getNoteService().getNoteByNoteId(element.getNoteId());
 1072  0
                 if (noteToDelete != null) {
 1073  0
                     getNoteService().deleteNote(noteToDelete);
 1074  
                 }
 1075  
             }
 1076  0
             routeHeaderVO.setNotesToDelete(null);
 1077  
         }
 1078  0
     }
 1079  
 
 1080  
     private static NoteService getNoteService() {
 1081  0
         return (NoteService) KEWServiceLocator.getService(KEWServiceLocator.NOTE_SERVICE);
 1082  
     }
 1083  
 
 1084  
     private static NoteDTO[] convertNotesArrayListToNoteVOArray(List notesArrayList) {
 1085  0
         if (notesArrayList.size() > 0) {
 1086  0
             NoteDTO[] noteVOArray = new NoteDTO[notesArrayList.size()];
 1087  0
             int i = 0;
 1088  
             Note tempNote;
 1089  
             NoteDTO tempNoteVO;
 1090  0
             for (Iterator it = notesArrayList.iterator(); it.hasNext();) {
 1091  0
                 tempNote = (Note) it.next();
 1092  0
                 tempNoteVO = new NoteDTO();
 1093  0
                 tempNoteVO.setNoteId(tempNote.getNoteId());
 1094  0
                 tempNoteVO.setDocumentId(tempNote.getDocumentId());
 1095  0
                 tempNoteVO.setNoteAuthorWorkflowId(tempNote.getNoteAuthorWorkflowId());
 1096  0
                 tempNoteVO.setNoteCreateDate(SQLUtils.convertTimestamp(tempNote.getNoteCreateDate()));
 1097  0
                 tempNoteVO.setNoteText(tempNote.getNoteText());
 1098  0
                 tempNoteVO.setLockVerNbr(tempNote.getLockVerNbr());
 1099  0
                 noteVOArray[i] = tempNoteVO;
 1100  0
                 i++;
 1101  
             }
 1102  0
             return noteVOArray;
 1103  
         } else {
 1104  0
             return null;
 1105  
         }
 1106  
     }
 1107  
 
 1108  
     public static SimulationCriteria convertReportCriteriaDTO(ReportCriteriaDTO criteriaVO) {
 1109  0
         if (criteriaVO == null) {
 1110  0
             return null;
 1111  
         }
 1112  0
         SimulationCriteria criteria = new SimulationCriteria();
 1113  0
         criteria.setDestinationNodeName(criteriaVO.getTargetNodeName());
 1114  0
         criteria.setDocumentId(criteriaVO.getDocumentId());
 1115  0
         criteria.setDocumentTypeName(criteriaVO.getDocumentTypeName());
 1116  0
         criteria.setXmlContent(criteriaVO.getXmlContent());
 1117  0
         criteria.setActivateRequests(criteriaVO.getActivateRequests());
 1118  0
         criteria.setFlattenNodes(criteriaVO.isFlattenNodes());
 1119  0
         if (criteriaVO.getRoutingPrincipalId() != null) {
 1120  0
                 Principal kPrinc = KEWServiceLocator.getIdentityHelperService().getPrincipal(criteriaVO.getRoutingPrincipalId());
 1121  0
             Person user = KimApiServiceLocator.getPersonService().getPerson(kPrinc.getPrincipalId());
 1122  0
             if (user == null) {
 1123  0
                 throw new RiceRuntimeException("Could not locate user for the given id: " + criteriaVO.getRoutingPrincipalId());
 1124  
             }
 1125  0
             criteria.setRoutingUser(user);
 1126  
         }
 1127  0
         if (criteriaVO.getRuleTemplateNames() != null) {
 1128  0
             for (int index = 0; index < criteriaVO.getRuleTemplateNames().length; index++) {
 1129  0
                 String ruleTemplateName = criteriaVO.getRuleTemplateNames()[index];
 1130  0
                 criteria.getRuleTemplateNames().add(ruleTemplateName);
 1131  
             }
 1132  
         }
 1133  0
         if (criteriaVO.getNodeNames() != null) {
 1134  0
             for (int i = 0; i < criteriaVO.getNodeNames().length; i++) {
 1135  0
                 String nodeName = criteriaVO.getNodeNames()[i];
 1136  0
                 criteria.getNodeNames().add(nodeName);
 1137  
             }
 1138  
         }
 1139  0
         if (criteriaVO.getTargetPrincipalIds() != null) {
 1140  0
             for (String targetPrincipalId : criteriaVO.getTargetPrincipalIds()) {
 1141  0
                 Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(targetPrincipalId);
 1142  0
                 criteria.getDestinationRecipients().add(new KimPrincipalRecipient(principal));
 1143  
             }
 1144  
         }
 1145  0
         if (criteriaVO.getActionsToTake() != null) {
 1146  0
             for (int index = 0; index < criteriaVO.getActionsToTake().length; index++) {
 1147  0
                 ReportActionToTakeDTO actionToTakeVO = criteriaVO.getActionsToTake()[index];
 1148  0
                 criteria.getActionsToTake().add(convertReportActionToTakeVO(actionToTakeVO));
 1149  
             }
 1150  
         }
 1151  0
         return criteria;
 1152  
     }
 1153  
 
 1154  
     public static SimulationActionToTake convertReportActionToTakeVO(ReportActionToTakeDTO actionToTakeVO) {
 1155  0
         if (actionToTakeVO == null) {
 1156  0
             return null;
 1157  
         }
 1158  0
         SimulationActionToTake actionToTake = new SimulationActionToTake();
 1159  0
         actionToTake.setNodeName(actionToTakeVO.getNodeName());
 1160  0
         if (StringUtils.isBlank(actionToTakeVO.getActionToPerform())) {
 1161  0
             throw new IllegalArgumentException("ReportActionToTakeVO must contain an action taken code and does not");
 1162  
         }
 1163  0
         actionToTake.setActionToPerform(actionToTakeVO.getActionToPerform());
 1164  0
         if (actionToTakeVO.getPrincipalId() == null) {
 1165  0
             throw new IllegalArgumentException("ReportActionToTakeVO must contain a principalId and does not");
 1166  
         }
 1167  0
         Principal kPrinc = KEWServiceLocator.getIdentityHelperService().getPrincipal(actionToTakeVO.getPrincipalId());
 1168  0
         Person user = KimApiServiceLocator.getPersonService().getPerson(kPrinc.getPrincipalId());
 1169  0
         if (user == null) {
 1170  0
             throw new RiceRuntimeException("Could not locate Person for the given id: " + actionToTakeVO.getPrincipalId());
 1171  
         }
 1172  0
         actionToTake.setUser(user);
 1173  0
         return actionToTake;
 1174  
     }
 1175  
 
 1176  
     public static RuleDelegationDTO convertRuleDelegation(RuleDelegation ruleDelegation) throws WorkflowException {
 1177  0
         if (ruleDelegation == null) {
 1178  0
             return null;
 1179  
         }
 1180  0
         RuleDelegationDTO ruleDelegationVO = new RuleDelegationDTO();
 1181  0
         ruleDelegationVO.setDelegationType(ruleDelegation.getDelegationType());
 1182  0
         ruleDelegationVO.setDelegationRule(convertRule(ruleDelegation.getDelegationRuleBaseValues()));
 1183  0
         return ruleDelegationVO;
 1184  
     }
 1185  
 
 1186  
     // public static RuleDelegation convertRuleExtensionVO(RuleExtensionVO ruleExtensionVO) throws WorkflowException {}
 1187  
 
 1188  
     public static Collection<RuleExtensionDTO> convertRuleExtension(RuleExtension ruleExtension) throws WorkflowException {
 1189  0
         if (ruleExtension == null) {
 1190  0
             return null;
 1191  
         }
 1192  0
         List<RuleExtensionDTO> extensionVOs = new ArrayList<RuleExtensionDTO>();
 1193  0
         for (Object element : ruleExtension.getExtensionValues()) {
 1194  0
             RuleExtensionValue extensionValue = (RuleExtensionValue) element;
 1195  0
             extensionVOs.add(new RuleExtensionDTO(extensionValue.getKey(), extensionValue.getValue()));
 1196  0
         }
 1197  0
         return extensionVOs;
 1198  
     }
 1199  
 
 1200  
     public static KeyValue convertRuleExtensionVO(RuleExtensionDTO ruleExtensionVO) throws WorkflowException {
 1201  0
         if (ruleExtensionVO == null) {
 1202  0
             return null;
 1203  
         }
 1204  0
         return new ConcreteKeyValue(ruleExtensionVO.getKey(), ruleExtensionVO.getValue());
 1205  
     }
 1206  
 
 1207  
     public static RuleResponsibilityDTO convertRuleResponsibility(RuleResponsibility ruleResponsibility) throws WorkflowException {
 1208  0
         if (ruleResponsibility == null) {
 1209  0
             return null;
 1210  
         }
 1211  0
         RuleResponsibilityDTO ruleResponsibilityVO = new RuleResponsibilityDTO();
 1212  0
         ruleResponsibilityVO.setActionRequestedCd(ruleResponsibility.getActionRequestedCd());
 1213  0
         ruleResponsibilityVO.setApprovePolicy(ruleResponsibility.getApprovePolicy());
 1214  0
         ruleResponsibilityVO.setPriority(ruleResponsibility.getPriority());
 1215  0
         ruleResponsibilityVO.setResponsibilityId(ruleResponsibility.getResponsibilityId());
 1216  0
         ruleResponsibilityVO.setRoleName(ruleResponsibility.getRole());
 1217  0
         if (ruleResponsibility.getPrincipal() != null) {
 1218  0
                 ruleResponsibilityVO.setPrincipalId(ruleResponsibility.getPrincipal().getPrincipalId());
 1219  0
         } else if (ruleResponsibility.getGroup() != null) {
 1220  0
                 ruleResponsibilityVO.setGroupId(ruleResponsibility.getGroup().getId());
 1221  0
         } else if (ruleResponsibility.getRole() != null) {
 1222  0
                 ruleResponsibilityVO.setRoleName(ruleResponsibility.getRole());
 1223  
         }
 1224  0
         for (Object element : ruleResponsibility.getDelegationRules()) {
 1225  0
             RuleDelegation ruleDelegation = (RuleDelegation) element;
 1226  0
             ruleResponsibilityVO.addDelegationRule(convertRuleDelegation(ruleDelegation));
 1227  0
         }
 1228  0
         return ruleResponsibilityVO;
 1229  
     }
 1230  
 
 1231  
     // public static KeyValuePair convertRuleResponsibilityVO(RuleResponsibilityVO ruleResponsibilityVO) throws
 1232  
     // WorkflowException {}
 1233  
 
 1234  
     public static RuleDTO convertRule(RuleBaseValues ruleValues) throws WorkflowException {
 1235  0
         if (ruleValues == null) {
 1236  0
             return null;
 1237  
         }
 1238  0
         RuleDTO rule = new RuleDTO();
 1239  0
         rule.setActiveInd(ruleValues.getActiveInd());
 1240  0
         rule.setDescription(ruleValues.getDescription());
 1241  0
         rule.setDocTypeName(ruleValues.getDocTypeName());
 1242  0
         rule.setFromDate(ruleValues.getFromDateString());
 1243  0
         rule.setToDate(ruleValues.getToDateString());
 1244  0
         rule.setForceAction(ruleValues.getForceAction());
 1245  0
         rule.setRuleTemplateId(ruleValues.getRuleTemplateId());
 1246  0
         rule.setRuleTemplateName(ruleValues.getRuleTemplateName());
 1247  
 
 1248  
         // get keyPair values to setup RuleExtensionVOs
 1249  0
         for (Object element : ruleValues.getRuleExtensions()) {
 1250  0
             RuleExtension ruleExtension = (RuleExtension) element;
 1251  0
             rule.addRuleExtensions(convertRuleExtension(ruleExtension));
 1252  0
         }
 1253  
         // get keyPair values to setup RuleExtensionVOs
 1254  0
         for (Object element : ruleValues.getResponsibilities()) {
 1255  0
             RuleResponsibility ruleResponsibility = (RuleResponsibility) element;
 1256  0
             rule.addRuleResponsibility(convertRuleResponsibility(ruleResponsibility));
 1257  0
         }
 1258  0
         return rule;
 1259  
     }
 1260  
 
 1261  
     public static DocSearchCriteriaDTO convertDocumentSearchCriteriaDTO(DocumentSearchCriteriaDTO criteriaVO) throws WorkflowException {
 1262  0
         DocSearchCriteriaDTO criteria = new DocSearchCriteriaDTO();
 1263  0
         criteria.setAppDocId(criteriaVO.getAppDocId());
 1264  0
         criteria.setApprover(criteriaVO.getApprover());
 1265  0
         criteria.setDocRouteStatus(criteriaVO.getDocRouteStatus());
 1266  0
         criteria.setDocTitle(criteriaVO.getDocTitle());
 1267  0
         criteria.setDocTypeFullName(criteriaVO.getDocTypeFullName());
 1268  0
         criteria.setDocVersion(criteriaVO.getDocVersion());
 1269  0
         criteria.setFromDateApproved(criteriaVO.getFromDateApproved());
 1270  0
         criteria.setFromDateCreated(criteriaVO.getFromDateCreated());
 1271  0
         criteria.setFromDateFinalized(criteriaVO.getFromDateFinalized());
 1272  0
         criteria.setFromDateLastModified(criteriaVO.getFromDateLastModified());
 1273  0
         criteria.setInitiator(criteriaVO.getInitiator());
 1274  0
         criteria.setIsAdvancedSearch((criteriaVO.isAdvancedSearch()) ? DocSearchCriteriaDTO.ADVANCED_SEARCH_INDICATOR_STRING : "NO");
 1275  0
         criteria.setSuperUserSearch((criteriaVO.isSuperUserSearch()) ? DocSearchCriteriaDTO.SUPER_USER_SEARCH_INDICATOR_STRING : "NO");
 1276  0
         criteria.setDocumentId(criteriaVO.getDocumentId());
 1277  0
         criteria.setViewer(criteriaVO.getViewer());
 1278  0
         criteria.setWorkgroupViewerName(criteriaVO.getGroupViewerName());
 1279  0
         criteria.setToDateApproved(criteriaVO.getToDateApproved());
 1280  0
         criteria.setToDateCreated(criteriaVO.getToDateCreated());
 1281  0
         criteria.setToDateFinalized(criteriaVO.getToDateFinalized());
 1282  0
         criteria.setToDateLastModified(criteriaVO.getToDateLastModified());
 1283  0
         criteria.setThreshold(criteriaVO.getThreshold());
 1284  0
         criteria.setSaveSearchForUser(criteriaVO.isSaveSearchForUser());
 1285  
 
 1286  
         // generate the route node criteria
 1287  0
         if ( (StringUtils.isNotBlank(criteriaVO.getDocRouteNodeName())) && (StringUtils.isBlank(criteriaVO.getDocTypeFullName())) ) {
 1288  0
             throw new WorkflowException("No document type name specified when attempting to search by route node name '" + criteriaVO.getDocRouteNodeName() + "'");
 1289  0
         } else if ( (StringUtils.isNotBlank(criteriaVO.getDocRouteNodeName())) && (StringUtils.isNotBlank(criteriaVO.getDocTypeFullName())) ) {
 1290  0
             criteria.setDocRouteNodeLogic(criteriaVO.getDocRouteNodeLogic());
 1291  0
             List routeNodes = KEWServiceLocator.getRouteNodeService().getFlattenedNodes(getDocumentTypeByName(criteria.getDocTypeFullName()), true);
 1292  0
             boolean foundRouteNode = false;
 1293  0
             for (Iterator iterator = routeNodes.iterator(); iterator.hasNext();) {
 1294  0
                 RouteNode routeNode = (RouteNode) iterator.next();
 1295  0
                 if (criteriaVO.getDocRouteNodeName().equals(routeNode.getRouteNodeName())) {
 1296  0
                     foundRouteNode = true;
 1297  0
                     break;
 1298  
                 }
 1299  0
             }
 1300  0
             if (!foundRouteNode) {
 1301  0
                 throw new WorkflowException("Could not find route node name '" + criteriaVO.getDocRouteNodeName() + "' for document type name '" + criteriaVO.getDocTypeFullName() + "'");
 1302  
             }
 1303  0
             criteria.setDocRouteNodeId(criteriaVO.getDocRouteNodeName());
 1304  
         }
 1305  
 
 1306  
         // build a map of the search attributes passed in from the client creating lists where keys are duplicated
 1307  0
         HashMap<String, List<String>> searchAttributeValues = new HashMap<String,List<String>>();
 1308  0
         for (KeyValue keyValueVO : criteriaVO.getSearchAttributeValues()) {
 1309  0
             if (searchAttributeValues.containsKey(keyValueVO.getKey())) {
 1310  0
                 searchAttributeValues.get(keyValueVO.getKey()).add(keyValueVO.getValue());
 1311  
             } else {
 1312  0
                 searchAttributeValues.put(keyValueVO.getKey(), Arrays.asList(new String[]{keyValueVO.getValue()}));
 1313  
             }
 1314  
         }
 1315  
         // build the list of SearchAttributeFormContainer objects
 1316  0
         List propertyFields = new ArrayList();
 1317  0
         for (String key : searchAttributeValues.keySet()) {
 1318  0
             List<String> values = searchAttributeValues.get(key);
 1319  0
             SearchAttributeFormContainer container = null;
 1320  0
             if (values.size() == 1) {
 1321  0
                 container = new SearchAttributeFormContainer(key, values.get(0));
 1322  0
             } else if (values.size() > 1) {
 1323  0
                 container = new SearchAttributeFormContainer(key, (String[])values.toArray());
 1324  
             }
 1325  0
             if (container != null) {
 1326  0
                 propertyFields.add(container);
 1327  
             }
 1328  0
         }
 1329  0
         DocSearchUtils.addSearchableAttributesToCriteria(criteria, propertyFields, true);
 1330  0
         return criteria;
 1331  
     }
 1332  
 
 1333  
     private static DocumentType getDocumentTypeByName(String documentTypeName) {
 1334  0
         return KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName);
 1335  
     }
 1336  
 
 1337  
     public static DocumentSearchResultDTO convertDocumentSearchResultComponents(DocumentSearchResultComponents searchResult) throws WorkflowException {
 1338  0
         DocumentSearchResultDTO resultsVO = new DocumentSearchResultDTO();
 1339  0
         resultsVO.setSearchResults(convertDocumentSearchResults(searchResult.getSearchResults()));
 1340  0
         return resultsVO;
 1341  
     }
 1342  
 
 1343  
     private static List<DocumentSearchResultRowDTO> convertDocumentSearchResults(List<DocumentSearchResult> searchResults) throws WorkflowException {
 1344  0
         List<DocumentSearchResultRowDTO> rowVOs = new ArrayList<DocumentSearchResultRowDTO>();
 1345  0
         for (DocumentSearchResult documentSearchResult : searchResults) {
 1346  0
             rowVOs.add(convertDocumentSearchResult(documentSearchResult));
 1347  
         }
 1348  0
         return rowVOs;
 1349  
     }
 1350  
 
 1351  
     public static DocumentSearchResultRowDTO convertDocumentSearchResult(DocumentSearchResult resultRow) throws WorkflowException {
 1352  0
         DocumentSearchResultRowDTO rowVO = new DocumentSearchResultRowDTO();
 1353  0
         List<ConcreteKeyValue> fieldValues = new ArrayList<ConcreteKeyValue>();
 1354  0
         for (KeyValueSort keyValueSort : resultRow.getResultContainers()) {
 1355  0
             fieldValues.add(new ConcreteKeyValue(keyValueSort.getKey(),keyValueSort.getUserDisplayValue()));
 1356  
         }
 1357  0
         rowVO.setFieldValues(fieldValues);
 1358  0
         return rowVO;
 1359  
     }
 1360  
 
 1361  
     public static DocumentStatusTransitionDTO convertDocumentStatusTransition(DocumentStatusTransition transition) throws WorkflowException {
 1362  0
             DocumentStatusTransitionDTO tranVO = new DocumentStatusTransitionDTO();
 1363  0
             tranVO.setStatusTransitionId(transition.getStatusTransitionId());
 1364  0
             tranVO.setDocumentId(transition.getDocumentId());
 1365  0
             tranVO.setOldAppDocStatus(transition.getOldAppDocStatus());
 1366  0
             tranVO.setNewAppDocStatus(transition.getNewAppDocStatus());
 1367  0
             tranVO.setStatusTransitionDate(transition.getStatusTransitionDate());            
 1368  0
             return tranVO;
 1369  
     }
 1370  
     
 1371  
     //    public static RuleBaseValues convertRuleVO(RuleVO ruleVO) throws WorkflowException {}
 1372  
 
 1373  
     private static void handleException(String message, Exception e) throws WorkflowException {
 1374  0
         if (e instanceof RuntimeException) {
 1375  0
             throw (RuntimeException) e;
 1376  0
         } else if (e instanceof WorkflowException) {
 1377  0
             throw (WorkflowException) e;
 1378  
         }
 1379  0
         throw new WorkflowException(message, e);
 1380  
     }
 1381  
 
 1382  
     //convert DocumentLink beans to array of DocumentLinkDTO
 1383  
     public static DocumentLinkDTO[] convertDocumentLink(Collection<DocumentLink> links) {
 1384  0
         if (links == null) {
 1385  0
             return null;
 1386  
         }
 1387  0
         DocumentLinkDTO[] docLinkVOs = new DocumentLinkDTO[links.size()];
 1388  
         
 1389  0
         int index = 0;
 1390  
         
 1391  0
         for (DocumentLink link: links) {
 1392  0
             docLinkVOs[index++] = convertDocumentLink(link);
 1393  
         }
 1394  0
         return docLinkVOs;
 1395  
     }
 1396  
     
 1397  
     //convert DocumentLink beans to list of DocumentLinkDTO
 1398  
     public static List<DocumentLinkDTO> convertDocumentLinkToArrayList(Collection<DocumentLink> links) {
 1399  0
         if (links == null) {
 1400  0
             return null;
 1401  
         }
 1402  0
         List<DocumentLinkDTO> docLinkVOs = new ArrayList<DocumentLinkDTO>(links.size());
 1403  
         
 1404  0
         for (DocumentLink link: links) {
 1405  0
             docLinkVOs.add(convertDocumentLink(link));
 1406  
         }
 1407  0
         return docLinkVOs;
 1408  
     }
 1409  
     
 1410  
     //covert DocumentLink bean to DocumentLinkDTO
 1411  
     public static DocumentLinkDTO convertDocumentLink(DocumentLink link) {
 1412  0
         if (link == null) {
 1413  0
             return null;
 1414  
         }
 1415  0
         DocumentLinkDTO linkVO = new DocumentLinkDTO();
 1416  0
         linkVO.setLinbkId(link.getDocLinkId());
 1417  0
         linkVO.setOrgnDocId(link.getOrgnDocId());
 1418  0
         linkVO.setDestDocId(link.getDestDocId());
 1419  
         
 1420  0
         return linkVO;
 1421  
     }
 1422  
 
 1423  
 }