ShapeDemo.java

Go to the documentation of this file.
00001 package cedar.hepdata.webapp;
00002 
00003 import org.hibernate.Session;
00004 import org.hibernate.Query;
00005 
00006 import cedar.hepdata.model.*;
00007 import cedar.hepdata.xml.*;
00008 import cedar.hepdata.util.*;
00009 import cedar.hepdata.db.*;
00010 
00011 import java.io.*;
00012 import java.util.*;
00013 import javax.servlet.*;
00014 import javax.servlet.http.*;
00015 
00016 import java.text.*;
00017 import java.lang.*;
00018 
00019 import org.exolab.castor.xml.Unmarshaller;
00020 
00021 
00027 public class ShapeDemo extends HttpServlet {
00028 
00030     public String getServletInfo() {
00031         return "Test servlet. Do not use.";
00032     }
00033 
00034 
00036     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
00037         response.setContentType("text/html");
00038         PrintWriter out = response.getWriter();
00039 
00040         out.println("<h1>Shape demo (DB)</h1>");
00041         out.println("<p>Reading in data from a Database !!!</p>");
00042 
00043         out.println(request.getParameter("irn"));
00044         String irn = request.getParameter("irn");
00045         int spiresid = Integer.parseInt(irn);
00046 
00047         String dataset = null;
00048         String yaxis = null;
00049         dataset = request.getParameter("dataset");
00050         yaxis = request.getParameter("yaxis");
00051         
00052         Paper p = null;
00053         try {
00054 // getting from xml file
00055 //            Data data = XMLUtils.getDataFromHepML(new File("/home/whalley/hd/hepdata-migration/shapes-hepml/"+ irn + ".hep.xml"));
00056 //          if (data != null) p = data.getFirstPaper();
00058 // getting from the db
00059             DbUtils.beginTransaction();
00060             Query q = DbUtils.createQuery("from Paper as p where p._spiresId = :irn"); 
00061             q.setLong("irn", spiresid) ;
00062             List results = q.list();
00063             for (Object oPaper : results) {
00064                 p = (Paper) oPaper;
00066                 if (p == null) throw new Exception("Null paper");
00067 
00068                 this.displayHeader(p,out);
00069                 if(dataset == null){
00070                    this.displayDatasetError(p,out,String.valueOf(1));
00071                 }
00072                 else{
00073                     this.displayDatasetError(p,out,dataset);
00074                 }
00075                 if(dataset == null){
00076                     this.displayDatasets(p,out);
00077                 }
00078                 else if(yaxis == null){
00079                     this.displayDataset(p,out,dataset);
00080                 }
00081                 else                  {
00082                    this.displayDataset(p,out,dataset,yaxis);
00083                 }
00084             }
00085             DbUtils.endTransaction();
00086         } catch (Exception e) {
00087             out.println("Exception: " + e.toString());
00088             out.println("<pre>");
00089             e.printStackTrace(out);
00090             out.println("</pre>");
00091             return;
00092         }
00093     }
00094 
00096     private void displayDatasets(Paper p,PrintWriter out){
00097         int nds = 0;
00098         for ( Dataset d : p.getDatasets() ) {
00099               nds++;
00100               String dataset = String.valueOf(nds);
00101               this.displayDataset(p,out,dataset);
00102         }
00103     }
00104 
00106     private void displayDataset(Paper p,PrintWriter out,String dataset){
00107         int ds = Integer.parseInt(dataset);
00108         Dataset d = p.getDataset(ds);
00109         int ya=0;  
00110         for (YAxis y : d.getYAxes()){
00111             ya++;
00112             String yaxis = String.valueOf(ya);
00113             this.displayDataset(p,out,dataset,yaxis);
00114         }
00115     }
00116 
00118     private void displayDataset(Paper p,PrintWriter out,String dataset,String yaxis){
00119         int ds = Integer.parseInt(dataset);
00120         int ya = Integer.parseInt(yaxis);
00121         Dataset d = p.getDataset(ds);
00122         YAxis y = d.getYAxis(ya);
00123 // count the number of x and y axes and the number of bins
00124 
00125         int nx = d.getXAxes().size();
00126         int npoints = 0;
00127         for (XAxis x : d.getXAxes()){npoints = x.getBins().size();}
00128            
00129 // counting done
00131 // output each y axis in turn in a separate table
00132 // first the reactions for this yaxis
00133         out.println("<table border=2>");
00134         int nspan = nx+1;
00135         out.println("<tr><th colspan=" + nspan + ">Dataset " + ds + " yaxis " + ya + "</th></tr>");
00136         for ( Reaction reac : y.getReactions() ){
00137             out.println("<tr><th colspan=" + nspan +">");
00138         
00139             for ( Particle part : reac.getInitialState() ){
00140                 out.println(part.getName() + " ");
00141             }
00142             out.println("-->");
00143             for ( Particle part : reac.getFinalState() ){
00144                 if (part.getMultRelation() != Relation.EQUALS) {out.println( part.getMultRelation() );}
00145                 if (part.getMultiplicity() > 1) {out.println( part.getMultiplicity() );}
00146                 out.println(part.getName() + " ");
00147             }
00148             out.println("</th></tr>");
00149         }
00150 
00151 // next the yaxis comments (non-numerical properties!)
00152         for (String comment : y.getComments()){
00153             out.println("<tr><th colspan=" + nspan +">" + comment + "</th></tr>");
00154         }
00155               
00156 // next the properties
00157         for ( Property prop: y.getProperties() ){
00158             out.println("<tr><th colspan=" + nspan +">");
00159             out.println( prop.getName() + " : ");
00160             if (prop.getLowValue() != null && prop.getHighValue() != null &&
00161                 prop.getLowValue().equals(prop.getHighValue())) {
00162                 out.println(prop.getLowValue());
00163             }
00164             else{out.println(prop.getLowValue() + " TO " + prop.getHighValue());}
00165             if (!prop.getUnit().toString().equals("num")){
00166                out.println(" " + prop.getUnit().toString());
00167             }
00168             out.println("</th></tr>" ) ;
00169         }             
00170 
00171 // here for the headers
00172         out.println("<tr>");
00173         for (XAxis x : d.getXAxes()){
00174             out.println("<th>" + x.getHeader() + "</th>");
00175         }
00176         out.println("<th>" + y.getHeader() + "</th>");
00177         out.println("</tr>");
00178               
00179 // finally the numbers themselves
00180 // first the xaxes
00181         Double f = null;
00182         Double high = null;
00183         Double low = null;
00184         out.println("<tr>");
00185         for (int ix=1 ; ix<=nx; ix++){
00186             XAxis xaxis = d.getXAxis(ix);
00187             out.println("<td><pre>");
00188             for (int ip=1 ; ip<=npoints; ip++){
00189                 for (Bin b : xaxis.getBins()){
00190                     if(b.getId() == ip) {
00191                         low = b.getLowValue();
00192                         high = b.getHighValue();
00193                         f = b.getFocus();
00194                         if(low != null){
00195                             out.println(forms(high,8,out) + " TO " + forms(low,8,out)); 
00196                         }
00197                         else{
00198                             out.println(forms(f,8,out)); 
00199                         }
00200                     }           
00201                 }               
00202             }           
00203             out.println("</pre></td>");
00204         }
00205          
00206 // next the yaxis
00207         
00208         StringBuffer s = new StringBuffer();
00209         s.append("<td><pre>");
00210         for (int ip=1 ; ip<=npoints; ip++){
00211             try {
00212                 Point pt2 = y.getPoint(ip);
00213                 s.append(forms(pt2.getValue(),15,out)); 
00214                 for ( Uncertainty e : pt2.getErrors() ){
00215                     if( e.getPlus().equals(e.getMinus()) ) {
00216                         s.append(" +- " + forms(e.getPlus(),15,out) );
00217                     }
00218                     else{
00219                         s.append(" +" + forms(e.getPlus(),15,out) + ",-" + forms(e.getMinus(),15,out) );
00220                     }
00221                     if ( e.getNormType() == ErrorNorm.PCT ) {
00222                         s.append(" (" + e.getNormType().toString() + ")");
00223                     }
00224                 }
00225                 s.append("<br>");
00226             } 
00227             catch (Exception e) {
00228                 s.append("  -  <br>");
00229             }
00230         }
00231         s.append("</pre></td>");
00232         out.println(s);
00233         out.println("</tr>");
00234         out.println("</table>");
00235     }
00236 
00238     private void displayHeader(Paper p,PrintWriter out){
00239         StringBuffer s = new StringBuffer();
00240         out.println("<pre>");
00241         out.println("irn = " + p.getSpiresId());
00242         out.println("red = " + p.getRedId());
00243         int icom = 0;
00244         for ( String comment : p.getComments() ){
00245             icom++;
00246             out.println(icom + ":" + comment);
00247         }
00248         int imod = 0;
00249         for ( Modification modification : p.getModifications() ){
00250             imod++;
00251             if(imod == 1){out.println("Created on " + modification.getTimestamp() + " by " + modification.getModifier());}
00252             else        {out.println("Modified on " + modification.getTimestamp() + " by " + modification.getModifier());}
00253         }
00254         for (String auth: p.getAuthors() ) {
00255             out.println("author: "  + auth);
00256         }
00257         out.println("Title:" + p.getTitle());
00258 
00259         out.println("Number of references are: " + p.getReferences().size()); 
00260 
00261         for (Reference ref : p.getReferences() ) {
00262             out.println("Reference:"  + ref.getDescription());
00263         }
00264         for (Experiment exp : p.getExperiments() ) {
00265             out.println("Experiment:"  + exp.getName());
00266             out.println("Laboratory:"  + exp.getLaboratory());
00267             out.println("InformalName:"  + exp.getInformalName());
00268         }
00269         out.println("</pre>");
00270     }
00271 
00273     private void displayDatasetError(Paper p,PrintWriter out,String dataset){
00274         int ds = Integer.parseInt(dataset);
00275         Dataset d = p.getDataset(ds);
00276         for (DatasetError de : d.getErrors() ) {
00277             out.println("\n Dataset Error");
00278             if (de.getComment() != null) { out.println("<br>" + de.getComment() ); }
00279             if (de.getPlus() != null) { out.println("<br>" + de.getPlus() ); }
00280             if (de.getMinus() != null) { out.println("<br>" + de.getMinus() ); }
00281             if (de.getSourceType() != null) { out.println("<br>" + de.getSourceType() ); }
00282             if (de.getNormType() != null) { out.println("<br>" + de.getNormType() ); }
00283         }
00284     }
00286     private  String forms(Double value,PrintWriter out){
00287         int width = 20;
00288         return this.forms(value,width,out);
00289         
00290     }
00291 
00292     private  String forms(Double value,int width,PrintWriter out){
00293         int position = width/2;
00294         return this.forms(value,width,position,out);
00295     }
00296 
00297     private String forms(Double value,int width, int position,PrintWriter out){
00298         String form = "##############.###############################";
00299         DecimalFormat myFormatter = new DecimalFormat(form);
00300         String sval = myFormatter.format(value);
00301         String sout = " ";
00302         for (int i=1 ; i<width; i++){
00303             sout = sout.concat(" ");
00304         }
00305         int lenval = sval.length(); 
00306         int dotpos = sval.indexOf(".");
00307         String before = sval;
00308         String after  = "";
00309         if(dotpos >= 0){
00310             before = sval.substring(0,dotpos);
00311             after  = sval.substring(dotpos+1,lenval);
00312         }
00313         int lenbefore = before.length();
00314         int lenafter = after.length();
00315         int lenout = sout.length();
00316         StringBuffer bout = new StringBuffer(sout);
00317         int lstart = 1;
00318         int l1 = lstart+lenbefore;
00319         int l2 = l1;
00320         int l3 = l2+1;
00321         int l4 = l3+lenafter;
00322         
00323         bout.replace(lstart,l1,before);
00324         bout.replace(l2,l2,".");
00325         bout.replace(l3,l4,after);
00326         sout = bout.toString();
00327         
00328         return sout;
00329     } 
00330 }

Generated on Tue Apr 21 15:55:00 2009 for HepData Web servlets by  doxygen 1.5.5