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 org.compass.core.CompassException;
020: import org.compass.core.mapping.CascadeMapping;
021: import org.compass.core.mapping.Mapping;
022:
023: /**
024: * @author kimchy
025: */
026: public class ParentMapping extends AbstractAccessorMapping implements
027: CascadeMapping {
028:
029: private Cascade[] cascades;
030:
031: private Boolean shouldCascadeDelete;
032: private Boolean shouldCascadeCreate;
033: private Boolean shouldCascadeSave;
034:
035: public Mapping copy() {
036: ParentMapping copy = new ParentMapping();
037: super .copy(copy);
038: copy.setName(getName());
039: copy.setSetter(getSetter());
040: copy.setGetter(getGetter());
041: copy.setCascades(cascades);
042: return copy;
043: }
044:
045: public boolean canBeCollectionWrapped() {
046: return false;
047: }
048:
049: public boolean controlsObjectNullability() {
050: return false;
051: }
052:
053: public Cascade[] getCascades() {
054: return cascades;
055: }
056:
057: public void setCascades(Cascade[] cascades) {
058: this .cascades = cascades;
059: }
060:
061: public Object getCascadeValue(Object root) throws CompassException {
062: return getGetter().get(root);
063: }
064:
065: public boolean shouldCascadeDelete() {
066: if (cascades == null) {
067: return false;
068: }
069: if (shouldCascadeDelete != null) {
070: return shouldCascadeDelete;
071: }
072: for (Cascade cascade : cascades) {
073: if (cascade == Cascade.DELETE || cascade == Cascade.ALL) {
074: shouldCascadeDelete = Boolean.TRUE;
075: }
076: }
077: if (shouldCascadeDelete == null) {
078: shouldCascadeDelete = Boolean.FALSE;
079: }
080: return shouldCascadeDelete;
081: }
082:
083: public boolean shouldCascadeCreate() {
084: if (cascades == null) {
085: return false;
086: }
087: if (shouldCascadeCreate != null) {
088: return shouldCascadeCreate;
089: }
090: for (Cascade cascade : cascades) {
091: if (cascade == Cascade.CREATE || cascade == Cascade.ALL) {
092: shouldCascadeCreate = Boolean.TRUE;
093: }
094: }
095: if (shouldCascadeCreate == null) {
096: shouldCascadeCreate = Boolean.FALSE;
097: }
098: return shouldCascadeCreate;
099: }
100:
101: public boolean shouldCascadeSave() {
102: if (cascades == null) {
103: return false;
104: }
105: if (shouldCascadeSave != null) {
106: return shouldCascadeSave;
107: }
108: for (Cascade cascade : cascades) {
109: if (cascade == Cascade.SAVE || cascade == Cascade.ALL) {
110: shouldCascadeSave = Boolean.TRUE;
111: }
112: }
113: if (shouldCascadeSave == null) {
114: shouldCascadeSave = Boolean.FALSE;
115: }
116: return shouldCascadeSave;
117: }
118:
119: public boolean shouldCascade(Cascade cascade) {
120: if (cascades == null || cascades.length == 0) {
121: return false;
122: }
123: if (cascade == Cascade.ALL) {
124: // if we pass ALL, it means that any cascading is enough (since cascades is not null, there is at least one)
125: return true;
126: } else if (cascade == Cascade.CREATE) {
127: return shouldCascadeCreate();
128: } else if (cascade == Cascade.SAVE) {
129: return shouldCascadeSave();
130: } else if (cascade == Cascade.DELETE) {
131: return shouldCascadeDelete();
132: } else {
133: throw new IllegalArgumentException(
134: "Should cascade can't handle [" + cascade + "]");
135: }
136: }
137: }
|