Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

FileUploader.java

Go to the documentation of this file.
00001 package cedar.jetweb.servlet;
00002 
00003 import java.util.HashMap;
00004 
00005 import javax.servlet.*;
00006 import javax.servlet.http.*;
00007 import java.io.*;
00008 import java.util.Enumeration;
00009 import java.util.Set;
00010 
00011 import cedar.jetweb.*;
00012 import cedar.jetweb.html.*;
00013 import cedar.jetweb.model.paper.Paper;
00014 import cedar.jetweb.model.plots.RealPlot;
00015 import cedar.jetweb.model.Model;
00016 import cedar.jetweb.model.plots.PlotSelection;
00017 import cedar.jetweb.model.RunSeries;
00018 import cedar.jetweb.job.LogFile;
00019 
00020 import com.oreilly.servlet.multipart.*;
00021 
00022 import cedar.jetweb.db.DBConnectionManager;
00023 import cedar.jetweb.db.DBObjectManager;
00024 import cedar.jetweb.db.DBConfig;
00025 import cedar.jetweb.db.DBGeneratorManager;
00026 import cedar.jetweb.generator.Generator;
00027 import cedar.jetweb.generator.GeneratorWrapper;
00028 import cedar.jetweb.xml.JetWebGeneratorReader;
00029 import cedar.jetweb.xml.JetWebLogReader;
00030 
00031 
00036 public class FileUploader extends HttpServlet {
00037 
00038     private String destinationDirName;
00039     private File destinationDir;
00040 
00041     private PrintWriter out;
00042     private int paperId;
00043 
00044     public void init(ServletConfig config) throws ServletException {
00045     super.init(config);
00046     // Set up configuration with servlet mode on. 
00047     JetWebConfig.init();
00048     }
00049 
00054     protected void doPost(HttpServletRequest request, 
00055               HttpServletResponse response)
00056     throws ServletException, java.io.IOException {
00057         upload(request,response);
00058     }
00059 
00064     protected void doGet(HttpServletRequest request, 
00065               HttpServletResponse response)
00066     throws ServletException, java.io.IOException {
00067         upload(request,response);
00068     }
00069 
00070 
00074     public void upload(HttpServletRequest request, HttpServletResponse response)
00075     throws ServletException, java.io.IOException {
00076       
00077     System.out.println("Called upload");
00078     //PrintWriter out = response.getWriter();
00079 
00080     out = response.getWriter();
00081 
00082     boolean isXML       = false;
00083     boolean isPaperText = false;
00084     boolean isDefaults = false;
00085     boolean isModel = false;
00086     int maxPlotNum = 0;
00087     String message = "";
00088     response.setContentType("text/html");
00089 
00090     try {
00091         MultipartParser mp = 
00092         new MultipartParser(request, 10*1024*1024); // 10MB
00093         Part part;
00094 
00095         HashMap<String,String> parameters = new HashMap<String,String>();
00096         
00097         while ((part = mp.readNextPart()) != null) {
00098         String name = part.getName();
00099         String plotNum = "";
00100 
00101         // Handle parameter parts. Determine the directory the file
00102         // is to be written into, the paper ID, and whether this is an XML
00103         // upload or not.
00104         if (part.isParam()) {
00105             ParamPart paramPart = (ParamPart) part;
00106             String value        = paramPart.getStringValue();
00107             if(name.equals("MODELXML")){
00108             isModel = true;
00109             }else if (name.equals("DEFAULTSUPLOAD")){
00110             isDefaults = true;
00111             }else if (name.equals("PLOTUPLOAD")) {
00112             isXML = true;
00113             } else if (name.equals("PAPERTEXTUPLOAD")) {
00114             isPaperText = true;
00115             } else if (name.equals("DIRECTORY")) {
00116             destinationDirName = value;
00117             } else if (name.equals("PAPERID")) {
00118             try {
00119                 paperId= Integer.parseInt(value);       
00120             } catch (Exception e) {
00121                 HTMLErrorWriter.write(out,e);
00122             }
00123             } else {
00124             parameters.put(name,value);
00125             }
00126 
00127         }
00128 
00129         if(part.isFile()){
00130             FilePart filePart = (FilePart) part;
00131             
00132             destinationDir = new File(destinationDirName);
00133             if (filePart.getFileName()!=null){
00134             System.out.println
00135                 ("Directory is "+destinationDirName+". File is "
00136                  +filePart.getFileName());
00137             
00138             if (! destinationDir.exists() ) {
00139                 System.out.println("ERROR: Specified dir = "
00140                            +destinationDirName
00141                            +" DOES NOT EXIST.");
00142             }
00143             
00144             if (isXML){
00145                 
00146                 try {
00147                 message = doPlotXMLUpload(filePart,parameters);
00148                 } catch (JetWebException jwe){
00149                 HTMLErrorWriter.write(out,jwe);
00150                 return;
00151                 }
00152             } else if(isDefaults){
00153                 try{
00154                 Generator gen = 
00155                     doDefaultsXMLUpload(filePart, parameters);
00156                 //HTMLGeneratorWriter.installedMessage(out,gen);
00157                 }catch (JetWebException jwe){
00158                 HTMLErrorWriter.write(out,jwe);
00159                 }
00160                 return;
00161             }else if(isModel){
00162                 try{
00163                 doModelUpload(filePart, request);
00164                 }catch (JetWebException err){
00165                 HTMLErrorWriter.write(out, err);
00166                 }
00167                 return;
00168             }else {
00169                 
00170                 JetWebException jwe = new 
00171                 JetWebException("Unknown file upload request."," ");
00172                 HTMLErrorWriter.write(out,jwe);
00173                 return;
00174             }
00175             }
00176         
00177 
00178         }
00179         }
00180     
00181         System.out.println("Paper ID="+paperId);
00182         
00183         // When we get here, we have all the parameters 
00184         // and so can start altering the plots.
00185         try {
00186         
00187         Paper paper = new Paper(paperId);       
00188         ServletUtils.deletePlots(parameters,paper);
00189         ServletUtils.modifyPlots(parameters,paper);
00190         ServletUtils.addPlot(parameters,paper,"NEW");
00191         System.out.println("back from ServeletUtils.addPlot()");
00192         HTMLPaperWriter pm = new HTMLPaperWriter
00193             (out, JetWebConfig.mainServletPath);
00194         System.out.println("Created new HTMLPaperWriter");
00195 
00196         pm.writePaperForm(paper," ");
00197 
00198         System.out.println("written blank message");
00199 
00200         pm.writePlotsForm(paper,message);
00201         System.out.println("written message " + message);
00202 
00203         pm.close();
00204         System.out.println("closed HTMLPaperWriter");
00205 
00206         return;
00207         
00208         } catch (JetWebException jwe){
00209         HTMLErrorWriter.write(out,jwe);
00210         }
00211     }catch (IOException lEx) {
00212         //this.getServletContext().log("ERROR: reading or saving file"+lEx.toString());
00213     }
00214     }
00215 
00216 
00217     private String doPlotXMLUpload(FilePart filePart,
00218                    HashMap<String,String> parameters) 
00219     throws JetWebException, IOException {
00220 
00221     String message   = "";
00222     String fileName = filePart.getFileName();
00223 
00224     String fileParm = filePart.getName();
00225     String plotNum  = fileParm.substring(fileParm.indexOf("-")+1);
00226 
00227     if (paperId != 0){
00228 
00229         System.out.println("Plot number "+plotNum);
00230         System.out.println("File name " + fileName );
00231         System.out.println("HZsteer plot id " + parameters.get("HZSTEERPLOTID"));
00232 
00233         System.out.println(parameters);
00234 
00235         if (fileName.equals("plot"+plotNum+".xml")) {
00236 
00237         long size = filePart.writeTo(destinationDir);
00238             
00239         System.out.println("replacing "+fileName);
00240         message = message+"<br>Successfully replaced file: "+fileName;
00241         parameters.put("UPDATED"+plotNum,"done");
00242         } else if(fileName.startsWith("n0") && fileName.endsWith(".aida") &&
00243               parameters.get("HZSTEERPLOTID")!=null &&
00244               !parameters.get("HZSTEERPLOTID").equals("")){
00245         //Then it's a file from hzsteer output.
00246 
00247         String plotDir = JetWebConfig.rootDirectory + "/" + 
00248             JetWebConfig.plotCacheDirName;
00249 
00250         System.out.println("found HZSteer output - writing to " +
00251                    plotDir);
00252 
00253         filePart.writeTo(new File(plotDir));
00254 
00255         //we save the file name in the parameters so that 
00256         //ServletUtils.addPlot can find it later.
00257 
00258         parameters.put("HZSTEERPLOT", 
00259                    plotDir + "/" + 
00260                    fileName.substring(fileName.lastIndexOf("/")+1));
00261 
00262         }else{
00263         System.out.println("Invalid file name");
00264         message = message+"XML file not replaced due to invalid name: "+fileName;
00265         }
00266 
00267     } else {
00268         JetWebException jwe = new 
00269         JetWebException("Paper ID is zero.","="+paperId);
00270         HTMLErrorWriter.write(out,jwe);
00271     }
00272 
00273     return message;
00274         
00275     }
00276 
00277     private Generator doDefaultsXMLUpload(FilePart filePart,
00278                    HashMap<String,String> parameters) 
00279     throws JetWebException, IOException {
00280 
00281     System.out.println("Called the xml defaults uploader");
00282 
00283     String fileName = filePart.getFileName();
00284 
00285     filePart.writeTo(destinationDir);
00286 
00287     GeneratorWrapper wrapper = null;
00288 
00289     System.out.println("opening xml file "+destinationDir+"/"+fileName);
00290 
00291     try{
00292         JetWebGeneratorReader reader = 
00293         new JetWebGeneratorReader(destinationDir+"/"+fileName);
00294         wrapper = reader.getGeneratorWrapper();
00295         
00296     }catch(Exception e){
00297         throw new JetWebException(e);
00298         }
00299 
00300     DBConnectionManager connManager = DBConfig.getDBConnectionManager();
00301     DBObjectManager objManager = new DBObjectManager();
00302     objManager.passDBConnection(connManager);
00303     String tableName =wrapper.toString()+"_defaults";
00304 
00305     Generator gen = wrapper.generator();
00306     String genName = gen.toString();
00307     int ii = genName.indexOf(".");
00308 
00309     while(ii>-1){
00310         genName = genName.substring(0,ii)+genName.substring(ii+1);
00311         ii = genName.indexOf(".");
00312     }
00313 
00314     ii = genName.indexOf("-");
00315     
00316     String genVersion = genName.substring(ii+1);
00317     genName = genName.substring(0, ii);
00318 
00319     Set<String> knownVersions = DBGeneratorManager.getVersions(genName);
00320 
00321     if(!knownVersions.contains(genVersion)){
00322 
00323 
00324         objManager.writeObject(wrapper, tableName, DBConfig.JETWEB);
00325                 
00326         // Now add the row to the generators table.
00327         System.out.println("using generator id = "+gen.getGeneratorId());
00328         DBGeneratorManager.add(wrapper.generator());
00329         
00330         System.out.println("used generator id = "+gen.getGeneratorId());
00331 
00332         HTMLGeneratorWriter.installedMessage(out,wrapper.generator());
00333         
00334 
00335     }else{
00336         HTMLGeneratorWriter.alreadyInstalledMessage
00337         (out, wrapper.generator());
00338     }
00339     return wrapper.generator();
00340     }
00341 
00342     private Model doModelUpload(FilePart filePart, 
00343                 HttpServletRequest request)
00344     throws JetWebException, IOException{
00345 
00346     String fileName = filePart.getFileName();
00347     filePart.writeTo(destinationDir);
00348 
00349     try{
00350         JetWebLogReader reader = 
00351         new JetWebLogReader(destinationDir+"/"+fileName);
00352         LogFile log = new LogFile(destinationDir+"/"+fileName);
00353         reader.fillLog(log);
00354         RunSeries rs = log.getRunSeries();
00355         PlotSelection selection = ServletUtils.makePlotSelection(request);
00356         // make this check only the sum plots!
00357         if (selection.fitIsEmpty()) { selection.setFitDefault(); }
00358         // make this check only the sum plots!
00359         if (selection.sumIsEmpty()) { selection.setSumDefault(); }
00360 
00361         ServletUtils.performResultSearch
00362         (rs, out, JetWebConfig.searchServletPath, selection, request);
00363 
00364     }catch(Exception e){
00365         throw new JetWebException(e);
00366     }
00367 
00368     return new Model();
00369     }
00370 }

Generated Wed Jan 17 09:14:27 GMT 2007