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 import org.apache.tapestry5.MarkupWriter;
00011
00012 import org.hibernate.*;
00013 import org.hibernate.criterion.*;
00014 import java.util.*;
00015
00016
00017 public class FindYAxisPhysics2{
00018
00019 @Inject
00020 private RequestGlobals _reqGlobals;
00021
00022 @Inject
00023 private org.hibernate.Session _session;
00024
00025 public String getQueryParam(String param){
00026 return _reqGlobals.getRequest().getParameter(param);
00027 }
00028
00029 public String getQueryParameters(){
00030 StringBuffer qs = new StringBuffer();
00031 String mid = "";
00032 int number = Integer.parseInt(getQueryParam("number"));
00033 int newstart = Integer.parseInt(getQueryParam("start"));
00034 newstart += number;
00035 qs.append(mid + "reac=" + getQueryParam("reac")); mid = "&";
00036 qs.append(mid + "obs=" + getQueryParam("obs")); mid = "&";
00037 qs.append(mid + "sqrtslo=" + getQueryParam("sqrtslo")); mid = "&";
00038 qs.append(mid + "number=" + getQueryParam("number") + mid + "start=" + Integer.toString(newstart));
00039 return qs.toString().replaceAll("\\+","%2B");
00040 }
00041
00042 public String getFirstNumber(){
00043 return getQueryParam("start");
00044 }
00045
00046 public String getSearchString(){
00047 StringBuffer qs = new StringBuffer();
00048 qs.append("(");
00049 qs.append(getQueryParam("reac"));
00050 qs.append(")");
00051 qs.append(" " + getQueryParam("obs"));
00052 qs.append(" " + getQueryParam("sqrtslo"));
00053 qs.append(" " + getQueryParam("sqrtshi"));
00054 return qs.toString();
00055 }
00056
00057 public String getLastNumber(){
00058 int first = Integer.parseInt(getQueryParam("start"));
00059 int number = Integer.parseInt(getQueryParam("number"));
00060 int last = first + number - 1;
00061 return Integer.toString(last);
00062 }
00063
00064 public String getPageSize(){
00065 return getQueryParam("number");
00066 }
00067
00068 public Long getCount(){
00069 return (Long) constructQuery("count").uniqueResult();
00070 }
00071
00072
00073 private String getInitialState(String r){
00074 String is="";
00075 int ipos = r.indexOf("-->");
00076 if(ipos > -1 ){ is = r.substring(0,ipos); }
00077 else { is = r; }
00078 return is.trim();
00079 }
00080 private String getFinalState(String r){
00081 String fs="";
00082 int ipos = r.indexOf("-->");
00083 if(ipos > -1) {fs = r.substring(ipos+3);}
00084 return fs.trim();
00085 }
00086
00087
00088
00089 private Paper _currentPaper;
00090
00091 public Paper getPaper() { return _currentPaper; }
00092 public void setPaper(Paper p) { _currentPaper = p; }
00093
00094 private Map <Paper,ArrayList<YAxis>> map = new HashMap<Paper,ArrayList<YAxis>>();
00095
00096 public Set <Paper> getPapers() {
00097 Query q = constructQuery();
00098 int first = Integer.parseInt(getQueryParam("start"));
00099 first=first-1;
00100
00101
00102
00103
00104 map.clear();
00105 for (Iterator it=q.iterate(); it.hasNext(); ){
00106 YAxis _yAxis = (YAxis) it.next();
00107 Paper _paper=_yAxis.getDataset().getPaper();
00108 if(map.get(_paper) == null) { map.put(_paper,new ArrayList<YAxis>());}
00109 map.get(_paper).add(_yAxis);
00110 if (map.size() > (Integer.parseInt(getQueryParam("number"))-1)) { break; }
00111 }
00112 return map.keySet();
00113 }
00114
00115
00116 private YAxis _yAxis;
00117
00118 public YAxis getYAxis() { return _yAxis; }
00119 public void setYAxis(YAxis yax) { _yAxis = yax; }
00120
00121 public List <YAxis> getYAxes(){
00122 return map.get(_currentPaper);
00123 }
00124
00125
00126
00127 private Query constructQuery(){
00128 return this.constructQuery("blank");
00129 }
00130
00131 private Query constructQuery(String flag){
00132
00133 List <String> isp = new ArrayList();
00134 List <String> fsp = new ArrayList();
00135 List <String> mfsp = new ArrayList();
00136
00137 if( getQueryParam("reac") != "") {
00138 String is = getInitialState(getQueryParam("reac"));
00139 java.util.StringTokenizer stis = new java.util.StringTokenizer(is);
00140 while(stis.hasMoreTokens()){
00141 isp.add(stis.nextToken());
00142 }
00143 String fs = getFinalState(getQueryParam("reac"));
00144 java.util.StringTokenizer stfs = new java.util.StringTokenizer(fs);
00145 String st = "";
00146 while(stfs.hasMoreTokens()){
00147 st = stfs.nextToken();
00148 if(st.matches("^[A-Za-z].*")){
00149 fsp.add(st);
00150 mfsp.add("1");
00151 }
00152 else{
00153 fsp.add(st.substring(1));
00154 mfsp.add(st.substring(0,1));
00155 }
00156 }
00157 }
00158 String theObs = "";
00159 if ( getQueryParam("obs") != "") { theObs = getQueryParam("obs"); }
00160
00161 String theSqrtsLo = "";
00162 if ( getQueryParam("sqrtslo") != "") { theSqrtsLo = getQueryParam("sqrtslo"); }
00163
00164
00165 Query q = null;
00166
00167
00168 StringBuffer b = new StringBuffer();
00169
00170
00171
00172 if(isp.size() > 0){
00173
00174 if (flag == "count"){
00175 b.append("select count(distinct yabeam) from YAxis yabeam");
00176 }
00177 else{
00178 b.append("select distinct yabeam from YAxis yabeam");
00179 }
00180
00181 b.append(" join yabeam._reactions yrbeam");
00182 b.append(" join yrbeam._initialState beam");
00183 if (isp.size() > 1){
00184 b.append(",YAxis yatarg");
00185 b.append(" join yatarg._reactions yrtarg");
00186 b.append(" join yrtarg._initialState targ");
00187 }
00188 for (int i = 1 ; i <= fsp.size(); i++){
00189 b.append(",YAxis yafsp"+i);
00190 b.append(" join yafsp"+i+"._reactions yrfsp"+i);
00191 b.append(" join yrfsp"+i+"._finalState fsp"+i);
00192 }
00193 if(theSqrtsLo != ""){
00194 b.append(",YAxis yaprop");
00195 b.append(" join yaprop._properties prop");
00196 }
00197
00198 b.append(" where beam.name=:beampart");
00199 if (isp.size() > 1){
00200 b.append(" and targ.name=:targpart");
00201 b.append(" and yrbeam = yrtarg");
00202 }
00203 for (int i = 1 ; i <= fsp.size(); i++){
00204 b.append(" and fsp"+i+".name=:fsp"+i+"part and fsp"+i+".multiplicity=:fsp"+i+"mult");
00205 b.append(" and yrbeam = yrfsp"+i);
00206 }
00207 if (theObs != "") { b.append(" and yabeam._observable like:observable"); }
00208 if(theSqrtsLo != ""){
00209 b.append(" and yabeam = yaprop");
00210 b.append(" and prop._name like 'sqrts' and prop._lowValue>:sqrtslo");
00211 }
00212
00213
00214
00215 q = _session.createQuery(b.toString());
00216 q.setString("beampart",isp.get(0));
00217 if (isp.size() > 1) {q.setString( "targpart",isp.get(1)); }
00218 for (int i = 1 ; i <= fsp.size(); i++){
00219 q.setString( "fsp"+i+"part",fsp.get(i-1));
00220 q.setString( "fsp"+i+"mult",mfsp.get(i-1));
00221 }
00222 System.out.println(theObs);
00223 if (theObs != "") {q.setString( "observable",theObs); }
00224 if (theSqrtsLo != "") {q.setString( "sqrtslo",theSqrtsLo); }
00225 }
00226
00227
00228 else if(fsp.size() > 0) {
00229
00230 if (flag == "count"){
00231 b.append("select count(distinct yafsp1) from YAxis yafsp1");
00232 }
00233 else{
00234 b.append("select distinct yafsp1 from YAxis yafsp1");
00235 }
00236
00237 b.append(" join yafsp1._reactions yrfsp1");
00238 b.append(" join yrfsp1._finalState fsp1");
00239 for (int i = 2 ; i <= fsp.size(); i++){
00240 b.append(",YAxis yafsp"+i);
00241 b.append(" join yafsp"+i+"._reactions yrfsp"+i);
00242 b.append(" join yrfsp"+i+"._finalState fsp"+i);
00243 }
00244 if(theSqrtsLo != ""){
00245 b.append(",YAxis yaprop");
00246 b.append(" join yaprop._properties prop");
00247 }
00248
00249 b.append(" where fsp1.name=:fsp1part and fsp1.multiplicity=:fsp1mult");
00250 for (int i = 2 ; i <= fsp.size(); i++){
00251 b.append(" and fsp"+i+".name=:fsp"+i+"part and fsp"+i+".multiplicity=:fsp"+i+"mult");
00252 b.append(" and yrfsp1 = yrfsp"+i);
00253 }
00254 if (theObs != ""){ b.append(" and yafsp1._observable like:observable"); }
00255 if(theSqrtsLo != ""){
00256 b.append(" and yafsp1 = yaprop");
00257 b.append(" and prop._name like 'sqrts' and prop._lowValue>:sqrtslo");
00258 }
00259
00260
00261 q = _session.createQuery(b.toString());
00262 for (int i = 1 ; i <= fsp.size(); i++){
00263 q.setString("fsp"+i+"part",fsp.get(i-1));
00264 q.setString("fsp"+i+"mult",mfsp.get(i-1));
00265 }
00266 if (theObs != "") {q.setString("observable",theObs);}
00267 if (theSqrtsLo != "") {q.setString( "sqrtslo",theSqrtsLo); }
00268 }
00269
00270 else if(theObs != "") {
00271 if (flag == "count"){
00272 b.append("select count(distinct yaobs) from YAxis yaobs");
00273 }
00274 else{
00275 b.append("select distinct yaobs from YAxis yaobs");
00276 }
00277 if(theSqrtsLo != ""){
00278 b.append(",YAxis yaprop");
00279 b.append(" join yaprop._properties prop");
00280 }
00281 b.append(" where yaobs._observable like:observable");
00282 if(theSqrtsLo != ""){
00283 b.append(" and yaobs = yaprop");
00284 b.append(" and prop._name like 'sqrts' and prop._lowValue>:sqrtslo");
00285 }
00286 q = _session.createQuery(b.toString());
00287 q.setString("observable",theObs);
00288 if (theSqrtsLo != "") {q.setString( "sqrtslo",theSqrtsLo); }
00289 }
00290
00291
00292 else if(theSqrtsLo != "") {
00293 if (flag == "count"){
00294 b.append("select count(distinct yaprop) from YAxis yaprop");
00295 }
00296 else{
00297 b.append("select distinct yaprop from YAxis yaprop");
00298 }
00299 b.append(" join yaprop._properties prop");
00300
00301 b.append(" where prop._name like 'sqrts' and prop._lowValue>:sqrtslo");
00302
00303 q = _session.createQuery(b.toString());
00304 q.setString("sqrtslo",theSqrtsLo);
00305 }
00306
00307 else{
00308 if (flag == "count"){
00309 b.append("select count(distinct ya) from YAxis ya");
00310 }
00311 else{
00312 b.append("select distinct ya from YAxis ya");
00313 }
00314 q = _session.createQuery(b.toString());
00315 }
00316
00317
00318
00319
00320
00321 System.out.println(q.toString());
00322 return q;
00323
00324 }
00325 }