00001 package cedar.hepdata.webapp.pages;
00002
00003 import cedar.hepdata.model.*;
00004 import cedar.hepdata.xml.*;
00005 import cedar.hepdata.db.*;
00006
00007 import org.apache.tapestry5.annotations.*;
00008 import org.apache.tapestry5.ioc.annotations.*;
00009 import org.apache.tapestry5.services.*;
00010
00011 import org.hibernate.*;
00012
00013 import java.util.*;
00014
00015 import java.awt.image.BufferedImage;
00016 import java.io.ByteArrayInputStream;
00017 import java.io.ByteArrayOutputStream;
00018 import java.io.IOException;
00019 import java.io.InputStream;
00020 import java.io.BufferedWriter;
00021 import java.io.FileWriter;
00022 import java.util.List;
00023
00024 import java.awt.Graphics2D;
00025 import java.awt.geom.Rectangle2D;
00026 import java.io.File;
00027 import java.io.OutputStream;
00028 import java.io.BufferedOutputStream;
00029 import java.io.FileOutputStream;
00030
00031 import com.lowagie.text.Document;
00032 import com.lowagie.text.Rectangle;
00033 import com.lowagie.text.DocumentException;
00034 import com.lowagie.text.pdf.PdfWriter;
00035 import com.lowagie.text.pdf.PdfContentByte;
00036 import com.lowagie.text.pdf.DefaultFontMapper;
00037
00038 import org.apache.tapestry5.ComponentResources;
00039 import org.apache.tapestry5.Link;
00040 import org.apache.tapestry5.MarkupWriter;
00041 import org.apache.tapestry5.StreamResponse;
00042 import org.apache.tapestry5.annotations.*;
00043 import org.apache.tapestry5.ioc.annotations.*;
00044 import org.apache.tapestry5.annotations.*;
00045 import org.apache.tapestry5.ioc.annotations.*;
00046 import org.apache.tapestry5.ioc.services.TypeCoercer;
00047 import org.apache.tapestry5.dom.Element;
00048 import org.apache.tapestry5.services.Response;
00049 import org.apache.tapestry5.annotations.*;
00050 import org.apache.tapestry5.ioc.annotations.*;
00051 import org.apache.tapestry5.annotations.*;
00052 import org.apache.tapestry5.ioc.annotations.*;
00053 import org.jfree.chart.ChartUtilities;
00054 import org.jfree.chart.JFreeChart;
00055 import org.jfree.chart.title.TextTitle;
00056 import org.jfree.data.xy.*;
00057
00058 import com.lowagie.text.Document;
00059
00060 import org.jfree.chart.JFreeChart;
00061 import org.jfree.chart.axis.NumberAxis;
00062 import org.jfree.chart.axis.ValueAxis;
00063 import org.jfree.chart.axis.LogarithmicAxis;
00064 import org.jfree.chart.plot.XYPlot;
00065 import org.jfree.chart.renderer.xy.XYErrorRenderer;
00066 import org.jfree.chart.renderer.xy.XYItemRenderer;
00067 import org.jfree.data.xy.XYIntervalSeries;
00068 import org.jfree.data.xy.XYIntervalSeriesCollection;
00069 import org.jfree.data.Range;
00070 import org.jfree.ui.HorizontalAlignment;
00071
00072
00073 public class ReactionPlot{
00074
00075 @Inject
00076 private RequestGlobals _reqGlobals;
00077
00078 @Inject
00079 private org.hibernate.Session _session;
00080
00081
00082
00083
00084 @InjectPage
00085 private SavePlot saveplot;
00086
00087
00088
00089 public String getQueryParam(String param){
00090 return _reqGlobals.getRequest().getParameter(param);
00091 }
00092
00093 @Persist
00094 private String hepid="123";
00095 public String setHepId(){
00096 hepid = _reqGlobals.getRequest().getParameter("hepid");
00097 return "";
00098 }
00099 public String getHepId(){
00100 return _reqGlobals.getRequest().getParameter("hepid");
00101 }
00102 public String getDsId(){
00103 return _reqGlobals.getRequest().getParameter("dsid");
00104 }
00105 public String getXId(){
00106 return _reqGlobals.getRequest().getParameter("xid");
00107 }
00108 public String getYId(){
00109 return _reqGlobals.getRequest().getParameter("yid");
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 public Long getSpiresId(){
00125 return _paper.getSpiresId();
00126 }
00127
00128 public String getQueryString(){
00129 return _reqGlobals.getRequest().getParameter("string");
00130 }
00131
00132 public String getXscale(){
00133 return _reqGlobals.getRequest().getParameter("xscale");
00134 }
00135 public String getNotXscale(){
00136 if(_reqGlobals.getRequest().getParameter("xscale").equals("lin")){ return "log";}
00137 else { return "lin";}
00138 }
00139
00140 public String getYscale(){
00141 return _reqGlobals.getRequest().getParameter("yscale");
00142 }
00143 public String getNotYscale(){
00144 if(_reqGlobals.getRequest().getParameter("yscale").equals("lin")){ return "log";}
00145 else { return "lin";}
00146 }
00147
00148 public String getSys(){
00149 return _reqGlobals.getRequest().getParameter("sys");
00150 }
00151 public String getNotSys(){
00152 if(_reqGlobals.getRequest().getParameter("sys").equals("yes")){ return "no";}
00153 else { return "yes";}
00154 }
00155
00156
00157 static private String _pdfname;
00158 public String getPdfName(){
00159 _pdfname = _reqGlobals.getRequest().getParameter("pdfname");
00160
00161
00162
00163
00164
00165
00166 return _pdfname;
00167 }
00168 public String setPdfName(){
00169 _pdfname = getPdfName();
00170 return "";
00171 }
00172 static private String _pdfselect;
00173 public String getPdfSelect(){
00174 return _reqGlobals.getRequest().getParameter("pdfselect");
00175 }
00176 public String setPdfSelect(){
00177 _pdfselect = getPdfSelect();
00178 return "";
00179 }
00180
00181 private String _select;
00182 public String getSelect() { return _select; }
00183 public void setSelect(String select) { _select = select; }
00184 public List <String> getSelects(){
00185 List <String> _selected= new ArrayList();
00186 List <String> _names = _reqGlobals.getRequest().getParameterNames();
00187 int size= _names.size();
00188 for (int i=0; i<size; i++){
00189 if(_names.get(i).startsWith("plot")){
00190 _selected.add(_names.get(i));
00191 }
00192 }
00193 return _selected;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 public List <YAxis> getSelectedYAxes(){
00242 List <YAxis> _selected = new ArrayList();
00243 List <String> _names = _reqGlobals.getRequest().getParameterNames();
00244 System.out.println(_names);
00245 int size= _names.size();
00246 for (int i=0; i<size; i++){
00247 if(_names.get(i).startsWith("plot")){
00248 int i1 = _names.get(i).indexOf("d");
00249 int i2 = _names.get(i).indexOf("y");
00250 String ds = _names.get(i).substring(i1+2,i2);
00251 String ya = _names.get(i).substring(i2+2);
00252 _selected.add(_paper.getDataset(Integer.parseInt(ds)).getYAxis(Integer.parseInt(ya)));
00253 }
00254 }
00255 return _selected;
00256 }
00257
00258 public String getFirstAuthorName(){
00259 int n = 0;
00260 String _firstauthor = "";
00261 for (String author : _paper.getAuthors()){
00262 n += 1;
00263 if(n == 1) {_firstauthor = author;}
00264 }
00265 String _refdate = "xx";
00266 for (Reference reference : _paper.getReferences()){
00267 _refdate = reference.getDate();
00268 }
00269 int _lref = _refdate.length();
00270 _firstauthor = _firstauthor + " " + _refdate.substring(_lref-2,_lref);
00271 return _firstauthor;
00272 }
00273
00274 public String getFirstPublished(){
00275 int n = 0;
00276 String _firstpublished = "Not Published";
00277 for (Reference ref : _paper.getReferences()){
00278 String refdesc = ref.getDescription();
00279 if(refdesc.startsWith("PL") ||
00280 refdesc.startsWith("PR") ||
00281 refdesc.startsWith("ZP") ||
00282 refdesc.startsWith("EPJ") ||
00283 refdesc.startsWith("NC") ||
00284 refdesc.startsWith("JPG") ||
00285 refdesc.startsWith("JHEP") ){
00286 n += 1;
00287 if(n == 1) { _firstpublished = refdesc;}
00288 }
00289 }
00290 return _firstpublished;
00291 }
00292 public String getFirstPreprint(){
00293 int n = 0;
00294 String _firstpreprint= "Not Known";
00295 for (Reference reference : _paper.getReferences()){
00296 String ref = reference.getDescription();
00297 if(ref.startsWith("PL") ||
00298 ref.startsWith("PR") ||
00299 ref.startsWith("ZP") ||
00300 ref.startsWith("EPJ") ||
00301 ref.startsWith("NC") ||
00302 ref.startsWith("JPG") ||
00303 ref.startsWith("JHEP") ){
00304 }
00305 else{
00306 n += 1;
00307 if(n == 1) { _firstpreprint = ref;}
00308
00309 }
00310 }
00311 return _firstpreprint;
00312 }
00313 public String getFirstExperimentName(){
00314 int n = 0;
00315 String _firstexperiment = "";
00316 for (Experiment experiment : _paper.getExperiments()){
00317 n += 1;
00318 if(n == 1) { _firstexperiment = experiment.getName();}
00319 }
00320 return _firstexperiment;
00321 }
00322 public String getFirstInformalName(){
00323 int n = 0;
00324 String _firstinformal = "";
00325 for (Experiment experiment : _paper.getExperiments()){
00326 n += 1;
00327 if(n == 1) { _firstinformal = experiment.getInformalName();}
00328 }
00329 return _firstinformal;
00330 }
00331 public String getTitle(){
00332 return _paper.getTitle();
00333 }
00334
00335 public String getFirstDatasetComment(){
00336 int n = 0;
00337 String _firstcomment = "";
00338 for ( String comment : _paper.getDataset(Integer.parseInt(getDsId())).getComments() ){
00339 n += 1;
00340 if(n == 1) { _firstcomment = comment;}
00341 }
00342 return _firstcomment;
00343 }
00344
00345
00346 private static YAxis _yaxis;
00347 public YAxis getYAxis() { return _yaxis; }
00348 public void setYAxis(YAxis yaxis) { _yaxis = yaxis; }
00349
00350
00351
00352 private Paper _paper=null;
00353 public List <YAxis> getYAxes(){
00354 List <YAxis> _ylist = new ArrayList();
00355 Query q=null;
00356 StringBuffer b = new StringBuffer();
00357
00358 b.append("select distinct p from Paper p where p._id = ");
00359 b.append(getHepId());
00360 q = _session.createQuery(b.toString());
00361 _paper = (Paper) q.uniqueResult();
00362 String yid=getYId();
00363 if(yid.equals("0")){yid = "1";}
00364 _ylist.add(_paper.getDataset(Integer.parseInt(getDsId())).getYAxis(Integer.parseInt(yid)));
00365 return _ylist;
00366 }
00367
00368 @Inject
00369 private ComponentResources _resources;
00370
00371
00372 public String setTitleStringBlank(){
00373 titlestring="";
00374
00375 return "";
00376 }
00377
00378 static private int numberRun = 0;
00379 static private HashMap seriesMap = new HashMap();
00380 static private HashMap xLabelMap = new HashMap();
00381 static private HashMap yLabelMap = new HashMap();
00382 static private HashMap commentMap = new HashMap();
00383 static private String titlestring = "";
00384 static private String caption = "";
00385
00386
00387 public Link getChart3(){
00388 if (numberRun > 10000) {
00389 numberRun=0;
00390 }
00391 int numberRunStart = numberRun;
00392 XYIntervalSeries series1 = null;
00393 if(!getSelectedYAxes().isEmpty()){
00394 System.out.println("there are selected plots");
00395 for (YAxis ya : getSelectedYAxes()){
00396 numberRun +=1;
00397 series1 = getDataSeries(ya,Integer.parseInt(getXId()),getXscale(),getYscale(),true);
00398 seriesMap.put(numberRun,series1);
00399 xLabelMap.put(numberRun,ya.getDataset().getXAxis(1).getHeader());
00400 yLabelMap.put(numberRun,ya.getHeader());
00401 }
00402
00403 }
00404 else if(getYId().equals("0")){
00405 for (YAxis ya : _yaxis.getDataset().getYAxes()){
00406 numberRun +=1;
00407 if(getSys().equals("yes")){
00408 series1 = getDataSeries(ya,Integer.parseInt(getXId()),getXscale(),getYscale(),true);
00409 }
00410 else{
00411 series1 = getDataSeries(ya,Integer.parseInt(getXId()),getXscale(),getYscale(),false);
00412 }
00413 seriesMap.put(numberRun,series1);
00414 xLabelMap.put(numberRun,ya.getDataset().getXAxis(1).getHeader());
00415 yLabelMap.put(numberRun,ya.getHeader());
00416 }
00417 }
00418 else{
00419 numberRun +=1;
00420 if(getSys().equals("yes")){
00421 series1 = getDataSeries(_yaxis,Integer.parseInt(getXId()),getXscale(),getYscale(),true);
00422 }
00423 else{
00424 series1 = getDataSeries(_yaxis,Integer.parseInt(getXId()),getXscale(),getYscale(),false);
00425 }
00426 seriesMap.put(numberRun,series1);
00427 xLabelMap.put(numberRun,_yaxis.getDataset().getXAxis(1).getHeader());
00428 yLabelMap.put(numberRun,_yaxis.getHeader());
00429 }
00430 int number = numberRun-numberRunStart;
00431 String xsize = "500";
00432 String ysize = "500";
00433
00434 return _resources.createActionLink("chart", false, new Object[]{xsize,ysize,numberRunStart,number,getXscale(),getYscale()});
00435 }
00436
00437
00438 public XYIntervalSeries getDataSeries(YAxis yaxis,int xid, String xscale,String yscale,Boolean systematics){
00439 XAxis _xaxis = yaxis.getDataset().getXAxis(xid);
00440 titlestring=titlestring.concat("\n" + yaxis.getDataset().getId().toString() + "/" + yaxis.getId().toString());
00441 for (String comment : yaxis.getDataset().getComments()){
00442 if(comment.startsWith("Location:")){
00443 titlestring=titlestring.concat(" - " + comment);
00444 }
00445 else{
00446 caption=comment;
00447 }
00448 }
00449 XYIntervalSeries series = new XYIntervalSeries(" ");
00450
00451
00452 series.setKey(yaxis.getDataset().getId().toString() + "/" + yaxis.getId().toString());
00453 for (int ip=1; ip<=yaxis.getPoints().size(); ip++){
00454 try{
00455 double y = yaxis.getPoint(ip).getValue();
00456 double eplus = 0.0;
00457 double eminus = 0.0;
00458 for (Uncertainty e : yaxis.getPoint(ip).getErrors()){
00459 Boolean use = true;
00460 if(e.getSourceType().toString().equals("systematic") && !systematics) { use = false; }
00461 if(use) {
00462 if(e.getNormType() == ErrorNorm.PCT){
00463 eplus = eplus + (y*e.getPlus()/100.0)*(y*e.getPlus()/100.0);
00464 eminus = eminus + (y*e.getMinus()/100.0)*(y*e.getMinus()/100.0);
00465 }
00466 else{
00467 eplus = eplus + e.getPlus()*e.getPlus();
00468 eminus = eminus - e.getMinus()*e.getMinus();
00469 }
00470 }
00471 }
00472 eplus = Math.sqrt(eplus);
00473 eminus = Math.sqrt(eminus);
00474 double yhigh = y + eplus;
00475 double ylow = y - eplus;
00476 double x;
00477 double xhigh;
00478 double xlow;
00479 if(_xaxis.getBin(ip).getLowValue() != null && _xaxis.getBin(ip).getHighValue() != null){
00480 x = (_xaxis.getBin(ip).getLowValue()+_xaxis.getBin(ip).getHighValue())/2;
00481 xlow = _xaxis.getBin(ip).getLowValue();
00482 xhigh = _xaxis.getBin(ip).getHighValue();
00483 }
00484 else{
00485 x = _xaxis.getBin(ip).getFocus();
00486 xhigh = _xaxis.getBin(ip).getFocus();
00487 xlow = _xaxis.getBin(ip).getFocus();
00488 }
00489 if(xscale.equals("lin") && yscale.equals("lin") ) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
00490 if(xscale.equals("log") && yscale.equals("lin") && xlow > 0.0 ) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
00491 if(xscale.equals("lin") && yscale.equals("log") && ylow > 0.0 ) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
00492 if(xscale.equals("log") && yscale.equals("log") && ylow > 0.0 && xlow > 0.0) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
00493 } catch(Exception e){}
00494 }
00495 return series;
00496 }
00497
00498
00499 public StreamResponse onChart(final int width, final int height, final int numberRunStart, int number, String _xscale, String _yscale){
00500
00501
00502 final XYIntervalSeriesCollection collection = new XYIntervalSeriesCollection();
00503 double xlow = 1000000.0;
00504 double xhigh = -1000000.0;
00505 double ylow = 1000000.0;
00506 double yhigh = -1000000.0;
00507 for (int n=1; n<=number; n++){
00508
00509 XYIntervalSeries series = (XYIntervalSeries) seriesMap.get(numberRunStart+n);
00510 int nitems = series.getItemCount();
00511 for (int nn=0; nn<nitems; nn++){
00512 if(series.getXLowValue(nn) < xlow){ xlow = series.getXLowValue(nn);};
00513 if(series.getXHighValue(nn) > xhigh){ xhigh = series.getXHighValue(nn);};
00514 if(series.getYLowValue(nn) < ylow){ ylow = series.getYLowValue(nn);};
00515 if(series.getYHighValue(nn) > yhigh){ yhigh = series.getYHighValue(nn);};
00516 }
00517
00518
00519 seriesMap.remove(numberRunStart+n);
00520 collection.addSeries(series);
00521 }
00522 xlow = xlow - 0.05*(xhigh-xlow);
00523 xhigh = xhigh + 0.05*(xhigh-xlow);
00524 ylow = ylow - 0.05*(yhigh-ylow);
00525 yhigh = yhigh + 0.05*(yhigh-ylow);
00526 if(xlow == xhigh){
00527 xlow = 0.90*xlow;
00528 xhigh= 1.10*xhigh;
00529 }
00530 IntervalXYDataset data1 = collection;
00531
00532
00533 final XYErrorRenderer renderer1 = new XYErrorRenderer();
00534 renderer1.setCapLength(3);
00535 final Range rangex = new Range(xlow,xhigh);
00536 final Range rangey = new Range(ylow,yhigh);
00537
00538 NumberAxis rangeAxisX = null;
00539 if(_xscale.equals("lin")){
00540 rangeAxisX = new NumberAxis();
00541 rangeAxisX.setRange(rangex);
00542 } else{
00543 LogarithmicAxis logaxistemp = new LogarithmicAxis("header");
00544 rangeAxisX = logaxistemp;
00545 }
00546 try {
00547 String xlabel = (String) xLabelMap.get(numberRunStart+1);
00548 rangeAxisX.setLabel(xlabel);
00549 }
00550 catch(Exception e){
00551 rangeAxisX.setLabel("xaxis label ?");
00552 }
00553
00554
00555 NumberAxis rangeAxisY = null;
00556 if(_yscale.equals("lin")) {
00557 rangeAxisY = new NumberAxis();
00558 rangeAxisY.setRange(rangey);
00559 } else{
00560 LogarithmicAxis logaxistemp = new LogarithmicAxis(_yaxis.getHeader());
00561
00562
00563
00564
00565 rangeAxisY = logaxistemp;
00566 }
00567 try{
00568 String ylabel = (String) yLabelMap.get(numberRunStart+1);
00569 rangeAxisY.setLabel(ylabel);
00570 }
00571 catch (Exception e){
00572 rangeAxisY.setLabel("yaxis label ?");
00573 }
00574 final XYPlot subplot1 = new XYPlot(data1, rangeAxisX, rangeAxisY, renderer1);
00575 final TextTitle title = new TextTitle(titlestring);
00576 title.setTextAlignment(HorizontalAlignment.LEFT);
00577 final JFreeChart chart = new JFreeChart(subplot1);
00578 chart.addSubtitle(title);
00579 System.out.println("pdfselect: " + _pdfselect + " pdfname: " + _pdfname);
00580 if(_pdfselect != null && _pdfselect.equals("yes")){
00581 if(_pdfname != null && !_pdfname.equals("")){
00582 File filename = new File(_pdfname);
00583 try {
00584 saveChartAsPDF(filename, chart, 500, 500);
00585
00586 } catch (IOException ie) {
00587
00588 }
00589 }
00590 }
00591 return new StreamResponse(){
00592 public String getContentType(){
00593 return "image/png";
00594 }
00595 public InputStream getStream() throws IOException {
00596 BufferedImage image = chart.createBufferedImage(width, height);
00597 ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ;
00598 ChartUtilities.writeBufferedImageAsJPEG(byteArray, image) ;
00599 return new ByteArrayInputStream(byteArray.toByteArray());
00600 }
00601 public void prepareResponse(Response response){}
00602 };
00603 }
00604 public static void saveChartAsPDF(File file,
00605 JFreeChart chart,
00606 int width,
00607 int height) throws IOException{
00608 OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
00609 writeChartAsPDF(out, chart, width, height);
00610 out.close();
00611 }
00612 public static void writeChartAsPDF(OutputStream out,
00613 JFreeChart chart,
00614 int width, int height) throws IOException{
00615 Rectangle pagesize = new Rectangle(width,height);
00616 Document document = new Document(pagesize, 50, 50, 50, 50);
00617 try {
00618 PdfWriter writer = PdfWriter.getInstance(document, out);
00619 document.addAuthor("JFreeChart");
00620 document.open();
00621 PdfContentByte cb = writer.getDirectContent();
00622 Graphics2D g2 = cb.createGraphics(width, height);
00623 Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
00624 chart.draw(g2, r2D, null);
00625 g2.dispose();
00626 }
00627 catch(DocumentException de){
00628 System.err.println(de.getMessage());
00629 }
00630 document.close();
00631 }
00632
00633
00634
00635 private Class nextPage;
00636 Object onSubmitFromSavePlot()
00637 {
00638 return nextPage;
00639 }
00640 @OnEvent(component="submit2Button")
00641 void onsubmitFromSavePage()
00642 {
00643 this.setHepId();
00644 saveplot.setHepId(getHepId());
00645 saveplot.setDsId(getDsId());
00646 saveplot.setYId(getYId());
00647 saveplot.setXId(getXId());
00648 saveplot.setYscale(getYscale());
00649 saveplot.setXscale(getXscale());
00650 saveplot.setSys(getSys());
00651 saveplot.setQueryString(getQueryString());
00652 nextPage=SavePlot.class;
00653 }
00654
00655
00656 }