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