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.control;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kim.api.identity.Person;
20  import org.kuali.rice.kim.api.identity.PersonService;
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.uif.field.InputField;
25  import org.kuali.rice.krad.uif.util.ScriptUtils;
26  import org.kuali.rice.krad.uif.view.View;
27  import org.kuali.rice.krad.uif.component.Component;
28  import org.kuali.rice.krad.uif.component.MethodInvokerConfig;
29  import org.kuali.rice.krad.uif.field.AttributeQuery;
30  import org.kuali.rice.krad.uif.widget.QuickFinder;
31  
32  /**
33   * Represents a user control, which is a special control to handle
34   * the input of a Person
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @BeanTag(name="userControl")
39  public class UserControl extends TextControl {
40      private static final long serialVersionUID = 7468340793076585869L;
41  
42      private String principalIdPropertyName;
43      private String personNamePropertyName;
44      private String personObjectPropertyName;
45  
46      public UserControl() {
47          super();
48      }
49  
50      /**
51       * @see org.kuali.rice.krad.uif.component.ComponentBase#performApplyModel(org.kuali.rice.krad.uif.view.View,
52       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
53       */
54      @Override
55      public void performApplyModel(View view, Object model, Component parent) {
56          super.performApplyModel(view, model, parent);
57  
58          if (!(parent instanceof InputField)) {
59              return;
60          }
61  
62          InputField field = (InputField) parent;
63          field.getAdditionalHiddenPropertyNames().add(principalIdPropertyName);
64  
65          if (!field.isReadOnly()) {
66              // add information fields
67              if (StringUtils.isNotBlank(personNamePropertyName)) {
68                  field.getPropertyNamesForAdditionalDisplay().add(personNamePropertyName);
69              } else {
70                  field.getPropertyNamesForAdditionalDisplay().add(personObjectPropertyName + ".name");
71              }
72  
73              // setup script to clear id field when name is modified
74              String idPropertyPath = field.getBindingInfo().getPropertyAdjustedBindingPath(principalIdPropertyName);
75              String onChangeScript = "setValue('" + ScriptUtils.escapeName(idPropertyPath) + "','');";
76  
77              if (StringUtils.isNotBlank(getOnChangeScript())) {
78                  onChangeScript = getOnChangeScript() + onChangeScript;
79              }
80              setOnChangeScript(onChangeScript);
81          }
82  
83          if (field.isReadOnly() && StringUtils.isBlank(field.getReadOnlyDisplaySuffixPropertyName())) {
84              if (StringUtils.isNotBlank(personNamePropertyName)) {
85                  field.setReadOnlyDisplaySuffixPropertyName(personNamePropertyName);
86              } else {
87                  field.setReadOnlyDisplaySuffixPropertyName(personObjectPropertyName + ".name");
88              }
89          }
90  
91          // setup field query for displaying name
92          AttributeQuery attributeQuery = new AttributeQuery();
93  
94          MethodInvokerConfig methodInvokerConfig = new MethodInvokerConfig();
95          PersonService personService = KimApiServiceLocator.getPersonService();
96          methodInvokerConfig.setTargetObject(personService);
97  
98          attributeQuery.setQueryMethodInvokerConfig(methodInvokerConfig);
99          attributeQuery.setQueryMethodToCall("getPersonByPrincipalName");
100         attributeQuery.getQueryMethodArgumentFieldList().add(field.getPropertyName());
101         attributeQuery.getReturnFieldMapping().put("principalId", principalIdPropertyName);
102 
103         if (StringUtils.isNotBlank(personNamePropertyName)) {
104             attributeQuery.getReturnFieldMapping().put("name", personNamePropertyName);
105         } else {
106             attributeQuery.getReturnFieldMapping().put("name", personObjectPropertyName + ".name");
107         }
108         field.setAttributeQuery(attributeQuery);
109 
110         // setup field lookup
111         QuickFinder quickFinder = field.getQuickfinder();
112         if (quickFinder.isRender()) {
113             if (StringUtils.isBlank(quickFinder.getDataObjectClassName())) {
114                 quickFinder.setDataObjectClassName(Person.class.getName());
115             }
116 
117             if (quickFinder.getFieldConversions().isEmpty()) {
118                 quickFinder.getFieldConversions().put("principalId", principalIdPropertyName);
119 
120                 if (StringUtils.isNotBlank(personNamePropertyName)) {
121                     quickFinder.getFieldConversions().put("name", personNamePropertyName);
122                 } else {
123                     quickFinder.getFieldConversions().put("name", personObjectPropertyName + ".name");
124                 }
125 
126                 quickFinder.getFieldConversions().put("principalName", field.getPropertyName());
127             }
128         }
129     }
130 
131     /**
132      * The name of the property on the parent object that holds the principal id
133      *
134      * @return String principalIdPropertyName
135      */
136     @BeanTagAttribute(name="principalIdPropertyName")
137     public String getPrincipalIdPropertyName() {
138         return principalIdPropertyName;
139     }
140 
141     /**
142      * Setter for the name of the property on the parent object that holds the principal id
143      *
144      * @param principalIdPropertyName
145      */
146     public void setPrincipalIdPropertyName(String principalIdPropertyName) {
147         this.principalIdPropertyName = principalIdPropertyName;
148     }
149 
150     /**
151      * The name of the property on the parent object that holds the person name
152      *
153      * @return String personNamePropertyName
154      */
155     @BeanTagAttribute(name="personNamePropertyName")
156     public String getPersonNamePropertyName() {
157         return personNamePropertyName;
158     }
159 
160     /**
161      * Setter for the name of the property on the parent object that holds the person name
162      *
163      * @param personNamePropertyName
164      */
165     public void setPersonNamePropertyName(String personNamePropertyName) {
166         this.personNamePropertyName = personNamePropertyName;
167     }
168 
169     /**
170      * The name of the property on the parent object that holds the person object
171      *
172      * @return String personObjectPropertyName
173      */
174     @BeanTagAttribute(name="personObjectPropertyName")
175     public String getPersonObjectPropertyName() {
176         return personObjectPropertyName;
177     }
178 
179     /**
180      * Setter for the name of the property on the parent object that holds the person object
181      *
182      * @param personObjectPropertyName
183      */
184     public void setPersonObjectPropertyName(String personObjectPropertyName) {
185         this.personObjectPropertyName = personObjectPropertyName;
186     }
187 }