View Javadoc

1   /*
2    * $Id: TableMetaDocRenderer.java,v 1.7 2006/09/26 15:34:42 clq2 Exp $
3    */
4   package org.astrogrid.tableserver.metadata;
5   
6   import java.io.IOException;
7   import org.astrogrid.dataservice.metadata.MetadataException;
8   import org.astrogrid.dataservice.service.DataServer;
9   import org.astrogrid.dataservice.service.ServletHelper;
10  import org.astrogrid.tableserver.metadata.ColumnInfo;
11  import org.astrogrid.tableserver.metadata.TableInfo;
12  import org.astrogrid.tableserver.metadata.TableMetaDocInterpreter;
13  import org.astrogrid.ucd.Ucd1Dictionary;
14  import org.astrogrid.ucd.Ucd1PlusDictionary;
15  import org.astrogrid.units.UnitDictionary;
16  import org.w3c.dom.Element;
17  import org.astrogrid.tableserver.test.SampleStarsPlugin;
18  import org.astrogrid.cfg.ConfigFactory;
19  import org.astrogrid.dataservice.queriers.DatabaseAccessException;
20  
21  /***
22   * Renders an RdbmsMetadata resource document in HTML suitable for including
23   * within the body element.  We could probably use
24   * an XSL sheet for this, but I can't be bothered as this is easier for me :-)
25   */
26  
27  public class TableMetaDocRenderer {
28     
29     public String renderMetaDoc()  {
30        StringBuffer html = new StringBuffer();
31        html.append("<h2>Table Meta-document for "+DataServer.getDatacenterName()+"</h2>");
32        
33        // Initialise SampleStars plugin if required (may not be initialised
34        // if admin has not run the self-tests)
35         try {
36           String plugin = ConfigFactory.getCommonConfig().getString(
37                "datacenter.querier.plugin");
38           if (plugin.equals(
39                  "org.astrogrid.tableserver.test.SampleStarsPlugin")) {
40              // This has no effect if the plugin is already initialised
41              SampleStarsPlugin.initialise();  // Just in case
42           }
43        }
44        catch (DatabaseAccessException dae) {
45           html.append(ServletHelper.exceptionAsHtml("Accessing database to generate Rdbms Table Metadoc ", dae, null));
46           return html.toString();
47        }
48  
49        Element metadoc = null;
50        try {
51           TableMetaDocInterpreter interpreter = new TableMetaDocInterpreter();
52  
53           String[] catalogs = interpreter.getCatalogs();
54           
55           if (catalogs.length == 0) {
56             html.append(ServletHelper.exceptionAsHtml("Server error: no catalog or table metadata are defined for this DSA/catalog installation;  please check your metadoc file and/or configuration!", new MetadataException("Metadata configuration error"), null));
57              return html.toString();
58           }
59           for (int cat = 0; cat < catalogs.length; cat++) {
60              TableInfo[] tables = interpreter.getTables(catalogs[cat]);
61              for (int table=0;table<tables.length;table++) {
62                 renderTable(html, interpreter, tables[table]);
63              }
64           }
65           return html.toString();
66        }
67        catch (IOException ioe) {
68           html.append(ServletHelper.exceptionAsHtml("Generating Rdbms Table Metadoc ", ioe, null));
69           return html.toString();
70        }
71     }
72  
73     public void renderTable(StringBuffer html, TableMetaDocInterpreter interpreter, TableInfo table) throws MetadataException {
74        html.append(
75           "<h3>Table '"+table.getName()+"'</h3>"+
76              "<p>"+emptyIfNull(table.getDescription()) +"</p>"+
77              "<p>"+
78              "<table border=1 summary='Column details for table "+table.getName()+"' cellpadding='5%'>"+
79                 "<tr>"+
80                    "<th>Column</th>"+
81                    "<th>Type</th>"+
82                    "<th><a href='"+ UnitDictionary.UNIT_REF+"'>Units</a></th>"+
83                    "<th>Dim Eq</th>"+
84                    "<th>Scale</th>"+
85                    "<th><a href='"+ Ucd1Dictionary.REF+"'>UCD1</a></th>"+
86                    "<th><a href='"+ Ucd1PlusDictionary.REF+"'>UCD1+</a></th>"+
87                    "<th>Error</th>"+
88                    "<th>Description</th>"+
89                    "<th>Links</th>"+
90                 "</tr>");
91  
92        ColumnInfo[] columns = interpreter.getColumns(table.getDataset(), table.getName());
93     
94        for (int col=0;col<columns.length;col++) {
95  
96           renderColumn(html, columns[col]);
97        }
98        
99        html.append(
100             "</table>");
101    }
102    
103    public void renderColumn(StringBuffer html, ColumnInfo column) {
104       
105       html.append(
106          "<tr>"+
107             "<th>"+column.getName() +"</th>"+
108             "<td>"+emptyIfNull(column.getPublicType())+"</td>"+
109             "<td>"+column.getUnits().toString() +"</td>"+
110             "<td>"+column.getUnits().getDimEq() +"</td>"+
111             "<td>"+column.getUnits().getDimScale()  +"</td>"+
112             "<td>"+emptyIfNull(column.getUcd("1")) +"</td>"+
113             "<td>"+emptyIfNull(column.getUcd("1+")) +"</td>"+
114             "<td>"+emptyIfNull(column.getErrorField()) +"</td>"+
115             "<td>"+emptyIfNull(column.getDescription()) +"</td>");
116 
117       //links
118       String[] links = column.getLinks();
119       for (int i = 0; i < links.length; i++) {
120       //   String name = links[i].getAttribute("Text");
121       //   String link = DomHelper.getValueOf(links[i]);
122 //         if ((name == null) || (name.length() ==0)) {
123 //            name = link;
124 //         }
125          html.append(
126             "<td><a href='"+links[i]+"'>"+links[i]+"</a></td>");
127       }
128 
129       html.append(
130          "</tr>");
131    }
132 
133    public static String emptyIfNull(String s) {
134       if (s==null) {
135          return "";
136       }
137       else return s;
138    }
139 
140 }
141