001: package simpleorm.quickstart;
002:
003: import org.apache.commons.sql.model.Column;
004: import org.apache.commons.sql.model.ForeignKey;
005: import org.apache.commons.sql.model.Reference;
006: import org.apache.commons.sql.model.Table;
007:
008: import java.io.IOException;
009: import java.io.Writer;
010:
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Map;
014: import java.util.Vector;
015:
016: /**
017: * @author <a href="mailto:richard.schmidt@inform6.com">Richard Schmidt</a>
018: *
019: */
020: final class ForeignKeyGenerate {
021: public ForeignKey key;
022: public Table table;
023: INiceNameFormatter niceName;
024: Map hmColumns; //key = column name, object = ColumnGenerate;
025:
026: public ForeignKeyGenerate(Table table, ForeignKey key,
027: INiceNameFormatter niceName, Map columns) {
028: this .key = key;
029: this .table = table;
030: this .niceName = niceName;
031: this .hmColumns = columns;
032: }
033:
034: /**Never used*/
035: public void outputStaticFields(Writer out) throws IOException {
036: }
037:
038: /**
039: Need to generate something like this:
040: public Task get_refTask(){
041: try {
042: return Task.findOrCreate(get_fldClientno(), get_fldMatterno(),
043: get_fldTaskno());
044: } catch (SException e) {
045: if (e.getMessage().indexOf("Null Primary key") > 0) {
046: return null;
047: }
048:
049: throw e;
050: }}
051: public void set_refTask( Task value){
052: set_fldClientno( value.get_fldClientno());
053: set_fldMatterno( value.get_fldMatterno());
054: set_fldTaskno( value.get_fldTaskno());
055: }
056: */
057: public void outputGettersAndSetters(Writer out) throws IOException {
058: String foreignTableName = niceName.niceNameForTable(key
059: .getForeignTable());
060: String keyName = niceName.niceNameForForeignKey(
061: table.getName(), key.getForeignTable());
062:
063: //Do the getter
064: //public Task get_refTask(){
065: out.write(" public " + foreignTableName + " get_" + keyName
066: + "(){\n");
067:
068: // try {
069: out.write(" try{\n");
070:
071: //return Task.findOrCreate( get_fldClientno(), get_fldMatterno(), get_fldTaskno());
072: //Assumes that references will be in primary key order!
073: out.write(" return " + foreignTableName
074: + ".findOrCreate(");
075:
076: Iterator itt = key.getReferences().iterator();
077:
078: while (itt.hasNext()) {
079: Reference ref = (Reference) itt.next();
080: out.write("get_"
081: + niceName.niceNameForColumn(table.getName(), ref
082: .getLocal()) + "()");
083:
084: if (itt.hasNext()) {
085: out.write(",");
086: }
087: }
088:
089: out.write(");\n");
090:
091: // } catch (SException e) {
092: // if (e.getMessage().indexOf("Null Primary key") > 0) {
093: // return null;
094: // }
095: // throw e;
096: // }
097: out.write(" } catch (SException e) {\n");
098: out
099: .write(" if (e.getMessage().indexOf(\"Null Primary key\") > 0) {\n");
100: out.write(" return null;\n");
101: out.write(" }\n");
102: out.write(" throw e;\n");
103: out.write(" }\n");
104: out.write(" }\n");
105:
106: //Now do the setter
107: //public void set_refTask( Task value){
108: out.write(" public void set_" + keyName + "( "
109: + foreignTableName + " value){\n");
110: itt = key.getReferences().iterator();
111:
112: while (itt.hasNext()) {
113: Reference ref = (Reference) itt.next();
114:
115: //set_fldClientno( value.get_fldClientno());
116: out.write(" set_"
117: + niceName.niceNameForColumn(table.getName(), ref
118: .getLocal())
119: + "( value.get_"
120: + niceName.niceNameForColumn(key.getForeignTable(),
121: ref.getForeign()) + "());\n");
122: }
123:
124: ;
125: out.write(" }\n\n");
126: }
127:
128: // /**example: Department fldRefDepartment"*/
129: // public AParam getParameters() {
130: // String methodParam = niceName.niceNameForTable(key.getForeignTable()) +
131: // " _ref" +
132: // niceName.niceNameForForeignKey(table.getName(),
133: // key.getForeignTable());
134: //
135: // String objectParam = " _ref" +
136: // niceName.niceNameForForeignKey(table.getName(),
137: // key.getForeignTable());
138: //
139: // return new AParam(methodParam, objectParam);
140: // }
141:
142: public boolean isPrimary() {
143: List list = key.getReferences();
144: Iterator itt = list.iterator();
145:
146: while (itt.hasNext()) {
147: Reference ref = (Reference) itt.next();
148: String column = ref.getLocal();
149:
150: if (table.findColumn(column).isPrimaryKey() == false) {
151: return false;
152: }
153: }
154:
155: return true;
156: }
157:
158: private List primaryColumnsNotInRefernce() {
159: List list = table.getPrimaryKeyColumns();
160:
161: Iterator itt = key.getReferences().iterator();
162:
163: while (itt.hasNext()) {
164: Reference ref = (Reference) itt.next();
165: Column column = table.findColumn(ref.getLocal());
166: list.remove(column);
167: }
168:
169: return list;
170: }
171:
172: private String foreignTableColumnName(Column column) {
173: Iterator itt = key.getReferences().iterator();
174:
175: while (itt.hasNext()) {
176: Reference ref = (Reference) itt.next();
177: if (ref.getLocal().equals(column.getName())) {
178: return ref.getForeign();
179: }
180: }
181: return "error";
182: }
183:
184: /**
185: public static Matter findOrCreate( Client _ref, String _fldMatterNo ){
186: return findOrCreate( _ref.get_fldClientno(),_fldMatterNo);
187: }
188: */
189:
190: public void outputFindOrCreate(Writer out) throws IOException {
191:
192: List columnsNotIn = primaryColumnsNotInRefernce();
193:
194: //public static Matter findOrCreate( Client _ref, String _fldMatterNo ){
195: out.write(" public static "
196: + niceName.niceNameForTable(table.getName())
197: + " findOrCreate( "
198: + niceName.niceNameForTable(key.getForeignTable())
199: + " _ref, ");
200: //gerenate the method paramters
201: Iterator itt = columnsNotIn.iterator();
202: while (itt.hasNext()) {
203: Column column = (Column) itt.next();
204: ColumnGenerate cg = (ColumnGenerate) hmColumns.get(column
205: .getName());
206: out.write(cg.getParameters().methodParam);
207: if (itt.hasNext()) {
208: out.write(" ,");
209: }
210: }
211: out.write("){\n");
212:
213: //return findOrCreate( _ref.get_fldClientno(),_fldMatterNo);
214: out.write(" return findOrCreate( ");
215:
216: //iterate through the primary columns again
217: Iterator primaryColumnsItt = table.getPrimaryKeyColumns()
218: .iterator();
219: while (primaryColumnsItt.hasNext()) {
220:
221: Column column = (Column) primaryColumnsItt.next();
222: if (columnsNotIn.contains(column) == false) {
223:
224: //This is one of the fields in the refernece
225: out.write("_ref.get_"
226: + niceName.niceNameForColumn(key
227: .getForeignTable(),
228: foreignTableColumnName(column)) + "()");
229: } else {
230:
231: //This is one of the fields passed as a parameter;
232: ColumnGenerate cg = (ColumnGenerate) hmColumns
233: .get(column.getName());
234: out.write(cg.getParameters().paramName);
235: }
236: if (primaryColumnsItt.hasNext()) {
237: out.write(", ");
238: }
239: }
240: out.write(");\n");
241:
242: out.write(" }\n\n");
243: }
244: }
|