Coverage Report - org.kuali.rice.kew.rule.web.RuleQuickLinksAction
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleQuickLinksAction
0%
0/61
0%
0/24
1.385
RuleQuickLinksAction$1
N/A
N/A
1.385
RuleQuickLinksAction$DocumentTypeQuickLinksStructure
0%
0/33
0%
0/14
1.385
RuleQuickLinksAction$RouteNodeForDisplay
0%
0/38
0%
0/4
1.385
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.kew.rule.web;
 18  
 
 19  
 import org.apache.log4j.Logger;
 20  
 import org.apache.struts.action.ActionForm;
 21  
 import org.apache.struts.action.ActionForward;
 22  
 import org.apache.struts.action.ActionMapping;
 23  
 import org.apache.struts.action.ActionMessages;
 24  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 25  
 import org.kuali.rice.core.api.criteria.Predicate;
 26  
 import org.kuali.rice.core.api.criteria.QueryByCriteria;
 27  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 28  
 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
 29  
 import org.kuali.rice.kew.engine.node.BranchPrototype;
 30  
 import org.kuali.rice.kew.engine.node.RouteNode;
 31  
 import org.kuali.rice.kew.engine.node.RouteNodeConfigParam;
 32  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 33  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 34  
 import org.kuali.rice.kew.rule.service.RuleService;
 35  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 36  
 import org.kuali.rice.kew.util.KEWConstants;
 37  
 import org.kuali.rice.kew.web.KewKualiAction;
 38  
 import org.kuali.rice.kim.api.permission.Permission;
 39  
 import org.kuali.rice.kim.api.responsibility.Responsibility;
 40  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 41  
 import org.kuali.rice.kim.api.group.Group;
 42  
 import org.kuali.rice.kim.bo.role.dto.KimPermissionInfo;
 43  
 import org.kuali.rice.kim.util.KimConstants;
 44  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 45  
 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
 46  
 import org.kuali.rice.krad.service.DocumentHelperService;
 47  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 48  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 49  
 import org.kuali.rice.krad.util.GlobalVariables;
 50  
 import org.kuali.rice.krad.util.KRADConstants;
 51  
 
 52  
 import javax.servlet.http.HttpServletRequest;
 53  
 import javax.servlet.http.HttpServletResponse;
 54  
 import java.util.ArrayList;
 55  
 import java.util.HashMap;
 56  
 import java.util.Iterator;
 57  
 import java.util.List;
 58  
 import java.util.Map;
 59  
 
 60  
 import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
 61  
 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
 62  
 
 63  
 
 64  
 /**
 65  
  * A Struts Action for building and interacting with the Rule Quick Links.
 66  
  *
 67  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 68  
  */
 69  0
 public class RuleQuickLinksAction extends KewKualiAction {
 70  
 
 71  0
         private static final Logger LOG = Logger.getLogger(RuleQuickLinksAction.class);
 72  
         
 73  
         private MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
 74  
         private DocumentHelperService documentHelperService;
 75  
 
 76  
     public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 77  0
             makeLookupPathParam(mapping, request);
 78  0
             establishRequiredState(request, form);
 79  0
         return mapping.findForward("basic");
 80  
     }
 81  
 
 82  
     @SuppressWarnings("unchecked")
 83  
         public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm form) throws Exception {
 84  0
         RuleQuickLinksForm qlForm = (RuleQuickLinksForm) form;
 85  
         List<DocumentType> documentTypes;
 86  0
         if (qlForm.getRootDocTypeName() != null) {
 87  0
                 documentTypes = new ArrayList<DocumentType>();
 88  0
             DocumentType docType = getDocumentTypeService().findByName(qlForm.getRootDocTypeName());
 89  0
             documentTypes.add(docType);
 90  0
             request.setAttribute("renderOpened", Boolean.TRUE);
 91  0
         } else {
 92  0
                 documentTypes = getDocumentTypeService().findAllCurrentRootDocuments();
 93  
         }
 94  0
         qlForm.setDocumentTypeQuickLinksStructures(getDocumentTypeDataStructure(documentTypes));
 95  0
             int shouldDisplayCount = 0;
 96  0
         for ( DocumentTypeQuickLinksStructure dt : qlForm.getDocumentTypeQuickLinksStructures() ) {
 97  0
                 if ( dt.isShouldDisplay() ) {
 98  0
                         shouldDisplayCount++;
 99  
                 }
 100  
         }
 101  0
             if ( shouldDisplayCount == 1 ) {
 102  0
             request.setAttribute("renderOpened", Boolean.TRUE);
 103  
             }
 104  0
         String documentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(DocumentType.class);
 105  
         try {
 106  0
             if ((documentTypeName != null) && getDocumentHelperService().getDocumentAuthorizer(documentTypeName).canInitiate(documentTypeName, GlobalVariables.getUserSession().getPerson())) {
 107  0
                 qlForm.setCanInitiateDocumentTypeDocument( true );
 108  
             }
 109  0
         } catch (Exception ex) {
 110  
                         // just skip - and don't display links
 111  0
                 LOG.error( "Unable to check initiation permission for "+ documentTypeName, ex );
 112  0
                 }
 113  
 
 114  0
         return null;
 115  
     }
 116  
 
 117  
     public ActionForward addDelegationRule(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 118  0
             ActionForward result = null;
 119  
             
 120  0
             String ruleTemplateId = request.getParameter("delegationRuleBaseValues.ruleTemplate.ruleTemplateId");
 121  0
         String docTypeName = request.getParameter("delegationRuleBaseValues.documentType.name");
 122  0
         List rules = getRuleService().search(docTypeName, null, ruleTemplateId, "", null, null, Boolean.FALSE, Boolean.TRUE, new HashMap(), null);
 123  
         
 124  0
         if (rules.size() == 1) {
 125  0
             RuleBaseValues rule = (RuleBaseValues)rules.get(0);
 126  0
             String url = ConfigContext.getCurrentContextConfig().getKEWBaseURL() +
 127  
                 "/DelegateRule.do?methodToCall=start" +
 128  
                     "&parentRuleId=" + rule.getRuleBaseValuesId();
 129  0
             result = new ActionForward(url, true);
 130  0
         } else {
 131  0
                 makeLookupPathParam(mapping, request);
 132  0
                 result = new ActionForward(ConfigContext.getCurrentContextConfig().getKRBaseURL() + 
 133  
                                 "/lookup.do?methodToCall=start&"+ stripMethodToCall(request.getQueryString()), true);
 134  
         }
 135  
         
 136  0
         return result;
 137  
     }
 138  
 
 139  
         private List getDocumentTypeDataStructure(List rootDocuments) {
 140  0
                 List documentTypeQuickLinksStructures = new ArrayList();
 141  0
                 for (Iterator iter = rootDocuments.iterator(); iter.hasNext();) {
 142  0
                         DocumentTypeQuickLinksStructure quickLinkStruct =new DocumentTypeQuickLinksStructure((DocumentType) iter.next());
 143  0
                         if (! quickLinkStruct.getFlattenedNodes().isEmpty() || ! quickLinkStruct.getChildrenDocumentTypes().isEmpty()) {
 144  0
                                 documentTypeQuickLinksStructures.add(quickLinkStruct);
 145  
                         }
 146  
 
 147  0
                 }
 148  
 
 149  0
                 return documentTypeQuickLinksStructures;
 150  
         }
 151  
 
 152  
 
 153  
         /**
 154  
      * A bean to hold a DocumentType with its flattened nodes for rendering purposes
 155  
      * on the quick links.
 156  
      *
 157  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 158  
      */
 159  0
     public static class DocumentTypeQuickLinksStructure {
 160  
         private DocumentType documentType;
 161  0
         private List<RouteNode> flattenedNodes = new ArrayList<RouteNode>();
 162  0
         private List<DocumentTypeQuickLinksStructure> childrenDocumentTypes = new ArrayList<DocumentTypeQuickLinksStructure>();
 163  0
         private List<Permission> permissions = null;
 164  
         
 165  0
         private DocumentTypeQuickLinksStructure(DocumentType documentType) {
 166  0
                         this.documentType = documentType;
 167  0
                         if ( documentType != null ) {
 168  0
                                 List<RouteNode> tempFlattenedNodes = KEWServiceLocator.getRouteNodeService()
 169  
                                                 .getFlattenedNodes( documentType, true );
 170  0
                                 for ( RouteNode routeNode : tempFlattenedNodes ) {
 171  0
                                         if ( routeNode.isFlexRM() || routeNode.isRoleNode() ) {
 172  0
                                                 flattenedNodes.add( new RouteNodeForDisplay( routeNode ) );
 173  
                                         }
 174  
                                 }
 175  0
                                 for ( Iterator<DocumentType> iter = documentType.getChildrenDocTypes().iterator(); iter.hasNext(); ) {
 176  0
                                         childrenDocumentTypes.add( new DocumentTypeQuickLinksStructure( iter.next() ) );
 177  
                                 }
 178  
                         }
 179  0
                 }
 180  
 
 181  
         public List getChildrenDocumentTypes() {
 182  0
             return childrenDocumentTypes;
 183  
         }
 184  
         public void setChildrenDocumentTypes(List<DocumentTypeQuickLinksStructure> childrenDocumentTypes) {
 185  0
             this.childrenDocumentTypes = childrenDocumentTypes;
 186  0
         }
 187  
         public DocumentType getDocumentType() {
 188  0
             return documentType;
 189  
         }
 190  
         public void setDocumentType(DocumentType documentType) {
 191  0
             this.documentType = documentType;
 192  0
         }
 193  
         public List getFlattenedNodes() {
 194  0
             return flattenedNodes;
 195  
         }
 196  
         public void setFlattenedNodes(List<RouteNode> flattenedNodes) {
 197  0
             this.flattenedNodes = flattenedNodes;
 198  0
         }
 199  
         public boolean isShouldDisplay() {
 200  
 //                        if (flattenedNodes.isEmpty()) {
 201  
 //                                for (DocumentTypeQuickLinksStructure docType : childrenDocumentTypes) {
 202  
 //                                        if (docType.isShouldDisplay()) {
 203  
 //                                                return true;
 204  
 //                                        }
 205  
 //                                }
 206  
 //                                return false;
 207  
 //                        }
 208  0
                         return true;
 209  
                 }
 210  
 
 211  
                 public List<Permission> getPermissions() {
 212  0
                         if ( permissions == null ) {
 213  
 //                                Logger sqlLogger = Logger.getLogger(SqlGeneratorSuffixableImpl.class);
 214  
 //                                sqlLogger.setLevel( Level.DEBUG );
 215  0
                                 Map<String,String> searchCriteria = new HashMap<String,String>();
 216  0
                                 searchCriteria.put("attributeName", "documentTypeName" );
 217  0
                                 searchCriteria.put("active", "Y");
 218  0
                                 searchCriteria.put("detailCriteria",
 219  
                                                 KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME+"="+getDocumentType().getName()
 220  
                                                 );
 221  0
                                 permissions = KimApiServiceLocator.getPermissionService().lookupPermissions( searchCriteria, false );
 222  
 //                                sqlLogger.setLevel( Level.INFO );
 223  
                         }
 224  0
                         return permissions;
 225  
                 }
 226  
                 
 227  
                 public boolean isHasRelatedPermissions() {
 228  0
                         return !getPermissions().isEmpty();
 229  
                 }
 230  
                 
 231  
                 public int getRelatedPermissionCount() {
 232  0
                         return getPermissions().size();
 233  
                 }
 234  
     }
 235  
 
 236  0
     public static class RouteNodeForDisplay extends RouteNode {
 237  
             private static final long serialVersionUID = 1L;
 238  
                 private RouteNode baseNode;
 239  
             
 240  0
                 public RouteNodeForDisplay( RouteNode baseNode ) {
 241  0
                         this.baseNode = baseNode;
 242  0
                 }
 243  
 
 244  
                 public boolean equals(Object obj) {
 245  0
                         return this.baseNode.equals(obj);
 246  
                 }
 247  
 
 248  
                 public String getActivationType() {
 249  0
                         return this.baseNode.getActivationType();
 250  
                 }
 251  
 
 252  
                 public BranchPrototype getBranch() {
 253  0
                         return this.baseNode.getBranch();
 254  
                 }
 255  
 
 256  
                 public List<RouteNodeConfigParam> getConfigParams() {
 257  0
                         return this.baseNode.getConfigParams();
 258  
                 }
 259  
 
 260  
                 public String getContentFragment() {
 261  0
                         return this.baseNode.getContentFragment();
 262  
                 }
 263  
 
 264  
                 public DocumentType getDocumentType() {
 265  0
                         return this.baseNode.getDocumentType();
 266  
                 }
 267  
 
 268  
                 public String getDocumentTypeId() {
 269  0
                         return this.baseNode.getDocumentTypeId();
 270  
                 }
 271  
                 public Group getExceptionWorkgroup() {
 272  0
                         return this.baseNode.getExceptionWorkgroup();
 273  
                 }
 274  
                 public String getExceptionWorkgroupId() {
 275  0
                         return this.baseNode.getExceptionWorkgroupId();
 276  
                 }
 277  
                 public String getExceptionWorkgroupName() {
 278  0
                         return this.baseNode.getExceptionWorkgroupName();
 279  
                 }
 280  
                 public Boolean getFinalApprovalInd() {
 281  0
                         return this.baseNode.getFinalApprovalInd();
 282  
                 }
 283  
                 public Integer getLockVerNbr() {
 284  0
                         return this.baseNode.getLockVerNbr();
 285  
                 }
 286  
                 public Boolean getMandatoryRouteInd() {
 287  0
                         return this.baseNode.getMandatoryRouteInd();
 288  
                 }
 289  
                 public List<RouteNode> getNextNodes() {
 290  0
                         return this.baseNode.getNextNodes();
 291  
                 }
 292  
                 public String getNodeType() {
 293  0
                         return this.baseNode.getNodeType();
 294  
                 }
 295  
                 public List<RouteNode> getPreviousNodes() {
 296  0
                         return this.baseNode.getPreviousNodes();
 297  
                 }
 298  
                 public String getRouteMethodCode() {
 299  0
                         return this.baseNode.getRouteMethodCode();
 300  
                 }
 301  
                 public String getRouteMethodName() {
 302  0
                         return this.baseNode.getRouteMethodName();
 303  
                 }
 304  
                 public String getRouteNodeId() {
 305  0
                         return this.baseNode.getRouteNodeId();
 306  
                 }
 307  
                 public String getRouteNodeName() {
 308  0
                         return this.baseNode.getRouteNodeName();
 309  
                 }
 310  
                 public RuleTemplate getRuleTemplate() {
 311  0
                         return this.baseNode.getRuleTemplate();
 312  
                 }
 313  
                 public int hashCode() {
 314  0
                         return this.baseNode.hashCode();
 315  
                 }
 316  
                 public boolean isExceptionGroupDefined() {
 317  0
                         return this.baseNode.isExceptionGroupDefined();
 318  
                 }
 319  
                 public boolean isFlexRM() {
 320  0
                         return this.baseNode.isFlexRM();
 321  
                 }
 322  
                 public boolean isRoleNode() {
 323  0
                         return this.baseNode.isRoleNode();
 324  
                 }
 325  
                 public String toString() {
 326  0
                         return this.baseNode.toString();
 327  
                 }
 328  
                 
 329  0
                 private List<Responsibility> responsibilities = null;
 330  
                 
 331  
                 public List<Responsibility> getResponsibilities() {
 332  0
                         if ( responsibilities == null ) {
 333  0
                                 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
 334  0
                 Predicate p = and(
 335  
                     equal("template.namespaceCode", KRADConstants.KUALI_RICE_WORKFLOW_NAMESPACE),
 336  
                     equal("template.name", KEWConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME),
 337  
                     equal("active", "Y"),
 338  
                     equal("attributes[documentTypeName]", getDocumentType().getName()),
 339  
                     equal("attributes[routeNodeName]", getRouteNodeName())
 340  
                 );
 341  0
                 builder.setPredicates(p);
 342  0
                                  responsibilities = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build()).getResults();
 343  
                         }
 344  0
                         return responsibilities;
 345  
                 }
 346  
                 
 347  
                 public int getResponsibilityCount() {
 348  0
                         return getResponsibilities().size();
 349  
                 }
 350  
                 
 351  
                 public boolean isHasResponsibility() {
 352  0
                         return !getResponsibilities().isEmpty();
 353  
                 }
 354  
     }
 355  
     
 356  
     private void makeLookupPathParam(ActionMapping mapping, HttpServletRequest request) {
 357  0
             String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + mapping.getModuleConfig().getPrefix();
 358  0
             request.setAttribute("basePath", basePath);
 359  0
     }
 360  
 
 361  
     private String stripMethodToCall(String queryString) {
 362  0
         return queryString.replaceAll("methodToCall=addDelegationRule&", "");
 363  
     }
 364  
 
 365  
     private DocumentTypeService getDocumentTypeService() {
 366  0
         return KEWServiceLocator.getDocumentTypeService();
 367  
     }
 368  
 
 369  
     private RuleService getRuleService() {
 370  0
             return KEWServiceLocator.getRuleService();
 371  
     }
 372  
     
 373  
         public DocumentHelperService getDocumentHelperService() {
 374  0
                 if(documentHelperService == null){
 375  0
                         documentHelperService = KRADServiceLocatorWeb.getDocumentHelperService();
 376  
                 }
 377  0
                 return documentHelperService;
 378  
         }
 379  
 
 380  
         public MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
 381  0
                 if(maintenanceDocumentDictionaryService == null){
 382  0
                         maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService();
 383  
                 }
 384  0
                 return maintenanceDocumentDictionaryService;
 385  
         }
 386  
 
 387  
         /**
 388  
          * @see org.kuali.rice.krad.web.struts.action.KualiAction#toggleTab(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 389  
          */
 390  
         @Override
 391  
         public ActionForward toggleTab(ActionMapping mapping, ActionForm form,
 392  
                         HttpServletRequest request, HttpServletResponse response)
 393  
                         throws Exception {
 394  
                 
 395  0
                 establishRequiredState(request, form);
 396  0
                 return super.toggleTab(mapping, form, request, response);
 397  
         }
 398  
 
 399  
 }