KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.lang.builder.ToStringBuilder;
23 import org.apache.cayenne.CayenneRuntimeException;
24 import org.apache.cayenne.util.XMLEncoder;
25 import org.apache.cayenne.util.XMLSerializable;
26
27 /**
28  * Defines a join between two attributes of a given relationship.
29  *
30  * @since 1.1
31  * @author Andrus Adamchik
32  */

33 public class DbJoin implements XMLSerializable {
34
35     protected DbRelationship relationship;
36     protected String JavaDoc sourceName;
37     protected String JavaDoc targetName;
38
39     protected DbJoin() {
40     }
41
42     public DbJoin(DbRelationship relationship) {
43         this.relationship = relationship;
44     }
45
46     public DbJoin(DbRelationship relationship, String JavaDoc sourceName, String JavaDoc targetName) {
47         this.relationship = relationship;
48         this.sourceName = sourceName;
49         this.targetName = targetName;
50     }
51
52     /**
53      * Returns a "reverse" join. Join source relationship is not set and must be
54      * initialized by the caller.
55      */

56     public DbJoin createReverseJoin() {
57         DbJoin reverse = new DbJoin();
58         reverse.setTargetName(sourceName);
59         reverse.setSourceName(targetName);
60         return reverse;
61     }
62
63     /**
64      * Returns DbAttribute on on the left side of the join.
65      */

66     public DbAttribute getSource() {
67         if (sourceName == null) {
68             return null;
69         }
70
71         Relationship r = getNonNullRelationship();
72         Entity entity = r.getSourceEntity();
73         if (entity == null) {
74             return null;
75         }
76
77         return (DbAttribute) entity.getAttribute(sourceName);
78     }
79
80     public DbAttribute getTarget() {
81         if (targetName == null) {
82             return null;
83         }
84
85         Relationship r = getNonNullRelationship();
86         Entity entity = r.getTargetEntity();
87         if (entity == null) {
88             return null;
89         }
90
91         return (DbAttribute) entity.getAttribute(targetName);
92     }
93
94     /**
95      * Prints itself as XML to the provided XMLEncoder.
96      */

97     public void encodeAsXML(XMLEncoder encoder) {
98         encoder.print("<db-attribute-pair");
99
100         // sanity check
101
if (getSourceName() != null) {
102             encoder.print(" source=\"");
103             encoder.print(getSourceName());
104             encoder.print("\"");
105         }
106
107         if (getTargetName() != null) {
108             encoder.print(" target=\"");
109             encoder.print(getTargetName());
110             encoder.print("\"");
111         }
112
113         encoder.println("/>");
114     }
115
116     public DbRelationship getRelationship() {
117         return relationship;
118     }
119
120     public String JavaDoc getSourceName() {
121         return sourceName;
122     }
123
124     public String JavaDoc getTargetName() {
125         return targetName;
126     }
127
128     public void setRelationship(DbRelationship relationship) {
129         this.relationship = relationship;
130     }
131
132     public void setSourceName(String JavaDoc string) {
133         sourceName = string;
134     }
135
136     public void setTargetName(String JavaDoc string) {
137         targetName = string;
138     }
139
140     private final DbRelationship getNonNullRelationship() {
141         if (relationship == null) {
142             throw new CayenneRuntimeException("Join has no parent Relationship.");
143         }
144
145         return relationship;
146     }
147
148     public String JavaDoc toString() {
149         ToStringBuilder builder = new ToStringBuilder(this);
150         builder.append("source", getSourceName());
151         builder.append("target", getTargetName());
152         return builder.toString();
153     }
154 }
155
Popular Tags