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