View Javadoc
1   /**
2    * Copyright 2005-2016 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.kim.api.KimConstants;
20  import org.kuali.rice.kim.api.role.Role;
21  import org.kuali.rice.kim.api.role.RoleService;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.kim.api.type.KimAttributeField;
24  import org.kuali.rice.kim.api.type.KimType;
25  import org.kuali.rice.kim.api.type.KimTypeInfoService;
26  import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
27  import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
28  import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
29  import org.kuali.rice.kim.impl.KIMPropertyConstants;
30  import org.kuali.rice.kim.impl.type.KimTypeAttributesHelper;
31  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
32  import org.kuali.rice.kim.service.UiDocumentService;
33  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
34  import org.kuali.rice.krad.bo.BusinessObject;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  import java.util.Map;
40  
41  /**
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class KimDocumentRoleMemberLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
45  
46      private static final long serialVersionUID = 1L;
47      private transient UiDocumentService uiDocumentService;
48      private transient RoleService roleService;
49      private transient KimTypeInfoService kimTypeInfoService;
50  
51      @SuppressWarnings("unchecked")
52      protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
53          List<KimDocumentRoleMember> searchResults = new ArrayList<KimDocumentRoleMember>();
54          IdentityManagementRoleDocument roleDocument = (IdentityManagementRoleDocument) GlobalVariables.getUserSession().retrieveObject(KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_SHORT_KEY);
55          if (roleDocument != null) {
56              String memberId = fieldValues.get(KimConstants.PrimaryKeyConstants.MEMBER_ID);
57              String memberTypeCode = fieldValues.get(KIMPropertyConstants.KimMember.MEMBER_TYPE_CODE);
58              String memberName = fieldValues.get(KimConstants.KimUIConstants.MEMBER_NAME);
59              String memberNamespaceCode = fieldValues.get(KimConstants.KimUIConstants.MEMBER_NAMESPACE_CODE);
60              String activeFromDate = fieldValues.get(KIMPropertyConstants.KimMember.ACTIVE_FROM_DATE);
61              String activeToDate = fieldValues.get(KIMPropertyConstants.KimMember.ACTIVE_TO_DATE);
62              List<KimDocumentRoleMember> currentRoleMembers = roleDocument.getMembers();
63              if (currentRoleMembers != null && !currentRoleMembers.isEmpty()) {
64                  for (KimDocumentRoleMember currentRoleMember : currentRoleMembers) {
65                      if ((StringUtils.isEmpty(memberId) || (StringUtils.isNotEmpty(memberId) && memberId.equals(currentRoleMember.getMemberId())))
66                              && (StringUtils.isEmpty(memberTypeCode) || (StringUtils.isNotEmpty(memberTypeCode) && memberTypeCode.equals(currentRoleMember.getMemberTypeCode())))
67                              && (StringUtils.isEmpty(memberName) || (StringUtils.isNotEmpty(memberName) && memberName.equals(currentRoleMember.getMemberName())))
68                              && (StringUtils.isEmpty(memberNamespaceCode) || (StringUtils.isNotEmpty(memberNamespaceCode) && memberNamespaceCode.equals(currentRoleMember.getMemberNamespaceCode())))
69                              && (StringUtils.isEmpty(activeFromDate) || (StringUtils.isNotEmpty(activeFromDate) && activeFromDate.equals(currentRoleMember.getActiveFromDate())))
70                              && (StringUtils.isEmpty(activeToDate) || (StringUtils.isNotEmpty(activeToDate) && activeToDate.equals(currentRoleMember.getActiveToDate())))) {
71                          searchResults.add(currentRoleMember);
72                      }
73                  }
74              }
75          } else {
76              searchResults = getUiDocumentService().getRoleMembers(fieldValues);
77          }
78          if (searchResults != null) {
79              for (KimDocumentRoleMember roleMember : searchResults) {
80                  roleMember.setQualifiersToDisplay(getQualifiersToDisplay(roleMember));
81              }
82          }
83          return searchResults;
84      }
85  
86      public String getQualifiersToDisplay(KimDocumentRoleMember roleMember) {
87          if (roleMember != null && StringUtils.isNotEmpty(roleMember.getRoleId()) &&
88                  roleMember.getQualifiers() != null && !roleMember.getQualifiers().isEmpty()) {
89              Role role = getRoleService().getRole(roleMember.getRoleId());
90              KimType kimType = null;
91              if (role != null) {
92                  kimType = getKimTypeInfoService().getKimType(role.getKimTypeId());
93              }
94              if (kimType != null) {
95                  KimTypeAttributesHelper attributesHelper = new KimTypeAttributesHelper(kimType);
96                  StringBuffer attributesToDisplay = new StringBuffer();
97                  KimAttributeField attribDefn;
98                  for (KimDocumentRoleQualifier attribute : roleMember.getQualifiers()) {
99                      if (attribute.getKimAttribute() != null) {
100                         attribDefn = attributesHelper.getAttributeDefinition(attribute.getKimAttribute().getAttributeName());
101                         attributesToDisplay.append(attribDefn != null ? attribDefn.getAttributeField().getLongLabel() : "");
102                         attributesToDisplay.append(KimConstants.KimUIConstants.NAME_VALUE_SEPARATOR);
103                         attributesToDisplay.append(attribute.getAttrVal());
104                         attributesToDisplay.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
105                     }
106                 }
107                 return stripEnd(attributesToDisplay.toString(), KimConstants.KimUIConstants.COMMA_SEPARATOR);
108             }
109         }
110         return "";
111     }
112 
113     private static String stripEnd(String toStripFrom, String toStrip){
114 		String stripped;
115 		if(toStripFrom==null) return null;
116 		if(toStrip==null) return toStripFrom;
117         if(toStrip.length() > toStripFrom.length()) return toStripFrom;
118 		if(toStripFrom.endsWith(toStrip)){
119 			StringBuffer buffer = new StringBuffer(toStripFrom);
120 			buffer.delete(buffer.length()-toStrip.length(), buffer.length());
121 			stripped = buffer.toString();
122 		} else stripped = toStripFrom;
123 		return stripped;
124 	}
125 
126     public RoleService getRoleService() {
127         if (roleService == null) {
128             roleService = KimApiServiceLocator.getRoleService();
129         }
130         return roleService;
131     }
132 
133     public KimTypeInfoService getKimTypeInfoService() {
134         if (kimTypeInfoService == null) {
135             kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
136         }
137         return kimTypeInfoService;
138     }
139 
140     public UiDocumentService getUiDocumentService() {
141         if (uiDocumentService == null) {
142             uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
143         }
144         return uiDocumentService;
145     }
146 }