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.extended;
018:
019: import org.compass.core.Property;
020: import org.compass.core.Resource;
021: import org.compass.core.ResourceFactory;
022: import org.compass.core.converter.ConversionException;
023: import org.compass.core.converter.Converter;
024: import org.compass.core.mapping.Mapping;
025: import org.compass.core.mapping.ResourcePropertyMapping;
026: import org.compass.core.marshall.MarshallingContext;
027:
028: /**
029: * @author kimchy
030: */
031: public class ObjectByteArrayConverter implements Converter {
032:
033: public boolean marshall(Resource resource, Object root,
034: Mapping mapping, MarshallingContext context)
035: throws ConversionException {
036:
037: ResourcePropertyMapping resourcePropertyMapping = (ResourcePropertyMapping) mapping;
038: ResourceFactory resourceFactory = context.getResourceFactory();
039:
040: // don't save a null value if the context does not states so
041: if (root == null && !handleNulls(context)) {
042: return false;
043: }
044:
045: String propertyName = resourcePropertyMapping.getPath()
046: .getPath();
047: Byte[] oValue = (Byte[]) root;
048: byte value[] = new byte[oValue.length];
049: for (int i = 0; i < oValue.length; i++) {
050: value[i] = oValue[i].byteValue();
051: }
052: Property p = resourceFactory.createProperty(propertyName,
053: value, resourcePropertyMapping.getStore());
054: p.setBoost(resourcePropertyMapping.getBoost());
055: resource.addProperty(p);
056:
057: return resourcePropertyMapping.getStore() != Property.Store.NO;
058: }
059:
060: public Object unmarshall(Resource resource, Mapping mapping,
061: MarshallingContext context) throws ConversionException {
062: ResourcePropertyMapping resourcePropertyMapping = (ResourcePropertyMapping) mapping;
063:
064: if (resourcePropertyMapping.getStore() == Property.Store.NO) {
065: // it is not stored, so don't bother with converting it
066: return null;
067: }
068:
069: String propertyName = resourcePropertyMapping.getPath()
070: .getPath();
071: Property p = resource.getProperty(propertyName);
072:
073: // don't set anything if null
074: if (p == null) {
075: return null;
076: }
077:
078: byte[] value = p.getBinaryValue();
079: Byte[] oValue = new Byte[value.length];
080: for (int i = 0; i < value.length; i++) {
081: oValue[i] = new Byte(value[i]);
082: }
083: return oValue;
084: }
085:
086: /**
087: * Should the converter handle nulls? Handling nulls means should the
088: * converter process nulls or not. Usually the converter will not
089: * persist null values, but sometimes it might be needed
090: * ({@link org.compass.core.marshall.MarshallingContext#handleNulls()}).
091: * <p/>
092: * Extracted to a method so special converters can control null handling.
093: *
094: * @param context
095: * @return <code>true</code> if the converter should handle null values
096: */
097: protected boolean handleNulls(MarshallingContext context) {
098: return context.handleNulls();
099: }
100: }
|