001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.mapping;
018:
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.compass.core.engine.naming.PropertyPath;
026: import org.compass.core.engine.subindex.SubIndexHash;
027: import org.compass.core.mapping.internal.DefaultAllMapping;
028: import org.compass.core.mapping.internal.InternalResourceMapping;
029:
030: /**
031: * @author kimchy
032: */
033: public abstract class AbstractResourceMapping extends
034: AbstractMultipleMapping implements InternalResourceMapping,
035: AliasMapping, PostProcessingMapping {
036:
037: private String alias;
038:
039: private SubIndexHash subIndexHash;
040:
041: private String[] extendedAliases = new String[0];
042:
043: private String[] extendingAliases = new String[0];
044:
045: private String analyzer;
046:
047: private float boost;
048:
049: private boolean isRoot = true;
050:
051: private AllMapping allMapping = new DefaultAllMapping();
052:
053: private SpellCheckType spellCheck = SpellCheckType.NA;
054:
055: private String uidProperty;
056:
057: private Mapping[] idMappings;
058:
059: private ResourcePropertyMapping[] idPropertyMappings;
060:
061: private CascadeMapping[] cascades;
062:
063: private Map<String, ResourcePropertyMapping[]> resourcePropertyMappingsByNameMap;
064:
065: private Map<PropertyPath, ResourcePropertyMapping> resourcePropertyMappingsByPathMap;
066:
067: private String[] resourcePropertyNames;
068:
069: private boolean hasSpecificAnalyzerPerResourceProperty;
070:
071: private ResourceAnalyzerController analyzerController;
072:
073: private BoostPropertyMapping boostPropertyMapping;
074:
075: /**
076: * Gets the idMappings of the resource.
077: */
078: public Mapping[] getIdMappings() {
079: if (idMappings == null) {
080: buildResourceIds();
081: }
082: return idMappings;
083: }
084:
085: public AllMapping getAllMapping() {
086: return allMapping;
087: }
088:
089: public void setAllMapping(AllMapping allMapping) {
090: this .allMapping = allMapping;
091: }
092:
093: public SpellCheckType getSpellCheck() {
094: return spellCheck;
095: }
096:
097: public void setSpellCheck(SpellCheckType spellCheck) {
098: this .spellCheck = spellCheck;
099: }
100:
101: public ResourcePropertyMapping[] getResourceIdMappings() {
102: if (idPropertyMappings == null) {
103: buildResourceIds();
104: }
105: return idPropertyMappings;
106: }
107:
108: protected void copy(AbstractResourceMapping resourceMapping) {
109: super .copy(resourceMapping);
110: shallowCopy(resourceMapping);
111: }
112:
113: public void shallowCopy(AbstractResourceMapping resourceMapping) {
114: super .shallowCopy(resourceMapping);
115: resourceMapping.setAlias(getAlias());
116: resourceMapping.setSubIndexHash(getSubIndexHash());
117: resourceMapping.setExtendedAliases(getExtendedAliases());
118: resourceMapping.setExtendingAliases(getExtendingAliases());
119: resourceMapping.setRoot(isRoot());
120: resourceMapping.setBoost(getBoost());
121: resourceMapping.setAnalyzer(getAnalyzer());
122: resourceMapping.setUIDPath(getUIDPath());
123: resourceMapping.setAllMapping(getAllMapping().copy());
124: resourceMapping.setSpellCheck(getSpellCheck());
125: }
126:
127: public void postProcess() throws MappingException {
128: doPostProcess();
129: buildResourcePropertyMap();
130: buildAnalyzerSpecificFlag();
131: buildResourceIds();
132: }
133:
134: protected abstract void doPostProcess() throws MappingException;
135:
136: private void buildResourceIds() {
137: ArrayList<Mapping> resourceIds = new ArrayList<Mapping>();
138: ArrayList<ResourcePropertyMapping> resourceIdPropertyMappings = new ArrayList<ResourcePropertyMapping>();
139: for (Iterator it = mappingsIt(); it.hasNext();) {
140: Mapping mapping = (Mapping) it.next();
141: if (mapping instanceof ResourceIdMappingProvider) {
142: Mapping[] tempIds = ((ResourceIdMappingProvider) mapping)
143: .getIdMappings();
144: resourceIds.addAll(Arrays.asList(tempIds));
145: ResourcePropertyMapping[] tempPropertyIds = ((ResourceIdMappingProvider) mapping)
146: .getResourceIdMappings();
147: resourceIdPropertyMappings.addAll(Arrays
148: .asList(tempPropertyIds));
149: }
150: }
151: idMappings = resourceIds
152: .toArray(new Mapping[resourceIds.size()]);
153: idPropertyMappings = resourceIdPropertyMappings
154: .toArray(new ResourcePropertyMapping[resourceIdPropertyMappings
155: .size()]);
156: }
157:
158: private void buildResourcePropertyMap() {
159: resourcePropertyMappingsByPathMap = new HashMap<PropertyPath, ResourcePropertyMapping>();
160: HashMap<String, ArrayList<ResourcePropertyMapping>> tempMap = new HashMap<String, ArrayList<ResourcePropertyMapping>>();
161:
162: ResourcePropertyMapping[] resourcePropertyMappings = getResourcePropertyMappings();
163: for (ResourcePropertyMapping resourcePropertyMapping : resourcePropertyMappings) {
164: resourcePropertyMappingsByPathMap.put(
165: resourcePropertyMapping.getPath(),
166: resourcePropertyMapping);
167:
168: ArrayList<ResourcePropertyMapping> propertyList = tempMap
169: .get(resourcePropertyMapping.getName());
170: if (propertyList == null) {
171: propertyList = new ArrayList<ResourcePropertyMapping>();
172: tempMap.put(resourcePropertyMapping.getName(),
173: propertyList);
174: }
175: propertyList.add(resourcePropertyMapping);
176: }
177: resourcePropertyNames = new String[tempMap.size()];
178: int i = 0;
179: resourcePropertyMappingsByNameMap = new HashMap<String, ResourcePropertyMapping[]>();
180: for (Map.Entry<String, ArrayList<ResourcePropertyMapping>> entry : tempMap
181: .entrySet()) {
182: String propertyName = entry.getKey();
183: resourcePropertyNames[i++] = propertyName;
184: ArrayList<ResourcePropertyMapping> propertyList = entry
185: .getValue();
186: resourcePropertyMappingsByNameMap
187: .put(
188: propertyName,
189: propertyList
190: .toArray(new ResourcePropertyMapping[propertyList
191: .size()]));
192: }
193: }
194:
195: private void buildAnalyzerSpecificFlag() {
196: for (String propertyName : resourcePropertyMappingsByNameMap
197: .keySet()) {
198: ResourcePropertyMapping[] mappings = resourcePropertyMappingsByNameMap
199: .get(propertyName);
200: // the system will validate in the mappings if there are different
201: // analyzers (or null) set to the same property mapping, here we can
202: // just use the first one
203: if (mappings[0].getAnalyzer() != null) {
204: // no need to say we have a specific analyzer if it is the same as the one set on the resource
205: if (analyzer == null
206: || !analyzer.equals(mappings[0].getAnalyzer())) {
207: hasSpecificAnalyzerPerResourceProperty = true;
208: }
209: }
210: }
211: }
212:
213: /**
214: * No duplicate names are allowed when added an id (applies the property
215: * names)
216: */
217: public int addMapping(Mapping mapping) {
218: if (mapping instanceof ResourceAnalyzerController) {
219: analyzerController = (ResourceAnalyzerController) mapping;
220: }
221: if (mapping instanceof BoostPropertyMapping) {
222: boostPropertyMapping = (BoostPropertyMapping) mapping;
223: }
224: return super .addMapping(mapping);
225: }
226:
227: public ResourcePropertyMapping[] getResourcePropertyMappings(
228: String propertyName) {
229: return resourcePropertyMappingsByNameMap.get(propertyName);
230: }
231:
232: public ResourcePropertyMapping getResourcePropertyMapping(
233: String propertyName) {
234: ResourcePropertyMapping[] retVal = getResourcePropertyMappings(propertyName);
235: if (retVal == null) {
236: return null;
237: }
238: return retVal[0];
239: }
240:
241: public ResourcePropertyMapping getResourcePropertyMappingByPath(
242: PropertyPath path) {
243: return resourcePropertyMappingsByPathMap.get(path);
244: }
245:
246: public CascadeMapping[] getCascadeMappings() {
247: return cascades;
248: }
249:
250: public boolean operationAllowed(CascadeMapping.Cascade cascade) {
251: if (isRoot()) {
252: return true;
253: }
254: if (cascades == null || cascades.length == 0) {
255: return false;
256: }
257: for (CascadeMapping cascade1 : cascades) {
258: if (cascade1.shouldCascade(cascade)) {
259: return true;
260: }
261: }
262: return false;
263: }
264:
265: public String getAlias() {
266: return alias;
267: }
268:
269: public void setAlias(String alias) {
270: this .alias = alias;
271: }
272:
273: public String[] getExtendedAliases() {
274: return extendedAliases;
275: }
276:
277: public void setExtendedAliases(String[] extendedMappings) {
278: this .extendedAliases = extendedMappings;
279: }
280:
281: public void setUIDPath(String uid) {
282: this .uidProperty = uid;
283: }
284:
285: public String getUIDPath() {
286: return this .uidProperty;
287: }
288:
289: public float getBoost() {
290: return boost;
291: }
292:
293: public void setBoost(float boost) {
294: this .boost = boost;
295: }
296:
297: public String getAnalyzer() {
298: return analyzer;
299: }
300:
301: public void setAnalyzer(String analyzer) {
302: this .analyzer = analyzer;
303: }
304:
305: public boolean isRoot() {
306: return isRoot;
307: }
308:
309: public void setRoot(boolean isRoot) {
310: this .isRoot = isRoot;
311: }
312:
313: public SubIndexHash getSubIndexHash() {
314: return subIndexHash;
315: }
316:
317: public void setSubIndexHash(SubIndexHash subIndexHash) {
318: this .subIndexHash = subIndexHash;
319: }
320:
321: public boolean hasSpecificAnalyzerPerResourceProperty() {
322: return hasSpecificAnalyzerPerResourceProperty;
323: }
324:
325: public ResourceAnalyzerController getAnalyzerController() {
326: return analyzerController;
327: }
328:
329: public void setAnalyzerController(
330: ResourceAnalyzerController analyzerController) {
331: this .analyzerController = analyzerController;
332: }
333:
334: public BoostPropertyMapping getBoostPropertyMapping() {
335: return boostPropertyMapping;
336: }
337:
338: public void setBoostPropertyMapping(
339: BoostPropertyMapping boostPropertyMapping) {
340: this .boostPropertyMapping = boostPropertyMapping;
341: }
342:
343: public String[] getResourcePropertyNames() {
344: return resourcePropertyNames;
345: }
346:
347: public String[] getExtendingAliases() {
348: return extendingAliases;
349: }
350:
351: public void setExtendingAliases(String[] extendingAliases) {
352: this .extendingAliases = extendingAliases;
353: }
354:
355: public void setCascades(CascadeMapping[] cascades) {
356: this.cascades = cascades;
357: }
358: }
|