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 import java.lang.Math;
00015
00016 import java.awt.image.BufferedImage;
00017 import java.io.ByteArrayInputStream;
00018 import java.io.ByteArrayOutputStream;
00019 import java.io.IOException;
00020 import java.io.InputStream;
00021 import java.util.List;
00022
00023 import org.apache.tapestry5.ComponentResources;
00024 import org.apache.tapestry5.Link;
00025 import org.apache.tapestry5.MarkupWriter;
00026 import org.apache.tapestry5.StreamResponse;
00027 import org.apache.tapestry5.annotations.*;
00028 import org.apache.tapestry5.ioc.annotations.*;
00029 import org.apache.tapestry5.annotations.*;
00030 import org.apache.tapestry5.ioc.annotations.*;
00031 import org.apache.tapestry5.ioc.services.TypeCoercer;
00032 import org.apache.tapestry5.dom.Element;
00033 import org.apache.tapestry5.services.Response;
00034 import org.jfree.chart.ChartUtilities;
00035 import org.jfree.chart.JFreeChart;
00036 import org.jfree.chart.title.TextTitle;
00037 import org.jfree.data.xy.*;
00038
00039 import org.jfree.chart.JFreeChart;
00040 import org.jfree.chart.axis.NumberAxis;
00041 import org.jfree.chart.axis.ValueAxis;
00042 import org.jfree.chart.axis.LogarithmicAxis;
00043 import org.jfree.chart.plot.XYPlot;
00044 import org.jfree.chart.renderer.xy.XYErrorRenderer;
00045 import org.jfree.chart.renderer.xy.XYItemRenderer;
00046 import org.jfree.data.xy.XYIntervalSeries;
00047 import org.jfree.data.xy.XYIntervalSeriesCollection;
00048 import org.jfree.data.Range;
00049
00050 public class Shapes{
00051
00052 @Inject
00053 private RequestGlobals _reqGlobals;
00054
00055 @Inject
00056 private org.hibernate.Session _session;
00057
00058
00059
00060 private Paper _paper;
00061 public Paper getPaper() { return _paper; }
00062 public void setPaper(Paper p) { _paper = p; }
00063
00064
00065
00066 private Dataset _dataset;
00067 public Dataset getDataset() { return _dataset; }
00068 public void setDataset(Dataset d) { _dataset = d; }
00069
00070
00071 public String getQueryParam(){
00072 return _reqGlobals.getRequest().getParameter("irn");
00073 }
00074
00075 public String getQueryParam(String param){
00076 return _reqGlobals.getRequest().getParameter(param);
00077 }
00078
00079 public List<Dataset> getDatasets(){
00080 String _sirn = this.getQueryParam("dataset");
00081 int _spiresid = Integer.parseInt(this.getQueryParam("irn"));
00082 Query q = null;
00083 if(_sirn.indexOf(",")>-1){
00084 int _i = _sirn.indexOf(",");
00085 String _s1 = _sirn.substring(0,_i);
00086 String _s2 = _sirn.substring( _i+1,_sirn.length() );
00087 if(_s1.indexOf("-")==-1 && _s2.indexOf("-")==-1 ){
00088
00089 int _ds1 = Integer.parseInt( _s1 );
00090 int _ds2 = Integer.parseInt( _s2 );
00091 q = _session.createQuery("select d from Dataset d,Paper p where p._spiresId = :irn and (d._localId = :ds1 or d._localId = :ds2) and d._paper = p");
00092 q.setLong("ds1", _ds1) ;
00093 q.setLong("ds2", _ds2) ;
00094 }
00095 if(_s1.indexOf("-")>-1 && _s2.indexOf("-")==-1 ){
00096
00097 int _i1 = _s1.indexOf("-");
00098 int _ds11 = Integer.parseInt( _s1.substring(0,_i1) );
00099 int _ds12 = Integer.parseInt( _s1.substring( _i1+1,_s1.length() ) );
00100 int _ds2 = Integer.parseInt( _s2 );
00101 q = _session.createQuery("select d from Dataset d,Paper p where p._spiresId = :irn and ((d._localId > :ds11 and d._localId < :ds12) or d._localId = :ds2) and d._paper = p");
00102 q.setLong("ds11", _ds11-1) ;
00103 q.setLong("ds12", _ds12+1) ;
00104 q.setLong("ds2", _ds2) ;
00105 }
00106 if(_s1.indexOf("-")==-1 && _s2.indexOf("-")>-1 ){
00107
00108 int _i2= _s2.indexOf("-");
00109 int _ds21 = Integer.parseInt( _s2.substring(0,_i2) );
00110 int _ds22 = Integer.parseInt( _s2.substring( _i2+1,_s2.length() ) );
00111 int _ds1 = Integer.parseInt( _s1 );
00112 q = _session.createQuery("select d from Dataset d,Paper p where p._spiresId = :irn and (d._localId = :ds1 or (d._localId > :ds21 and d._localId < :ds22)) and d._paper = p");
00113 q.setLong("ds21", _ds21-1) ;
00114 q.setLong("ds22", _ds22+1) ;
00115 q.setLong("ds1", _ds1) ;
00116 }
00117 if(_s1.indexOf("-")>-1 && _s2.indexOf("-")>-1 ){
00118
00119 int _i1 = _s1.indexOf("-");
00120 int _i2 = _s2.indexOf("-");
00121 int _ds11 = Integer.parseInt( _s1.substring(0,_i1) );
00122 int _ds12 = Integer.parseInt( _s1.substring( _i1+1,_s1.length() ) );
00123 int _ds21 = Integer.parseInt( _s2.substring(0,_i2) );
00124 int _ds22 = Integer.parseInt( _s2.substring( _i2+1,_s2.length() ) );
00125 q = _session.createQuery("select d from Dataset d,Paper p where p._spiresId = :irn and ((d._localId > :ds11 and d._localId < :ds12) or (d._localId > :ds21 and d._localId < :ds22)) and d._paper = p");
00126 q.setLong("ds11", _ds11-1) ;
00127 q.setLong("ds12", _ds12+1) ;
00128 q.setLong("ds21", _ds21-1) ;
00129 q.setLong("ds22", _ds22+1) ;
00130 }
00131 }
00132 else{
00133 if(_sirn.indexOf("-")>-1){
00134
00135 int _i = _sirn.indexOf("-");
00136 int _ds1 = Integer.parseInt( _sirn.substring(0,_i) );
00137 int _ds2 = Integer.parseInt( _sirn.substring( _i+1,_sirn.length() ) );
00138 q = _session.createQuery("select d from Dataset d,Paper p where p._spiresId = :irn and d._localId > :ds1 and d._localId < :ds2 and d._paper = p");
00139 q.setLong("ds1", _ds1-1) ;
00140 q.setLong("ds2", _ds2+1) ;
00141 }
00142 else{
00143
00144 int _ds = Integer.parseInt(_sirn);
00145 q = _session.createQuery("select d from Dataset d,Paper p where p._spiresId = :irn and d._localId = :ds and d._paper = p");
00146 q.setLong("ds", _ds) ;
00147 }
00148 }
00149 q.setLong("irn", _spiresid) ;
00150 return q.list();
00151 }
00152 public List <Paper> getPapers(){
00153 int _spiresid = Integer.parseInt(this.getQueryParam("irn"));
00154
00155 Query q = _session.createQuery("from Paper p where p._spiresId = :irn");
00156 q.setLong("irn", _spiresid) ;
00157 return q.list();
00158
00159 }
00160
00161
00162
00163 static private YAxis _yaxis;
00164 public YAxis getYAxis() { return _yaxis; }
00165 public void setYAxis(YAxis y) {_yaxis = y; }
00166
00167
00168
00169
00170
00171 public List <YAxis> getYAxes(){
00172 List <YAxis> yaxs = new ArrayList();
00173 if(getQueryParam("yaxis").indexOf(",")>-1){
00174 String words[] = getQueryParam("yaxis").split(",");
00175 for (int i=0; i<words.length; i++){
00176 yaxs.add(_dataset.getYAxis(Integer.parseInt(words[i])));
00177 }
00178 }
00179 else if(getQueryParam("yaxis").indexOf("-")>-1){
00180 String words[] = getQueryParam("yaxis").split("-");
00181 for (int i=Integer.parseInt(words[0]); i<=Integer.parseInt(words[1]); i++){
00182 yaxs.add(_dataset.getYAxis(i));
00183 }
00184 }
00185 else{
00186 int _ya = Integer.parseInt(this.getQueryParam("yaxis"));
00187 yaxs.add(_dataset.getYAxis(_ya));
00188 }
00189 return yaxs;
00190 }
00191
00192 @Inject
00193 private ComponentResources _resources;
00194
00195
00196 static private int numberRun = 0;
00197 static private HashMap seriesMap = new HashMap();
00198 static private HashMap xLabelMap = new HashMap();
00199 static private HashMap yLabelMap = new HashMap();
00200 static private HashMap commentMap = new HashMap();
00201 static private String titlestring = "Title:";
00202 public Link getChart3(){
00203 if (numberRun > 10000) {
00204 numberRun=0;
00205 }
00206 numberRun +=1;
00207 XYIntervalSeries series1 = getDataSeries(_yaxis);
00208 seriesMap.put(numberRun,series1);
00209 xLabelMap.put(numberRun,_yaxis.getDataset().getXAxis(1).getHeader());
00210 yLabelMap.put(numberRun,_yaxis.getHeader());
00211 for (String comment : _yaxis.getDataset().getComments()){
00212 commentMap.put(numberRun,comment);
00213 }
00214 return _resources.createActionLink("chart", false, new Object[]{"400","400",numberRun,"1"});
00215 }
00216
00217 public XYIntervalSeries getDataSeries(YAxis yaxis){
00218 XAxis _xaxis = yaxis.getDataset().getXAxis(1);
00219
00220 for (String comment : yaxis.getDataset().getComments()){
00221 System.out.println(comment);
00222
00223 }
00224 XYIntervalSeries series = new XYIntervalSeries("data1");
00225 for (int ip=1; ip<=yaxis.getPoints().size(); ip++){
00226 try{
00227 double y = yaxis.getPoint(ip).getValue();
00228 double eplus = 0.0;
00229 double eminus = 0.0;
00230 for (Uncertainty e : yaxis.getPoint(ip).getErrors()){
00231 eplus = eplus + e.getPlus()*e.getPlus();
00232 eminus = eminus - e.getMinus()*e.getMinus();
00233 }
00234 eplus = Math.sqrt(eplus);
00235 eminus = Math.sqrt(eminus);
00236 double yhigh = y + eplus;
00237 double ylow = y - eplus;
00238 double x;
00239 double xhigh;
00240 double xlow;
00241 if(_xaxis.getBin(ip).getLowValue() != null && _xaxis.getBin(ip).getHighValue() != null){
00242 x = (_xaxis.getBin(ip).getLowValue()+_xaxis.getBin(ip).getHighValue())/2;
00243 xlow = _xaxis.getBin(ip).getLowValue();
00244 xhigh = _xaxis.getBin(ip).getHighValue();
00245 }
00246 else{
00247 x = _xaxis.getBin(ip).getFocus();
00248 xhigh = _xaxis.getBin(ip).getFocus();
00249 xlow = _xaxis.getBin(ip).getFocus();
00250 }
00251 series.add(x,xlow,xhigh,y,ylow,yhigh);
00252 System.out.println("x" + x + " " + xhigh + " " + xlow + " " + y + " " + yhigh + " " + ylow);
00253 }
00254 catch(Exception ex){}
00255 }
00256 return series;
00257 }
00258
00259 public StreamResponse onChart(final int width, final int height, final int numberRun, int number){
00260
00261 final XYIntervalSeriesCollection collection = new XYIntervalSeriesCollection();
00262 double xlow = 1000000.0;
00263 double xhigh = -1000000.0;
00264 double ylow = 1000000.0;
00265 double yhigh = -1000000.0;
00266 for (int n=0; n<number; n++){
00267 XYIntervalSeries series = (XYIntervalSeries) seriesMap.get(numberRun+n);
00268 int nitems = series.getItemCount();
00269 for (int nn=0; nn<nitems; nn++){
00270 if(series.getXLowValue(nn) < xlow){ xlow = series.getXLowValue(nn);};
00271 if(series.getXHighValue(nn) > xhigh){ xhigh = series.getXHighValue(nn);};
00272 if(series.getYLowValue(nn) < ylow){ ylow = series.getYLowValue(nn);};
00273 if(series.getYHighValue(nn) > yhigh){ yhigh = series.getYHighValue(nn);};
00274 }
00275 seriesMap.remove(numberRun+n);
00276 collection.addSeries(series);
00277 }
00278 xlow = xlow - 0.15*(xhigh-xlow);
00279 xhigh = xhigh + 0.15*(xhigh-xlow);
00280 ylow = ylow - 0.15*(yhigh-ylow);
00281 yhigh = yhigh + 0.15*(yhigh-ylow);
00282 if(xlow == xhigh){
00283 xlow = 0.90*xlow;
00284 xhigh= 1.10*xhigh;
00285 }
00286 IntervalXYDataset data1 = collection;
00287
00288 final XYItemRenderer renderer1 = new XYErrorRenderer();
00289
00290 final Range rangey = new Range(ylow,yhigh);
00291
00292 final NumberAxis rangeAxisX = new NumberAxis();
00293 try {
00294 String xlabel = (String) xLabelMap.get(numberRun);
00295 rangeAxisX.setLabel(xlabel);
00296 }
00297 catch(Exception e){
00298 rangeAxisX.setLabel("xaxis label ?");
00299 }
00300
00301
00302 rangeAxisX.setAutoRangeIncludesZero(true);
00303 final NumberAxis rangeAxisY = new NumberAxis(_yaxis.getHeader());
00304
00306
00307 try{
00308 String ylabel = (String) yLabelMap.get(numberRun);
00309 rangeAxisY.setLabel(ylabel);
00310 }
00311 catch (Exception e){
00312 rangeAxisY.setLabel("yaxis label ?");
00313 }
00317 rangeAxisY.setRange(rangey);
00318 final XYPlot subplot1 = new XYPlot(data1, rangeAxisX, rangeAxisY, renderer1);
00319 String titlestring = (String) commentMap.get(numberRun);
00320 final TextTitle title = new TextTitle(titlestring);
00321 final JFreeChart chart = new JFreeChart(subplot1);
00322 chart.addSubtitle(title);
00323 return new StreamResponse(){
00324 public String getContentType(){
00325 return "image/jpeg";
00326 }
00327 public InputStream getStream() throws IOException {
00328 BufferedImage image = chart.createBufferedImage(width, height);
00329 ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ;
00330 ChartUtilities.writeBufferedImageAsJPEG(byteArray, image) ;
00331 return new ByteArrayInputStream(byteArray.toByteArray());
00332 }
00333 public void prepareResponse(Response response){}
00334 };
00335 }
00336
00337 }