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.ArrayList;
20: import java.util.Iterator;
21:
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.AbstractResourceMapping;
26: import org.compass.core.mapping.CascadeMapping;
27: import org.compass.core.mapping.CompassMapping;
28: import org.compass.core.mapping.Mapping;
29: import org.compass.core.mapping.MappingException;
30: import org.compass.core.mapping.osem.AbstractCollectionMapping;
31:
32: /**
33: * Goes over all the {@link org.compass.core.mapping.ResourceMapping}s mappings and
34: * finds all the {@link org.compass.core.mapping.CascadeMapping} in order to set them
35: * at the resource mapping level {@link org.compass.core.mapping.ResourceMapping#getCascadeMappings()}.
36: *
37: * @author kimchy
38: */
39: public class CascadingMappingProcessor implements MappingProcessor {
40:
41: public CompassMapping process(CompassMapping compassMapping,
42: PropertyNamingStrategy namingStrategy,
43: ConverterLookup converterLookup, CompassSettings settings)
44: throws MappingException {
45:
46: for (Iterator compIt = compassMapping.mappingsIt(); compIt
47: .hasNext();) {
48: Object mapping = compIt.next();
49: if (!(mapping instanceof AbstractResourceMapping)) {
50: continue;
51: }
52: AbstractResourceMapping resourceMapping = (AbstractResourceMapping) mapping;
53: ArrayList cascades = new ArrayList();
54: for (Iterator it = resourceMapping.mappingsIt(); it
55: .hasNext();) {
56: Mapping m = (Mapping) it.next();
57: if (m instanceof CascadeMapping) {
58: cascades.add(m);
59: } else if (m instanceof AbstractCollectionMapping) {
60: // TODO maybe it is better for colleciton mapping to implement CascadeMapping? It might not apply to all its element mappings
61: AbstractCollectionMapping cm = (AbstractCollectionMapping) m;
62: if (cm.getElementMapping() instanceof CascadeMapping) {
63: cascades.add(cm.getElementMapping());
64: }
65: }
66: }
67: if (cascades.size() > 0) {
68: resourceMapping.setCascades((CascadeMapping[]) cascades
69: .toArray(new CascadeMapping[cascades.size()]));
70: } else {
71: resourceMapping.setCascades(null);
72: }
73: }
74:
75: return compassMapping;
76: }
77: }
|