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.config.process;
18:
19: import java.util.Iterator;
20:
21: import org.compass.core.config.CompassEnvironment;
22: import org.compass.core.config.CompassSettings;
23: import org.compass.core.converter.ConverterLookup;
24: import org.compass.core.engine.naming.PropertyNamingStrategy;
25: import org.compass.core.mapping.CompassMapping;
26: import org.compass.core.mapping.Mapping;
27: import org.compass.core.mapping.MappingException;
28: import org.compass.core.mapping.MultipleMapping;
29: import org.compass.core.mapping.internal.InternalResourcePropertyMapping;
30:
31: /**
32: * <p>Goes through each {@link org.compass.core.mapping.osem.ClassMapping} and handles its
33: * null value definition. If {@link org.compass.core.config.CompassEnvironment.NullValue#NULL_VALUE}
34: * is set, will use it to set all unset null values. If it is not set, will leave the null
35: * values as is.
36: *
37: * <p>In case it is set, then will disable null value for mappings that have null value of
38: * {@link org.compass.core.config.CompassEnvironment.NullValue#DISABLE_NULL_VALUE_FOR_MAPPING}
39: * value.
40: *
41: * @author kimchy
42: */
43: public class NullValueMappingProcessor implements MappingProcessor {
44:
45: private String globalNullValue;
46:
47: public CompassMapping process(CompassMapping compassMapping,
48: PropertyNamingStrategy namingStrategy,
49: ConverterLookup converterLookup, CompassSettings settings)
50: throws MappingException {
51:
52: globalNullValue = settings
53: .getSetting(CompassEnvironment.NullValue.NULL_VALUE);
54:
55: for (Iterator rIt = compassMapping.mappingsIt(); rIt.hasNext();) {
56: Mapping mapping = (Mapping) rIt.next();
57: processMapping(mapping);
58: }
59: return compassMapping;
60: }
61:
62: private void processMapping(Mapping mapping)
63: throws MappingException {
64: if ((mapping instanceof InternalResourcePropertyMapping)) {
65: InternalResourcePropertyMapping resourcePropertyMapping = (InternalResourcePropertyMapping) mapping;
66: if (!resourcePropertyMapping.hasNullValue()) {
67: resourcePropertyMapping.setNullValue(globalNullValue);
68: } else if (CompassEnvironment.NullValue.DISABLE_NULL_VALUE_FOR_MAPPING
69: .equals(resourcePropertyMapping.getNullValue())) {
70: resourcePropertyMapping.setNullValue(null);
71: }
72: }
73:
74: if (mapping instanceof MultipleMapping) {
75: MultipleMapping multipleMapping = (MultipleMapping) mapping;
76: for (Iterator it = multipleMapping.mappingsIt(); it
77: .hasNext();) {
78: processMapping((Mapping) it.next());
79: }
80: }
81: }
82: }
|