Coverage Report - org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleLookupableHelperServiceImpl
0%
0/121
0%
0/62
2.455
RoleLookupableHelperServiceImpl$1
0%
0/2
N/A
2.455
 
 1  
 /*
 2  
  * Copyright 2007-2009 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  
 package org.kuali.rice.kim.lookup;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.util.ConcreteKeyValue;
 20  
 import org.kuali.rice.core.util.KeyValue;
 21  
 import org.kuali.rice.kew.util.KEWConstants;
 22  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 23  
 import org.kuali.rice.kim.api.type.KimType;
 24  
 import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;
 25  
 import org.kuali.rice.kim.impl.role.RoleBo;
 26  
 import org.kuali.rice.kim.impl.role.RoleDao;
 27  
 import org.kuali.rice.kim.impl.type.KimTypeBo;
 28  
 import org.kuali.rice.kim.impl.type.KimTypeLookupableHelperServiceImpl;
 29  
 import org.kuali.rice.kim.util.KimCommonUtilsInternal;
 30  
 import org.kuali.rice.kim.util.KimConstants;
 31  
 import org.kuali.rice.kim.web.struts.form.IdentityManagementRoleDocumentForm;
 32  
 import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
 33  
 import org.kuali.rice.kns.lookup.HtmlData;
 34  
 import org.kuali.rice.kns.web.struts.form.KualiForm;
 35  
 import org.kuali.rice.kns.web.struts.form.LookupForm;
 36  
 import org.kuali.rice.kns.web.ui.Field;
 37  
 import org.kuali.rice.kns.web.ui.Row;
 38  
 import org.kuali.rice.krad.bo.BusinessObject;
 39  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 40  
 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
 41  
 import org.kuali.rice.krad.datadictionary.KimAttributeDefinition;
 42  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 43  
 import org.kuali.rice.krad.service.ModuleService;
 44  
 import org.kuali.rice.krad.util.BeanPropertyComparator;
 45  
 import org.kuali.rice.krad.util.GlobalVariables;
 46  
 import org.kuali.rice.krad.util.KRADConstants;
 47  
 import org.kuali.rice.krad.util.UrlFactory;
 48  
 
 49  
 import java.util.ArrayList;
 50  
 import java.util.Collection;
 51  
 import java.util.Collections;
 52  
 import java.util.Comparator;
 53  
 import java.util.HashMap;
 54  
 import java.util.List;
 55  
 import java.util.Map;
 56  
 import java.util.Properties;
 57  
 
 58  
 /**
 59  
  * This is a description of what this class does - shyu don't forget to fill this in. 
 60  
  * 
 61  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 62  
  *
 63  
  */
 64  0
 public class RoleLookupableHelperServiceImpl extends KimLookupableHelperServiceImpl {
 65  
 
 66  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RoleLookupableHelperServiceImpl.class);
 67  
 
 68  
         // need this so kimtypeId value can be retained in 'rows'
 69  
         // 1st pass populate the grprows
 70  
         // 2nd pass for jsp, no populate, so return the existing one. 
 71  0
         private List<Row> roleRows = new ArrayList<Row>();
 72  0
         private List<Row> attrRows = new ArrayList<Row>();
 73  
         private RoleDao roleDao;
 74  
         private String typeId;
 75  
         private AttributeDefinitionMap attrDefinitions;
 76  
         
 77  
     @Override
 78  
     public List<HtmlData> getCustomActionUrls(BusinessObject bo, List pkNames) {
 79  0
             RoleBo roleBo = (RoleBo) bo;
 80  0
         List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
 81  0
             if(allowsNewOrCopyAction(KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_TYPE_NAME)){
 82  0
                     anchorHtmlDataList.add(getEditRoleUrl(roleBo));
 83  
             }
 84  0
             return anchorHtmlDataList;
 85  
     }
 86  
     
 87  
     protected HtmlData getEditRoleUrl(RoleBo roleBo) {
 88  0
         Properties parameters = new Properties();
 89  0
         parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
 90  0
         parameters.put(KRADConstants.PARAMETER_COMMAND, KEWConstants.INITIATE_COMMAND);
 91  0
         parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_TYPE_NAME);
 92  0
         parameters.put(KimConstants.PrimaryKeyConstants.SUB_ROLE_ID, roleBo.getId());
 93  0
         if (StringUtils.isNotBlank(getReturnLocation())) {
 94  0
                 parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());
 95  
                 }
 96  0
         String href = UrlFactory.parameterizeUrl(KimCommonUtilsInternal.getKimBasePath()+KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_ACTION, parameters);
 97  
         
 98  0
         HtmlData.AnchorHtmlData anchorHtmlData = new HtmlData.AnchorHtmlData(href,
 99  
                         KRADConstants.DOC_HANDLER_METHOD, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL);
 100  0
         return anchorHtmlData;
 101  
     }
 102  
 
 103  
     @Override
 104  
         protected HtmlData getReturnAnchorHtmlData(BusinessObject businessObject, Properties parameters, LookupForm lookupForm, List returnKeys, BusinessObjectRestrictions businessObjectRestrictions){
 105  0
             RoleBo roleBo = (RoleBo) businessObject;
 106  0
             HtmlData anchorHtmlData = super.getReturnAnchorHtmlData(businessObject, parameters, lookupForm, returnKeys, businessObjectRestrictions);
 107  
             
 108  
             // prevent derived roles from being selectable (except for identityManagementRoleDocuments)        
 109  0
             KualiForm myForm = (KualiForm) GlobalVariables.getUserSession().retrieveObject(getDocFormKey());
 110  0
             if (myForm == null || !(myForm instanceof IdentityManagementRoleDocumentForm)){
 111  0
                     if(KimTypeLookupableHelperServiceImpl.hasDerivedRoleTypeService(KimTypeBo.to(roleBo.getKimRoleType()))){
 112  0
                             ((HtmlData.AnchorHtmlData)anchorHtmlData).setHref("");
 113  
                     }
 114  
             }
 115  0
             return anchorHtmlData;
 116  
     }
 117  
     
 118  
     @Override
 119  
     public List<? extends BusinessObject> getSearchResults(java.util.Map<String,String> fieldValues) {
 120  0
             for (Map.Entry<String,String> entry : fieldValues.entrySet()) {
 121  0
                 if (entry.getKey().equals(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID)) {
 122  0
                         entry.getValue();
 123  0
                         break;
 124  
                 }
 125  
         }
 126  
   //          List<RoleImpl> roles = roleDao.getRoles(fieldValues, kimTypeId);
 127  0
         List<RoleBo> baseLookup = (List<RoleBo>)super.getSearchResults(fieldValues);
 128  
 
 129  0
         return baseLookup;
 130  
     }
 131  
 
 132  
         private List<KeyValue> getRoleTypeOptions() {
 133  0
                 List<KeyValue> options = new ArrayList<KeyValue>();
 134  0
                 options.add(new ConcreteKeyValue("", ""));
 135  
 
 136  0
                 Collection<KimType> kimGroupTypes = KimApiServiceLocator.getKimTypeInfoService().findAllKimTypes();
 137  
                 // get the distinct list of type IDs from all roles in the system
 138  0
         for (KimType kimType : kimGroupTypes) {
 139  0
             if (KimTypeLookupableHelperServiceImpl.hasRoleTypeService(kimType)) {
 140  0
                 String value = kimType.getNamespaceCode().trim() + KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR + kimType.getName().trim();
 141  0
                 options.add(new ConcreteKeyValue(kimType.getId(), value));
 142  0
             }
 143  
         }
 144  0
         Collections.sort(options, new Comparator<KeyValue>() {
 145  
            @Override
 146  
                 public int compare(KeyValue k1, KeyValue k2) {
 147  0
                return k1.getValue().compareTo(k2.getValue());
 148  
            }
 149  
         });
 150  0
                 return options;
 151  
         }
 152  
 
 153  
         private String getAttrDefnId(AttributeDefinition definition) {
 154  0
         return ((KimAttributeDefinition)definition).getKimAttrDefnId();
 155  
     }
 156  
         
 157  
         public List<Row> getRoleRows() {
 158  0
                 return this.roleRows;
 159  
         }
 160  
 
 161  
         public void setRoleRows(List<Row> roleRows) {
 162  0
                 this.roleRows = roleRows;
 163  0
         }
 164  
 
 165  
         public RoleDao getRoleDao() {
 166  0
                 return this.roleDao;
 167  
         }
 168  
 
 169  
         public void setRoleDao(RoleDao roleDao) {
 170  0
                 this.roleDao = roleDao;
 171  0
         }
 172  
 
 173  
         public AttributeDefinitionMap getAttrDefinitions() {
 174  0
                 return this.attrDefinitions;
 175  
         }
 176  
 
 177  
         public void setAttrDefinitions(AttributeDefinitionMap attrDefinitions) {
 178  0
                 this.attrDefinitions = attrDefinitions;
 179  0
         }
 180  
 
 181  
         public List<Row> getAttrRows() {
 182  0
                 return this.attrRows;
 183  
         }
 184  
 
 185  
         public void setAttrRows(List<Row> attrRows) {
 186  0
                 this.attrRows = attrRows;
 187  0
         }
 188  
 
 189  
         public String getTypeId() {
 190  0
                 return this.typeId;
 191  
         }
 192  
 
 193  
         public void setTypeId(String typeId) {
 194  0
                 this.typeId = typeId;
 195  0
         }
 196  
 
 197  
         @Override
 198  
         public List<Row> getRows() {
 199  0
                 new ArrayList<Row>();
 200  0
                 if (getRoleRows().isEmpty()) {
 201  0
                         List<Row> rows = super.getRows();
 202  0
                         List<Row> returnRows = new ArrayList<Row>();
 203  0
                         for (Row row : rows) {
 204  0
                                 for (int i = row.getFields().size() - 1; i >= 0; i--) {
 205  0
                                         Field field = row.getFields().get(i);
 206  0
                                         if (field.getPropertyName().equals("kimTypeId")) {
 207  0
                                                 Field typeField = new Field();
 208  0
                                                 typeField.setFieldLabel("Type");
 209  0
                                                 typeField.setPropertyName("kimTypeId");
 210  0
                                                 typeField.setFieldValidValues(getRoleTypeOptions());
 211  0
                                                 typeField.setFieldType(Field.DROPDOWN);
 212  0
                                                 typeField.setMaxLength(100);
 213  0
                                                 typeField.setSize(40);
 214  
                                                 // row.getFields().set(i, new Field("Type", "", Field.DROPDOWN_REFRESH,
 215  
                                                 // false, "kimTypeId", "", getGroupTypeOptions(), null));
 216  0
                                                 row.getFields().set(i, typeField);
 217  
                                         }
 218  
                                 }
 219  0
                                 returnRows.add(row);
 220  
                         }
 221  0
                         setRoleRows(returnRows);
 222  
                         //setAttrRows(setupAttributeRows());
 223  
                 }
 224  0
                 if (getAttrRows().isEmpty()) {
 225  
                         //setAttrDefinitions(new AttributeDefinitionMap());
 226  0
                         return getRoleRows();
 227  
                 } else {
 228  0
                         List<Row> fullRows = new ArrayList<Row>();
 229  0
                         fullRows.addAll(getRoleRows());
 230  
                         //fullRows.addAll(getAttrRows());
 231  0
                         return fullRows;
 232  
                 }
 233  
                 
 234  
         }
 235  
 
 236  
         @Override
 237  
         protected List<? extends BusinessObject> getSearchResultsHelper(
 238  
                         Map<String, String> fieldValues, boolean unbounded) {
 239  
         List searchResults;
 240  0
             Map<String,String> nonBlankFieldValues = new HashMap<String, String>();
 241  0
             boolean includeAttr = false;
 242  0
             for (String fieldName : fieldValues.keySet()) {
 243  0
                     if (StringUtils.isNotBlank(fieldValues.get(fieldName)) ) {
 244  0
                             nonBlankFieldValues.put(fieldName, fieldValues.get(fieldName));
 245  0
                             if (fieldName.contains(".")) {
 246  0
                                     includeAttr = true;
 247  
                             }
 248  
                     }
 249  
             }
 250  
 
 251  0
             if (includeAttr) {
 252  0
                 ModuleService eboModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService( getBusinessObjectClass() );
 253  0
                 BusinessObjectEntry ddEntry = eboModuleService.getExternalizableBusinessObjectDictionaryEntry(getBusinessObjectClass());
 254  0
                 Map<String,String> filteredFieldValues = new HashMap<String, String>();
 255  0
                 for (String fieldName : nonBlankFieldValues.keySet()) {
 256  0
                         if (ddEntry.getAttributeNames().contains(fieldName) || fieldName.contains(".")) {
 257  0
                                 filteredFieldValues.put(fieldName, nonBlankFieldValues.get(fieldName));
 258  
                         }
 259  
                 }
 260  0
                 searchResults = eboModuleService.getExternalizableBusinessObjectsListForLookup(getBusinessObjectClass(), (Map)filteredFieldValues, unbounded);
 261  
 
 262  0
             } else {
 263  0
                     searchResults = super.getSearchResultsHelper(fieldValues, unbounded);
 264  
             }
 265  0
         List defaultSortColumns = getDefaultSortColumns();
 266  0
         if (defaultSortColumns.size() > 0) {
 267  0
             Collections.sort(searchResults, new BeanPropertyComparator(defaultSortColumns, true));
 268  
         }
 269  0
         return searchResults;
 270  
 
 271  
         }
 272  
         
 273  
         private static final String ROLE_ID_URL_KEY = "&"+KimConstants.PrimaryKeyConstants.SUB_ROLE_ID+"=";
 274  
         /**
 275  
          * @see org.kuali.rice.krad.lookup.AbstractLookupableHelperServiceImpl#getInquiryUrl(org.kuali.rice.krad.bo.BusinessObject, java.lang.String)
 276  
          */
 277  
         @Override
 278  
         public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
 279  0
                 HtmlData.AnchorHtmlData inquiryHtmlData = (HtmlData.AnchorHtmlData)super.getInquiryUrl(bo, propertyName);
 280  0
                 if(inquiryHtmlData!=null && StringUtils.isNotBlank(inquiryHtmlData.getHref()) && inquiryHtmlData.getHref().contains(ROLE_ID_URL_KEY)) {
 281  0
                         inquiryHtmlData.setHref(getCustomRoleInquiryHref(getBackLocation(), inquiryHtmlData.getHref()));
 282  
                 }
 283  0
                 return inquiryHtmlData;
 284  
         }
 285  
 
 286  
         public static String getCustomRoleInquiryHref(String href){
 287  0
                 return getCustomRoleInquiryHref("", href);
 288  
         }
 289  
         
 290  
         static String getCustomRoleInquiryHref(String backLocation, String href){
 291  0
         Properties parameters = new Properties();
 292  0
         String hrefPart = "";
 293  0
             String docTypeAction = "";
 294  0
             if(StringUtils.isBlank(backLocation) || backLocation.contains(KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_ACTION)
 295  
                             || !backLocation.contains(KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION)){
 296  0
                     docTypeAction = KimConstants.KimUIConstants.KIM_ROLE_INQUIRY_ACTION;
 297  
             } else{
 298  0
                     docTypeAction = KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION;
 299  
             }
 300  0
                 if (StringUtils.isNotBlank(href) && href.contains(ROLE_ID_URL_KEY)) {
 301  0
                         int idx1 = href.indexOf("&"+KimConstants.PrimaryKeyConstants.SUB_ROLE_ID+"=");
 302  0
                     int idx2 = href.indexOf("&", idx1+1);
 303  0
                     if (idx2 < 0) {
 304  0
                             idx2 = href.length();
 305  
                     }
 306  0
                 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_INQUIRY);
 307  0
                 hrefPart = href.substring(idx1, idx2);
 308  
             }
 309  0
                 return UrlFactory.parameterizeUrl(KimCommonUtilsInternal.getKimBasePath()+docTypeAction, parameters)+hrefPart;
 310  
         }
 311  
 
 312  
 }