01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.compass.core.engine.utils;
18:
19: import org.compass.core.Property;
20: import org.compass.core.Resource;
21: import org.compass.core.engine.SearchEngineException;
22: import org.compass.core.mapping.CompassMapping;
23: import org.compass.core.mapping.Mapping;
24: import org.compass.core.mapping.ResourceMapping;
25:
26: /**
27: * @author kimchy
28: */
29: public abstract class ResourceHelper {
30:
31: private ResourceHelper() {
32: }
33:
34: public static Property[] toIds(Resource resource,
35: CompassMapping mapping) throws SearchEngineException {
36: ResourceMapping resourceMapping = mapping
37: .getRootMappingByAlias(resource.getAlias());
38: if (resourceMapping == null) {
39: throw new SearchEngineException(
40: "Failed to find mappings for alias ["
41: + resource.getAlias() + "]");
42: }
43: return toIds(resource, resourceMapping);
44: }
45:
46: /**
47: * Same as {@link #toIds(org.compass.core.Resource, org.compass.core.mapping.ResourceMapping, boolean)}
48: * with idsMustExist set the <code>true</code>.
49: */
50: public static Property[] toIds(Resource resource,
51: ResourceMapping resourceMapping)
52: throws SearchEngineException {
53: return toIds(resource, resourceMapping, true);
54: }
55:
56: /**
57: * Gets the id properties based on the resource mapping from the give resource. If
58: * must the flag idsMustExists is set, will throw an exception if id value not found,
59: * otherise will return null.
60: */
61: public static Property[] toIds(Resource resource,
62: ResourceMapping resourceMapping, boolean idsMustExist)
63: throws SearchEngineException {
64: Mapping[] pMappings = resourceMapping.getResourceIdMappings();
65: Property[] ids = new Property[pMappings.length];
66: for (int i = 0; i < pMappings.length; i++) {
67: ids[i] = resource.getProperty(pMappings[i].getPath()
68: .getPath());
69: if (ids[i] == null) {
70: if (!idsMustExist) {
71: return null;
72: }
73: throw new SearchEngineException("Id with path ["
74: + pMappings[i].getPath().getPath()
75: + "] for alias [" + resource.getAlias()
76: + "] not found");
77: }
78: if (!ids[i].isIndexed() || ids[i].isTokenized()
79: || !ids[i].isStored()) {
80: throw new SearchEngineException("Id ["
81: + ids[i].getName() + "] for alias ["
82: + resource.getAlias()
83: + "] must be stored and un_tokenized");
84: }
85: }
86: return ids;
87: }
88: }
|