1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.impl.document.search;
17
18 import com.google.common.base.Function;
19 import com.google.common.base.Predicate;
20 import com.google.common.collect.Iterables;
21 import com.google.common.collect.Lists;
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.lang.ArrayUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.rice.core.api.CoreApiServiceLocator;
26 import org.kuali.rice.kns.lookup.LookupableHelperService;
27 import org.kuali.rice.kns.web.ui.Field;
28 import org.kuali.rice.kns.web.ui.Row;
29 import org.kuali.rice.krad.util.KRADConstants;
30
31 import javax.annotation.Nullable;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38
39
40
41 class FormFields {
42 private final Collection<Row> rows;
43
44 FormFields(Collection<Row> rows) {
45 this.rows = rows;
46 }
47
48
49
50
51
52
53
54
55 Map<String, String[]> getFieldValues() {
56 Map<String, String[]> values = new HashMap<String, String[]>();
57 for (Field field : getFields()) {
58 String[] value;
59 if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
60 value = new String[] { field.getPropertyValue() };
61 } else {
62
63 value = field.getPropertyValues();
64 }
65 values.put(field.getPropertyName(), value);
66 }
67 return values;
68 }
69
70 Field getField(final String name) {
71 return Iterables.tryFind(getFields(), new Predicate<Field>() {
72 public boolean apply(@Nullable Field input) {
73 return StringUtils.equals(name, input.getPropertyName());
74 }
75 }).orNull();
76 }
77
78 void setFieldValue(String name, String value) {
79 setFieldValue(name, new String[]{value});
80 }
81
82 void setFieldValue(String name, String[] value) {
83 setFieldValues(Collections.singletonMap(name, value));
84 }
85
86
87
88
89
90 void setFieldValues(Map<String, String[]> values) {
91 for (Field field: getFields()) {
92 if (StringUtils.isNotBlank(field.getPropertyName())) {
93 String[] value = values.get(field.getPropertyName());
94 if (ArrayUtils.isNotEmpty(value)) {
95 setFieldValue(field, value);
96 }
97 }
98 }
99 }
100
101 void applyFieldAuthorizations(LookupableHelperService lhs) {
102 for (Field f: getFields()) {
103 lhs.applyFieldAuthorizationsFromNestedLookups(f);
104 }
105 }
106
107 List<Field> getFieldList() {
108 return Lists.newArrayList(getFields());
109 }
110
111
112
113
114 void setFieldValue(Field field, String[] values) {
115 if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
116 field.setPropertyValue(CollectionUtils.get(values, 0));
117 } else {
118
119 field.setPropertyValues(values);
120 }
121 }
122
123 Iterable<Field> getFields() {
124 return Iterables.concat(Iterables.transform(this.rows, new Function<Row, Iterable<Field>>() {
125 @Override
126 public Iterable<Field> apply(@Nullable Row row) {
127 return row.getFields();
128 }
129 }));
130 }
131 }