01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.core.converter.extended;
18:
19: import org.compass.core.Property;
20: import org.compass.core.Resource;
21: import org.compass.core.ResourceFactory;
22: import org.compass.core.converter.ConversionException;
23: import org.compass.core.converter.Converter;
24: import org.compass.core.mapping.Mapping;
25: import org.compass.core.mapping.ResourcePropertyMapping;
26: import org.compass.core.marshall.MarshallingContext;
27:
28: /**
29: * @author kimchy
30: */
31: public class PrimitiveByteArrayConverter implements Converter {
32:
33: public boolean marshall(Resource resource, Object root,
34: Mapping mapping, MarshallingContext context)
35: throws ConversionException {
36:
37: ResourcePropertyMapping resourcePropertyMapping = (ResourcePropertyMapping) mapping;
38: ResourceFactory resourceFactory = context.getResourceFactory();
39:
40: // don't save a null value if the context does not states so
41: if (root == null && !handleNulls(context)) {
42: return false;
43: }
44:
45: String propertyName = resourcePropertyMapping.getPath()
46: .getPath();
47: byte[] value = (byte[]) root;
48: Property p = resourceFactory.createProperty(propertyName,
49: value, resourcePropertyMapping.getStore());
50: p.setBoost(resourcePropertyMapping.getBoost());
51: resource.addProperty(p);
52:
53: return resourcePropertyMapping.getStore() != Property.Store.NO;
54: }
55:
56: public Object unmarshall(Resource resource, Mapping mapping,
57: MarshallingContext context) throws ConversionException {
58: ResourcePropertyMapping resourcePropertyMapping = (ResourcePropertyMapping) mapping;
59:
60: if (resourcePropertyMapping.getStore() == Property.Store.NO) {
61: // it is not stored, so don't bother with converting it
62: return null;
63: }
64:
65: String propertyName = resourcePropertyMapping.getPath()
66: .getPath();
67: Property p = resource.getProperty(propertyName);
68:
69: // don't set anything if null
70: if (p == null) {
71: return null;
72: }
73:
74: return p.getBinaryValue();
75: }
76:
77: /**
78: * Should the converter handle nulls? Handling nulls means should the
79: * converter process nulls or not. Usually the converter will not
80: * persist null values, but sometimes it might be needed
81: * ({@link org.compass.core.marshall.MarshallingContext#handleNulls()}).
82: * <p/>
83: * Extracted to a method so special converters can control null handling.
84: *
85: * @param context
86: * @return <code>true</code> if the converter should handle null values
87: */
88: protected boolean handleNulls(MarshallingContext context) {
89: return context.handleNulls();
90: }
91:
92: }
|