View Javadoc

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