001: package com.bm.introspectors.relations;
002:
003: import java.util.Set;
004:
005: import javax.persistence.CascadeType;
006: import javax.persistence.FetchType;
007:
008: import com.bm.introspectors.Property;
009: import com.bm.utils.Ejb3Utils;
010:
011: /**
012: * Abstract class for representation of relations.
013: *
014: * @author Daniel Wiese
015: */
016: public abstract class AbstractRelation implements EntityReleationInfo {
017:
018: private final Class sourceClass;
019:
020: private final Class targetClass;
021:
022: private final Property sourceProperty;
023:
024: private final Property targetProperty;
025:
026: private final FetchType fetchType;
027:
028: private final boolean isUnidirectional;
029:
030: private final CascadeType[] cascadeType;
031:
032: /**
033: * Default constructor.
034: *
035: * @param sourceClass -
036: * the type of the source entity bean
037: * @param targetClass -
038: * the type of the target entity bean
039: * @param sourceProperty -
040: * the property of the source entity bean
041: * @param targetProperty -
042: * the property of the target entity bean
043: * @param fetchType -
044: * fetch type
045: * @param cascadeType -
046: * cascade type
047: *
048: */
049: public AbstractRelation(Class sourceClass, Class targetClass,
050: Property sourceProperty, Property targetProperty,
051: FetchType fetchType, CascadeType[] cascadeType) {
052: this .sourceClass = sourceClass;
053: this .targetClass = targetClass;
054: this .targetProperty = targetProperty;
055: this .sourceProperty = sourceProperty;
056: this .fetchType = fetchType;
057: this .cascadeType = cascadeType;
058: this .isUnidirectional = (targetProperty == null);
059: }
060:
061: /**
062: * Returns the cascadeType.
063: * @return Returns the cascadeType.
064: */
065: public CascadeType[] getCascadeType() {
066: return cascadeType;
067: }
068:
069: /**
070: * Returns the fetchType.
071: * @return Returns the fetchType.
072: */
073: public FetchType getFetchType() {
074: return fetchType;
075: }
076:
077: /**
078: * Returns the sourceClass.
079: *
080: * @return Returns the sourceClass.
081: */
082: public Class getSourceClass() {
083: return this .sourceClass;
084: }
085:
086: /**
087: * Returns the sourceProperty.
088: *
089: * @return Returns the sourceProperty.
090: */
091: public Property getSourceProperty() {
092: return this .sourceProperty;
093: }
094:
095: /**
096: * Returns the targetClass.
097: *
098: * @return Returns the targetClass.
099: */
100: public Class getTargetClass() {
101: return this .targetClass;
102: }
103:
104: /**
105: * Returns the targetProperty.
106: *
107: * @return Returns the targetProperty.
108: */
109: public Property getTargetProperty() {
110: return this .targetProperty;
111: }
112:
113: /**
114: * If the preperty is unidirectional.
115: * @return the isUnidirectional
116: */
117: public boolean isUnidirectional() {
118: return isUnidirectional;
119: }
120:
121: /**
122: *{@inheritDoc}
123: */
124: @Override
125: public String toString() {
126: final StringBuilder sb = new StringBuilder();
127: sb.append("[" + this .getReleationType() + "] ").append(
128: "Source class: ").append(
129: Ejb3Utils.getShortClassName(this .sourceClass)).append(
130: "\n");
131: sb.append("Source attrib: ").append(
132: this .sourceProperty.getName()).append("\n");
133: sb.append("Target class: ").append(
134: Ejb3Utils.getShortClassName(this .targetClass)).append(
135: "\n");
136: sb.append("Target attrib: ").append(
137: this .targetProperty.getName());
138: return sb.toString();
139: }
140:
141: /**
142: * True wenn the delete operatio is cascading.
143: * @return when the delete operation is cascading
144: */
145: public boolean isCascadeOnDelete() {
146: boolean back = false;
147: if (this .cascadeType != null) {
148: for (CascadeType current : this .cascadeType) {
149: if ((current.equals(CascadeType.ALL))
150: || (current.equals(CascadeType.REMOVE))) {
151: back = true;
152: break;
153: }
154: }
155: }
156:
157: return back;
158: }
159:
160: /**
161: * Returns the primary key property (or properties, in case of a composite key) for the
162: * class that is target of the relation
163: * @return primary key property / properties.
164: */
165: public Set<Property> getTargetKeyProperty() {
166: return null;
167: }
168:
169: }
|