KickJava   Java API By Example, From Geeks To Geeks.

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


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.amber.field.AbstractField;
32 import com.caucho.config.ConfigException;
33 import com.caucho.util.L10N;
34
35 import javax.annotation.PostConstruct;
36 import java.util.Collection JavaDoc;
37
38 /**
39  * Configuration for a cmp-relation.
40  */

41 public class CmpRelationRole {
42   private static L10N L = new L10N(CmpRelationRole.class);
43
44   private CmpRelation _relation;
45   private CmpRelationRole _target;
46   
47   private String JavaDoc _location;
48   
49   private String JavaDoc _ejbName;
50   private String JavaDoc _fieldName;
51   
52   private boolean _cascadeDelete;
53   private String JavaDoc _multiplicity;
54   
55   private SqlRelation []_sqlColumns = new SqlRelation[0];
56   private String JavaDoc _orderBy;
57
58   private AbstractField _amberField;
59   private Class JavaDoc _javaType;
60
61   private boolean _isImplicit;
62
63   /**
64    * Creates a new cmp-relation
65    */

66   public CmpRelationRole(CmpRelation relation)
67   {
68     _relation = relation;
69   }
70
71   /**
72    * Sets the description.
73    */

74   public void setDescription(String JavaDoc description)
75   {
76   }
77
78   /**
79    * Gets the owning relation.
80    */

81   public CmpRelation getRelation()
82   {
83     return _relation;
84   }
85
86   /**
87    * Gets the target
88    */

89   public CmpRelationRole getTarget()
90   {
91     return _target;
92   }
93
94   /**
95    * Sets the target
96    */

97   public void setTarget(CmpRelationRole target)
98   {
99     _target = target;
100   }
101
102   /**
103    * Sets the location.
104    */

105   public void setConfigLocation(String JavaDoc filename, int line)
106   {
107     _location = filename + ":" + line + ": ";
108   }
109
110   /**
111    * Gets the location.
112    */

113   public String JavaDoc getLocation()
114   {
115     return _location;
116   }
117
118   /**
119    * Sets the ejb-relationship-role-name
120    */

121   public void setEjbRelationshipRoleName(String JavaDoc name)
122   {
123   }
124
125   /**
126    * Returns the source ejb name.
127    */

128   public String JavaDoc getEJBName()
129   {
130     return _ejbName;
131   }
132
133   /**
134    * Sets the ejb-name.
135    */

136   public void setEJBName(String JavaDoc ejbName)
137   {
138     _ejbName = ejbName;
139   }
140
141   /**
142    * Returns the field name.
143    */

144   public String JavaDoc getFieldName()
145   {
146     return _fieldName;
147   }
148
149   /**
150    * Sets the field name.
151    */

152   public void setFieldName(String JavaDoc fieldName)
153   {
154     _fieldName = fieldName;
155   }
156
157   /**
158    * Returns the cascade-delete property.
159    */

160   public boolean getCascadeDelete()
161   {
162     return _cascadeDelete;
163   }
164
165   /**
166    * Sets the cascade-delete property.
167    */

168   public void setCascadeDelete(boolean cascadeDelete)
169   {
170     _cascadeDelete = cascadeDelete;
171   }
172
173   /**
174    * Returns the multiplicity property.
175    */

176   public String JavaDoc getMultiplicity()
177   {
178     return _multiplicity;
179   }
180
181   /**
182    * Sets the source multiplicity property.
183    */

184   public void setMultiplicity(String JavaDoc multiplicity)
185     throws ConfigException
186   {
187     _multiplicity = multiplicity;
188
189     if (multiplicity == null ||
190         ! multiplicity.equals("One") &&
191         ! multiplicity.equals("Many"))
192       throw new ConfigException(L.l("`{0}' is an unknown multiplicity. `One' and `Many' are the only allowed values.", multiplicity));
193   }
194
195   /**
196    * Returns the source sql columns.
197    */

198   public SqlRelation []getSQLColumns()
199   {
200     return _sqlColumns;
201   }
202
203   /**
204    * Add a sql columns.
205    */

206   public void addSQLColumn(String JavaDoc sqlColumn, String JavaDoc references)
207   {
208     SqlRelation relation = new SqlRelation(_fieldName);
209
210     relation.setSQLColumn(sqlColumn);
211     relation.setReferences(references);
212
213     if (_sqlColumns == null)
214       _sqlColumns = new SqlRelation[] { relation };
215     else {
216       SqlRelation []newColumns = new SqlRelation[_sqlColumns.length + 1];
217       System.arraycopy(_sqlColumns, 0, newColumns, 0, _sqlColumns.length);
218
219       newColumns[_sqlColumns.length] = relation;
220       _sqlColumns = newColumns;
221     }
222   }
223
224   public SqlColumn createSqlColumn()
225   {
226     return new SqlColumn();
227   }
228
229   /**
230    * Returns the order-by property.
231    */

232   public String JavaDoc getOrderBy()
233   {
234     return _orderBy;
235   }
236
237   /**
238    * Sets the order-by property.
239    */

240   public void setOrderBy(String JavaDoc orderBy)
241   {
242     _orderBy = orderBy;
243   }
244
245   /**
246    * Returns true if the role is implicit.
247    */

248   public boolean isImplicit()
249   {
250     return _isImplicit;
251   }
252
253   /**
254    * Sets true if the role is implicit.
255    */

256   public void setImplicit(boolean isImplicit)
257   {
258     _isImplicit = isImplicit;
259   }
260
261   /**
262    * Sets the Java type.
263    */

264   public void setJavaType(Class JavaDoc cl)
265   {
266     _javaType = cl;
267   }
268
269   /**
270    * Sets the amber type.
271    */

272   public void setAmberField(AbstractField field)
273   {
274     _amberField = field;
275   }
276
277   /**
278    * Gets the amber type.
279    */

280   public AbstractField getAmberField()
281   {
282     return _amberField;
283   }
284
285   /**
286    * Return true for collections.
287    */

288   public boolean isCollection()
289   {
290     return _javaType != null && Collection JavaDoc.class.isAssignableFrom(_javaType);
291   }
292
293   /**
294    * Merges with the target.
295    */

296   public void merge(CmpRelationRole newRole)
297   {
298     if (_sqlColumns.length == 0)
299       _sqlColumns = newRole.getSQLColumns();
300   }
301
302   // EJB config
303

304   /**
305    * Sets the relationship-role-source.
306    */

307   public RoleSource createRelationshipRoleSource()
308   {
309     return new RoleSource();
310   }
311   
312   /**
313    * Sets the relationship-role-source.
314    */

315   public CmrField createCmrField()
316   {
317     return new CmrField();
318   }
319
320   /**
321    * Returns true if this is the same relation.
322    */

323   public boolean equals(Object JavaDoc o)
324   {
325     if (! (o instanceof CmpRelationRole))
326       return false;
327
328     CmpRelationRole role = (CmpRelationRole) o;
329
330     if (! _ejbName.equals(role._ejbName))
331       return false;
332
333     if (_fieldName == null || role._fieldName == null)
334       return _fieldName == role._fieldName;
335     
336     if (! _fieldName.equals(role._fieldName))
337       return false;
338
339     return true;
340   }
341
342   public class RoleSource {
343     public void setEJBName(String JavaDoc name)
344     {
345       CmpRelationRole.this.setEJBName(name);
346     }
347   }
348
349   public class CmrField {
350     public void setCmrFieldName(String JavaDoc name)
351     {
352       CmpRelationRole.this.setFieldName(name);
353     }
354     
355     public void setCmrFieldType(String JavaDoc name)
356     {
357       // XXX: CmpRelationRole.this.setFieldName(name);
358
}
359
360     public SqlColumn createSqlColumn()
361     {
362       return new SqlColumn();
363     }
364   }
365
366   public class SqlColumn {
367     private String JavaDoc _value;
368     private String JavaDoc _references;
369
370     public void setReferences(String JavaDoc references)
371     {
372       _references = references;
373     }
374
375     public void setValue(String JavaDoc value)
376     {
377       _value = value;
378     }
379
380     public void addText(String JavaDoc value)
381     {
382       _value = value;
383     }
384
385     @PostConstruct
386     public void init()
387     {
388       CmpRelationRole.this.addSQLColumn(_value, _references);
389     }
390   }
391 }
392
Popular Tags