View Javadoc

1   /**
2    * Copyright 2005-2013 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.group.Group;
20  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
23  import org.kuali.rice.krad.uif.UifConstants;
24  import org.kuali.rice.krad.uif.util.ComponentFactory;
25  import org.kuali.rice.krad.uif.view.View;
26  import org.kuali.rice.krad.uif.component.Component;
27  import org.kuali.rice.krad.uif.field.InputField;
28  import org.kuali.rice.krad.uif.widget.QuickFinder;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  /**
34   * Represents a group control, which is a special control to handle
35   * the input of a KIM Group by group name
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  @BeanTag(name = "kimGroupControl-bean", parent = "Uif-KimGroupControl")
40  public class GroupControl extends TextControl implements FilterableLookupCriteriaControl {
41      private static final long serialVersionUID = 5598459655735440981L;
42  
43      private String namespaceCodePropertyName;
44      private String groupIdPropertyName;
45  
46      public GroupControl() {
47          super();
48      }
49  
50      @Override
51      public void performApplyModel(View view, Object model, Component parent) {
52          super.performApplyModel(view, model, parent);
53  
54          if (!(parent instanceof InputField)) {
55              return;
56          }
57  
58          InputField field = (InputField) parent;
59  
60          if (StringUtils.isNotBlank(groupIdPropertyName)) {
61              field.getAdditionalHiddenPropertyNames().add(groupIdPropertyName);
62          }
63  
64          buildGroupQuickfinder(view, model, field);
65      }
66  
67      protected void buildGroupQuickfinder(View view,Object model, InputField field) {
68          QuickFinder quickFinder = field.getQuickfinder();
69  
70          // don't build quickfinder if explicitly turned off
71          if ((quickFinder != null) && !quickFinder.isRender()) {
72              return;
73          }
74  
75          boolean quickfinderCreated = false;
76          if (quickFinder == null) {
77              quickFinder = ComponentFactory.getQuickFinder();
78              view.assignComponentIds(quickFinder);
79  
80              field.setQuickfinder(quickFinder);
81  
82              quickfinderCreated = true;
83          }
84  
85          if (field.getQuickfinder() != null) {
86              if (StringUtils.isBlank(field.getQuickfinder().getDataObjectClassName())) {
87                  field.getQuickfinder().setDataObjectClassName("org.kuali.rice.kim.impl.group.GroupBo");
88              }
89  
90              if (field.getQuickfinder().getFieldConversions().isEmpty()) {
91                  if (StringUtils.isNotBlank(groupIdPropertyName)) {
92                      field.getQuickfinder().getFieldConversions().put("id", groupIdPropertyName);
93                  }
94  
95                  field.getQuickfinder().getFieldConversions().put("name", field.getPropertyName());
96  
97                  if (StringUtils.isNotBlank(namespaceCodePropertyName)) {
98                      field.getQuickfinder().getFieldConversions().put("namespaceCode", namespaceCodePropertyName);
99                  }
100             }
101 
102             if (field.getQuickfinder().getLookupParameters().isEmpty()) {
103                 if (StringUtils.isNotBlank(namespaceCodePropertyName)) {
104                     field.getQuickfinder().getLookupParameters().put(namespaceCodePropertyName, "namespaceCode");
105                 }
106             }
107         }
108 
109         // if we created the quickfinder here it will have missed the initialize and apply model phase (it
110         // will be attached to the field for finalize)
111         if (quickfinderCreated) {
112             view.getViewHelperService().spawnSubLifecyle(view, model, quickFinder, field,
113                     UifConstants.ViewPhases.INITIALIZE, UifConstants.ViewPhases.APPLY_MODEL);
114         }
115 
116     }
117 
118     /**
119      * The name of the property on the parent object that holds the group namespace
120      *
121      * @return namespaceCodePropertyName
122      */
123     @BeanTagAttribute(name="namespaceCodePropertyName")
124     public String getNamespaceCodePropertyName() {
125         return namespaceCodePropertyName;
126     }
127 
128     /**
129      * Setter for the name of the property on the parent object that holds the group namespace
130      *
131      * @param namespaceCodePropertyName
132      */
133     public void setNamespaceCodePropertyName(String namespaceCodePropertyName) {
134         this.namespaceCodePropertyName = namespaceCodePropertyName;
135     }
136 
137     /**
138      * The name of the property on the parent object that holds the group id
139      *
140      * @return groupIdPropertyName
141      */
142     @BeanTagAttribute(name="groupIdPropertyName")
143     public String getGroupIdPropertyName() {
144         return groupIdPropertyName;
145     }
146 
147     /**
148      * Setter for the name of the property on the parent object that holds the group id
149      *
150      * @param groupIdPropertyName
151      */
152     public void setGroupIdPropertyName(String groupIdPropertyName) {
153         this.groupIdPropertyName = groupIdPropertyName;
154     }
155 
156     /**
157      * @see FilterableLookupCriteriaControl#filterSearchCriteria(String, java.util.Map)
158      */
159     @Override
160     public Map<String, String> filterSearchCriteria(String propertyName, Map<String, String> searchCriteria) {
161         Map<String, String> filteredSearchCriteria = new HashMap<String, String>(searchCriteria);
162 
163         // check valid groupId
164         // ToDo: move the groupId check and setting to the validation stage.  At that point
165         //       an error should be displayed to the user that the group name and namespace is invalid.
166         String groupName = searchCriteria.get(propertyName);
167         String groupNamespaceCd = searchCriteria.get(namespaceCodePropertyName);
168         if (StringUtils.isNotBlank(groupName) && StringUtils.isNotBlank(groupNamespaceCd)) {
169             Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(groupNamespaceCd,groupName);
170             if( group == null) {
171                 return null;
172             } else {
173                 filteredSearchCriteria.put(groupIdPropertyName, group.getId());
174             }
175         }
176 
177         // filter
178         filteredSearchCriteria.remove(propertyName);
179         filteredSearchCriteria.remove(namespaceCodePropertyName);
180 
181         return filteredSearchCriteria;
182     }
183 
184     /**
185      * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
186      */
187     @Override
188     protected <T> void copyProperties(T component) {
189         super.copyProperties(component);
190         GroupControl groupControlCopy = (GroupControl) component;
191         groupControlCopy.setNamespaceCodePropertyName(this.namespaceCodePropertyName);
192         groupControlCopy.setGroupIdPropertyName(this.groupIdPropertyName);
193     }
194 }