View Javadoc

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.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       * Preserves Field values, saving single or array property value depending on field type; single property value is
50       * converted into String[1]
51       * This implementation makes the assumption that a Field can either represent a single property value, or an array
52       * of values but not both! (only one is preserved)
53       * @return a Map<String, String[]> containing field values depending on field type
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                  //multi value, set to values
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       * Overrides Row Field values with Map values
88       * @param values the fieldvalues
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      * Sets a Field value appropriately, depending on whether it is a "multi-value" field type
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             //multi value, set to values
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 }