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.krad.uif.util;
17  
18  import com.google.common.collect.Maps;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22  import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
23  import org.kuali.rice.krad.uif.UifConstants;
24  import org.kuali.rice.krad.util.KRADUtils;
25  
26  import java.io.Serializable;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  /**
31   * This object represents a url in the Krad framework.  The url can be set explicitly to a specific href or a
32   * controller
33   * plus a viewId can be provided (at very minimum).  By default, the krad base bean config points the baseUrl property
34   * to 'krad.url' configuration property and the methodToCall to 'start', but these can be reset to any value as needed.
35   *
36   * <p>
37   * If href is not set, the generated value of href is constructed (in general) as follows:<br/>
38   * baseUrl + /controllerMapping + ? + methodToCall param + viewId param + other parameters
39   * <br/>
40   * with any necessary tokens to construct a valid url.  If baseUrl is not provided, the url is not valid and a
41   * blank string is returned.
42   * </p>
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  @BeanTag(name = "url-bean", parent = "Uif-Url")
47  public class UrlInfo extends UifDictionaryBeanBase implements Serializable {
48  
49      private static final long serialVersionUID = 3195177614468120958L;
50  
51      private String href;
52      private String originalHref;
53      private String baseUrl;
54      private String controllerMapping;
55      private String viewType;
56      private String viewId;
57      private String pageId;
58      private String formKey;
59      private String methodToCall;
60      private Map<String, String> requestParameters;
61  
62      /**
63       * Base constructor
64       */
65      public UrlInfo() {}
66  
67      /**
68       * Constructor that initializes an href value
69       *
70       * @param href the href value
71       */
72      public UrlInfo(String href) {
73          this.href = href;
74          this.originalHref = href;
75      }
76  
77      /**
78       * Constructor that sets the base url construction properties
79       *
80       * @param baseUrl the baseUrl
81       * @param controllerMapping the controllerMapping
82       * @param viewId the id of the view
83       * @param methodToCall the methodToCall
84       */
85      public UrlInfo(String baseUrl, String controllerMapping, String viewId, String methodToCall) {
86          this.baseUrl = baseUrl;
87          this.controllerMapping = controllerMapping;
88          this.viewId = viewId;
89          this.methodToCall = methodToCall;
90      }
91  
92      /**
93       * Generate the url based on properties of this object
94       *
95       * @return the generatedUrl, blank if not a valid url (no baseUrl value provided)
96       */
97      protected String generateUrl() {
98          String generatedUrl = "";
99  
100         if (StringUtils.isBlank(baseUrl)) {
101             return generatedUrl;
102         }
103 
104         generatedUrl = baseUrl;
105 
106         if (StringUtils.isNotBlank(controllerMapping) && !controllerMapping.startsWith("/")) {
107             generatedUrl = generatedUrl + "/" + controllerMapping;
108         } else if (StringUtils.isNotBlank(controllerMapping)) {
109             generatedUrl = generatedUrl + controllerMapping;
110         }
111 
112         Map<String, String> allRequestParameters = new HashMap<String, String>();
113 
114         if (StringUtils.isNotBlank(methodToCall)) {
115             allRequestParameters.put(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME, methodToCall);
116         }
117 
118         if (StringUtils.isNotBlank(viewId)) {
119             allRequestParameters.put(UifConstants.UrlParams.VIEW_ID, viewId);
120         }
121 
122         if (StringUtils.isNotBlank(pageId)) {
123             allRequestParameters.put(UifConstants.UrlParams.PAGE_ID, pageId);
124         }
125 
126         if (StringUtils.isNotBlank(formKey)) {
127             allRequestParameters.put(UifConstants.UrlParams.FORM_KEY, formKey);
128         }
129 
130         if (requestParameters != null) {
131             allRequestParameters.putAll(requestParameters);
132         }
133 
134         //add the request parameters
135         generatedUrl = generatedUrl + KRADUtils.getRequestStringFromMap(allRequestParameters);
136 
137         return generatedUrl;
138     }
139 
140     /**
141      * Get the href value for this url object.  This is the main call to this url object as it provides the full href
142      * value represented by this object.
143      *
144      * <p>
145      * If href has NOT been explicitly set to a value, the href is generated by
146      * constructing pieces of the url set through the properties of this url object.
147      * The generated value of href is constructed (in general) as follows:<br/>
148      * baseUrl + /controllerMapping + ? + methodToCall param + viewId param + other parameters
149      * <br/>
150      * with any necessary tokens to construct a valid url.  If baseUrl is not provided, the url is not valid and a
151      * blank string is returned.
152      * </p>
153      *
154      * @return THE href represented by this url object, or blank if not valid
155      */
156     @BeanTagAttribute(name = "href")
157     public String getHref() {
158         if (StringUtils.isBlank(this.href)) {
159             this.href = generateUrl();
160         }
161 
162         return href;
163     }
164 
165     /**
166      * Explicitly set the href value - if this is called with a value, all other properties of the url object are
167      * ignored.  This call is basically a full override.  This also sets the orginalHref value.
168      *
169      * @param href
170      */
171     public void setHref(String href) {
172         this.href = href;
173         this.originalHref = href;
174     }
175 
176     /**
177      * The base url value (the value that comes before other properties).  Default base bean value is set to use
178      * 'krad.url' of the configuration properties.
179      *
180      * @return the baseUrl
181      */
182     @BeanTagAttribute(name = "baseUrl")
183     public String getBaseUrl() {
184         return baseUrl;
185     }
186 
187     /**
188      * Set the baseUrl
189      *
190      * @param baseUrl
191      */
192     public void setBaseUrl(String baseUrl) {
193         this.baseUrl = baseUrl;
194     }
195 
196     /**
197      * The controllerMapping for the url (string that represents the controllerMapping path appended to baseUrl)
198      *
199      * @return the controllerMapping string
200      */
201     @BeanTagAttribute(name = "controllerMapping")
202     public String getControllerMapping() {
203         return controllerMapping;
204     }
205 
206     /**
207      * Set the controllerMapping
208      *
209      * @param controllerMapping
210      */
211     public void setControllerMapping(String controllerMapping) {
212         this.controllerMapping = controllerMapping;
213     }
214 
215     /**
216      * The viewType representing the View's base type
217      *
218      * @return the viewType
219      */
220     @BeanTagAttribute(name = "viewType")
221     public String getViewType() {
222         return viewType;
223     }
224 
225     /**
226      * Set the viewType
227      *
228      * @param viewType
229      */
230     public void setViewType(String viewType) {
231         this.viewType = viewType;
232     }
233 
234     /**
235      * ViewId representing the view by id to retrieve
236      *
237      * @return the viewId
238      */
239     @BeanTagAttribute(name = "viewId")
240     public String getViewId() {
241         return viewId;
242     }
243 
244     /**
245      * Set viewId
246      *
247      * @param viewId
248      */
249     public void setViewId(String viewId) {
250         this.viewId = viewId;
251     }
252 
253     /**
254      * PageId representing the page of the view to retrieve by id
255      *
256      * @return the pageId
257      */
258     @BeanTagAttribute(name = "pageId")
259     public String getPageId() {
260         return pageId;
261     }
262 
263     /**
264      * Set pageId
265      *
266      * @param pageId
267      */
268     public void setPageId(String pageId) {
269         this.pageId = pageId;
270     }
271 
272     /**
273      * FormKey representing the key of the form data to retrieve
274      *
275      * @return the formKey
276      */
277     @BeanTagAttribute(name = "formKey")
278     public String getFormKey() {
279         return formKey;
280     }
281 
282     /**
283      * Set the formKey
284      *
285      * @param formKey
286      */
287     public void setFormKey(String formKey) {
288         this.formKey = formKey;
289     }
290 
291     /**
292      * MethodToCall representing the methodToCall on the controller (default base bean value is 'start')
293      *
294      * @return methodToCall on controller
295      */
296     @BeanTagAttribute(name = "methodToCall")
297     public String getMethodToCall() {
298         return methodToCall;
299     }
300 
301     /**
302      * Set the methodToCall
303      *
304      * @param methodToCall
305      */
306     public void setMethodToCall(String methodToCall) {
307         this.methodToCall = methodToCall;
308     }
309 
310     /**
311      * Map of key value pairs that will be appended to the request parameters to pass in any custom data
312      *
313      * @return the requestParameters map
314      */
315     @BeanTagAttribute(name = "requestParameters", type = BeanTagAttribute.AttributeType.MAPVALUE)
316     public Map<String, String> getRequestParameters() {
317         return requestParameters;
318     }
319 
320     /**
321      * Set the requestParameters
322      *
323      * @param requestParameters
324      */
325     public void setRequestParameters(Map<String, String> requestParameters) {
326         this.requestParameters = requestParameters;
327     }
328 
329     /**
330      * The original(set) href value.  This is generally used to determine if the href was explicitly set and not
331      * generated by this url object.
332      *
333      * @return the original(set) href value
334      */
335     public String getOriginalHref() {
336         return originalHref;
337     }
338 
339     /**
340      * toString override returns the href value of url
341      *
342      * @return href value
343      */
344     @Override
345     public String toString() {
346         return this.getHref();
347     }
348 
349     /**
350      * toString returns the original href value of url
351      *
352      * @return original href value
353      */
354     public void setOriginalHref(String originalHref) {
355         this.originalHref = originalHref;
356     }
357 
358     @Override
359     protected <T> void copyProperties(T dictionaryBaseBean) {
360         super.copyProperties(dictionaryBaseBean);
361 
362         UrlInfo urlInfoCopy = (UrlInfo) dictionaryBaseBean;
363 
364         urlInfoCopy.setHref(this.href);
365         urlInfoCopy.setOriginalHref(this.originalHref);
366         urlInfoCopy.setBaseUrl(this.baseUrl);
367         urlInfoCopy.setControllerMapping(this.controllerMapping);
368         urlInfoCopy.setViewType(this.viewType);
369         urlInfoCopy.setViewId(this.viewId);
370         urlInfoCopy.setPageId(this.pageId);
371         urlInfoCopy.setFormKey(this.formKey);
372         urlInfoCopy.setMethodToCall(this.methodToCall);
373 
374         if (this.requestParameters != null) {
375             Map<String, String> requestParametersCopy = Maps.newHashMapWithExpectedSize(this.requestParameters.size());
376             for (Map.Entry requestParameter : requestParameters.entrySet()) {
377                 requestParametersCopy.put(requestParameter.getKey().toString(), requestParameter.getValue().toString());
378             }
379 
380             urlInfoCopy.setRequestParameters(requestParametersCopy);
381         }
382     }
383 }