Coverage Report - org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleLookupableHelperServiceImpl
0%
0/163
0%
0/82
3.087
RoleLookupableHelperServiceImpl$1
0%
0/2
N/A
3.087
 
 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.ClassLoaderUtils;
 20  
 import org.kuali.rice.core.util.KeyLabelPair;
 21  
 import org.kuali.rice.kew.util.KEWConstants;
 22  
 import org.kuali.rice.kim.bo.impl.RoleImpl;
 23  
 import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;
 24  
 import org.kuali.rice.kim.bo.types.dto.KimTypeInfo;
 25  
 import org.kuali.rice.kim.dao.KimRoleDao;
 26  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 27  
 import org.kuali.rice.kim.service.support.KimTypeService;
 28  
 import org.kuali.rice.kim.util.KimCommonUtils;
 29  
 import org.kuali.rice.kim.util.KimConstants;
 30  
 import org.kuali.rice.kns.authorization.BusinessObjectRestrictions;
 31  
 import org.kuali.rice.kns.bo.BusinessObject;
 32  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 33  
 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
 34  
 import org.kuali.rice.kns.datadictionary.KimDataDictionaryAttributeDefinition;
 35  
 import org.kuali.rice.kns.datadictionary.KimNonDataDictionaryAttributeDefinition;
 36  
 import org.kuali.rice.kns.lookup.HtmlData;
 37  
 import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
 38  
 import org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder;
 39  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 40  
 import org.kuali.rice.kns.service.ModuleService;
 41  
 import org.kuali.rice.kns.util.BeanPropertyComparator;
 42  
 import org.kuali.rice.kns.util.KNSConstants;
 43  
 import org.kuali.rice.kns.util.UrlFactory;
 44  
 import org.kuali.rice.kns.web.struts.form.LookupForm;
 45  
 import org.kuali.rice.kns.web.ui.Field;
 46  
 import org.kuali.rice.kns.web.ui.Row;
 47  
 
 48  
 import java.util.*;
 49  
 
 50  
 /**
 51  
  * This is a description of what this class does - shyu don't forget to fill this in. 
 52  
  * 
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  *
 55  
  */
 56  0
 public class RoleLookupableHelperServiceImpl extends KimLookupableHelperServiceImpl {
 57  
 
 58  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RoleLookupableHelperServiceImpl.class);
 59  
 
 60  
         // need this so kimtypeId value can be retained in 'rows'
 61  
         // 1st pass populate the grprows
 62  
         // 2nd pass for jsp, no populate, so return the existing one. 
 63  0
         private List<Row> roleRows = new ArrayList<Row>();
 64  0
         private List<Row> attrRows = new ArrayList<Row>();
 65  
         private KimRoleDao roleDao; 
 66  
         private String typeId;
 67  
         private AttributeDefinitionMap attrDefinitions;
 68  
         
 69  
     @Override
 70  
     public List<HtmlData> getCustomActionUrls(BusinessObject bo, List pkNames) {
 71  0
             RoleImpl roleImpl = (RoleImpl) bo;
 72  0
         List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
 73  0
             if(allowsNewOrCopyAction(KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_TYPE_NAME)){
 74  0
                     anchorHtmlDataList.add(getEditRoleUrl(roleImpl));
 75  
             }
 76  0
             return anchorHtmlDataList;
 77  
     }
 78  
     
 79  
     protected HtmlData getEditRoleUrl(RoleImpl roleImpl) {
 80  0
             String href = "";
 81  
 
 82  0
         Properties parameters = new Properties();
 83  0
         parameters.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.DOC_HANDLER_METHOD);
 84  0
         parameters.put(KNSConstants.PARAMETER_COMMAND, KEWConstants.INITIATE_COMMAND);
 85  0
         parameters.put(KNSConstants.DOCUMENT_TYPE_NAME, KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_TYPE_NAME);
 86  0
         parameters.put(KimConstants.PrimaryKeyConstants.ROLE_ID, roleImpl.getRoleId());
 87  0
         if (StringUtils.isNotBlank(getReturnLocation())) {
 88  0
                 parameters.put(KNSConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());         
 89  
                 }
 90  0
         href = UrlFactory.parameterizeUrl(KimCommonUtils.getKimBasePath()+KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_ACTION, parameters);
 91  
         
 92  0
         AnchorHtmlData anchorHtmlData = new AnchorHtmlData(href, 
 93  
                         KNSConstants.DOC_HANDLER_METHOD, KNSConstants.MAINTENANCE_EDIT_METHOD_TO_CALL);
 94  0
         return anchorHtmlData;
 95  
     }
 96  
 
 97  
     protected HtmlData getReturnAnchorHtmlData(BusinessObject businessObject, Properties parameters, LookupForm lookupForm, List returnKeys, BusinessObjectRestrictions businessObjectRestrictions){
 98  0
             RoleImpl roleImpl = (RoleImpl) businessObject;
 99  0
             HtmlData anchorHtmlData = super.getReturnAnchorHtmlData(businessObject, parameters, lookupForm, returnKeys, businessObjectRestrictions);
 100  0
             if(KimTypeLookupableHelperServiceImpl.hasDerivedRoleTypeService(roleImpl.getKimRoleType())){
 101  0
                     ((AnchorHtmlData)anchorHtmlData).setHref("");
 102  
             }
 103  0
             return anchorHtmlData;
 104  
     }
 105  
     
 106  
     @Override
 107  
     public List<? extends BusinessObject> getSearchResults(java.util.Map<String,String> fieldValues) {
 108  
 //            String principalName = fieldValues.get("principalName");
 109  
 //            fieldValues.put("principalName","");
 110  0
         String kimTypeId = null;
 111  0
         for (Map.Entry<String,String> entry : fieldValues.entrySet()) {
 112  0
                 if (entry.getKey().equals(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID)) {
 113  0
                         kimTypeId=entry.getValue();
 114  0
                         break;
 115  
                 }
 116  
         }
 117  
   //          List<RoleImpl> roles = roleDao.getRoles(fieldValues, kimTypeId);
 118  0
         List<RoleImpl> baseLookup = (List<RoleImpl>)super.getSearchResults(fieldValues);
 119  
 
 120  0
         return baseLookup;
 121  
     }
 122  
 
 123  
         private List<KeyLabelPair> getRoleTypeOptions() {
 124  0
                 List<KeyLabelPair> options = new ArrayList<KeyLabelPair>();
 125  0
                 options.add(new KeyLabelPair("", ""));
 126  
 
 127  0
                 Collection<KimTypeInfo> kimGroupTypes = KIMServiceLocator.getTypeInfoService().getAllTypes();
 128  
                 // get the distinct list of type IDs from all roles in the system
 129  0
         for (KimTypeInfo kimType : kimGroupTypes) {
 130  0
             if (KimTypeLookupableHelperServiceImpl.hasRoleTypeService(kimType)) {
 131  0
                 String value = kimType.getNamespaceCode().trim() + KNSConstants.FIELD_CONVERSION_PAIR_SEPARATOR + kimType.getName().trim();
 132  0
                 options.add(new KeyLabelPair(kimType.getKimTypeId(), value));
 133  0
             }
 134  
         }
 135  0
         Collections.sort(options, new Comparator<KeyLabelPair>() {
 136  
            public int compare(KeyLabelPair k1, KeyLabelPair k2) {
 137  0
                return k1.getLabel().compareTo(k2.getLabel());
 138  
            }
 139  
         });
 140  0
                 return options;
 141  
         }
 142  
 
 143  
         private List<Row> setupAttributeRows() {
 144  0
                 List<Row> returnRows = new ArrayList<Row>();
 145  0
                 for (Row row : getRoleRows()) {
 146  0
                         Field field = (Field) row.getFields().get(0);
 147  0
                         if (field.getPropertyName().equals("kimTypeId") && StringUtils.isNotBlank(field.getPropertyValue())) {
 148  0
                                 if (StringUtils.isBlank(getTypeId()) || !getTypeId().equals(field.getPropertyValue())) {
 149  0
                                         setTypeId(field.getPropertyValue());
 150  0
                                         setAttrRows(new ArrayList<Row>());
 151  
                                                                                 
 152  0
                                         KimTypeInfo kimType = getTypeInfoService().getKimType(field.getPropertyValue() );
 153  
                                         // TODO what if servicename is null.  also check other places that have similar issue
 154  
                                         // use default_service ?
 155  0
                                 KimTypeService kimTypeService = KimCommonUtils.getKimTypeService(kimType);
 156  0
                                 if ( kimTypeService != null ) {
 157  0
                                         AttributeDefinitionMap definitions = kimTypeService.getAttributeDefinitions(kimType.getKimTypeId());
 158  0
                                         setAttrDefinitions(definitions);
 159  0
                                         if(definitions!=null){
 160  0
                                             for ( AttributeDefinition definition : definitions.values()) {
 161  0
                                                         List<Field> fields = new ArrayList<Field>();
 162  0
                                                                 Field typeField = new Field();
 163  
                                                                 //String attrDefnId = mapEntry.getKey().substring(mapEntry.getKey().indexOf("."), mapEntry.getKey().length());
 164  
                 //                                                String attrDefnId = definition.getId();
 165  0
                                                                 String attrDefnId = getAttrDefnId(definition);
 166  
                                                                 // if it is DD, then attrDefn.getLabel() is null; has to get from DDAttrdefn
 167  0
                                                                 typeField.setFieldLabel(definition.getLabel());
 168  
                                                                 // with suffix  in case name is the same as bo property 
 169  0
                                                                 typeField.setPropertyName(definition.getName()+"."+attrDefnId);
 170  0
                                                                 if (definition.getControl().isSelect()) {
 171  
                                                                 try {
 172  0
                                                                     KeyValuesFinder finder = (KeyValuesFinder) ClassLoaderUtils.getClass(definition.getControl().getValuesFinderClass()).newInstance();
 173  0
                                                                         typeField.setFieldValidValues(finder.getKeyValues());
 174  0
                                                                         typeField.setFieldType(Field.DROPDOWN);
 175  
                                                                 }
 176  0
                                                                 catch (InstantiationException e) {
 177  0
                                                                     throw new RuntimeException(e.getMessage());
 178  
                                                                 }
 179  0
                                                                 catch (IllegalAccessException e) {
 180  0
                                                                     throw new RuntimeException(e.getMessage());
 181  0
                                                                 }
 182  
                                                                 } else {
 183  0
                                                                         typeField.setMaxLength(definition.getMaxLength());
 184  0
                                                                         typeField.setSize(definition.getControl().getSize());
 185  0
                                                                         typeField.setFieldType(Field.TEXT);
 186  
                                                                 }
 187  0
                                                                 fields.add(typeField);
 188  0
                                                                 returnRows.add(new Row(fields));
 189  0
                                             }
 190  
                                         }
 191  
                             }
 192  0
                                 } else {
 193  0
                                         return getAttrRows();
 194  
                                 }
 195  0
                         } else if (field.getPropertyName().equals("kimTypeId") && StringUtils.isBlank(field.getPropertyValue())) {
 196  0
                                 setTypeId(""); 
 197  
                         }
 198  0
                 }
 199  0
                 return returnRows;
 200  
 
 201  
         }
 202  
     private String getAttrDefnId(AttributeDefinition definition) {
 203  0
             if (definition instanceof KimDataDictionaryAttributeDefinition) {
 204  0
                     return ((KimDataDictionaryAttributeDefinition)definition).getKimAttrDefnId();
 205  
             } else {
 206  0
                     return ((KimNonDataDictionaryAttributeDefinition)definition).getKimAttrDefnId();
 207  
 
 208  
             }
 209  
     }
 210  
         
 211  
         public List<Row> getRoleRows() {
 212  0
                 return this.roleRows;
 213  
         }
 214  
 
 215  
         public void setRoleRows(List<Row> roleRows) {
 216  0
                 this.roleRows = roleRows;
 217  0
         }
 218  
 
 219  
         public KimRoleDao getRoleDao() {
 220  0
                 return this.roleDao;
 221  
         }
 222  
 
 223  
         public void setRoleDao(KimRoleDao roleDao) {
 224  0
                 this.roleDao = roleDao;
 225  0
         }
 226  
 
 227  
         public AttributeDefinitionMap getAttrDefinitions() {
 228  0
                 return this.attrDefinitions;
 229  
         }
 230  
 
 231  
         public void setAttrDefinitions(AttributeDefinitionMap attrDefinitions) {
 232  0
                 this.attrDefinitions = attrDefinitions;
 233  0
         }
 234  
 
 235  
         public List<Row> getAttrRows() {
 236  0
                 return this.attrRows;
 237  
         }
 238  
 
 239  
         public void setAttrRows(List<Row> attrRows) {
 240  0
                 this.attrRows = attrRows;
 241  0
         }
 242  
 
 243  
         public String getTypeId() {
 244  0
                 return this.typeId;
 245  
         }
 246  
 
 247  
         public void setTypeId(String typeId) {
 248  0
                 this.typeId = typeId;
 249  0
         }
 250  
 
 251  
         @Override
 252  
         public List<Row> getRows() {
 253  0
                 List<Row> attributeRows = new ArrayList<Row>();
 254  0
                 if (getRoleRows().isEmpty()) {
 255  0
                         List<Row> rows = super.getRows();
 256  0
                         List<Row> returnRows = new ArrayList<Row>();
 257  0
                         for (Row row : rows) {
 258  0
                                 for (int i = row.getFields().size() - 1; i >= 0; i--) {
 259  0
                                         Field field = (Field) row.getFields().get(i);
 260  0
                                         if (field.getPropertyName().equals("kimTypeId")) {
 261  0
                                                 Field typeField = new Field();
 262  0
                                                 typeField.setFieldLabel("Type");
 263  0
                                                 typeField.setPropertyName("kimTypeId");
 264  0
                                                 typeField.setFieldValidValues(getRoleTypeOptions());
 265  0
                                                 typeField.setFieldType(Field.DROPDOWN);
 266  0
                                                 typeField.setMaxLength(100);
 267  0
                                                 typeField.setSize(40);
 268  
                                                 // row.getFields().set(i, new Field("Type", "", Field.DROPDOWN_REFRESH,
 269  
                                                 // false, "kimTypeId", "", getGroupTypeOptions(), null));
 270  0
                                                 row.getFields().set(i, typeField);
 271  
                                         }
 272  
                                 }
 273  0
                                 returnRows.add(row);
 274  
                         }
 275  0
                         setRoleRows(returnRows);
 276  
                         //setAttrRows(setupAttributeRows());
 277  
                 }
 278  0
                 if (getAttrRows().isEmpty()) {
 279  
                         //setAttrDefinitions(new AttributeDefinitionMap());
 280  0
                         return getRoleRows();
 281  
                 } else {
 282  0
                         List<Row> fullRows = new ArrayList<Row>();
 283  0
                         fullRows.addAll(getRoleRows());
 284  
                         //fullRows.addAll(getAttrRows());
 285  0
                         return fullRows;
 286  
                 }
 287  
                 
 288  
         }
 289  
 
 290  
         @Override
 291  
         protected List<? extends BusinessObject> getSearchResultsHelper(
 292  
                         Map<String, String> fieldValues, boolean unbounded) {
 293  
         List searchResults;
 294  0
             Map<String,String> nonBlankFieldValues = new HashMap<String, String>();
 295  0
             boolean includeAttr = false;
 296  0
             for (String fieldName : fieldValues.keySet()) {
 297  0
                     if (StringUtils.isNotBlank(fieldValues.get(fieldName)) ) {
 298  0
                             nonBlankFieldValues.put(fieldName, fieldValues.get(fieldName));
 299  0
                             if (fieldName.contains(".")) {
 300  0
                                     includeAttr = true;
 301  
                             }
 302  
                     }
 303  
             }
 304  
 
 305  0
             if (includeAttr) {
 306  0
                 ModuleService eboModuleService = KNSServiceLocator.getKualiModuleService().getResponsibleModuleService( getBusinessObjectClass() );
 307  0
                 BusinessObjectEntry ddEntry = eboModuleService.getExternalizableBusinessObjectDictionaryEntry(getBusinessObjectClass());
 308  0
                 Map<String,String> filteredFieldValues = new HashMap<String, String>();
 309  0
                 for (String fieldName : nonBlankFieldValues.keySet()) {
 310  0
                         if (ddEntry.getAttributeNames().contains(fieldName) || fieldName.contains(".")) {
 311  0
                                 filteredFieldValues.put(fieldName, nonBlankFieldValues.get(fieldName));
 312  
                         }
 313  
                 }
 314  0
                 searchResults = eboModuleService.getExternalizableBusinessObjectsListForLookup(getBusinessObjectClass(), (Map)filteredFieldValues, unbounded);
 315  
 
 316  0
             } else {
 317  0
                     searchResults = super.getSearchResultsHelper(fieldValues, unbounded);
 318  
             }
 319  0
         List defaultSortColumns = getDefaultSortColumns();
 320  0
         if (defaultSortColumns.size() > 0) {
 321  0
             Collections.sort(searchResults, new BeanPropertyComparator(defaultSortColumns, true));
 322  
         }
 323  0
         return searchResults;
 324  
 
 325  
         }
 326  
         
 327  
         private static final String ROLE_ID_URL_KEY = "&"+KimConstants.PrimaryKeyConstants.ROLE_ID+"=";
 328  
         /**
 329  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject, java.lang.String)
 330  
          */
 331  
         @Override
 332  
         public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
 333  0
                 AnchorHtmlData inquiryHtmlData = (AnchorHtmlData)super.getInquiryUrl(bo, propertyName);
 334  0
                 if(inquiryHtmlData!=null && StringUtils.isNotBlank(inquiryHtmlData.getHref()) && inquiryHtmlData.getHref().contains(ROLE_ID_URL_KEY))
 335  0
                         inquiryHtmlData.setHref(getCustomRoleInquiryHref(getBackLocation(), inquiryHtmlData.getHref()));
 336  0
                 return inquiryHtmlData;
 337  
         }
 338  
 
 339  
         public static String getCustomRoleInquiryHref(String href){
 340  0
                 return getCustomRoleInquiryHref("", href);
 341  
         }
 342  
         
 343  
         static String getCustomRoleInquiryHref(String backLocation, String href){
 344  0
         Properties parameters = new Properties();
 345  0
         String hrefPart = "";
 346  0
             String docTypeAction = "";
 347  0
             if(StringUtils.isBlank(backLocation) || backLocation.contains(KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_ACTION)
 348  
                             || !backLocation.contains(KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION)){
 349  0
                     docTypeAction = KimConstants.KimUIConstants.KIM_ROLE_INQUIRY_ACTION;
 350  
             } else{
 351  0
                     docTypeAction = KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION;
 352  
             }
 353  0
                 if (StringUtils.isNotBlank(href) && href.contains(ROLE_ID_URL_KEY)) {
 354  0
                         int idx1 = href.indexOf("&"+KimConstants.PrimaryKeyConstants.ROLE_ID+"=");
 355  0
                     int idx2 = href.indexOf("&", idx1+1);
 356  0
                     if (idx2 < 0) {
 357  0
                             idx2 = href.length();
 358  
                     }
 359  0
                 parameters.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.PARAM_MAINTENANCE_VIEW_MODE_INQUIRY);
 360  0
                 hrefPart = href.substring(idx1, idx2);
 361  
             }
 362  0
                 return UrlFactory.parameterizeUrl(KimCommonUtils.getKimBasePath()+docTypeAction, parameters)+hrefPart;
 363  
         }
 364  
 
 365  
 }