View Javadoc

1   package org.paneris.jal.controller;
2   
3   import java.io.FileNotFoundException;
4   import java.io.FileWriter;
5   import java.io.PrintWriter;
6   import java.sql.Connection;
7   import java.sql.ResultSet;
8   import java.sql.Statement;
9   import java.util.Hashtable;
10  
11  import org.paneris.jal.model.DBConnectionManager;
12  import org.paneris.jal.model.DDRecord;
13  import org.paneris.jal.model.SystemProperties;
14  import org.paneris.user.model.User;
15  import org.webmacro.Template;
16  import org.webmacro.servlet.HandlerException;
17  import org.webmacro.servlet.WMServlet;
18  import org.webmacro.servlet.WebContext;
19  
20  /**
21   *  Create a Melati DSD from a JAL database
22   *
23   *
24   * @author  timp
25   * @version 2.0
26   *
27   * @see WMServlet
28   *
29   **/
30  public class CreateDSD extends WMServlet {
31  
32    private static final long serialVersionUID = 1L;
33    /* The datadictionarytypes CANNOT be relied upon ;(
34     * so we load and check them against their names.
35     */
36    private static final int TEXT = 1;
37    private static final int TEXTAREA = 2;
38    private static final int INTEGER = 3;
39    private static final int NUMBER = 4;
40    private static final int DATE = 5;
41    private static final int BOOLEAN = 6;
42    private static final int ID = 7;
43    private static final int LOOKUP = 8;
44    private static final int PASSWORD = 9;
45    private static final int LONG = 10;
46    private static final int DATETIME = 11;
47    private static final int AUTORANDOM = 12;
48    private static final int PREFORMATTEDTEXTAREA = 13;
49    private static final int ESCAPEDTEXT = 14;
50    private static final int CURRENCY = 15;
51    private static final int LOOKUPNODROP = 16;
52    private static final int TIMESTAMP = 17;
53    private static final int UPLOADURL = 18;
54    private static final int DATEDROP = 19;
55    private static final int DATETIMEDROP = 20;
56  
57    private DBConnectionManager connMgr = DBConnectionManager.getInstance();
58    private Connection conn;
59  
60    /**
61     * The main entry point.
62     *
63     * @param context a <code>WebContext</code> value
64     * @return a <code>Template</code> value
65    
66     * @throws HandlerException if anything goes wrong
67     **/
68    public Template handle(WebContext context) throws HandlerException {
69  
70      //
71      // Get path
72      // Which is where we will write DSD file to.
73      //
74      String message = "";
75      String db = context.getForm("db");
76      if (db == null) {
77        throw new HandlerException("No datasource specified.");
78      } else {
79        conn = connMgr.getConnection("CreateDSD", db);
80        if (conn == null)
81          throw new HandlerException("Can't get connection " + db);
82      }
83      context.put("db", db);
84      String path = "";
85      try {
86        SystemProperties sp = new SystemProperties(db);
87        path = sp.getProperty("panerisdir");
88        path = path + db + "/poem/";
89      } catch (Exception e) {
90        path = "/paneris/" + db + "/poem/";
91      }
92      context.put("path", path);
93  
94      //
95      // Read form
96      //
97      String dsd = context.getForm("dsd");
98  
99      // check login
100     String templateName = null;
101     try {
102       templateName =
103         User.checkLoggedIn(
104           "controller",
105           this.getClass().getName(),
106           context,
107           " create a DSD from a Jal Database.");
108     } catch (Exception e) {
109       throw new HandlerException("Could not get user:" + e.toString());
110     }
111     if (templateName == null) {
112       templateName = context.getForm("wmtemplate");
113       //
114       // input screen
115       //
116       if (dsd == null) {
117         if (templateName == null)
118           templateName = "jal/view/CreateDSD.wm";
119         context.put("wmtemplate", templateName);
120       }
121       //
122       // results screen
123       //
124       else {
125         if (templateName == null)
126           templateName = "jal/view/CreatedDSD.wm";
127         context.put("wmtemplate", templateName);
128 
129         String dsdFile = new String(path + db + ".dsd");
130         context.put("dsdFile", dsdFile);
131         PrintWriter pw;
132         try {
133           pw = new PrintWriter(new FileWriter(dsdFile), true);
134         } catch (FileNotFoundException e) {
135           throw new HandlerException(
136             "Problem creating FileWriter; <br>Does the directory "
137               + path
138               + "exist?"
139               + "<br>Try<br>"
140               + "mkdir "
141               + path
142               + "<br>"
143               + "chgrp nobody  "
144               + path
145               + "<br>"
146               + "chmod g+w  "
147               + path
148               + "<br><br>"
149               + e.toString());
150         } catch (Exception e) {
151           throw new HandlerException(
152             "Problem creating FileWriter:<br>" + e.toString());
153         }
154 
155         Statement s1;
156         try {
157           s1 = conn.createStatement();
158         } catch (Exception e) {
159           throw new HandlerException(e.toString());
160         }
161 
162         String sqlString =
163           "SELECT id, type FROM datadictionarytypes ORDER BY id";
164         ResultSet typesRS;
165         Hashtable typeMap = new Hashtable();
166         try {
167           typesRS = s1.executeQuery(sqlString);
168           Integer typeId = null;
169           String typeName;
170           while (typesRS.next()) {
171             typeId = new Integer(typesRS.getInt(1));
172             typeName = typesRS.getString(2);
173             //           System.err.println("   else if (typename.equals(\"" + typeName.trim() + "\") {\n                        typeMap.put(typeID," + typeName.toUpperCase() + ");\n             }");
174 
175             if (typeName.trim().equals("text")) {
176               typeMap.put(typeId, new Integer(TEXT));
177             } else if (typeName.trim().equals("textarea")) {
178               typeMap.put(typeId, new Integer(TEXTAREA));
179             } else if (typeName.trim().equals("integer")) {
180               typeMap.put(typeId, new Integer(INTEGER));
181             } else if (typeName.trim().equals("number")) {
182               typeMap.put(typeId, new Integer(NUMBER));
183             } else if (typeName.trim().equals("date")) {
184               typeMap.put(typeId, new Integer(DATE));
185             } else if (typeName.trim().equals("boolean")) {
186               typeMap.put(typeId, new Integer(BOOLEAN));
187             } else if (typeName.trim().equals("id")) {
188               typeMap.put(typeId, new Integer(ID));
189             } else if (typeName.trim().equals("lookup")) {
190               typeMap.put(typeId, new Integer(LOOKUP));
191             } else if (typeName.trim().equals("password")) {
192               typeMap.put(typeId, new Integer(PASSWORD));
193             } else if (typeName.trim().equals("long")) {
194               typeMap.put(typeId, new Integer(LONG));
195             } else if (typeName.trim().equals("datetime")) {
196               typeMap.put(typeId, new Integer(DATETIME));
197             } else if (typeName.trim().equals("autorandom")) {
198               typeMap.put(typeId, new Integer(AUTORANDOM));
199             } else if (typeName.trim().equals("preformattedtextarea")) {
200               typeMap.put(typeId, new Integer(PREFORMATTEDTEXTAREA));
201             } else if (typeName.trim().equals("escapedtext")) {
202               typeMap.put(typeId, new Integer(ESCAPEDTEXT));
203             } else if (typeName.trim().equals("currency")) {
204               typeMap.put(typeId, new Integer(CURRENCY));
205             } else if (typeName.trim().equals("lookupnodrop")) {
206               typeMap.put(typeId, new Integer(LOOKUPNODROP));
207             } else if (typeName.trim().equals("timestamp")) {
208               typeMap.put(typeId, new Integer(TIMESTAMP));
209             } else if (typeName.trim().equals("uploadurl")) {
210               typeMap.put(typeId, new Integer(UPLOADURL));
211             } else if (typeName.trim().equals("datedrop")) {
212               typeMap.put(typeId, new Integer(DATEDROP));
213             } else if (typeName.trim().equals("datetimedrop")) {
214               typeMap.put(typeId, new Integer(DATETIMEDROP));
215             } else {
216               throw new HandlerException("Unrecognised type:" + typeName);
217             }
218 
219           }
220         } catch (Exception e) {
221           throw new HandlerException(e.toString());
222         }
223 
224         sqlString =
225           "SELECT * FROM datadictionarytables ORDER BY type, displayorder";
226         ResultSet tablesRS;
227         Statement s2;
228         try {
229           s2 = conn.createStatement();
230         } catch (Exception e) {
231           throw new HandlerException(e.toString());
232         }
233         try {
234           tablesRS = s2.executeQuery(sqlString);
235         } catch (Exception e) {
236           throw new HandlerException(e.toString());
237         }
238         pw.println("package org.paneris." + db + ".poem;");
239         pw.println("");
240 
241         try {
242           while (tablesRS.next()) {
243 
244             DDRecord tableRec = null;
245             try {
246               tableRec = new DDRecord(db, "datadictionarytables", tablesRS);
247             } catch (Exception e) {
248               throw new HandlerException(e.toString());
249             }
250 
251             if (tableRec.getFieldValue("type").equals("3")) {
252               //ignore system tables
253             } else {
254 
255               //id,tablename,displayname,type,
256               //displayorder,cached,
257               //defaultresultsperpage,
258               //maxresults,listedit,
259               //updatecontroller
260               pw.println(
261                 "table " + capitalised(tableRec.getFieldValue("tablename")));
262               pw.println(
263                 "   (displayorder = "
264                   + tableRec.getFieldValue("displayorder").trim()
265                   + ")");
266               pw.println(
267                 "   (displayname = \""
268                   + tableRec.getFieldValue("displayname").trim()
269                   + "\")");
270               pw.println(
271                 "   (description = \""
272                   + tableRec.getFieldValue("displayname").trim()
273                   + "\")");
274               if (tableRec.getFieldValue("type").equals("1")) {
275                 pw.println("   (category = \"Data\")");
276               } else if (tableRec.getFieldValue("type").equals("2")) {
277                 pw.println("   (category = \"Code\")");
278               } else if (tableRec.getFieldValue("type").equals("4")) {
279                 pw.println("   (category = \"Bridge\")");
280               } else if (tableRec.getFieldValue("type").equals("5")) {
281                 pw.println("   (category = \"Authorisation\")");
282               } else {
283                 throw new Exception(
284                   "Unrecognised table type: " + tableRec.getFieldValue("type"));
285               }
286               if (tableRec.getFieldValue("cached").equals("true")) {
287                 pw.println("   (cachelimit = unlimited)");
288                 pw.println("   (seqcached)");
289 
290               }
291               pw.println("   {");
292               sqlString =
293                 "SELECT * FROM datadictionary"
294                   + " WHERE tablename = "
295                   + tableRec.getFieldValue("id").trim()
296                   + " ORDER BY displayorder";
297               // System.err.println("Sqlstring:" + sqlString);
298               ResultSet fieldsRS = null;
299               Statement s3;
300               try {
301                 s3 = conn.createStatement();
302               } catch (Exception e) {
303                 throw new HandlerException(e.toString());
304               }
305               try {
306                 fieldsRS = s3.executeQuery(sqlString);
307               } catch (Exception e) {
308                 throw new HandlerException(e.toString());
309               }
310               //"id","fieldname","tablename","displayname","type",
311               //"displayorder","size","height","selectdisplay",
312               //"mandatory","enforceunique",
313               //"relationshiptable","relationshipfield",
314               //"listdisplay","defaultvalue"
315               while (fieldsRS.next()) {
316                 DDRecord fieldRec = null;
317                 try {
318                   fieldRec = new DDRecord(db, "datadictionary", fieldsRS);
319                 } catch (Exception e) {
320                   throw new HandlerException(e.toString());
321                 }
322                 pw.println("");
323 
324                 String type = fieldRec.getFieldValue("type");
325                 // System.err.println("type:" + type);
326                 Integer key = Integer.valueOf(type);
327                 Integer typeI = (Integer) typeMap.get(key);
328                 if (typeI == null) throw new HandlerException("Type not found for " + key);
329                 switch (typeI.intValue()) {
330                   case TEXT : //'text','Text');
331                     pw.println(
332                       "      String " + fieldRec.getFieldValue("fieldname"));
333 
334                     pw.println("        (size = unlimited)");
335                     pw.println(
336                       "        (width = "
337                         + fieldRec.getFieldValue("size").trim()
338                         + ")");
339                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
340                       pw.println("        (nullable)");
341                     }
342 
343                     pw.println(
344                       "        (displayname = \""
345                         + fieldRec.getFieldValue("displayname").trim()
346                         + "\")");
347                     pw.println(
348                       "        (description = \""
349                         + fieldRec.getFieldValue("displayname").trim()
350                         + "\")");
351                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
352                       pw.println("        (displaylevel = summary)");
353                     } else {
354                       pw.println("        (displaylevel = record)");
355                     }
356                     pw.println("        (searchability = yes)");
357                     pw.println(
358                       "        (renderinfo=\"org.melati.poem.StringPoemType\");");
359 
360                     break;
361                   case TEXTAREA : //'textarea','Text Area');
362                     pw.println(
363                       "      String " + fieldRec.getFieldValue("fieldname"));
364 
365                     pw.println("        (size = unlimited)");
366                     pw.println(
367                       "        (width = "
368                         + fieldRec.getFieldValue("size").trim()
369                         + ")");
370                     try {
371                       String height = fieldRec.getFieldValue("height");
372                       if (height == null) height = "0";
373                       if (height.equals("")) height = "0";
374                       if (Integer.parseInt(height.trim()) > 0) {
375                         pw.println(
376                           "        (height = "
377                             + height.trim()
378                             + ")");
379                      }
380                     } catch  (Exception e) { throw new HandlerException(e.toString()); }
381                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
382                       pw.println("        (nullable)");
383                     }
384                     pw.println(
385                       "        (displayname = \""
386                         + fieldRec.getFieldValue("displayname").trim()
387                         + "\")");
388                     pw.println(
389                       "        (description = \""
390                         + fieldRec.getFieldValue("displayname").trim()
391                         + "\")");
392                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
393                       pw.println("        (displaylevel = summary)");
394                     } else {
395                       pw.println("        (displaylevel = record)");
396                     }
397                     pw.println("        (searchability = yes)");
398                     pw.println(
399                       "        (renderinfo=\"org.melati.poem.StringPoemType\");");
400 
401                     break;
402                   case INTEGER : //'integer','integer');
403                     pw.println(
404                       "      Integer " + fieldRec.getFieldValue("fieldname"));
405                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
406                       pw.println("        (nullable)");
407                     }
408                     pw.println(
409                       "        (displayname = \""
410                         + fieldRec.getFieldValue("displayname").trim()
411                         + "\")");
412                     pw.println(
413                       "        (description = \""
414                         + fieldRec.getFieldValue("displayname").trim()
415                         + "\")");
416                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
417                       pw.println("        (displaylevel = summary)");
418                     } else {
419                       pw.println("        (displaylevel = record)");
420                     }
421                     pw.println("        (searchability = no)");
422                     pw.println(
423                       "        (renderinfo=\"org.melati.poem.IntegerPoemType\");");
424                     break;
425                   case NUMBER : //'number','Number');
426                     pw.println(
427                       "       Double " + fieldRec.getFieldValue("fieldname"));
428                     if (fieldRec.getFieldValue("mandatory").equals("true")) {
429                     } else {
430                       pw.println("        (nullable)");
431                     }
432                     pw.println(
433                       "        (displayname = \""
434                         + fieldRec.getFieldValue("displayname").trim()
435                         + "\")");
436                     pw.println(
437                       "        (description = \""
438                         + fieldRec.getFieldValue("displayname").trim()
439                         + "\")");
440                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
441                       pw.println("        (displaylevel = summary)");
442                     } else {
443                       pw.println("        (displaylevel = record)");
444                     }
445                     pw.println("        (searchability = no)");
446                     pw.println(
447                       "        (renderinfo=\"org.melati.poem.DoublePoemType\");");
448                     break;
449                   case DATE : //'date','Date');
450                     pw.println(
451                       "      Date " + fieldRec.getFieldValue("fieldname"));
452                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
453                       pw.println("        (nullable)");
454                     }
455                     pw.println(
456                       "        (displayname = \""
457                         + fieldRec.getFieldValue("displayname").trim()
458                         + "\")");
459                     pw.println(
460                       "        (description = \""
461                         + fieldRec.getFieldValue("displayname").trim()
462                         + "\")");
463                     pw.println("        (displaylevel = record)");
464                     pw.println("        (searchability = no)");
465                     pw.println(
466                       "        (renderinfo=\"org.melati.poem.DatePoemType\");");
467                     break;
468                   case BOOLEAN : //'boolean','Boolean');
469                     pw.println(
470                       "       Boolean " + fieldRec.getFieldValue("fieldname"));
471                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
472                       pw.println("        (nullable)");
473                     }
474                     pw.println(
475                       "        (displayname = \""
476                         + fieldRec.getFieldValue("displayname").trim()
477                         + "\")");
478                     pw.println(
479                       "        (description = \""
480                         + fieldRec.getFieldValue("displayname").trim()
481                         + "\")");
482                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
483                       pw.println("        (displaylevel = summary)");
484                     } else {
485                       pw.println("        (displaylevel = record)");
486                     }
487                     pw.println("        (searchability = no)");
488                     pw.println(
489                       "        (renderinfo=\"org.melati.poem.BooleanPoemType\");");
490 
491                     break;
492                   case ID : //'id','ID');
493                     pw.println("     (primary) Integer id;");
494                     break;
495                   case LOOKUP : //'lookup','Lookup Field');
496                     {
497                         DDRecord relTable = null;
498                         try {
499                           relTable = new DDRecord(
500                             db,
501                             new Integer(
502                               fieldRec.getFieldValue("relationshiptable")));
503                         } catch (Exception e) { 
504                           throw new HandlerException(e.toString() + 
505                               " : relTable null for LOOKUP, type:"+ type + 
506                               " name:" + fieldRec.getFieldValue("fieldname") +
507                               " rt:" + fieldRec.getFieldValue("relationshiptable")); 
508                         }
509                       String relTableName = relTable.metaData.getTableName();
510 
511                       pw.println(
512                         "        "
513                           + capitalised(relTableName)
514                           + " "
515                           + fieldRec.getFieldValue("fieldname"));
516                       if (fieldRec
517                         .getFieldValue("mandatory")
518                         .equals("false")) {
519                         pw.println("        (nullable)");
520                       }
521                       pw.println(
522                         "        (displayname = \""
523                           + fieldRec.getFieldValue("displayname").trim()
524                           + "\")");
525                       pw.println(
526                         "        (description = \""
527                           + fieldRec.getFieldValue("displayname").trim()
528                           + "\")");
529                       if (fieldRec
530                         .getFieldValue("listdisplay")
531                         .equals("true")) {
532                         pw.println("        (displaylevel = summary)");
533                       } else {
534                         pw.println("        (displaylevel = record)");
535                       }
536                       pw.println("        (searchability = yes);");
537                     }
538                     break;
539                   case PASSWORD : //'password','Password');
540                     pw.println(
541                       "      Password " + fieldRec.getFieldValue("fieldname"));
542                     pw.println("        (size = unlimited)");
543                     pw.println(
544                       "        (width = "
545                         + fieldRec.getFieldValue("size").trim()
546                         + ")");
547                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
548                       pw.println("        (nullable)");
549                     }
550                     pw.println(
551                       "        (displayname = \""
552                         + fieldRec.getFieldValue("displayname").trim()
553                         + "\")");
554                     pw.println(
555                       "        (description = \""
556                         + fieldRec.getFieldValue("displayname").trim()
557                         + "\")");
558                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
559                       pw.println("        (displaylevel = summary)");
560                     } else {
561                       pw.println("        (displaylevel = record)");
562                     }
563                     pw.println("        (searchability = no)");
564                     pw.println(
565                       "        (renderinfo=\"org.melati.poem.PasswordPoemType\");");
566                     break;
567                   case LONG : //'long','Long int4');
568                     pw.println(
569                       "      Integer " + fieldRec.getFieldValue("fieldname"));
570                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
571                       pw.println("        (nullable)");
572                     }
573                     pw.println(
574                       "        (displayname = \""
575                         + fieldRec.getFieldValue("displayname").trim()
576                         + "\")");
577                     pw.println(
578                       "        (description = \""
579                         + fieldRec.getFieldValue("displayname").trim()
580                         + "\")");
581                     pw.println("        (displaylevel = record)");
582                     pw.println("        (searchability = no)");
583                     pw.println(
584                       "        (renderinfo=\"org.melati.poem.IntegerPoemType\");");
585                     break;
586                   case DATETIME : //'datetime','Date / Time');
587                     pw.println(
588                       "      Timestamp " + fieldRec.getFieldValue("fieldname"));
589                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
590                       pw.println("        (nullable)");
591                     }
592                     pw.println(
593                       "        (displayname = \""
594                         + fieldRec.getFieldValue("displayname").trim()
595                         + "\")");
596                     pw.println(
597                       "        (description = \""
598                         + fieldRec.getFieldValue("displayname").trim()
599                         + "\")");
600                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
601                       pw.println("        (displaylevel = summary)");
602                     } else {
603                       pw.println("        (displaylevel = record)");
604                     }
605                     pw.println("        (searchability = no)");
606                     pw.println(
607                       "        (renderinfo=\"org.melati.poem.TimestampPoemType\");");
608                     break;
609                   case AUTORANDOM : //'autorandom','Automatically sent to a random value when record created');
610                     pw.println("//FIXME - Autorandom field should be removed");
611                     pw.println(
612                       "      String " + fieldRec.getFieldValue("fieldname"));
613 
614                     pw.println("        (size = unlimited)");
615                     if (fieldRec.getFieldValue("size").trim().equals("0")) {
616                       pw.println("        (width = 50)");
617                     } else {
618                       pw.println(
619                         "        (width = "
620                           + fieldRec.getFieldValue("size").trim()
621                           + ")");
622                     }
623                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
624                       pw.println("        (nullable)");
625                     }
626                     pw.println(
627                       "        (displayname = \""
628                         + fieldRec.getFieldValue("displayname").trim()
629                         + "\")");
630                     pw.println(
631                       "        (description = \""
632                         + fieldRec.getFieldValue("displayname").trim()
633                         + "\")");
634                     pw.println("        (displaylevel = record)");
635                     pw.println("        (searchability = no)");
636                     pw.println(
637                       "        (renderinfo=\"org.melati.poem.StringPoemType\");");
638                     message += "Autorandom field "
639                       + fieldRec.getFieldValue("displayname")
640                       + " found in table "
641                       + capitalised(tableRec.getFieldValue("tablename"))
642                       + "<br>\n";
643 
644                     break;
645                   case PREFORMATTEDTEXTAREA : //'preformattedtextarea','Preformatted Text Area');
646                     pw.println(
647                       "      String " + fieldRec.getFieldValue("fieldname"));
648 
649                     pw.println("        (size = unlimited)");
650                     pw.println(
651                       "        (width = "
652                         + fieldRec.getFieldValue("size").trim()
653                         + ")");
654 
655                     try {
656                       String height = fieldRec.getFieldValue("height");
657                       if (height == null) height = "0";
658                       if (height.equals("")) height = "0";
659                       if (Integer
660                         .parseInt(height.trim())
661                         > 0) {
662                         pw.println(
663                           "        (height = "
664                             + height.trim()
665                             + ")");
666                       }
667                     } catch (Exception e) { throw new HandlerException(e.toString()); }
668                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
669                       pw.println("        (nullable)");
670                     }
671                     pw.println(
672                       "        (displayname = \""
673                         + fieldRec.getFieldValue("displayname").trim()
674                         + "\")");
675                     pw.println(
676                       "        (description = \""
677                         + fieldRec.getFieldValue("displayname").trim()
678                         + "\")");
679                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
680                       pw.println("        (displaylevel = summary)");
681                     } else {
682                       pw.println("        (displaylevel = record)");
683                     }
684                     pw.println("        (searchability = no)");
685                     pw.println(
686                       "        (renderinfo=\"org.melati.poem.StringPoemType\");");
687                     break;
688                   case ESCAPEDTEXT : //'escapedtext','Escaped Text');
689                     pw.println(
690                       "      String " + fieldRec.getFieldValue("fieldname"));
691 
692                     pw.println("        (size = unlimited)");
693                     pw.println(
694                       "        (width = "
695                         + fieldRec.getFieldValue("size").trim()
696                         + ")");
697                     try {
698                       String height = fieldRec.getFieldValue("height");
699                       if (height == null) height = "0";
700                       if (height.equals("")) height = "0";
701                       if (Integer
702                         .parseInt(height.trim())
703                         > 0) {
704                         pw.println(
705                           "        (height = "
706                             + height.trim()
707                             + ")");
708                       }
709                     } catch (Exception e) { throw new HandlerException(e.toString()); }
710                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
711                       pw.println("        (nullable)");
712                     }
713                     pw.println(
714                       "        (displayname = \""
715                         + fieldRec.getFieldValue("displayname").trim()
716                         + "\")");
717                     pw.println(
718                       "        (description = \""
719                         + fieldRec.getFieldValue("displayname").trim()
720                         + "\")");
721                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
722                       pw.println("        (displaylevel = summary)");
723                     } else {
724                       pw.println("        (displaylevel = record)");
725                     }
726                     pw.println("        (searchability = no)");
727                     pw.println(
728                       "        (renderinfo=\"org.melati.poem.StringPoemType\");");
729                     break;
730                   case CURRENCY : //'currency','Currency');
731                     pw.println(
732                       "      Double " + fieldRec.getFieldValue("fieldname"));
733                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
734                       pw.println("        (nullable)");
735                     }
736                     pw.println(
737                       "        (displayname = \""
738                         + fieldRec.getFieldValue("displayname").trim()
739                         + "\")");
740                     pw.println(
741                       "        (description = \""
742                         + fieldRec.getFieldValue("displayname").trim()
743                         + "\")");
744                     pw.println("        (displaylevel = record)");
745                     pw.println("        (searchability = no)");
746                     pw.println(
747                       "        (renderinfo=\"org.melati.poem.DoublePoemType\");");
748                     break;
749                   case LOOKUPNODROP : //'lookupnodrop','Lookup Field(no dropdown)');
750                     {
751                         DDRecord relTable = null;
752                         try {
753                           relTable = new DDRecord(
754                             db,
755                             new Integer(
756                               fieldRec.getFieldValue("relationshiptable")));
757                         } catch (Exception e) { 
758                           throw new HandlerException(e.toString() + 
759                               " : relTable null for LOOKUPNODROP, type:" + type + 
760                               "name:" + fieldRec.getFieldValue("fieldname")); 
761                         }
762                       String relTableName = relTable.metaData.getTableName();
763 
764                       pw.println(
765                         "        "
766                           + capitalised(relTableName)
767                           + " "
768                           + fieldRec.getFieldValue("fieldname"));
769                       if (fieldRec
770                         .getFieldValue("mandatory")
771                         .equals("false")) {
772                         pw.println("        (nullable)");
773                       }
774                       pw.println(
775                         "        (displayname = \""
776                           + fieldRec.getFieldValue("displayname").trim()
777                           + "\")");
778                       pw.println(
779                         "        (description = \""
780                           + fieldRec.getFieldValue("displayname").trim()
781                           + "\")");
782                       pw.println("        (displaylevel = record)");
783                       pw.println("        (searchability = yes);");
784                     }
785                     break;
786                   case TIMESTAMP : //'timestamp','Timestamp');
787                     pw.println(
788                       "      Timestamp " + fieldRec.getFieldValue("fieldname"));
789                     pw.println(
790                       "        (displayname = \""
791                         + fieldRec.getFieldValue("displayname").trim()
792                         + "\")");
793                     pw.println(
794                       "        (description = \""
795                         + fieldRec.getFieldValue("displayname").trim()
796                         + "\")");
797                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
798                       pw.println("        (displaylevel = summary)");
799                     } else {
800                       pw.println("        (displaylevel = record)");
801                     }
802                     pw.println("        (searchability = no)");
803                     pw.println(
804                       "        (renderinfo=\"org.melati.poem.TimestampPoemType\");");
805                     break;
806                   case UPLOADURL : //'uploadurl','URL for an uploaded file');
807                     pw.println(
808                       "      String " + fieldRec.getFieldValue("fieldname"));
809 
810                     pw.println("        (size = unlimited)");
811                     pw.println(
812                       "        (width = "
813                         + fieldRec.getFieldValue("size").trim()
814                         + ")");
815                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
816                       pw.println("        (nullable)");
817                     }
818                     pw.println(
819                       "        (displayname = \""
820                         + fieldRec.getFieldValue("displayname").trim()
821                         + "\")");
822                     pw.println(
823                       "        (description = \""
824                         + fieldRec.getFieldValue("displayname").trim()
825                         + "\")");
826                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
827                       pw.println("        (displaylevel = summary)");
828                     } else {
829                       pw.println("        (displaylevel = record)");
830                     }
831                     pw.println("        (searchability = no)");
832                     pw.println(
833                       "        (renderinfo=\"org.melati.poem.UploadURLPoemType\");");
834 
835                     break;
836 
837                   case DATEDROP : //'datedrop','Date Drop List');
838                     pw.println(
839                       "      Date " + fieldRec.getFieldValue("fieldname"));
840                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
841                       pw.println("        (nullable)");
842                     }
843                     pw.println(
844                       "        (displayname = \""
845                         + fieldRec.getFieldValue("displayname").trim()
846                         + "\")");
847                     pw.println(
848                       "        (description = \""
849                         + fieldRec.getFieldValue("displayname").trim()
850                         + "\")");
851                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
852                       pw.println("        (displaylevel = summary)");
853                     } else {
854                       pw.println("        (displaylevel = record)");
855                     }
856                     pw.println("        (searchability = no)");
857                     pw.println(
858                       "        (renderinfo=\"org.melati.poem.DatePoemType-dropdown\");");
859                     break;
860                   case DATETIMEDROP : //'datetimedrop','Date/Time Drop List');
861                     pw.println(
862                       "      Date " + fieldRec.getFieldValue("fieldname"));
863                     if (fieldRec.getFieldValue("mandatory").equals("false")) {
864                       pw.println("        (nullable)");
865                     }
866                     pw.println(
867                       "        (displayname = \""
868                         + fieldRec.getFieldValue("displayname").trim()
869                         + "\")");
870                     pw.println(
871                       "        (description = \""
872                         + fieldRec.getFieldValue("displayname").trim()
873                         + "\")");
874                     if (fieldRec.getFieldValue("listdisplay").equals("true")) {
875                       pw.println("        (displaylevel = summary)");
876                     } else {
877                       pw.println("        (displaylevel = record)");
878                     }
879                     pw.println("        (searchability = no)");
880                     pw.println(
881                       "        (renderinfo=\"org.melati.poem.DatePoemType-dropdown\");");
882                     break;
883 
884                   default :
885                     throw new HandlerException(
886                       "Unrecognised field type: "
887                         + fieldRec.getFieldValue("type"));
888                 }
889               }
890               pw.println("   }");
891               pw.println("");
892             }
893           }
894 
895           pw.close();
896 
897         } catch (Exception e) {
898           e.printStackTrace(System.err);
899           throw new HandlerException(e.toString());
900         }
901 
902       }
903     }
904 
905     context.put("message", message);
906 
907     //
908     // Done the work. Return the template
909     //
910 
911     connMgr.freeConnection(db, conn);
912     // return the appropriate template
913     try {
914       return (Template) context.getBroker().get("template", templateName);
915     } catch (Exception e) {
916       throw new HandlerException("Could not locate template: " + templateName);
917     }
918   }
919 
920   /**
921    * @param name
922    * @return name with first letter in upper case
923    */
924   public static String capitalised(String name) {
925     char suffix[] = name.toCharArray();
926     suffix[0] = Character.toUpperCase(suffix[0]);
927     return new String(suffix);
928   }
929 }