KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > CmpRelation


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.cfg;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.L10N;
33
34 import javax.annotation.PostConstruct;
35
36 /**
37  * Configuraton for a cmp-relation.
38  */

39 public class CmpRelation {
40   private static final L10N L = new L10N(CmpRelation.class);
41
42   private EjbConfig _config;
43
44   private String JavaDoc _location = "";
45
46   private String JavaDoc _name;
47   private String JavaDoc _sqlTable;
48   
49   private CmpRelationRole _sourceRole;
50   private CmpRelationRole _targetRole;
51
52   private int _roleCount;
53
54   /**
55    * Creates a new cmp-relation
56    */

57   public CmpRelation(EjbConfig config)
58   {
59     _config = config;
60     _sourceRole = new CmpRelationRole(this);
61     _targetRole = new CmpRelationRole(this);
62
63     _sourceRole.setTarget(_targetRole);
64     _targetRole.setTarget(_sourceRole);
65   }
66
67   /**
68    * Creates a new cmp-relation
69    */

70   public CmpRelation()
71   {
72     _sourceRole = new CmpRelationRole(this);
73     _targetRole = new CmpRelationRole(this);
74
75     _sourceRole.setTarget(_targetRole);
76     _targetRole.setTarget(_sourceRole);
77   }
78
79   /**
80    * Sets the location.
81    */

82   public void setConfigLocation(String JavaDoc filename, int line)
83   {
84     _location = filename + ":" + line + ": ";
85   }
86
87   /**
88    * Gets the location.
89    */

90   public String JavaDoc getLocation()
91   {
92     return _location;
93   }
94
95   /**
96    * Returns the relation name.
97    */

98   public String JavaDoc getName()
99   {
100     return _name;
101   }
102
103   /**
104    * Sets the relation name.
105    */

106   public void setName(String JavaDoc name)
107   {
108     _name = name;
109   }
110
111   /**
112    * Sets the relation name.
113    */

114   public void setEJBRelationName(String JavaDoc name)
115   {
116     _name = name;
117   }
118
119   /**
120    * Returns the SQL table for a three-table relation.
121    */

122   public String JavaDoc getSQLTable()
123   {
124     return _sqlTable;
125   }
126
127   /**
128    * Sets the SQL tarble for a three-table relation.
129    */

130   public void setSQLTable(String JavaDoc sqlTable)
131   {
132     _sqlTable = sqlTable;
133   }
134
135   /**
136    * Returns the source role.
137    */

138   public CmpRelationRole getSourceRole()
139   {
140     if (_sourceRole.getFieldName() == null &&
141         _targetRole.getFieldName() != null)
142       return _targetRole;
143     else
144       return _sourceRole;
145   }
146
147   /**
148    * Returns the target role.
149    */

150   public CmpRelationRole getTargetRole()
151   {
152     if (_sourceRole.getFieldName() == null &&
153         _targetRole.getFieldName() != null)
154       return _sourceRole;
155     else
156       return _targetRole;
157   }
158
159   /**
160    * Returns the source ejb name.
161    */

162   public String JavaDoc getSourceEJB()
163   {
164     return getSourceRole().getEJBName();
165   }
166
167   /**
168    * Sets the source ejb.
169    */

170   public void setSourceEJB(String JavaDoc sourceEJB)
171   {
172     _sourceRole.setEJBName(sourceEJB);
173   }
174
175   /**
176    * Returns the source field name.
177    */

178   public String JavaDoc getSourceField()
179   {
180     return getSourceRole().getFieldName();
181   }
182
183   /**
184    * Sets the source field.
185    */

186   public void setSourceField(String JavaDoc sourceField)
187   {
188     _sourceRole.setFieldName(sourceField);
189   }
190
191   /**
192    * Returns the source cascade-delete property.
193    */

194   public boolean getSourceCascadeDelete()
195   {
196     return getSourceRole().getCascadeDelete();
197   }
198
199   /**
200    * Sets the source cascade-delete property.
201    */

202   public void setSourceCascadeDelete(boolean sourceCascadeDelete)
203   {
204     _sourceRole.setCascadeDelete(sourceCascadeDelete);
205   }
206
207   /**
208    * Returns the source multiplicity property.
209    */

210   public String JavaDoc getSourceMultiplicity()
211   {
212     return getSourceRole().getMultiplicity();
213   }
214
215   /**
216    * Sets the source multiplicity property.
217    */

218   public void setSourceMultiplicity(String JavaDoc sourceMultiplicity)
219     throws ConfigException
220   {
221     _sourceRole.setMultiplicity(sourceMultiplicity);
222   }
223
224   /**
225    * Returns the source sql columns.
226    */

227   public SqlRelation []getSourceSQLColumns()
228   {
229     return getSourceRole().getSQLColumns();
230   }
231
232   /**
233    * Add a source sql columns.
234    */

235   public void addSourceSQLColumn(String JavaDoc sqlColumn, String JavaDoc references)
236   {
237     _sourceRole.addSQLColumn(sqlColumn, references);
238   }
239
240   /**
241    * Creates a target sql column.
242    */

243   public CmpRelationRole.SqlColumn createSourceSqlColumn()
244   {
245     return _sourceRole.createSqlColumn();
246   }
247
248   /**
249    * Returns the source order-by property.
250    */

251   public String JavaDoc getSourceOrderBy()
252   {
253     return getSourceRole().getOrderBy();
254   }
255
256   /**
257    * Sets the source order-by property.
258    */

259   public void setSourceOrderBy(String JavaDoc sourceOrderBy)
260   {
261     _sourceRole.setOrderBy(sourceOrderBy);
262   }
263
264   /**
265    * Returns the target ejb name.
266    */

267   public String JavaDoc getTargetEJB()
268   {
269     return getTargetRole().getEJBName();
270   }
271
272   /**
273    * Sets the target ejb.
274    */

275   public void setTargetEJB(String JavaDoc targetEJB)
276   {
277     _targetRole.setEJBName(targetEJB);
278   }
279
280   /**
281    * Returns the target field name.
282    */

283   public String JavaDoc getTargetField()
284   {
285     return getTargetRole().getFieldName();
286   }
287
288   /**
289    * Sets the target field.
290    */

291   public void setTargetField(String JavaDoc targetField)
292   {
293     _targetRole.setFieldName(targetField);
294   }
295
296   /**
297    * Returns the target cascade-delete property.
298    */

299   public boolean getTargetCascadeDelete()
300   {
301     return getTargetRole().getCascadeDelete();
302   }
303
304   /**
305    * Sets the target cascade-delete property.
306    */

307   public void setTargetCascadeDelete(boolean targetCascadeDelete)
308   {
309     _targetRole.setCascadeDelete(targetCascadeDelete);
310   }
311
312   /**
313    * Returns the target multiplicity property.
314    */

315   public String JavaDoc getTargetMultiplicity()
316   {
317     return getTargetRole().getMultiplicity();
318   }
319
320   /**
321    * Sets the target multiplicity property.
322    */

323   public void setTargetMultiplicity(String JavaDoc targetMultiplicity)
324     throws ConfigException
325   {
326     _targetRole.setMultiplicity(targetMultiplicity);
327   }
328
329   /**
330    * Returns the target sql columns.
331    */

332   public SqlRelation []getTargetSQLColumns()
333   {
334     return getTargetRole().getSQLColumns();
335   }
336
337   /**
338    * Add a target sql columns.
339    */

340   public void addTargetSQLColumn(String JavaDoc sqlColumn, String JavaDoc references)
341   {
342     _targetRole.addSQLColumn(sqlColumn, references);
343   }
344
345   /**
346    * Creates a target sql column.
347    */

348   public CmpRelationRole.SqlColumn createTargetSqlColumn()
349   {
350     return _targetRole.createSqlColumn();
351   }
352
353   /**
354    * Returns the target order-by property.
355    */

356   public String JavaDoc getTargetOrderBy()
357   {
358     return getTargetRole().getOrderBy();
359   }
360
361   /**
362    * Sets the target order-by property.
363    */

364   public void setTargetOrderBy(String JavaDoc targetOrderBy)
365   {
366     _targetRole.setOrderBy(targetOrderBy);
367   }
368
369   /**
370    * Configures the ejb-relationship-role
371    */

372   public CmpRelationRole createEjbRelationshipRole()
373     throws ConfigException
374   {
375     _roleCount++;
376     
377     if (_roleCount == 1)
378       return _sourceRole;
379     else if (_roleCount == 2)
380       return _targetRole;
381     else
382       throw new ConfigException(L.l("ejb-relation requires two ejb-relationship-role elements"));
383   }
384
385   /**
386    * Merges the relation.
387    */

388   public void merge(CmpRelation newRel)
389     throws ConfigException
390   {
391     if (_sqlTable == null)
392       _sqlTable = newRel.getSQLTable();
393
394     _sourceRole.merge(newRel.getSourceRole());
395     _targetRole.merge(newRel.getTargetRole());
396   }
397
398   /**
399    * Initialization sanity checks.
400    */

401   @PostConstruct
402   public void init()
403     throws ConfigException
404   {
405     if (getSourceEJB() == null)
406       throw new ConfigException(L.l("ejb-relation needs a source EJB."));
407     
408     if (getTargetEJB() == null)
409       throw new ConfigException(L.l("ejb-relation needs a target EJB."));
410
411     if (getSourceField() == null)
412       throw new ConfigException(L.l("ejb-relation needs a source field."));
413   }
414     
415   /**
416    * Returns true if this is the same relation.
417    */

418   public boolean equals(Object JavaDoc o)
419   {
420     if (! (o instanceof CmpRelation))
421       return false;
422
423     CmpRelation relation = (CmpRelation) o;
424
425     return _sourceRole.equals(relation._sourceRole);
426   }
427 }
428
Popular Tags