KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > map > Relationship


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.map;
21
22 import java.io.Serializable JavaDoc;
23
24 import org.apache.commons.lang.builder.ToStringBuilder;
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.util.CayenneMapEntry;
27 import org.apache.cayenne.util.XMLSerializable;
28
29 /**
30  * Defines a relationship between two entities. In a DataMap graph relationships represent
31  * "arcs" connecting entity "nodes". Relationships are directional, i.e. they have a
32  * notion of source and target entity. This makes DataMap a "digraph".
33  */

34 public abstract class Relationship implements CayenneMapEntry, XMLSerializable,
35         Serializable JavaDoc {
36
37     protected String JavaDoc name;
38     protected Entity sourceEntity;
39
40     protected String JavaDoc targetEntityName;
41     protected boolean toMany;
42
43     /**
44      * Creates an unnamed relationship.
45      */

46     public Relationship() {
47     }
48
49     /**
50      * Creates a named relationship.
51      */

52     public Relationship(String JavaDoc name) {
53         setName(name);
54     }
55
56     public String JavaDoc getName() {
57         return name;
58     }
59
60     public void setName(String JavaDoc name) {
61         this.name = name;
62     }
63
64     /**
65      * Returns relationship source entity.
66      */

67     public Entity getSourceEntity() {
68         return sourceEntity;
69     }
70
71     /**
72      * Sets relationship source entity.
73      */

74     public void setSourceEntity(Entity sourceEntity) {
75         this.sourceEntity = sourceEntity;
76     }
77
78     /**
79      * Returns a target entity of the relationship.
80      */

81     public abstract Entity getTargetEntity();
82
83     /**
84      * Sets relationship target entity. Internally calls <code>setTargetEntityName</code>.
85      */

86     public void setTargetEntity(Entity targetEntity) {
87         if (targetEntity != null) {
88             setTargetEntityName(targetEntity.getName());
89         }
90         else {
91             setTargetEntityName(null);
92         }
93     }
94
95     /**
96      * Returns the name of a target entity.
97      */

98     public String JavaDoc getTargetEntityName() {
99         return targetEntityName;
100     }
101
102     /**
103      * Sets the name of relationship target entity.
104      */

105     public void setTargetEntityName(String JavaDoc targetEntityName) {
106         this.targetEntityName = targetEntityName;
107     }
108
109     /**
110      * Returns a boolean value that determines relationship multiplicity. This defines
111      * semantics of the connection between two nodes described by the source and target
112      * entities. E.g. to-many relationship between two Persistent object classes means
113      * that a source object would have a collection of target objects. This is a read-only
114      * property.
115      */

116     public boolean isToMany() {
117         return toMany;
118     }
119
120     public Object JavaDoc getParent() {
121         return getSourceEntity();
122     }
123
124     public void setParent(Object JavaDoc parent) {
125         if (parent != null && !(parent instanceof Entity)) {
126             throw new IllegalArgumentException JavaDoc("Expected null or Entity, got: " + parent);
127         }
128
129         setSourceEntity((Entity) parent);
130     }
131
132     /**
133      * Returns guaranteed non-null MappingNamespace of this relationship. If it happens to
134      * be null, and exception is thrown. This method is intended for internal use by
135      * Relationship class.
136      */

137     final MappingNamespace getNonNullNamespace() {
138         Entity entity = getSourceEntity();
139
140         if (entity == null) {
141             throw new CayenneRuntimeException("Relationship '"
142                     + getName()
143                     + "' has no parent Entity.");
144         }
145
146         return entity.getNonNullNamespace();
147     }
148
149     /**
150      * Overrides Object.toString() to return informative description.
151      */

152     public String JavaDoc toString() {
153         return new ToStringBuilder(this).append("name", getName()).append(
154                 "toMany",
155                 isToMany()).toString();
156     }
157 }
158
Popular Tags