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.marshall;
018:
019: import org.compass.core.CompassException;
020: import org.compass.core.Resource;
021: import org.compass.core.ResourceFactory;
022: import org.compass.core.accessor.Setter;
023: import org.compass.core.converter.ConverterLookup;
024: import org.compass.core.converter.mapping.ResourceMappingConverter;
025: import org.compass.core.engine.SearchEngine;
026: import org.compass.core.mapping.CompassMapping;
027: import org.compass.core.mapping.Mapping;
028: import org.compass.core.mapping.ResourceMapping;
029: import org.compass.core.mapping.osem.ObjectMapping;
030: import org.compass.core.spi.AliasedObject;
031: import org.compass.core.spi.InternalCompassSession;
032:
033: /**
034: * @author kimchy
035: */
036: public class DefaultMarshallingStrategy implements MarshallingStrategy {
037:
038: private CompassMapping mapping;
039:
040: private SearchEngine searchEngine;
041:
042: private ConverterLookup converterLookup;
043:
044: private InternalCompassSession session;
045:
046: private ResourceFactory resourceFactory;
047:
048: public DefaultMarshallingStrategy(CompassMapping mapping,
049: SearchEngine searchEngine, ConverterLookup converterLookup,
050: InternalCompassSession session) {
051: this .mapping = mapping;
052: this .searchEngine = searchEngine;
053: this .converterLookup = converterLookup;
054: this .session = session;
055: this .resourceFactory = session.getCompass()
056: .getResourceFactory();
057: }
058:
059: public Resource marshallIds(Object id) {
060: if (id instanceof AliasedObject) {
061: return marshallIds(((AliasedObject) id).getAlias(), id);
062: }
063: return marshallIds(id.getClass(), id);
064: }
065:
066: public Resource marshallIds(String alias, Object id) {
067: ResourceMapping resourceMapping = mapping
068: .getRootMappingByAlias(alias);
069: if (resourceMapping == null) {
070: return null;
071: }
072: return marshallIds(resourceMapping, id);
073: }
074:
075: public Resource marshallIds(Class clazz, Object id) {
076: ResourceMapping resourceMapping = mapping
077: .getRootMappingByClass(clazz);
078: if (resourceMapping == null) {
079: return null;
080: }
081: return marshallIds(resourceMapping, id);
082: }
083:
084: public Resource marshallIds(ResourceMapping resourceMapping,
085: Object id) {
086: Resource idResource = resourceFactory
087: .createResource(resourceMapping.getAlias());
088: marshallIds(idResource, resourceMapping, id, createContext());
089: return idResource;
090: }
091:
092: public boolean marshallIds(Resource resource,
093: ResourceMapping resourceMapping, Object id,
094: MarshallingContext context) {
095: return ((ResourceMappingConverter) resourceMapping
096: .getConverter()).marshallIds(resource, id,
097: resourceMapping, context);
098: }
099:
100: public void marshallIds(Object root, Object id) {
101: ResourceMapping resourceMapping = mapping
102: .getMappingByClass(root.getClass());
103: if (resourceMapping == null) {
104: throw new MarshallingException(
105: "No resource mapping is defined for class ["
106: + root.getClass() + "]");
107: }
108: Mapping[] ids = resourceMapping.getIdMappings();
109: if (ids == null || ids.length == 0) {
110: return;
111: }
112: Object[] idsValues = unmarshallIds(resourceMapping, id,
113: createContext());
114: for (int i = 0; i < idsValues.length; i++) {
115: setId(root, idsValues[i], (ObjectMapping) ids[i]);
116: }
117: }
118:
119: public void marshallIds(ResourceMapping resourceMapping,
120: Object root, Object id) {
121: Mapping[] ids = resourceMapping.getIdMappings();
122: if (ids.length == 0) {
123: return;
124: }
125: Object[] idsValues = unmarshallIds(resourceMapping, id,
126: createContext());
127: for (int i = 0; i < idsValues.length; i++) {
128: setId(root, idsValues[i], (ObjectMapping) ids[i]);
129: }
130: }
131:
132: private void setId(Object root, Object id, ObjectMapping mdMapping) {
133: Setter setter = mdMapping.getSetter();
134: setter.set(root, id);
135: }
136:
137: public Object[] unmarshallIds(String alias, Object id) {
138: ResourceMapping resourceMapping = mapping
139: .getRootMappingByAlias(alias);
140: return unmarshallIds(resourceMapping, id, createContext());
141: }
142:
143: public Object[] unmarshallIds(Class clazz, Object id) {
144: ResourceMapping resourceMapping = mapping
145: .findRootMappingByClass(clazz);
146: return unmarshallIds(resourceMapping, id, createContext());
147: }
148:
149: public Object[] unmarshallIds(ResourceMapping resourceMapping,
150: Object id, MarshallingContext context) {
151: return ((ResourceMappingConverter) resourceMapping
152: .getConverter()).unmarshallIds(id, resourceMapping,
153: context);
154: }
155:
156: public Resource marshall(String alias, Object root) {
157: ResourceMapping resourceMapping = mapping
158: .getRootMappingByAlias(alias);
159: if (resourceMapping == null) {
160: return null;
161: }
162: Resource resource = resourceFactory.createResource(alias);
163: resourceMapping.getConverter().marshall(resource, root,
164: resourceMapping, createContext());
165: return resource;
166: }
167:
168: public Resource marshall(Object root) {
169: if (root instanceof AliasedObject) {
170: return marshall(((AliasedObject) root).getAlias(), root);
171: }
172: ResourceMapping resourceMapping = mapping
173: .getRootMappingByClass(root.getClass());
174: if (resourceMapping == null) {
175: return null;
176: }
177: Resource resource = resourceFactory
178: .createResource(resourceMapping.getAlias());
179: resourceMapping.getConverter().marshall(resource, root,
180: resourceMapping, createContext());
181: return resource;
182: }
183:
184: public Object unmarshall(Resource resource) throws CompassException {
185: return unmarshall(resource, createContext());
186: }
187:
188: public Object unmarshall(Resource resource,
189: MarshallingContext context) throws CompassException {
190: ResourceMapping resourceMapping = mapping
191: .getRootMappingByAlias(resource.getAlias());
192: if (resourceMapping == null) {
193: throw new MarshallingException(
194: "No mapping is defined for alias [ "
195: + resource.getAlias() + "]");
196: }
197: return resourceMapping.getConverter().unmarshall(resource,
198: resourceMapping, context);
199: }
200:
201: private MarshallingContext createContext() {
202: return new DefaultMarshallingContext(mapping, searchEngine,
203: converterLookup, session, this);
204: }
205: }
|