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.lucene;
018:
019: import java.io.Reader;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.compass.core.Property;
026: import org.compass.core.Resource;
027: import org.compass.core.engine.SearchEngineException;
028: import org.compass.core.lucene.engine.LuceneSearchEngineFactory;
029: import org.compass.core.spi.MultiResource;
030: import org.compass.core.spi.ResourceKey;
031: import org.compass.core.util.StringUtils;
032:
033: /**
034: * @author kimchy
035: */
036: public class LuceneMultiResource implements MultiResource,
037: Map<String, Property[]> {
038:
039: private LuceneResource currentResource;
040:
041: private ArrayList<Resource> resources = new ArrayList<Resource>();
042:
043: private String alias;
044:
045: private transient LuceneSearchEngineFactory searchEngineFactory;
046:
047: public LuceneMultiResource(String alias,
048: LuceneSearchEngineFactory searchEngineFactory) {
049: this .alias = alias;
050: this .searchEngineFactory = searchEngineFactory;
051: currentResource = new LuceneResource(alias, searchEngineFactory);
052: resources.add(currentResource);
053: }
054:
055: // MultiResource inteface
056:
057: public int size() {
058: return resources.size();
059: }
060:
061: public Resource currentResource() {
062: return currentResource;
063: }
064:
065: public ResourceKey resourceKey() {
066: return currentResource.resourceKey();
067: }
068:
069: public String getSubIndex() {
070: return currentResource.getSubIndex();
071: }
072:
073: public void addResource() {
074: currentResource = new LuceneResource(alias, searchEngineFactory);
075: resources.add(currentResource);
076: }
077:
078: public Resource resource(int i) {
079: return resources.get(i);
080: }
081:
082: public void clear() {
083: resources.clear();
084: currentResource = null;
085: }
086:
087: // Resource interfaces
088:
089: public String getAlias() {
090: return currentResource.getAlias();
091: }
092:
093: public String getUID() {
094: return currentResource.getUID();
095: }
096:
097: public String getId() {
098: return currentResource.getId();
099: }
100:
101: public String[] getIds() {
102: return currentResource.getIds();
103: }
104:
105: public Property getIdProperty() {
106: return currentResource.getIdProperty();
107: }
108:
109: public Property[] getIdProperties() {
110: return currentResource.getIdProperties();
111: }
112:
113: public String getValue(String name) {
114: return currentResource.getValue(name);
115: }
116:
117: public Object getObject(String name) {
118: return currentResource.getObject(name);
119: }
120:
121: public String[] getValues(String name) {
122: return currentResource.getValues(name);
123: }
124:
125: public Resource addProperty(String name, Object value)
126: throws SearchEngineException {
127: currentResource.addProperty(name, value);
128: return this ;
129: }
130:
131: public Resource addProperty(String name, Reader value)
132: throws SearchEngineException {
133: currentResource.addProperty(name, value);
134: return this ;
135: }
136:
137: public Resource addProperty(Property property) {
138: currentResource.addProperty(property);
139: return this ;
140: }
141:
142: public Resource setProperty(String name, Object value)
143: throws SearchEngineException {
144: currentResource.setProperty(name, value);
145: return this ;
146: }
147:
148: public Resource setProperty(String name, Reader value)
149: throws SearchEngineException {
150: currentResource.setProperty(name, value);
151: return this ;
152: }
153:
154: public Resource setProperty(Property property) {
155: currentResource.setProperty(property);
156: return this ;
157: }
158:
159: public Resource removeProperty(String name) {
160: currentResource.removeProperty(name);
161: return this ;
162: }
163:
164: public Resource removeProperties(String name) {
165: currentResource.removeProperties(name);
166: return this ;
167: }
168:
169: public Property getProperty(String name) {
170: return currentResource.getProperty(name);
171: }
172:
173: public Property[] getProperties(String name) {
174: return currentResource.getProperties(name);
175: }
176:
177: public Property[] getProperties() {
178: return currentResource.getProperties();
179: }
180:
181: public float getBoost() {
182: return currentResource.getBoost();
183: }
184:
185: public Resource setBoost(float boost) {
186: currentResource.setBoost(boost);
187: return this ;
188: }
189:
190: public void addUID() {
191: currentResource.addUID();
192: }
193:
194: public void copy(Resource resource) {
195: clear();
196: if (resource instanceof MultiResource) {
197: MultiResource multiResource = (MultiResource) resource;
198: for (int i = 0; i < multiResource.size(); i++) {
199: addResource();
200: currentResource.copy(multiResource.resource(i));
201: }
202: } else {
203: currentResource = (LuceneResource) resource;
204: resources.add(resource);
205: }
206: }
207:
208: public String toString() {
209: if (resources.size() == 1) {
210: return resource(0).toString();
211: }
212: return StringUtils.collectionToCommaDelimitedString(resources);
213: }
214:
215: // methods from the map interface
216:
217: public boolean isEmpty() {
218: return currentResource.isEmpty();
219: }
220:
221: public boolean containsKey(Object key) {
222: return currentResource.containsKey(key);
223: }
224:
225: public boolean containsValue(Object value) {
226: return currentResource.containsValue(value);
227: }
228:
229: public Collection<Property[]> values() {
230: return currentResource.values();
231: }
232:
233: public void putAll(Map<? extends String, ? extends Property[]> t) {
234: currentResource.putAll(t);
235: }
236:
237: public Set<Map.Entry<String, Property[]>> entrySet() {
238: return currentResource.entrySet();
239: }
240:
241: public Set<String> keySet() {
242: return currentResource.keySet();
243: }
244:
245: public Property[] get(Object key) {
246: return currentResource.get(key);
247: }
248:
249: public Property[] remove(Object key) {
250: return currentResource.remove(key);
251: }
252:
253: public Property[] put(String key, Property[] value) {
254: return currentResource.put(key, value);
255: }
256: }
|