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.converter.mapping;
018:
019: import java.io.Reader;
020: import java.util.HashMap;
021:
022: import org.compass.core.Property;
023: import org.compass.core.Resource;
024: import org.compass.core.converter.ConversionException;
025: import org.compass.core.engine.SearchEngineException;
026:
027: /**
028: * @author kimchy
029: */
030: public class CollectionResourceWrapper implements Resource {
031:
032: public class PropertiesWrapper {
033:
034: public String name;
035:
036: public Property[] properties;
037:
038: public int counter;
039:
040: public int hashCode() {
041: return name.hashCode();
042: }
043:
044: public boolean equals(Object object) {
045: PropertiesWrapper copy = (PropertiesWrapper) object;
046: return name.equals(copy.name);
047: }
048: }
049:
050: private Resource resource;
051:
052: private HashMap propertiesMap = new HashMap();
053:
054: public CollectionResourceWrapper(Resource resource) {
055: this .resource = resource;
056: }
057:
058: public String getValue(String name) {
059: return computeProperty(name).getStringValue();
060: }
061:
062: public Object getObject(String name) {
063: return computeProperty(name).getObjectValue();
064: }
065:
066: public String[] getValues(String name) {
067: return resource.getValues(name);
068: }
069:
070: public String getAlias() {
071: return resource.getAlias();
072: }
073:
074: public String getId() {
075: throw new ConversionException("should not be called");
076: }
077:
078: public String getUID() {
079: throw new ConversionException("should not be called");
080: }
081:
082: public String[] getIds() {
083: throw new ConversionException("should not be called");
084: }
085:
086: public Property getIdProperty() {
087: throw new ConversionException("should not be called");
088: }
089:
090: public Property[] getIdProperties() {
091: throw new ConversionException("should not be called");
092: }
093:
094: public Resource addProperty(String name, Object value)
095: throws SearchEngineException {
096: throw new ConversionException("should not be called");
097: }
098:
099: public Resource addProperty(String name, Reader value)
100: throws SearchEngineException {
101: throw new ConversionException("should not be called");
102: }
103:
104: public Resource addProperty(Property property) {
105: throw new ConversionException("should not be called");
106: }
107:
108: public Resource removeProperty(String name) {
109: throw new ConversionException("should not be called");
110: }
111:
112: public Resource removeProperties(String name) {
113: throw new ConversionException("should not be called");
114: }
115:
116: public Resource setProperty(String name, Object value)
117: throws SearchEngineException {
118: throw new ConversionException("should not be called");
119: }
120:
121: public Resource setProperty(String name, Reader value)
122: throws SearchEngineException {
123: throw new ConversionException("should not be called");
124: }
125:
126: public Resource setProperty(Property property) {
127: throw new ConversionException("should not be called");
128: }
129:
130: public Property getProperty(String name) {
131: return computeProperty(name);
132: }
133:
134: private Property computeProperty(String name) {
135: PropertiesWrapper wrapper = (PropertiesWrapper) propertiesMap
136: .get(name);
137: if (wrapper == null) {
138: wrapper = new PropertiesWrapper();
139: wrapper.name = name;
140: wrapper.properties = getProperties(name);
141: propertiesMap.put(name, wrapper);
142: }
143:
144: if (wrapper.properties.length == 0) {
145: return null;
146: }
147:
148: if (wrapper.counter >= wrapper.properties.length) {
149: // we are asking for data that was not marshalled, return null
150: return null;
151: }
152: return wrapper.properties[wrapper.counter++];
153: }
154:
155: public void rollbackGetProperty(String name) {
156: PropertiesWrapper wrapper = (PropertiesWrapper) propertiesMap
157: .get(name);
158: if (wrapper == null) {
159: return;
160: }
161: if (wrapper.properties.length == 0) {
162: return;
163: }
164: wrapper.counter--;
165: }
166:
167: public Property[] getProperties(String name) {
168: return resource.getProperties(name);
169: }
170:
171: public Property[] getProperties() {
172: return null;
173: }
174:
175: public float getBoost() {
176: return resource.getBoost();
177: }
178:
179: public Resource setBoost(float boost) {
180: resource.setBoost(boost);
181: return this ;
182: }
183:
184: public void copy(Resource resource) {
185: this.resource.copy(resource);
186: }
187:
188: }
|