| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CompareFieldCreateModifier |
|
| 1.9166666666666667;1.917 |
| 1 | /* | |
| 2 | * Copyright 2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 1.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/ecl1.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.kns.uif.modifier; | |
| 17 | ||
| 18 | import java.util.ArrayList; | |
| 19 | import java.util.HashSet; | |
| 20 | import java.util.List; | |
| 21 | import java.util.Set; | |
| 22 | ||
| 23 | import org.apache.commons.lang.StringUtils; | |
| 24 | import org.apache.log4j.Logger; | |
| 25 | import org.kuali.rice.kns.uif.UifPropertyPaths; | |
| 26 | import org.kuali.rice.kns.uif.container.Group; | |
| 27 | import org.kuali.rice.kns.uif.container.View; | |
| 28 | import org.kuali.rice.kns.uif.core.Component; | |
| 29 | import org.kuali.rice.kns.uif.field.HeaderField; | |
| 30 | import org.kuali.rice.kns.uif.util.ComponentUtils; | |
| 31 | ||
| 32 | /** | |
| 33 | * Generates <code>Field</code> instances to produce a comparison view among | |
| 34 | * objects of the same type | |
| 35 | * | |
| 36 | * <p> | |
| 37 | * Modifier is initialized with a List of <code>ComparableInfo</code> instances. | |
| 38 | * For each comparable info, a copy of the configured group field is made and | |
| 39 | * adjusted to the binding object path for the comparable. The comparison fields | |
| 40 | * are ordered based on the configured order property of the comparable. In | |
| 41 | * addition, a <code>HeaderField<code> can be generated to label each group | |
| 42 | * of comparison fields. | |
| 43 | * </p> | |
| 44 | * | |
| 45 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 46 | */ | |
| 47 | public class CompareFieldCreateModifier extends ComponentModifierBase { | |
| 48 | 0 | private static final Logger LOG = Logger.getLogger(CompareFieldCreateModifier.class); |
| 49 | ||
| 50 | private static final long serialVersionUID = -6285531580512330188L; | |
| 51 | ||
| 52 | private int defaultOrderSequence; | |
| 53 | private boolean generateCompareHeaders; | |
| 54 | ||
| 55 | private HeaderField headerFieldPrototype; | |
| 56 | private List<ComparableInfo> comparables; | |
| 57 | ||
| 58 | 0 | public CompareFieldCreateModifier() { |
| 59 | 0 | defaultOrderSequence = 1; |
| 60 | 0 | generateCompareHeaders = true; |
| 61 | ||
| 62 | 0 | comparables = new ArrayList<ComparableInfo>(); |
| 63 | 0 | } |
| 64 | ||
| 65 | /** | |
| 66 | * Generates the comparison fields | |
| 67 | * | |
| 68 | * <p> | |
| 69 | * First the configured List of <code>ComparableInfo</code> instances are | |
| 70 | * sorted based on their order property. Then if generateCompareHeaders is | |
| 71 | * set to true, a <code>HeaderField</code> is created for each comparable | |
| 72 | * using the headerFieldPrototype and the headerText given by the | |
| 73 | * comparable. Finally for each field configured on the <code>Group</code>, | |
| 74 | * a corresponding comparison field is generated for each comparable and | |
| 75 | * adjusted to the binding object path given by the comparable in addition | |
| 76 | * to suffixing the id and setting the readOnly property | |
| 77 | * </p> | |
| 78 | * | |
| 79 | * @see org.kuali.rice.kns.uif.modifier.ComponentModifier#performModification(org.kuali.rice.kns.uif.container.View, | |
| 80 | * org.kuali.rice.kns.uif.core.Component) | |
| 81 | */ | |
| 82 | @SuppressWarnings("unchecked") | |
| 83 | @Override | |
| 84 | public void performModification(View view, Component component) { | |
| 85 | 0 | if ((component != null) && !(component instanceof Group)) { |
| 86 | 0 | throw new IllegalArgumentException("Compare field initializer only support Group components, found type: " |
| 87 | + component.getClass()); | |
| 88 | } | |
| 89 | ||
| 90 | 0 | if (component == null) { |
| 91 | 0 | return; |
| 92 | } | |
| 93 | ||
| 94 | 0 | LOG.debug("Running compare field initializer on component: " + component.getId()); |
| 95 | ||
| 96 | // list to hold the generated compare items | |
| 97 | 0 | List<Component> comparisonItems = new ArrayList<Component>(); |
| 98 | ||
| 99 | // sort comparables by their order property | |
| 100 | 0 | List<ComparableInfo> groupComparables = (List<ComparableInfo>) ComponentUtils.sort(comparables, |
| 101 | defaultOrderSequence); | |
| 102 | ||
| 103 | // generate compare header | |
| 104 | 0 | if (isGenerateCompareHeaders()) { |
| 105 | 0 | for (ComparableInfo comparable : groupComparables) { |
| 106 | 0 | HeaderField compareHeaderField = ComponentUtils.copy(headerFieldPrototype); |
| 107 | 0 | compareHeaderField.setHeaderText(comparable.getHeaderText()); |
| 108 | ||
| 109 | 0 | comparisonItems.add(compareHeaderField); |
| 110 | 0 | } |
| 111 | } | |
| 112 | ||
| 113 | // generate the compare items from the configured group | |
| 114 | 0 | Group group = (Group) component; |
| 115 | 0 | for (Component item : group.getItems()) { |
| 116 | 0 | int defaultSuffix = 0; |
| 117 | 0 | for (ComparableInfo comparable : groupComparables) { |
| 118 | 0 | Component compareItem = ComponentUtils.copy(item); |
| 119 | ||
| 120 | 0 | ComponentUtils.setComponentPropertyDeep(compareItem, UifPropertyPaths.BIND_OBJECT_PATH, |
| 121 | comparable.getBindingObjectPath()); | |
| 122 | 0 | if (comparable.isReadOnly()) { |
| 123 | 0 | compareItem.setReadOnly(true); |
| 124 | 0 | compareItem.setConditionalReadOnly(""); |
| 125 | } | |
| 126 | ||
| 127 | 0 | comparisonItems.add(compareItem); |
| 128 | 0 | defaultSuffix++; |
| 129 | 0 | } |
| 130 | 0 | } |
| 131 | ||
| 132 | // update the group's list of components | |
| 133 | 0 | group.setItems(comparisonItems); |
| 134 | 0 | } |
| 135 | ||
| 136 | /** | |
| 137 | * Generates an id suffix for the comparable item | |
| 138 | * | |
| 139 | * <p> | |
| 140 | * If the idSuffix to use if configured on the <code>ComparableInfo</code> | |
| 141 | * it will be used, else the given integer index will be used with an | |
| 142 | * underscore | |
| 143 | * </p> | |
| 144 | * | |
| 145 | * @param comparable | |
| 146 | * - comparable info to check for id suffix | |
| 147 | * @param index | |
| 148 | * - sequence integer | |
| 149 | * @return String id suffix | |
| 150 | * @see org.kuali.rice.kns.uif.modifier.ComparableInfo.getIdSuffix() | |
| 151 | */ | |
| 152 | protected String getIdSuffix(ComparableInfo comparable, int index) { | |
| 153 | 0 | String idSuffix = comparable.getIdSuffix(); |
| 154 | 0 | if (StringUtils.isBlank(idSuffix)) { |
| 155 | 0 | idSuffix = "_" + index; |
| 156 | } | |
| 157 | ||
| 158 | 0 | return idSuffix; |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * @see org.kuali.rice.kns.uif.modifier.ComponentModifier#getSupportedComponents() | |
| 163 | */ | |
| 164 | @Override | |
| 165 | public Set<Class<? extends Component>> getSupportedComponents() { | |
| 166 | 0 | Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>(); |
| 167 | 0 | components.add(Group.class); |
| 168 | ||
| 169 | 0 | return components; |
| 170 | } | |
| 171 | ||
| 172 | /** | |
| 173 | * Indicates the starting integer sequence value to use for | |
| 174 | * <code>ComparableInfo</code> instances that do not have the order property | |
| 175 | * set | |
| 176 | * | |
| 177 | * @return int default sequence starting value | |
| 178 | */ | |
| 179 | public int getDefaultOrderSequence() { | |
| 180 | 0 | return this.defaultOrderSequence; |
| 181 | } | |
| 182 | ||
| 183 | /** | |
| 184 | * Setter for the default sequence starting value | |
| 185 | * | |
| 186 | * @param defaultOrderSequence | |
| 187 | */ | |
| 188 | public void setDefaultOrderSequence(int defaultOrderSequence) { | |
| 189 | 0 | this.defaultOrderSequence = defaultOrderSequence; |
| 190 | 0 | } |
| 191 | ||
| 192 | /** | |
| 193 | * Indicates whether a <code>HeaderField</code> should be created for each | |
| 194 | * group of comparison fields | |
| 195 | * | |
| 196 | * <p> | |
| 197 | * If set to true, for each group of comparison fields a header field will | |
| 198 | * be created using the headerFieldPrototype configured on the modifier with | |
| 199 | * the headerText property of the comparable | |
| 200 | * </p> | |
| 201 | * | |
| 202 | * @return boolean true if the headers should be created, false if no | |
| 203 | * headers should be created | |
| 204 | */ | |
| 205 | public boolean isGenerateCompareHeaders() { | |
| 206 | 0 | return this.generateCompareHeaders; |
| 207 | } | |
| 208 | ||
| 209 | /** | |
| 210 | * Setter for the generate comparison headers indicator | |
| 211 | * | |
| 212 | * @param generateCompareHeaders | |
| 213 | */ | |
| 214 | public void setGenerateCompareHeaders(boolean generateCompareHeaders) { | |
| 215 | 0 | this.generateCompareHeaders = generateCompareHeaders; |
| 216 | 0 | } |
| 217 | ||
| 218 | /** | |
| 219 | * Prototype instance to use for creating the <code>HeaderField</code> for | |
| 220 | * each group of comparison fields (if generateCompareHeaders is true) | |
| 221 | * | |
| 222 | * @return HeaderField header field prototype | |
| 223 | */ | |
| 224 | public HeaderField getHeaderFieldPrototype() { | |
| 225 | 0 | return this.headerFieldPrototype; |
| 226 | } | |
| 227 | ||
| 228 | /** | |
| 229 | * Setter for the header field prototype | |
| 230 | * | |
| 231 | * @param headerFieldPrototype | |
| 232 | */ | |
| 233 | public void setHeaderFieldPrototype(HeaderField headerFieldPrototype) { | |
| 234 | 0 | this.headerFieldPrototype = headerFieldPrototype; |
| 235 | 0 | } |
| 236 | ||
| 237 | /** | |
| 238 | * List of <code>ComparableInfo</code> instances the compare fields should | |
| 239 | * be generated for | |
| 240 | * | |
| 241 | * <p> | |
| 242 | * For each comparable, a copy of the fields configured for the | |
| 243 | * <code>Group</code> will be created for the comparison view | |
| 244 | * </p> | |
| 245 | * | |
| 246 | * @return List<ComparableInfo> comparables to generate fields for | |
| 247 | */ | |
| 248 | public List<ComparableInfo> getComparables() { | |
| 249 | 0 | return this.comparables; |
| 250 | } | |
| 251 | ||
| 252 | /** | |
| 253 | * Setter for the list of comparable info instances | |
| 254 | * | |
| 255 | * @param comparables | |
| 256 | */ | |
| 257 | public void setComparables(List<ComparableInfo> comparables) { | |
| 258 | 0 | this.comparables = comparables; |
| 259 | 0 | } |
| 260 | ||
| 261 | } |