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 import org.hibernate.criterion.*;
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.util.List;
00021
00022 import org.apache.tapestry5.ComponentResources;
00023 import org.apache.tapestry5.Link;
00024 import org.apache.tapestry5.MarkupWriter;
00025 import org.apache.tapestry5.StreamResponse;
00026 import org.apache.tapestry5.annotations.*;
00027 import org.apache.tapestry5.ioc.annotations.*;
00028 import org.apache.tapestry5.annotations.*;
00029 import org.apache.tapestry5.ioc.annotations.*;
00030 import org.apache.tapestry5.ioc.services.TypeCoercer;
00031 import org.apache.tapestry5.dom.Element;
00032 import org.apache.tapestry5.services.Response;
00033 import org.jfree.chart.ChartUtilities;
00034 import org.jfree.chart.JFreeChart;
00035 import org.jfree.chart.title.TextTitle;
00036 import org.jfree.data.xy.*;
00037
00038 import org.jfree.chart.JFreeChart;
00039 import org.jfree.chart.axis.NumberAxis;
00040 import org.jfree.chart.axis.ValueAxis;
00041 import org.jfree.chart.axis.LogarithmicAxis;
00042 import org.jfree.chart.plot.XYPlot;
00043 import org.jfree.chart.renderer.xy.XYErrorRenderer;
00044 import org.jfree.chart.renderer.xy.XYItemRenderer;
00045 import org.jfree.data.xy.XYIntervalSeries;
00046 import org.jfree.data.xy.XYIntervalSeriesCollection;
00047 import org.jfree.data.Range;
00048
00049
00050 public class Hepreac{
00051
00052 @Inject
00053 private RequestGlobals _reqGlobals;
00054
00055 @Inject
00056 private org.hibernate.Session _session;
00057
00058
00059 public String getPath(){
00060 return _reqGlobals.getRequest().getPath();
00061 }
00062 public String getContextPath(){
00063 return _reqGlobals.getRequest().getContextPath();
00064 }
00065
00066 public String getTestPath(){
00067 return getPath().substring(getPath().lastIndexOf("/")+1);
00068 }
00069
00070 public String getQueryParam(String param){
00071 return _reqGlobals.getRequest().getParameter(param);
00072 }
00073
00074
00075
00076 private Paper _paper;
00077 public Paper getPaper() { return _paper; }
00078 public void setPaper(Paper p) { _paper = p; }
00079
00080 public List <Paper> getPapers(){
00081 Query q = constructQuery();
00082
00083 return q.list();
00084 }
00085
00086
00087 private Dataset _dataset;
00088 public Dataset getDataset() { return _dataset; }
00089 public void setDataset(Dataset d) { _dataset = d; }
00090 public SortedSet <Dataset> getDatasets(){
00091 return _paper.getDatasets();
00092 }
00093
00094
00095 static private YAxis _yaxis;
00096 public YAxis getYAxis() { return _yaxis; }
00097 public void setYAxis(YAxis y) { _yaxis = y; }
00098 public SortedSet <YAxis> getYAxes(){
00099 return _dataset.getYAxes();
00100 }
00101
00102
00103
00104
00105
00106 private Query constructQuery(){
00107 System.out.println("path is: " + getPath());
00108
00109 String s0 = getPath().substring(getPath().indexOf("/")+1);
00110 String s1 = s0.substring(s0.indexOf("/")+1);
00111 String theIrn="";
00112 if(s1.indexOf("/") < 0 ){theIrn = s1;}
00113 else {theIrn = s1.substring(0,s1.indexOf("/"));}
00114
00115
00116
00117 Query q = null;
00118
00119
00120 StringBuffer b = new StringBuffer();
00121
00122
00123 b.append("select distinct p from Paper p where p._spiresId = :irn");
00124 q = _session.createQuery(b.toString());
00125 q.setString( "irn",theIrn);
00126
00127 return q;
00128
00129 }
00130
00131
00132 public String getFirstAuthorName(){
00133 ckbxs.put(0,false);
00134 int n = 0;
00135 String _firstauthor = "";
00136 for (String author : _paper.getAuthors()){
00137 n += 1;
00138 if(n == 1) {_firstauthor = author;}
00139 }
00140 String _refdate = "xx";
00141 for (Reference reference : _paper.getReferences()){
00142 _refdate = reference.getDate();
00143 }
00144 int _lref = _refdate.length();
00145 _firstauthor = _firstauthor + " " + _refdate.substring(_lref-2,_lref);
00146 return _firstauthor;
00147 }
00148
00149 public String getFirstExperimentName(){
00150 int n = 0;
00151 String _firstexperiment = "";
00152 for (Experiment experiment : _paper.getExperiments()){
00153 n += 1;
00154 if(n == 1) { _firstexperiment = experiment.getName();}
00155 }
00156 return _firstexperiment;
00157 }
00158
00159 public String getFirstInformalName(){
00160 int n = 0;
00161 String _firstinformal = "";
00162 for (Experiment experiment : _paper.getExperiments()){
00163 n += 1;
00164 if(n == 1) { _firstinformal = experiment.getInformalName();}
00165 }
00166 return _firstinformal;
00167 }
00168
00169 public String getFirstPublished(){
00170 int n = 0;
00171 String _firstpublished = "Not Published";
00172 for (Reference ref : _paper.getReferences()){
00173 String refdesc = ref.getDescription();
00174 if(ref.getType().equals("JOUR")){
00175 n += 1;
00176 if(n == 1) { _firstpublished = refdesc;}
00177 }
00178 }
00179 return _firstpublished;
00180 }
00181
00182 public String getFirstPreprint(){
00183 int n = 0;
00184 String _firstpreprint= "Not Known";
00185 System.out.println("number of references:" + _paper.getReferences().size());
00186 for (Reference ref : _paper.getReferences()){
00187 String refdesc = ref.getDescription();
00188 if(!ref.getType().equals("JOUR")){
00189 n += 1;
00190 if(n == 1) { _firstpreprint = refdesc;}
00191
00192 }
00193 }
00194 return _firstpreprint;
00195 }
00196
00197
00198 public String getTitle(){
00199 return _paper.getTitle();
00200 }
00201
00202 private Map <Integer,Boolean> ckbxs = new HashMap();
00203 static private int _maxmap = 0;
00204 @Persist
00205 private boolean ckbx;
00206 private int _nloop = -1;
00207 public boolean isCkbx(){
00208 _nloop += 1;
00209 if(_nloop > _maxmap){ _nloop=0;}
00210 System.out.println("calling isCkbx" + _nloop);
00211 return ckbxs.get(_nloop);
00212 }
00213 public void setCkbx(boolean ckbx){
00214 _nloop += 1;
00215 if(_nloop > _maxmap){ _nloop=0;}
00216 ckbxs.put(_nloop,ckbx);
00217 System.out.println("calling setCkbx" + _nloop);
00218 }
00219
00220 @Inject
00221 private ComponentResources _resources;
00222
00223
00224 static private int numberRun = 0;
00225 static private HashMap seriesMap = new HashMap();
00226 static private HashMap xLabelMap = new HashMap();
00227 static private HashMap yLabelMap = new HashMap();
00228 static private HashMap commentMap = new HashMap();
00229 static private String titlestring = "Title:";
00230
00231
00232 public Link getChart3(){
00233 if (numberRun > 10000) {
00234 numberRun=0;
00235 }
00236 numberRun +=1;
00237 XYIntervalSeries series1 = getDataSeries(_yaxis);
00238 seriesMap.put(numberRun,series1);
00239 xLabelMap.put(numberRun,_yaxis.getDataset().getXAxis(1).getHeader());
00240 yLabelMap.put(numberRun,_yaxis.getHeader());
00241 return _resources.createActionLink("chart", false, new Object[]{"400","400",numberRun,"1"});
00242 }
00243
00244
00245 public XYIntervalSeries getDataSeries(YAxis yaxis){
00246 XAxis _xaxis = yaxis.getDataset().getXAxis(1);
00247
00248 for (String comment : yaxis.getDataset().getComments()){
00249 System.out.println(comment);
00250 titlestring = comment;
00251 }
00252 XYIntervalSeries series = new XYIntervalSeries("data1");
00253 for (int ip=1; ip<=yaxis.getPoints().size(); ip++){
00254 try{
00255 double y = yaxis.getPoint(ip).getValue();
00256 double eplus = 0.0;
00257 double eminus = 0.0;
00258 for (Uncertainty e : yaxis.getPoint(ip).getErrors()){
00259 if(e.getNormType() == ErrorNorm.PCT){
00260 eplus = eplus + (y*e.getPlus()/100.0)*(y*e.getPlus()/100.0);
00261 eminus = eminus + (y*e.getMinus()/100.0)*(y*e.getMinus()/100.0);
00262 }
00263 else{
00264 eplus = eplus + e.getPlus()*e.getPlus();
00265 eminus = eminus - e.getMinus()*e.getMinus();
00266 }
00267 }
00268 eplus = Math.sqrt(eplus);
00269 eminus = Math.sqrt(eminus);
00270 double yhigh = y + eplus;
00271 double ylow = y - eplus;
00272 double x;
00273 double xhigh;
00274 double xlow;
00275 if(_xaxis.getBin(ip).getLowValue() != null && _xaxis.getBin(ip).getHighValue() != null){
00276 x = (_xaxis.getBin(ip).getLowValue()+_xaxis.getBin(ip).getHighValue())/2;
00277 xlow = _xaxis.getBin(ip).getLowValue();
00278 xhigh = _xaxis.getBin(ip).getHighValue();
00279 }
00280 else{
00281 x = _xaxis.getBin(ip).getFocus();
00282 xhigh = _xaxis.getBin(ip).getFocus();
00283 xlow = _xaxis.getBin(ip).getFocus();
00284 }
00285 series.add(x,xlow,xhigh,y,ylow,yhigh);
00286 } catch(Exception e){}
00287 }
00288 return series;
00289 }
00290
00291
00292 public StreamResponse onChart(final int width, final int height, final int numberRun, int number){
00293
00294 final XYIntervalSeriesCollection collection = new XYIntervalSeriesCollection();
00295 double xlow = 1000000.0;
00296 double xhigh = -1000000.0;
00297 double ylow = 1000000.0;
00298 double yhigh = -1000000.0;
00299 for (int n=0; n<number; n++){
00300
00301 XYIntervalSeries series = (XYIntervalSeries) seriesMap.get(numberRun+n);
00302 int nitems = series.getItemCount();
00303 for (int nn=0; nn<nitems; nn++){
00304 if(series.getXLowValue(nn) < xlow){ xlow = series.getXLowValue(nn);};
00305 if(series.getXHighValue(nn) > xhigh){ xhigh = series.getXHighValue(nn);};
00306 if(series.getYLowValue(nn) < ylow){ ylow = series.getYLowValue(nn);};
00307 if(series.getYHighValue(nn) > yhigh){ yhigh = series.getYHighValue(nn);};
00308 }
00309 seriesMap.remove(numberRun+n);
00310 collection.addSeries(series);
00311 }
00312 xlow = xlow - 0.05*(xhigh-xlow);
00313 xhigh = xhigh + 0.05*(xhigh-xlow);
00314 ylow = ylow - 0.05*(yhigh-ylow);
00315 yhigh = yhigh + 0.05*(yhigh-ylow);
00316 if(xlow == xhigh){
00317 xlow = 0.90*xlow;
00318 xhigh= 1.10*xhigh;
00319 }
00320 IntervalXYDataset data1 = collection;
00321
00322 final XYItemRenderer renderer1 = new XYErrorRenderer();
00323 final Range rangex = new Range(xlow,xhigh);
00324 final Range rangey = new Range(ylow,yhigh);
00325 final NumberAxis rangeAxisX = new NumberAxis();
00326 try {
00327 String xlabel = (String) xLabelMap.get(numberRun);
00328 rangeAxisX.setLabel(xlabel);
00329 }
00330 catch(Exception e){
00331 rangeAxisX.setLabel("xaxis label ?");
00332 }
00333 rangeAxisX.setRange(rangex);
00334
00335 final NumberAxis rangeAxisY = new NumberAxis();
00336 try{
00337 String ylabel = (String) yLabelMap.get(numberRun);
00338 rangeAxisY.setLabel(ylabel);
00339 }
00340 catch (Exception e){
00341 rangeAxisY.setLabel("yaxis label ?");
00342 }
00343 rangeAxisY.setRange(rangey);
00344
00345
00346
00347
00348 final XYPlot subplot1 = new XYPlot(data1, rangeAxisX, rangeAxisY, renderer1);
00349 final TextTitle title = new TextTitle(titlestring);
00350 final JFreeChart chart = new JFreeChart(subplot1);
00351 chart.addSubtitle(title);
00352 return new StreamResponse(){
00353 public String getContentType(){
00354 return "image/jpeg";
00355 }
00356 public InputStream getStream() throws IOException {
00357 BufferedImage image = chart.createBufferedImage(width, height);
00358 ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ;
00359 ChartUtilities.writeBufferedImageAsJPEG(byteArray, image) ;
00360 return new ByteArrayInputStream(byteArray.toByteArray());
00361 }
00362 public void prepareResponse(Response response){}
00363 };
00364 }
00365
00366 }