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.datadictionary.validation;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20 import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
21 import org.kuali.rice.krad.uif.UifPropertyPaths;
22 import org.kuali.rice.krad.uif.field.InputField;
23 import org.kuali.rice.krad.uif.util.ComponentUtils;
24 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
25 import org.kuali.rice.krad.uif.view.View;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33 * AttributeValueReader which can read the correct values from all InputFields which exist on the View
34 *
35 * @author Kuali Rice Team (rice.collab@kuali.org)
36 */
37 public class ViewAttributeValueReader extends BaseAttributeValueReader {
38 private View view;
39 private Object form;
40
41 private List<Constrainable> inputFields = new ArrayList<Constrainable>();
42 private Map<String, InputField> inputFieldMap = new HashMap<String, InputField>();
43
44 /**
45 * Constructor for ViewAttributeValueReader, the View must already be indexed and
46 * the InputFields must have already be initialized for this reader to work properly
47 *
48 * @param view the View to validate
49 * @param form model object representing the View's form data
50 */
51 public ViewAttributeValueReader(View view, Object form) {
52 this.view = view;
53 this.form = form;
54
55 List<InputField> containerInputFields = ComponentUtils.getAllInputFieldsWithinContainer(view);
56 for (InputField field : containerInputFields) {
57 if (StringUtils.isNotBlank(field.getName())) {
58 inputFields.add(field);
59 inputFieldMap.put(field.getName(), field);
60 }
61 }
62 }
63
64 /* TODO allow it to be page specific only
65 public ViewAttributeValueReader(View view, Page page, UifFormBase form) {
66 this.view = view;
67 this.form = form;
68 for(DataField field: view.getViewIndex().getDataFieldIndex().values()){
69 if(field instanceof Constrainable){
70 inputFields.add((Constrainable)field);
71 }
72 }
73 }*/
74
75 /**
76 * Gets the definition which is an InputField on the View/Page
77 */
78 @Override
79 public Constrainable getDefinition(String attributeName) {
80 InputField field = inputFieldMap.get(attributeName);
81 if (field != null) {
82 return field;
83 } else {
84 return null;
85 }
86 }
87
88 /**
89 * Gets all InputFields (which extend Constrainable)
90 *
91 * @return
92 */
93 @Override
94 public List<Constrainable> getDefinitions() {
95 return inputFields;
96 }
97
98 /**
99 * Returns the label associated with the InputField which has that AttributeName
100 *
101 * @param attributeName
102 * @return
103 */
104 @Override
105 public String getLabel(String attributeName) {
106 InputField field = inputFieldMap.get(attributeName);
107 if (field != null) {
108 return field.getLabel();
109 } else {
110 return null;
111 }
112 }
113
114 /**
115 * Returns the View object
116 *
117 * @return view set in the constructor
118 */
119 @Override
120 public Object getObject() {
121 return view;
122 }
123
124 /**
125 * Not used for this reader, returns null
126 *
127 * @return null
128 */
129 @Override
130 public Constrainable getEntry() {
131 return null;
132 }
133
134 /**
135 * Returns current attributeName which represents the path
136 *
137 * @return attributeName set on this reader
138 */
139 @Override
140 public String getPath() {
141 return this.attributeName;
142 }
143
144 /**
145 * Gets the type of value for this AttributeName as represented on the Form
146 *
147 * @param attributeName
148 * @return
149 */
150 @Override
151 public Class<?> getType(String attributeName) {
152 Object fieldValue = ObjectPropertyUtils.getPropertyValue(form, attributeName);
153 return fieldValue.getClass();
154 }
155
156 /**
157 * If the current attribute being evaluated is a field of an addLine return false because it should not
158 * be evaluated during Validation.
159 *
160 * @return false if InputField is part of an addLine for a collection, true otherwise
161 */
162 @Override
163 public boolean isReadable() {
164 if (attributeName != null && attributeName.contains(UifPropertyPaths.NEW_COLLECTION_LINES)) {
165 return false;
166 }
167 return true;
168 }
169
170 /**
171 * Return value of the field for the attributeName currently set on this reader
172 *
173 * @param <X> return type
174 * @return value of the field for the attributeName currently set on this reader
175 * @throws AttributeValidationException
176 */
177 @Override
178 public <X> X getValue() throws AttributeValidationException {
179 X fieldValue = null;
180 if (StringUtils.isNotBlank(this.attributeName)) {
181 fieldValue = ObjectPropertyUtils.<X>getPropertyValue(form, this.attributeName);
182 }
183 return fieldValue;
184 }
185
186 /**
187 * Return value of the field for the attributeName passed in
188 *
189 * @param attributeName name (which represents a path) of the value to be retrieved on the Form
190 * @param <X> return type
191 * @return value of that attributeName represents on the form
192 * @throws AttributeValidationException
193 */
194 @Override
195 public <X> X getValue(String attributeName) throws AttributeValidationException {
196 X fieldValue = null;
197 if (StringUtils.isNotBlank(attributeName)) {
198 fieldValue = ObjectPropertyUtils.<X>getPropertyValue(form, this.attributeName);
199 }
200 return fieldValue;
201 }
202
203 /**
204 * Cones this AttributeValueReader
205 *
206 * @return AttributeValueReader
207 */
208 @Override
209 public AttributeValueReader clone() {
210 ViewAttributeValueReader clone = new ViewAttributeValueReader(view, form);
211 clone.setAttributeName(this.attributeName);
212 return clone;
213 }
214
215 }