001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.cfg21;
030:
031: import com.caucho.ejb.cfg.*;
032: import com.caucho.config.ConfigException;
033: import com.caucho.util.L10N;
034:
035: import javax.annotation.PostConstruct;
036:
037: /**
038: * Configuraton for a cmp-relation.
039: */
040: public class CmpRelation {
041: private static final L10N L = new L10N(CmpRelation.class);
042:
043: private EjbConfig _config;
044:
045: private String _location = "";
046:
047: private String _name;
048: private String _sqlTable;
049:
050: private CmpRelationRole _sourceRole;
051: private CmpRelationRole _targetRole;
052:
053: private int _roleCount;
054:
055: /**
056: * Creates a new cmp-relation
057: */
058: public CmpRelation(EjbConfig config) {
059: _config = config;
060: _sourceRole = new CmpRelationRole(this );
061: _targetRole = new CmpRelationRole(this );
062:
063: _sourceRole.setTarget(_targetRole);
064: _targetRole.setTarget(_sourceRole);
065: }
066:
067: /**
068: * Creates a new cmp-relation
069: */
070: public CmpRelation() {
071: _sourceRole = new CmpRelationRole(this );
072: _targetRole = new CmpRelationRole(this );
073:
074: _sourceRole.setTarget(_targetRole);
075: _targetRole.setTarget(_sourceRole);
076: }
077:
078: /**
079: * Sets the location.
080: */
081: public void setConfigLocation(String filename, int line) {
082: _location = filename + ":" + line + ": ";
083: }
084:
085: /**
086: * Gets the location.
087: */
088: public String getLocation() {
089: return _location;
090: }
091:
092: /**
093: * Returns the relation name.
094: */
095: public String getName() {
096: return _name;
097: }
098:
099: /**
100: * Sets the relation name.
101: */
102: public void setName(String name) {
103: _name = name;
104: }
105:
106: /**
107: * Sets the relation name.
108: */
109: public void setEJBRelationName(String name) {
110: _name = name;
111: }
112:
113: /**
114: * Returns the SQL table for a three-table relation.
115: */
116: public String getSQLTable() {
117: return _sqlTable;
118: }
119:
120: /**
121: * Sets the SQL tarble for a three-table relation.
122: */
123: public void setSQLTable(String sqlTable) {
124: _sqlTable = sqlTable;
125: }
126:
127: /**
128: * Returns the source role.
129: */
130: public CmpRelationRole getSourceRole() {
131: if (_sourceRole.getFieldName() == null
132: && _targetRole.getFieldName() != null)
133: return _targetRole;
134: else
135: return _sourceRole;
136: }
137:
138: /**
139: * Returns the target role.
140: */
141: public CmpRelationRole getTargetRole() {
142: if (_sourceRole.getFieldName() == null
143: && _targetRole.getFieldName() != null)
144: return _sourceRole;
145: else
146: return _targetRole;
147: }
148:
149: /**
150: * Returns the source ejb name.
151: */
152: public String getSourceEJB() {
153: return getSourceRole().getEJBName();
154: }
155:
156: /**
157: * Sets the source ejb.
158: */
159: public void setSourceEJB(String sourceEJB) {
160: _sourceRole.setEJBName(sourceEJB);
161: }
162:
163: /**
164: * Returns the source field name.
165: */
166: public String getSourceField() {
167: return getSourceRole().getFieldName();
168: }
169:
170: /**
171: * Sets the source field.
172: */
173: public void setSourceField(String sourceField) {
174: _sourceRole.setFieldName(sourceField);
175: }
176:
177: /**
178: * Returns the source cascade-delete property.
179: */
180: public boolean getSourceCascadeDelete() {
181: return getSourceRole().getCascadeDelete();
182: }
183:
184: /**
185: * Sets the source cascade-delete property.
186: */
187: public void setSourceCascadeDelete(boolean sourceCascadeDelete) {
188: _sourceRole.setCascadeDelete(sourceCascadeDelete);
189: }
190:
191: /**
192: * Returns the source multiplicity property.
193: */
194: public String getSourceMultiplicity() {
195: return getSourceRole().getMultiplicity();
196: }
197:
198: /**
199: * Sets the source multiplicity property.
200: */
201: public void setSourceMultiplicity(String sourceMultiplicity)
202: throws ConfigException {
203: _sourceRole.setMultiplicity(sourceMultiplicity);
204: }
205:
206: /**
207: * Returns the source sql columns.
208: */
209: public SqlRelation[] getSourceSQLColumns() {
210: return getSourceRole().getSQLColumns();
211: }
212:
213: /**
214: * Add a source sql columns.
215: */
216: public void addSourceSQLColumn(String sqlColumn, String references) {
217: _sourceRole.addSQLColumn(sqlColumn, references);
218: }
219:
220: /**
221: * Creates a target sql column.
222: */
223: public CmpRelationRole.SqlColumn createSourceSqlColumn() {
224: return _sourceRole.createSqlColumn();
225: }
226:
227: /**
228: * Returns the source order-by property.
229: */
230: public String getSourceOrderBy() {
231: return getSourceRole().getOrderBy();
232: }
233:
234: /**
235: * Sets the source order-by property.
236: */
237: public void setSourceOrderBy(String sourceOrderBy) {
238: _sourceRole.setOrderBy(sourceOrderBy);
239: }
240:
241: /**
242: * Returns the target ejb name.
243: */
244: public String getTargetEJB() {
245: return getTargetRole().getEJBName();
246: }
247:
248: /**
249: * Sets the target ejb.
250: */
251: public void setTargetEJB(String targetEJB) {
252: _targetRole.setEJBName(targetEJB);
253: }
254:
255: /**
256: * Returns the target field name.
257: */
258: public String getTargetField() {
259: return getTargetRole().getFieldName();
260: }
261:
262: /**
263: * Sets the target field.
264: */
265: public void setTargetField(String targetField) {
266: _targetRole.setFieldName(targetField);
267: }
268:
269: /**
270: * Returns the target cascade-delete property.
271: */
272: public boolean getTargetCascadeDelete() {
273: return getTargetRole().getCascadeDelete();
274: }
275:
276: /**
277: * Sets the target cascade-delete property.
278: */
279: public void setTargetCascadeDelete(boolean targetCascadeDelete) {
280: _targetRole.setCascadeDelete(targetCascadeDelete);
281: }
282:
283: /**
284: * Returns the target multiplicity property.
285: */
286: public String getTargetMultiplicity() {
287: return getTargetRole().getMultiplicity();
288: }
289:
290: /**
291: * Sets the target multiplicity property.
292: */
293: public void setTargetMultiplicity(String targetMultiplicity)
294: throws ConfigException {
295: _targetRole.setMultiplicity(targetMultiplicity);
296: }
297:
298: /**
299: * Returns the target sql columns.
300: */
301: public SqlRelation[] getTargetSQLColumns() {
302: return getTargetRole().getSQLColumns();
303: }
304:
305: /**
306: * Add a target sql columns.
307: */
308: public void addTargetSQLColumn(String sqlColumn, String references) {
309: _targetRole.addSQLColumn(sqlColumn, references);
310: }
311:
312: /**
313: * Creates a target sql column.
314: */
315: public CmpRelationRole.SqlColumn createTargetSqlColumn() {
316: return _targetRole.createSqlColumn();
317: }
318:
319: /**
320: * Returns the target order-by property.
321: */
322: public String getTargetOrderBy() {
323: return getTargetRole().getOrderBy();
324: }
325:
326: /**
327: * Sets the target order-by property.
328: */
329: public void setTargetOrderBy(String targetOrderBy) {
330: _targetRole.setOrderBy(targetOrderBy);
331: }
332:
333: /**
334: * Configures the ejb-relationship-role
335: */
336: public CmpRelationRole createEjbRelationshipRole()
337: throws ConfigException {
338: _roleCount++;
339:
340: if (_roleCount == 1)
341: return _sourceRole;
342: else if (_roleCount == 2)
343: return _targetRole;
344: else
345: throw new ConfigException(
346: L
347: .l("ejb-relation requires two ejb-relationship-role elements"));
348: }
349:
350: /**
351: * Merges the relation.
352: */
353: public void merge(CmpRelation newRel) throws ConfigException {
354: if (_sqlTable == null)
355: _sqlTable = newRel.getSQLTable();
356:
357: _sourceRole.merge(newRel.getSourceRole());
358: _targetRole.merge(newRel.getTargetRole());
359: }
360:
361: /**
362: * Initialization sanity checks.
363: */
364: @PostConstruct
365: public void init() throws ConfigException {
366: if (getSourceEJB() == null)
367: throw new ConfigException(L
368: .l("ejb-relation needs a source EJB."));
369:
370: if (getTargetEJB() == null)
371: throw new ConfigException(L
372: .l("ejb-relation needs a target EJB."));
373:
374: if (getSourceField() == null)
375: throw new ConfigException(L
376: .l("ejb-relation needs a source field."));
377: }
378:
379: /**
380: * Returns true if this is the same relation.
381: */
382: public boolean equals(Object o) {
383: if (!(o instanceof CmpRelation))
384: return false;
385:
386: CmpRelation relation = (CmpRelation) o;
387:
388: return _sourceRole.equals(relation._sourceRole);
389: }
390: }
|