| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ViewIndex |
|
| 1.7777777777777777;1.778 |
| 1 | /* | |
| 2 | * Copyright 2007 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.krad.uif.container; | |
| 17 | ||
| 18 | import org.apache.commons.lang.StringUtils; | |
| 19 | import org.kuali.rice.krad.uif.core.Component; | |
| 20 | import org.kuali.rice.krad.uif.field.AttributeField; | |
| 21 | ||
| 22 | import java.io.Serializable; | |
| 23 | import java.util.HashMap; | |
| 24 | import java.util.Map; | |
| 25 | ||
| 26 | /** | |
| 27 | * Holds field indexes of a <code>View</code> instance for retrieval | |
| 28 | * | |
| 29 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 30 | */ | |
| 31 | public class ViewIndex implements Serializable { | |
| 32 | private static final long serialVersionUID = 4700818801272201371L; | |
| 33 | ||
| 34 | private Map<String, Component> index; | |
| 35 | private Map<String, AttributeField> attributeFieldIndex; | |
| 36 | private Map<String, CollectionGroup> collectionsIndex; | |
| 37 | ||
| 38 | /** | |
| 39 | * Constructs new instance and performs indexing on View instance | |
| 40 | * | |
| 41 | * @param view - view instance to index | |
| 42 | */ | |
| 43 | 0 | public ViewIndex(View view) { |
| 44 | 0 | index(view); |
| 45 | 0 | } |
| 46 | ||
| 47 | /** | |
| 48 | * Walks through the View tree and indexes all components found. All components | |
| 49 | * are indexed by their IDs with the special indexing done for certain components | |
| 50 | * | |
| 51 | * <p> | |
| 52 | * <code>AttributeField</code> instances are indexed by the attribute path. | |
| 53 | * This is useful for retrieving the AttributeField based on the incoming | |
| 54 | * request parameter | |
| 55 | * </p> | |
| 56 | * | |
| 57 | * <p> | |
| 58 | * <code>CollectionGroup</code> instances are indexed by the collection | |
| 59 | * path. This is useful for retrieving the CollectionGroup based on the | |
| 60 | * incoming request parameter | |
| 61 | * </p> | |
| 62 | */ | |
| 63 | protected void index(View view) { | |
| 64 | 0 | index = new HashMap<String, Component>(); |
| 65 | 0 | attributeFieldIndex = new HashMap<String, AttributeField>(); |
| 66 | 0 | collectionsIndex = new HashMap<String, CollectionGroup>(); |
| 67 | ||
| 68 | 0 | indexComponent(view); |
| 69 | 0 | } |
| 70 | ||
| 71 | /** | |
| 72 | * Adds an entry to the main index for the given component. If the component | |
| 73 | * is of type <code>AttributeField</code> or <code>CollectionGroup</code> an | |
| 74 | * entry is created in the corresponding indexes for those types as well. Then | |
| 75 | * the #indexComponent method is called for each of the component's children | |
| 76 | * | |
| 77 | * <p> | |
| 78 | * If the component is already contained in the indexes, it will be replaced | |
| 79 | * </p> | |
| 80 | * | |
| 81 | * @param component - component instance to index | |
| 82 | */ | |
| 83 | public void indexComponent(Component component) { | |
| 84 | 0 | if (component == null) { |
| 85 | 0 | return; |
| 86 | } | |
| 87 | ||
| 88 | 0 | index.put(component.getId(), component); |
| 89 | ||
| 90 | 0 | if (component instanceof AttributeField) { |
| 91 | 0 | AttributeField field = (AttributeField) component; |
| 92 | 0 | attributeFieldIndex.put(field.getBindingInfo().getBindingPath(), field); |
| 93 | 0 | } else if (component instanceof CollectionGroup) { |
| 94 | 0 | CollectionGroup collectionGroup = (CollectionGroup) component; |
| 95 | 0 | collectionsIndex.put(collectionGroup.getBindingInfo().getBindingPath(), collectionGroup); |
| 96 | } | |
| 97 | ||
| 98 | 0 | for (Component nestedComponent : component.getNestedComponents()) { |
| 99 | 0 | indexComponent(nestedComponent); |
| 100 | } | |
| 101 | 0 | } |
| 102 | ||
| 103 | /** | |
| 104 | * Retrieves a <code>Component</code> from the view index by Id | |
| 105 | * | |
| 106 | * @param id - id for the component to retrieve | |
| 107 | * @return Component instance found in index, or null if no such component exists | |
| 108 | */ | |
| 109 | public Component getComponentById(String id) { | |
| 110 | 0 | return index.get(id); |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * Retrieves a <code>AttributeField</code> instance from the index | |
| 115 | * | |
| 116 | * @param attributePath - full path of the attribute (from the form) | |
| 117 | * @return AttributeField instance for the path or Null if not found | |
| 118 | */ | |
| 119 | public AttributeField getAttributeFieldByPath(String attributePath) { | |
| 120 | 0 | return attributeFieldIndex.get(attributePath); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Retrieves a <code>AttributeField</code> instance that has the given property name | |
| 125 | * specified (note this is not the full binding path and first match is returned) | |
| 126 | * | |
| 127 | * @param propertyName - property name for field to retrieve | |
| 128 | * @return AttributeField instance found or null if not found | |
| 129 | */ | |
| 130 | public AttributeField getAttributeFieldByPropertyName(String propertyName) { | |
| 131 | 0 | AttributeField attributeField = null; |
| 132 | ||
| 133 | 0 | for (AttributeField field : attributeFieldIndex.values()) { |
| 134 | 0 | if (StringUtils.equals(propertyName, field.getPropertyName())) { |
| 135 | 0 | attributeField = field; |
| 136 | 0 | break; |
| 137 | } | |
| 138 | } | |
| 139 | ||
| 140 | 0 | return attributeField; |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * Gets the Map that contains attribute field indexing information. The Map | |
| 145 | * key points to an attribute binding path, and the Map value is the | |
| 146 | * <code>AttributeField</code> instance | |
| 147 | * | |
| 148 | * @return Map<String, AttributeField> attribute fields index map | |
| 149 | */ | |
| 150 | public Map<String, AttributeField> getAttributeFieldIndex() { | |
| 151 | 0 | return this.attributeFieldIndex; |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * Gets the Map that contains collection indexing information. The Map key | |
| 156 | * gives the binding path to the collection, and the Map value givens the | |
| 157 | * <code>CollectionGroup</code> instance | |
| 158 | * | |
| 159 | * @return Map<String, CollectionGroup> collection index map | |
| 160 | */ | |
| 161 | public Map<String, CollectionGroup> getCollectionsIndex() { | |
| 162 | 0 | return this.collectionsIndex; |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * Retrieves a <code>CollectionGroup</code> instance from the index | |
| 167 | * | |
| 168 | * @param collectionPath - full path of the collection (from the form) | |
| 169 | * @return CollectionGroup instance for the collection path or Null if not | |
| 170 | * found | |
| 171 | */ | |
| 172 | public CollectionGroup getCollectionGroupByPath(String collectionPath) { | |
| 173 | 0 | return collectionsIndex.get(collectionPath); |
| 174 | } | |
| 175 | } |