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.osem;
018:
019: import java.util.Map;
020:
021: import org.compass.core.Property;
022: import org.compass.core.Resource;
023: import org.compass.core.ResourceFactory;
024: import org.compass.core.converter.ConversionException;
025: import org.compass.core.mapping.Mapping;
026: import org.compass.core.mapping.osem.ClassMapping;
027: import org.compass.core.mapping.osem.HasRefAliasMapping;
028: import org.compass.core.mapping.osem.ReferenceMapping;
029: import org.compass.core.marshall.MarshallingContext;
030: import org.compass.core.marshall.MarshallingEnvironment;
031:
032: /**
033: * @author kimchy
034: */
035: public class ReferenceMappingConverter extends
036: AbstractRefAliasMappingConverter {
037:
038: private static final Object DISABLE_UID_MARK = new Object();
039:
040: protected boolean doMarshall(Resource resource, Object root,
041: HasRefAliasMapping hasRefAliasMapping,
042: ClassMapping refMapping, MarshallingContext context)
043: throws ConversionException {
044: Object current = context
045: .getAttribute(MarshallingEnvironment.ATTRIBUTE_CURRENT);
046:
047: ReferenceMapping referenceMapping = (ReferenceMapping) hasRefAliasMapping;
048:
049: // handle null values when needed
050: if (root == null) {
051: if (!refMapping.isSupportUnmarshall()) {
052: return false;
053: }
054: if (!context.handleNulls()) {
055: return false;
056: }
057: Mapping[] ids = refMapping.getResourceIdMappings();
058: boolean store = false;
059: for (Mapping id : ids) {
060: store |= id.getConverter().marshall(resource,
061: context.getResourceFactory().getNullValue(),
062: id, context);
063: }
064: return store;
065: }
066:
067: // only add specilized properties for un-marshalling when it is supported
068: if (refMapping.isSupportUnmarshall()) {
069: ResourceFactory resourceFactory = context
070: .getResourceFactory();
071: if (refMapping.isPoly()
072: && refMapping.getPolyClass() == null) {
073: // if the class is defined as poly, persist the class name as well
074: String className = root.getClass().getName();
075: Property p = resourceFactory
076: .createProperty(refMapping.getClassPath()
077: .getPath(), className,
078: Property.Store.YES,
079: Property.Index.UN_TOKENIZED);
080: p.setOmitNorms(true);
081: resource.addProperty(p);
082: }
083: }
084: context.setAttribute(
085: ClassMappingConverter.DISABLE_UID_MARSHALLING,
086: DISABLE_UID_MARK);
087: boolean stored = context.getMarshallingStrategy().marshallIds(
088: resource, refMapping, root, context);
089: context
090: .removeAttribute(ClassMappingConverter.DISABLE_UID_MARSHALLING);
091:
092: if (referenceMapping.getRefCompMapping() != null) {
093: context.setAttribute(
094: MarshallingEnvironment.ATTRIBUTE_PARENT, current);
095: stored |= referenceMapping.getRefCompMapping()
096: .getConverter().marshall(resource, root,
097: referenceMapping.getRefCompMapping(),
098: context);
099: }
100: return stored;
101: }
102:
103: protected Object doUnmarshall(Resource resource,
104: HasRefAliasMapping hasRefAliasMapping,
105: ClassMapping refMapping, MarshallingContext context)
106: throws ConversionException {
107: Object[] ids = context.getMarshallingStrategy().unmarshallIds(
108: refMapping, resource, context);
109: if (ids == null) {
110: // the reference was not marshalled
111: return null;
112: }
113: Map<Object, Object> attributes = context.removeAttributes();
114: Object retVal = context.getSession().get(refMapping.getAlias(),
115: ids, context);
116: context.restoreAttributes(attributes);
117: return retVal;
118: }
119:
120: protected boolean rollbackClassNameOnPoly() {
121: return false;
122: }
123: }
|