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 java.util.HashMap;
020: import java.util.Map;
021:
022: import org.compass.core.ResourceFactory;
023: import org.compass.core.converter.ConverterLookup;
024: import org.compass.core.engine.SearchEngine;
025: import org.compass.core.engine.naming.PropertyNamingStrategy;
026: import org.compass.core.engine.naming.PropertyPath;
027: import org.compass.core.mapping.CompassMapping;
028: import org.compass.core.spi.InternalCompassSession;
029: import org.compass.core.spi.ResourceKey;
030:
031: /**
032: * @author kimchy
033: */
034: public class DefaultMarshallingContext implements MarshallingContext {
035:
036: private static final Object nullValue = new Object();
037:
038: private CompassMapping mapping;
039:
040: private SearchEngine searchEngine;
041:
042: private ConverterLookup converterLookup;
043:
044: private InternalCompassSession session;
045:
046: private MarshallingStrategy marshallingStrategy;
047:
048: private PropertyNamingStrategy propertyNamingStrategy;
049:
050: private Map<Object, Object> attributes = new HashMap<Object, Object>();
051:
052: private Map<PropertyPath, Object> nullValuesPath = new HashMap<PropertyPath, Object>();
053:
054: private Map<ResourceKey, Object> unmarshalled = new HashMap<ResourceKey, Object>();
055:
056: private Map<Object, Object> marshalled = new HashMap<Object, Object>();
057:
058: public DefaultMarshallingContext(CompassMapping mapping,
059: SearchEngine searchEngine, ConverterLookup converterLookup,
060: InternalCompassSession session,
061: MarshallingStrategy marshallingStrategy) {
062: this .mapping = mapping;
063: this .searchEngine = searchEngine;
064: this .converterLookup = converterLookup;
065: this .session = session;
066: this .marshallingStrategy = marshallingStrategy;
067: this .propertyNamingStrategy = session.getCompass()
068: .getPropertyNamingStrategy();
069: }
070:
071: public void clearContext() {
072: this .attributes.clear();
073: this .nullValuesPath.clear();
074: this .unmarshalled.clear();
075: this .marshalled.clear();
076: }
077:
078: public void setUnmarshalled(ResourceKey key, Object obj) {
079: unmarshalled.put(key, obj);
080: }
081:
082: public Object getUnmarshalled(ResourceKey key) {
083: Object obj = session.getFirstLevelCache().get(key);
084: if (obj != null) {
085: return obj;
086: }
087: return unmarshalled.get(key);
088: }
089:
090: public void setMarshalled(Object key, Object value) {
091: marshalled.put(key, value);
092: }
093:
094: public Object getMarshalled(Object key) {
095: return marshalled.get(key);
096: }
097:
098: public void setHandleNulls(PropertyPath path) {
099: nullValuesPath.put(path, nullValue);
100: }
101:
102: public void removeHandleNulls(PropertyPath path) {
103: nullValuesPath.remove(path);
104: }
105:
106: public boolean handleNulls() {
107: return nullValuesPath.size() > 0;
108: }
109:
110: public ConverterLookup getConverterLookup() {
111: return converterLookup;
112: }
113:
114: public ResourceFactory getResourceFactory() {
115: return searchEngine.getSearchEngineFactory()
116: .getResourceFactory();
117: }
118:
119: public SearchEngine getSearchEngine() {
120: return searchEngine;
121: }
122:
123: public CompassMapping getCompassMapping() {
124: return mapping;
125: }
126:
127: public InternalCompassSession getSession() {
128: return session;
129: }
130:
131: public MarshallingStrategy getMarshallingStrategy() {
132: return marshallingStrategy;
133: }
134:
135: public Object getAttribute(Object key) {
136: return attributes.get(key);
137: }
138:
139: public void setAttribute(Object key, Object value) {
140: attributes.put(key, value);
141: }
142:
143: public Object removeAttribute(Object key) {
144: return attributes.remove(key);
145: }
146:
147: public boolean hasAttribute(Object key) {
148: return attributes.containsKey(key);
149: }
150:
151: public Map<Object, Object> removeAttributes() {
152: Map<Object, Object> retAttributes = attributes;
153: this .attributes = new HashMap<Object, Object>();
154: return retAttributes;
155: }
156:
157: public void restoreAttributes(Map<Object, Object> attributes) {
158: this .attributes = attributes;
159: }
160:
161: public PropertyNamingStrategy getPropertyNamingStrategy() {
162: return propertyNamingStrategy;
163: }
164: }
|