001: package net.sourceforge.jaxor.parser;
002:
003: import net.sourceforge.jaxor.util.SystemException;
004:
005: import java.util.ArrayList;
006: import java.util.Collections;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: public class Entity {
011: private String _name;
012: private List _attributes = new ArrayList();
013: private String _alias;
014: private String _extends;
015: private String _implements ;
016: private String _select_all_sql = "";
017: private List _aggregates = new ArrayList();
018: private PrimaryKey _primaryKey;
019: private String _list;
020: private List _queries = new ArrayList();
021: private List _fields = new ArrayList();
022: private List _declarations = new ArrayList();
023: private List _methods = new ArrayList();
024: private List _foreignKeys = new ArrayList();
025: private Javadoc _javadoc = null;
026: private String _impl;
027: private String _finder;
028:
029: public Entity(String name) {
030: _name = name;
031: }
032:
033: public Entity() {
034: }
035:
036: public void addJavadoc(Javadoc doc) {
037: _javadoc = doc;
038: }
039:
040: public Javadoc getJavadoc() {
041: return _javadoc;
042: }
043:
044: public void setName(String name) {
045: _name = name;
046: }
047:
048: public void setImpl(String im) {
049: _impl = im;
050: }
051:
052: public void setFinder(String finder) {
053: _finder = finder;
054: }
055:
056: public String getImpl() {
057: return _impl;
058: }
059:
060: public String getTableName() {
061: return _name;
062: }
063:
064: public String getJavaName() {
065: if (_alias == null)
066: return JavaNameGenerator.toJavaName(getTableName());
067: return _alias;
068: }
069:
070: public String getListClass() {
071: if (_list == null)
072: return getGeneratedListName();
073: return _list;
074: }
075:
076: public String getRuntimeImpl() {
077: if (getImpl() == null)
078: return getImplName();
079: return getImpl();
080: }
081:
082: public String getGeneratedListName() {
083: return getJavaName() + "List";
084: }
085:
086: public String getIteratorName() {
087: return getJavaName() + "Iterator";
088: }
089:
090: public String getFinderImpl() {
091: return getJavaName() + "FinderBase";
092: }
093:
094: public String getFinder() {
095: if (_finder != null)
096: return _finder;
097: return getFinderImpl();
098: }
099:
100: public void setList(String l) {
101: _list = l;
102: }
103:
104: public String getInterfaceName() {
105: return getJavaName() + "Entity";
106: }
107:
108: public String getReturnType() {
109: return getInterfaceName();
110: }
111:
112: public String getEntityRowName() {
113: return getJavaName() + "EntityRow";
114: }
115:
116: public String getFinderName() {
117: return getJavaName() + "Finder";
118: }
119:
120: public String getEntityResultSetClass() {
121: return getJavaName() + "ResultSet";
122: }
123:
124: public String getEntityQueryResultClass() {
125: return getJavaName() + "Query";
126: }
127:
128: public String getMetaName() {
129: return getJavaName() + "MetaRow";
130: }
131:
132: public String getImplName() {
133: return getJavaName() + "Base";
134: }
135:
136: public void setSelectAllSql(String s) {
137: _select_all_sql = s;
138: }
139:
140: public String getSelectAllSql() {
141: return _select_all_sql;
142: }
143:
144: public void addAttribute(Attribute attribute) {
145: _attributes.add(attribute);
146: }
147:
148: public void addEntityRef(EntityRef f) {
149: _foreignKeys.add(f);
150: }
151:
152: public void addPrimaryKey(PrimaryKey key) {
153: if (_primaryKey != null)
154: throw new SystemException(
155: "Can only define one Primary Key Set");
156: _primaryKey = key;
157: }
158:
159: public PrimaryKey getPrimaryKey() {
160: return _primaryKey;
161: }
162:
163: public List getAttributesAndPrimaryKeys() {
164: List all = new ArrayList();
165: all.addAll(_attributes);
166: all.addAll(_primaryKey.getKeys());
167: return all;
168: }
169:
170: public AttributeList getAttributeList() {
171: return new AttributeList(getAttributes());
172: }
173:
174: public List getAttributes() {
175: List all = new ArrayList();
176: all.addAll(_attributes);
177: all.addAll(_primaryKey.getKeys());
178: for (Iterator iterator = _aggregates.iterator(); iterator
179: .hasNext();) {
180: Aggregate aggregate = (Aggregate) iterator.next();
181: all.addAll(aggregate.getAttributes());
182: }
183: return Collections.unmodifiableList(all);
184: }
185:
186: public boolean hasPrimaryKey() {
187: return getAttributeList().getPrimaryKey().size() > 0;
188: }
189:
190: public Query getSelectByPrimaryKey(Jaxor j) {
191: List attr = getAttributeList().getPrimaryKey();
192: String name = "selectBy";
193: for (Iterator iterator = attr.iterator(); iterator.hasNext();) {
194: Attribute attribute = (Attribute) iterator.next();
195: name += attribute.getJavaName();
196: if (iterator.hasNext())
197: name += "And";
198: }
199:
200: Query query = new Query(name, "");
201: for (Iterator iterator = attr.iterator(); iterator.hasNext();) {
202: Attribute attribute = (Attribute) iterator.next();
203: Param param = new Param(attribute, j);
204: query.addParam(param);
205: }
206: return query;
207: }
208:
209: public void setAlias(String alias) {
210: _alias = alias;
211: }
212:
213: public void setExtends(String extendsOverride) {
214: _extends = extendsOverride;
215: }
216:
217: public String getExtends() {
218: return _extends;
219: }
220:
221: public void setImplements(String implements Override) {
222: _implements = implements Override;
223: }
224:
225: public String getImplements() {
226: return _implements ;
227: }
228:
229: public List getAggregates() {
230: return _aggregates;
231: }
232:
233: public void addAggregate(Aggregate aggregate) {
234: _aggregates.add(aggregate);
235: }
236:
237: public List getForeignFields(EntityCollection coll, Jaxor jaxor) {
238: return new ArrayList(_foreignKeys);
239: }
240:
241: public List getFields(EntityCollection coll, Jaxor j) {
242: List results = new ArrayList(_fields);
243: return results;
244: }
245:
246: public void addListRef(ListRef ref) {
247: _foreignKeys.add(ref);
248: }
249:
250: public void addField(Field f) {
251: _fields.add(f);
252: }
253:
254: public void addQuery(Query q) {
255: _queries.add(q);
256: }
257:
258: public void addMethod(JMethod m) {
259: _methods.add(m);
260: }
261:
262: public List getMethods() {
263: return Collections.unmodifiableList(_methods);
264: }
265:
266: public void addDeclarations(Declarations d) {
267: _declarations.add(d);
268: }
269:
270: public List getQueries(Jaxor j) {
271: List allQueries = new ArrayList();
272: allQueries.addAll(_queries);
273: allQueries.addAll(createQueriesForAttributes(j));
274: return allQueries;
275: }
276:
277: private List createQueriesForAttributes(Jaxor j) {
278: List results = new ArrayList();
279: List attr = getAttributeList().asList();
280: for (Iterator iterator = attr.iterator(); iterator.hasNext();) {
281: Attribute attribute = (Attribute) iterator.next();
282: String sqlClause = "where " + attribute.getName() + "=?";
283: Query query = new Query("selectBy"
284: + attribute.getJavaName(), sqlClause);
285: Param param = new Param(attribute, j);
286: param.setMapper(attribute.getMapperName(j));
287: query.addParam(param);
288: results.add(query);
289: }
290: return results;
291:
292: }
293:
294: public List getDeclarations() {
295: return Collections.unmodifiableList(_declarations);
296: }
297:
298: public Attribute findAttribute(String key) {
299: return getAttributeList().findAttribute(key);
300: }
301:
302: public List getEntityRefs() {
303: return _foreignKeys;
304: }
305:
306: }
|