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.mapping.osem;
018:
019: import java.util.HashMap;
020:
021: import org.compass.core.CompassException;
022: import org.compass.core.mapping.CascadeMapping;
023:
024: /**
025: * @author kimchy
026: */
027: public abstract class AbstractRefAliasMapping extends
028: AbstractAccessorMapping implements HasRefAliasMapping,
029: CascadeMapping {
030:
031: private String[] refAliases;
032:
033: private ClassMapping[] refClassMappings;
034:
035: private Class refClass;
036:
037: private HashMap<String, ClassMapping> refAliasesMap = new HashMap<String, ClassMapping>();
038:
039: private Cascade[] cascades;
040:
041: private Boolean shouldCascadeDelete;
042: private Boolean shouldCascadeCreate;
043: private Boolean shouldCascadeSave;
044:
045: protected void copy(AbstractRefAliasMapping mapping) {
046: super .copy(mapping);
047: mapping.setCascades(cascades);
048: if (refAliases != null) {
049: String[] copyRefAliases = new String[refAliases.length];
050: System.arraycopy(refAliases, 0, copyRefAliases, 0,
051: refAliases.length);
052: mapping.setRefAliases(copyRefAliases);
053: }
054: mapping.setRefClass(getRefClass());
055: if (refClassMappings != null) {
056: ClassMapping[] copyRefClassMappings = new ClassMapping[refClassMappings.length];
057: System.arraycopy(refClassMappings, 0, copyRefClassMappings,
058: 0, refClassMappings.length);
059: mapping.setRefClassMappings(copyRefClassMappings);
060: }
061: }
062:
063: public boolean hasRefAlias(String refAlias) {
064: return refAliasesMap.get(refAlias) != null;
065: }
066:
067: public boolean hasAtLeastOneRefAlias(String[] refAliases) {
068: if (refAliases == null) {
069: return false;
070: }
071: for (String refAlias : refAliases) {
072: if (hasRefAlias(refAlias)) {
073: return true;
074: }
075: }
076: return false;
077: }
078:
079: public ClassMapping getRefClassMapping(String alias) {
080: return refAliasesMap.get(alias);
081: }
082:
083: public String[] getRefAliases() {
084: return refAliases;
085: }
086:
087: public void setRefAliases(String[] refAliases) {
088: this .refAliases = refAliases;
089: }
090:
091: public ClassMapping[] getRefClassMappings() {
092: return refClassMappings;
093: }
094:
095: public void setRefClassMappings(ClassMapping[] refClassMappings) {
096: this .refClassMappings = refClassMappings;
097: if (refClassMappings != null) {
098: for (ClassMapping refClassMapping : refClassMappings) {
099: refAliasesMap.put(refClassMapping.getAlias(),
100: refClassMapping);
101: }
102: }
103: }
104:
105: public Class getRefClass() {
106: return refClass;
107: }
108:
109: public void setRefClass(Class refClass) {
110: this .refClass = refClass;
111: }
112:
113: public Cascade[] getCascades() {
114: return cascades;
115: }
116:
117: public void setCascades(Cascade[] cascades) {
118: this .cascades = cascades;
119: }
120:
121: public Object getCascadeValue(Object root) throws CompassException {
122: return getGetter().get(root);
123: }
124:
125: public boolean shouldCascadeDelete() {
126: if (cascades == null) {
127: return false;
128: }
129: if (shouldCascadeDelete != null) {
130: return shouldCascadeDelete;
131: }
132: for (Cascade cascade : cascades) {
133: if (cascade == Cascade.DELETE || cascade == Cascade.ALL) {
134: shouldCascadeDelete = Boolean.TRUE;
135: }
136: }
137: if (shouldCascadeDelete == null) {
138: shouldCascadeDelete = Boolean.FALSE;
139: }
140: return shouldCascadeDelete;
141: }
142:
143: public boolean shouldCascadeCreate() {
144: if (cascades == null) {
145: return false;
146: }
147: if (shouldCascadeCreate != null) {
148: return shouldCascadeCreate;
149: }
150: for (Cascade cascade : cascades) {
151: if (cascade == Cascade.CREATE || cascade == Cascade.ALL) {
152: shouldCascadeCreate = Boolean.TRUE;
153: }
154: }
155: if (shouldCascadeCreate == null) {
156: shouldCascadeCreate = Boolean.FALSE;
157: }
158: return shouldCascadeCreate;
159: }
160:
161: public boolean shouldCascadeSave() {
162: if (cascades == null) {
163: return false;
164: }
165: if (shouldCascadeSave != null) {
166: return shouldCascadeSave;
167: }
168: for (Cascade cascade : cascades) {
169: if (cascade == Cascade.SAVE || cascade == Cascade.ALL) {
170: shouldCascadeSave = Boolean.TRUE;
171: }
172: }
173: if (shouldCascadeSave == null) {
174: shouldCascadeSave = Boolean.FALSE;
175: }
176: return shouldCascadeSave;
177: }
178:
179: public boolean shouldCascade(Cascade cascade) {
180: if (cascades == null || cascades.length == 0) {
181: return false;
182: }
183: if (cascade == Cascade.ALL) {
184: // if we pass ALL, it means that any cascading is enough (since cascades is not null, there is at least one)
185: return true;
186: } else if (cascade == Cascade.CREATE) {
187: return shouldCascadeCreate();
188: } else if (cascade == Cascade.SAVE) {
189: return shouldCascadeSave();
190: } else if (cascade == Cascade.DELETE) {
191: return shouldCascadeDelete();
192: } else {
193: throw new IllegalArgumentException(
194: "Should cascade can't handle [" + cascade + "]");
195: }
196: }
197: }
|