View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.uif.field;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.kim.api.role.Role;
21  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25  import org.kuali.rice.krad.service.ModuleService;
26  import org.kuali.rice.krad.uif.UifConstants;
27  import org.kuali.rice.krad.uif.UifParameters;
28  import org.kuali.rice.krad.uif.util.LifecycleElement;
29  import org.kuali.rice.krad.util.UrlFactory;
30  
31  import java.util.Map;
32  import java.util.Properties;
33  
34  /**
35   * Field that encloses a link to a role element.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  @BeanTag(name = "roleLinkField", parent = "Uif-RoleLinkField")
40  public class RoleLinkField extends LinkField {
41      private static final long serialVersionUID = -7175947391547723712L;
42      private static final Logger LOG = Logger.getLogger(RoleLinkField.class);
43  
44      private String roleId;
45      private String roleName;
46      private String roleNamespaceCode;
47      private String dataObjectClassName;
48      private String baseInquiryUrl;
49      private boolean namespaceInLinkText;
50      private boolean disableLink;
51      private Map<String, String> additionalInquiryParameters;
52  
53      /**
54       * PerformFinalize override - calls super, corrects the field's Label for attribute to point to this field's
55       * content.
56       *
57       * @param model the model
58       * @param parent the parent component
59       */
60      @Override
61      public void performFinalize(Object model, LifecycleElement parent) {
62          super.performFinalize(model, parent);
63  
64          if (StringUtils.isNotBlank(roleId)) {
65              setHref(buildInquiryUrl());
66              setLinkText(getRoleNameByRoleId(roleId));
67          } else {
68              if (StringUtils.isNotBlank(roleName) && StringUtils.isNotBlank(roleNamespaceCode) ) {
69                  setHref(buildInquiryUrl());
70                  setLinkText(getRoleNameByNamespaceAndName(roleNamespaceCode, roleName));
71              }
72  
73              if (StringUtils.isBlank(roleName) && StringUtils.isNotBlank(roleNamespaceCode) ) {
74                  setDisableLink(true);
75  
76                  if (isNamespaceInLinkText()){
77                      setLinkText(roleNamespaceCode);
78                  }
79              }
80  
81              if (StringUtils.isNotBlank(roleName) && StringUtils.isBlank(roleNamespaceCode) ) {
82                  setDisableLink(true);
83                  setLinkText(roleName);
84              }
85          }
86  
87          if (StringUtils.isBlank(getHref())) {
88              setDisableLink(true);
89          }
90  
91          if (StringUtils.isBlank(getLinkText())){
92              setLinkText(UifConstants.KimLink.BLANK_LINK);
93          }
94      }
95  
96      protected String buildInquiryUrl() {
97          Class<?> inquiryObjectClass;
98          try {
99              inquiryObjectClass = Class.forName(getDataObjectClassName());
100         } catch (ClassNotFoundException e) {
101             LOG.error("Unable to get class for: " + getDataObjectClassName());
102             throw new RuntimeException(e);
103         }
104 
105         Properties urlParameters = new Properties();
106         urlParameters.setProperty(UifParameters.DATA_OBJECT_CLASS_NAME, inquiryObjectClass.getName());
107         urlParameters.setProperty(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
108 
109         if (StringUtils.isNotBlank(roleId)) {
110             urlParameters.setProperty(UifConstants.KimLink.ROLE_ID, roleId);
111         } else {
112             if (StringUtils.isNotBlank(roleName)) {
113                 urlParameters.setProperty(UifConstants.KimLink.ROLE_NAME, roleName);
114                 urlParameters.setProperty(UifConstants.KimLink.ROLE_NAMESPACE_CODE, roleNamespaceCode);
115             }
116         }
117 
118         for (Map.Entry<String, String> inquiryParameter : additionalInquiryParameters.entrySet()) {
119             // add additional inquiry parameter to URL
120             urlParameters.setProperty(inquiryParameter.getKey(), inquiryParameter.getValue());
121         }
122 
123         // build inquiry URL
124         String inquiryUrl;
125 
126         // check for EBOs for an alternate inquiry URL
127         ModuleService responsibleModuleService =
128                 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
129         if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
130             inquiryUrl = responsibleModuleService.getExternalizableDataObjectInquiryUrl(inquiryObjectClass,
131                     urlParameters);
132         } else {
133             inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(), urlParameters);
134         }
135 
136         return inquiryUrl;
137     }
138 
139     protected String getRoleNameByRoleId(String roleId) {
140         Role role = KimApiServiceLocator.getRoleService().getRole(roleId);
141         if (role == null) {
142             // disable link and display roleId
143             setDisableLink(true);
144             return roleId;
145         }
146 
147         if (isNamespaceInLinkText()){
148             return role.getNamespaceCode() + " " + role.getName();
149         }
150 
151         return role.getName();
152     }
153 
154     protected String getRoleNameByNamespaceAndName(String namespaceCode, String roleName) {
155         Role role = KimApiServiceLocator.getRoleService().getRoleByNamespaceCodeAndName(namespaceCode, roleName);
156         if (role == null) {
157             // disable link
158             setDisableLink(true);
159 
160             if (isNamespaceInLinkText()){
161                 return this.roleNamespaceCode + " " + this.roleName;
162             } else {
163                 return this.roleName;
164             }
165         }
166 
167         if (isNamespaceInLinkText()){
168             return role.getNamespaceCode() + " " + role.getName();
169         }
170 
171         return role.getName();
172     }
173 
174     /**
175      * RoleName key used as a parameter of generated inquiry link url.
176      *
177      * @return value of RoleName key parameter
178      */
179     @BeanTagAttribute
180     public String getRoleName() {
181         return roleName;
182     }
183 
184     public void setRoleName(String roleName) {
185         this.roleName = roleName;
186     }
187 
188     /**
189      * RoleNamespaceCode key used as a parameter of generated inquiry link url.
190      *
191      * @return value of RoleNamespaceCode key parameter
192      */
193     @BeanTagAttribute
194     public String getRoleNamespaceCode() {
195         return roleNamespaceCode;
196     }
197 
198     public void setRoleNamespaceCode(String roleNamespaceCode) {
199         this.roleNamespaceCode = roleNamespaceCode;
200     }
201 
202     /**
203      * RoleId key used as a parameter of generated inquiry link url.
204      *
205      * @return value of RoleId key parameter
206      */
207     @BeanTagAttribute
208     public String getRoleId() {
209         return roleId;
210     }
211 
212     public void setRoleId(String roleId) {
213         this.roleId = roleId;
214     }
215 
216     /**
217      * DataObjectClassName used as a parameter of generated inquiry link url.
218      *
219      * @return value of DataObjectClassName parameter
220      */
221     @BeanTagAttribute
222     public String getDataObjectClassName() {
223         return dataObjectClassName;
224     }
225 
226     public void setDataObjectClassName(String dataObjectClassName) {
227         this.dataObjectClassName = dataObjectClassName;
228     }
229 
230     /**
231      * BaseInquiryUrl is the foundation of generated inquiry link url.
232      *
233      * @return value of BaseInquiryUrl part of inquiry link url
234      */
235     @BeanTagAttribute
236     public String getBaseInquiryUrl() {
237         return baseInquiryUrl;
238     }
239 
240     public void setBaseInquiryUrl(String baseInquiryUrl) {
241         this.baseInquiryUrl = baseInquiryUrl;
242     }
243 
244     /**
245      * True if namespaceCode is to be included in this links linkText, false otherwise.
246      *
247      * @return true if namespaceCode is to be included in linkText
248      */
249     @BeanTagAttribute
250     public boolean isNamespaceInLinkText() {
251         return namespaceInLinkText;
252     }
253 
254     public void setNamespaceInLinkText(boolean namespaceInLinkText) {
255         this.namespaceInLinkText = namespaceInLinkText;
256     }
257 
258     /**
259      * True if this fields link is disabled, false otherwise.
260      *
261      * @return true if link disabled
262      */
263     @BeanTagAttribute
264     public boolean isDisableLink() {
265         return disableLink;
266     }
267 
268     public void setDisableLink(boolean disableLink) {
269         this.disableLink = disableLink;
270     }
271 
272     /**
273      * Map that determines what additional properties from a calling view will be sent to properties on the inquiry data object.
274      *
275      * <p>
276      * When invoking an inquiry view, a query is done against the inquiries configured data object and the resulting
277      * record is display. The values for the properties configured within the inquiry parameters Map, in addition to the
278      * inquiry key parameters, will be passed along as values for the inquiry data object properties (thus they form the
279      * criteria for the inquiry)
280      * </p>
281      *
282      * @return mapping of calling additional view properties to inquiry data object properties
283      */
284     @BeanTagAttribute
285     public Map<String, String> getAdditionalInquiryParameters() {
286         return additionalInquiryParameters;
287     }
288 
289     public void setAdditionalInquiryParameters(Map<String, String> additionalInquiryParameters) {
290         this.additionalInquiryParameters = additionalInquiryParameters;
291     }
292 }