View Javadoc

1   /**
2    * Copyright 2005-2012 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.widget;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.uif.UifConstants;
20  import org.kuali.rice.krad.uif.UifParameters;
21  import org.kuali.rice.krad.uif.field.InputField;
22  import org.kuali.rice.krad.uif.view.View;
23  import org.kuali.rice.krad.uif.component.BindingInfo;
24  import org.kuali.rice.krad.uif.component.Component;
25  import org.kuali.rice.krad.uif.field.ActionField;
26  import org.kuali.rice.krad.util.UrlFactory;
27  
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  import java.util.Properties;
32  
33  /**
34   * Widget for rendering an Direct Inquiry link icon next to a input field
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class DirectInquiry extends Inquiry {
39      private static final long serialVersionUID = -2490979579285984314L;
40      
41      private ActionField directInquiryActionField;
42  
43      private boolean adjustInquiryParameters;
44      private BindingInfo fieldBindingInfo;
45  
46      public DirectInquiry() {
47          super();
48  
49          adjustInquiryParameters = false;
50      }
51  
52      /**
53       * @see org.kuali.rice.krad.uif.widget.WidgetBase#performFinalize(org.kuali.rice.krad.uif.view.View,
54       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
55       */
56      @Override
57      public void performFinalize(View view, Object model, Component parent) {
58          super.performFinalize(view, model, parent);
59  
60          // only set inquiry if enabled
61          if (!isRender() || isReadOnly()) {
62              return;
63          }
64  
65          // set render to false until we find an inquiry class
66          setRender(false);
67  
68          InputField field = (InputField) parent;
69  
70          // determine whether inquiry parameters will need adjusted
71          if (StringUtils.isBlank(getDataObjectClassName())
72                  || (getInquiryParameters() == null)
73                  || getInquiryParameters().isEmpty()) {
74              // if inquiry parameters not given, they will not be adjusted by super
75              adjustInquiryParameters = true;
76              fieldBindingInfo = field.getBindingInfo();
77          }
78  
79      	setupLink(view, model, field);
80      }
81  
82      /**
83       * Builds the inquiry link and onclick script based on the given inquiry class and parameters
84       * 
85       * @param dataObject
86       *            - parent object that contains the data (used to pull inquiry
87       *            parameters)
88       * @param propertyName
89       *            - name of the property the inquiry is set on
90       * @param inquiryObjectClass
91       *            - class of the object the inquiry should point to
92       * @param inquiryParms
93       *            - map of key field mappings for the inquiry
94       */
95  	public void buildInquiryLink(Object dataObject, String propertyName,
96  			Class<?> inquiryObjectClass, Map<String, String> inquiryParms) {
97  		Properties urlParameters = new Properties();
98  
99  		urlParameters.put(UifParameters.DATA_OBJECT_CLASS_NAME,
100 				inquiryObjectClass.getName());
101 		urlParameters.put(UifParameters.METHOD_TO_CALL,
102 				UifConstants.MethodToCallNames.START);
103 
104 		// Direct inquiry
105 		String inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(),
106 				urlParameters);
107 		StringBuilder paramMapString = new StringBuilder();
108 
109 		// Check if lightbox is set. Get lightbox options.
110 		String lightBoxOptions = "";
111 		boolean lightBoxShow = directInquiryActionField.getLightBoxDirectInquiry() != null;
112 		if (lightBoxShow) {
113 			lightBoxOptions = directInquiryActionField.getLightBoxDirectInquiry()
114 					.getComponentOptionsJSString();
115 		}
116 
117 		// Build parameter string using the actual names of the fields as on the
118 		// html page
119         for (Entry<String, String> inquiryParameter : inquiryParms.entrySet()) {
120             String inquiryParameterFrom = inquiryParameter.getKey();
121             if (adjustInquiryParameters && (fieldBindingInfo != null)) {
122                 inquiryParameterFrom = fieldBindingInfo.getPropertyAdjustedBindingPath(inquiryParameterFrom);
123             }
124             paramMapString.append(inquiryParameterFrom);
125             paramMapString.append(":");
126             paramMapString.append(inquiryParameter.getValue());
127             paramMapString.append(",");
128         }
129 		paramMapString.deleteCharAt(paramMapString.length() - 1);
130 
131 		// Create onlick script to open the inquiry window on the click event
132 		// of the direct inquiry
133 		StringBuilder onClickScript = new StringBuilder("showDirectInquiry(\"");
134 		onClickScript.append(inquiryUrl);
135 		onClickScript.append("\", \"");
136 		onClickScript.append(paramMapString);
137 		onClickScript.append("\", ");
138 		onClickScript.append(lightBoxShow);
139 		onClickScript.append(", ");
140 		onClickScript.append(lightBoxOptions);
141 		onClickScript.append(");");
142 
143 		directInquiryActionField.setBlockValidateDirty(true);
144 		directInquiryActionField.setClientSideJs(onClickScript.toString());
145 
146 		setRender(true);
147 	}
148 
149     /**
150      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
151      */
152     @Override
153     public List<Component> getComponentsForLifecycle() {
154         List<Component> components = super.getComponentsForLifecycle();
155 
156         components.add(directInquiryActionField);
157 
158         return components;
159     }	
160 	
161 	/**
162 	 * @return the directInquiryActionField
163 	 */
164 	public ActionField getDirectInquiryActionField() {
165 		return this.directInquiryActionField;
166 	}
167 
168 	/**
169 	 * @param directInquiryActionField the directInquiryActionField to set
170 	 */
171 	public void setDirectInquiryActionField(ActionField directInquiryActionField) {
172 		this.directInquiryActionField = directInquiryActionField;
173 	}
174 
175 }