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