From: <rv...@us...> - 2009-06-23 14:21:02
|
Revision: 82 http://treebase.svn.sourceforge.net/treebase/?rev=82&view=rev Author: rvos Date: 2009-06-23 11:10:17 +0000 (Tue, 23 Jun 2009) Log Message: ----------- Refactored to take advantage of AbstractDownloadController superclass. Commented out (but kept in place) all now superfluous code. Modified Paths: -------------- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAMatrixController.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusFileController.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusRCTFileController.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeBlockController.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeController.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAnAnalysisStepController.java Added Paths: ----------- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAStudyController.java Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAMatrixController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAMatrixController.java 2009-06-23 11:08:34 UTC (rev 81) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAMatrixController.java 2009-06-23 11:10:17 UTC (rev 82) @@ -20,25 +20,26 @@ package org.cipres.treebase.web.controllers; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +//import java.io.File; +//import java.io.FileWriter; +//import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import org.cipres.treebase.TreebaseUtil; import org.cipres.treebase.domain.matrix.Matrix; import org.cipres.treebase.domain.matrix.MatrixService; +import org.cipres.treebase.domain.nexus.NexusDataSet; import org.cipres.treebase.domain.study.Study; import org.cipres.treebase.domain.study.StudyService; import org.cipres.treebase.domain.taxon.TaxonLabelSet; import org.cipres.treebase.web.util.ControllerUtil; -import org.cipres.treebase.web.util.WebUtil; +//import org.cipres.treebase.web.util.WebUtil; /** * DownloadAMatrixController.java @@ -52,8 +53,8 @@ * */ -public class DownloadAMatrixController implements Controller { - private static final Logger LOGGER = Logger.getLogger(DownloadAMatrixController.class); +public class DownloadAMatrixController extends AbstractDownloadController implements Controller { + //private static final Logger LOGGER = Logger.getLogger(DownloadAMatrixController.class); private MatrixService mMatrixService; private StudyService mStudyService; @@ -103,7 +104,6 @@ // + request.getSession().getServletContext().getRealPath("/NexusFileDownload")); // System.out.println("PATH INFO: " + request.getPathInfo()); // - // TODO: if (request.getParameter("matrixid") == null) { return null; @@ -113,40 +113,29 @@ //Study s = getStudyService().findByID(studyId); long matrixId = Long.parseLong(request.getParameter("matrixid")); - String fileName = getFileName(matrixId); +// String fileName = getFileName(matrixId,request); - String downloadDir = request.getSession().getServletContext().getRealPath( - TreebaseUtil.FILESEP + "NexusFileDownload") - + TreebaseUtil.FILESEP + request.getRemoteUser(); +// String downloadDir = request.getSession().getServletContext().getRealPath( +// TreebaseUtil.FILESEP + "NexusFileDownload") +// + TreebaseUtil.FILESEP + request.getRemoteUser(); +// String downloadDir = getDownloadDir(request); - long start = System.currentTimeMillis(); +// long start = System.currentTimeMillis(); - generateAFileDynamically(request, matrixId, downloadDir); - WebUtil.downloadFile(response, downloadDir, fileName); + generateAFileDynamically(request, response, matrixId); +// WebUtil.downloadFile(response, downloadDir, fileName); - long end = System.currentTimeMillis(); +// long end = System.currentTimeMillis(); +// +// if (LOGGER.isDebugEnabled()) { +// LOGGER.debug("TIME DIFFERENCE FOR A SINGAL FILE IN DOWNLOAD A MATRIX CONTROLLER: " +// + (end - start)); +// } - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("TIME DIFFERENCE FOR A SINGAL FILE IN DOWNLOAD A MATRIX CONTROLLER: " - + (end - start)); - } - return null; // new ModelAndView("treeList", Constants.TREE_LIST, phyloTrees); } /** - * This method adds "M" as Prefix to the matrix ID and ".nex" as Suffix. - * - * @param id (Matrix id) - * @return returns the file name to be used for down loading - */ - - private String getFileName(long id) { - - return "M" + id + ".nex"; - } - - /** * This method is responsible for generating a Matrix file in a particular down load directory. * But first It should get All the Taxa associated with a Matrix id and get the max length of * the Taxon Labels and put Taxa and remaining matrix information in a separate ArrayLists @@ -155,7 +144,7 @@ * @param pMatrixId It is Matrix id. * @param downloadDirName down load directory where files will be created */ - + /* private void generateAFileDynamically(HttpServletRequest request, long pMatrixId, String downloadDirName) { Study pStudy = ControllerUtil.findStudy(request, mStudyService); @@ -183,7 +172,7 @@ matrix.generateNexusBlock(matrixContent); // matrixContent.append("END;\n"); // Not Needed - String tmp = getFileName(pMatrixId); + String tmp = getFileName(pMatrixId,request); try { @@ -203,5 +192,36 @@ } } + */ + @Override + protected String getFileNamePrefix() { + return "M"; + } + + @Override + protected String getFileContent(long objectId, HttpServletRequest request) { + Study pStudy = ControllerUtil.findStudy(request, getStudyService()); + Matrix matrix = getMatrixService().findByID(objectId); + TaxonLabelSet taxa = matrix.getTaxa(); + + if ( getFormat(request) == FORMAT_NEXML ) { + NexusDataSet pNexusDataSet = new NexusDataSet(); + pNexusDataSet.getTaxonLabelSets().add(taxa); + pNexusDataSet.getMatrices().add(matrix); + return getNexmlService().serialize(pNexusDataSet); + } + else { // FORMAT_NEXUS or none + StringBuilder matrixContent = new StringBuilder(); + matrixContent.append("#NEXUS\n"); + TreebaseUtil.attachStudyHeader(pStudy, matrixContent); + if (taxa != null) { + //one taxon per line, no line number: + taxa.buildNexusBlockTaxa(matrixContent, true, false); + } + matrix.generateNexusBlock(matrixContent); + return matrixContent.toString(); + } + } + } Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusFileController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusFileController.java 2009-06-23 11:08:34 UTC (rev 81) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusFileController.java 2009-06-23 11:10:17 UTC (rev 82) @@ -22,17 +22,17 @@ package org.cipres.treebase.web.controllers; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +//import java.io.File; +//import java.io.FileWriter; +//import java.io.IOException; import java.sql.Clob; -import java.sql.SQLException; +//import java.sql.SQLException; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; @@ -42,7 +42,7 @@ import org.cipres.treebase.domain.study.StudyService; import org.cipres.treebase.domain.tree.PhyloTreeService; import org.cipres.treebase.web.util.ControllerUtil; -import org.cipres.treebase.web.util.WebUtil; +//import org.cipres.treebase.web.util.WebUtil; /** * @@ -54,9 +54,9 @@ * @author madhu * */ -public class DownloadANexusFileController implements Controller { +public class DownloadANexusFileController extends AbstractDownloadController implements Controller { - private static final Logger LOGGER = Logger.getLogger(DownloadANexusFileController.class); +// private static final Logger LOGGER = Logger.getLogger(DownloadANexusFileController.class); private PhyloTreeService mPhyloTreeService; private StudyService mStudyService; @@ -109,39 +109,40 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { - // TODO Auto-generated method stub - String sep = System.getProperty("file.separator"); - String downloadDir = request.getSession().getServletContext().getRealPath( - "/NexusFileDownload") - + sep + request.getRemoteUser(); +// String sep = System.getProperty("file.separator"); +// String downloadDir = request.getSession().getServletContext().getRealPath( +// "/NexusFileDownload") +// + sep + request.getRemoteUser(); +// String downloadDir = getDownloadDir(request); +// +// String treeId = request.getParameter("treeid"); +// String matrixId = request.getParameter("matrixid"); +// String nexusFile = request.getParameter("nexusfile"); +// +// if (treeId == null && matrixId == null && nexusFile == null) { +// return null; +// } +// long clickedParameterId = 0L; +// String nexusFileName = null; +// +// if (treeId != null) { +// clickedParameterId = Long.parseLong(treeId); +// nexusFileName = getPhyloTreeService().findByID(clickedParameterId).getNexusFileName(); +// } else if (matrixId != null) { +// clickedParameterId = Long.parseLong(matrixId); +// nexusFileName = getMatrixService().findByID(clickedParameterId).getNexusFileName(); +// } else if (nexusFile != null) { +// nexusFileName = (nexusFile).replaceAll("%20", "_"); +// } +// +// if (nexusFileName == null) { +// return null; +// } + + generateAFileDynamically(request,response,0L); +// generateAFileDynamically(request, downloadDir, nexusFileName); +// WebUtil.downloadFile(response, downloadDir, nexusFileName + org); - String treeId = request.getParameter("treeid"); - String matrixId = request.getParameter("matrixid"); - String nexusFile = request.getParameter("nexusfile"); - - if (treeId == null && matrixId == null && nexusFile == null) { - return null; - } - long clickedParameterId = 0L; - String nexusFileName = null; - - if (treeId != null) { - clickedParameterId = Long.parseLong(treeId); - nexusFileName = getPhyloTreeService().findByID(clickedParameterId).getNexusFileName(); - } else if (matrixId != null) { - clickedParameterId = Long.parseLong(matrixId); - nexusFileName = getMatrixService().findByID(clickedParameterId).getNexusFileName(); - } else if (nexusFile != null) { - nexusFileName = (nexusFile).replaceAll("%20", "_"); - } - - if (nexusFileName == null) { - return null; - } - - generateAFileDynamically(request, downloadDir, nexusFileName); - WebUtil.downloadFile(response, downloadDir, nexusFileName + org); - return null; } @@ -149,7 +150,8 @@ * @param req HttpServletRequest * @param downloadDirName String Name of the directory where file(s) will be downloaded * @param fileName String name of the download file - */ + */ + /* private void generateAFileDynamically( HttpServletRequest req, String downloadDirName, @@ -199,6 +201,72 @@ sqle.printStackTrace(); } + } + */ + + @Override + protected String getFileName(long objectId,HttpServletRequest req) { + String treeId = req.getParameter("treeid"); + String matrixId = req.getParameter("matrixid"); + String nexusFile = req.getParameter("nexusfile"); + + if (treeId == null && matrixId == null && nexusFile == null) { + return null; + } + long clickedParameterId = 0L; + String nexusFileName = null; + if (treeId != null) { + clickedParameterId = Long.parseLong(treeId); + nexusFileName = getPhyloTreeService().findByID(clickedParameterId).getNexusFileName(); + } else if (matrixId != null) { + clickedParameterId = Long.parseLong(matrixId); + nexusFileName = getMatrixService().findByID(clickedParameterId).getNexusFileName(); + } else if (nexusFile != null) { + nexusFileName = (nexusFile).replaceAll("%20", "_"); + } + return ( nexusFileName + org ).replaceAll(TreebaseUtil.ANEMPTYSPACE, "_"); + } + + @Override + protected String getFileNamePrefix() { + // Not necessary because we override getFileName + return null; + } + + @Override + protected String getFileContent(long objectId, HttpServletRequest req) { + String nexusFileName = null; + String treeId = req.getParameter("treeid"); + String matrixId = req.getParameter("matrixid"); + String nexusFile = req.getParameter("nexusfile"); + long clickedParameterId = 0L; + if (treeId != null) { + clickedParameterId = Long.parseLong(treeId); + nexusFileName = getPhyloTreeService().findByID(clickedParameterId).getNexusFileName(); + } else if (matrixId != null) { + clickedParameterId = Long.parseLong(matrixId); + nexusFileName = getMatrixService().findByID(clickedParameterId).getNexusFileName(); + } else if (nexusFile != null) { + nexusFileName = (nexusFile).replaceAll("%20", "_"); + } + if (nexusFileName == null) { + return null; + } + Study study = ControllerUtil.findStudy(req, mStudyService); + Map<String, Clob> nexusMap = study.getNexusFiles(); + Clob clob = nexusMap.get(nexusFileName); + String clobStr = "File Not Found. File Name is: " + nexusFileName; + if (clob != null) { + try { + int clobLength = (int) clob.length(); + char[] clobchars = new char[clobLength]; + clob.getCharacterStream().read(clobchars); + clobStr = new String(clobchars); + } catch ( Exception e ) { + e.printStackTrace(); + } + } + return clobStr; } } Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusRCTFileController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusRCTFileController.java 2009-06-23 11:08:34 UTC (rev 81) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadANexusRCTFileController.java 2009-06-23 11:10:17 UTC (rev 82) @@ -21,8 +21,8 @@ */ package org.cipres.treebase.web.controllers; -import java.io.File; -import java.io.FileWriter; +//import java.io.File; +//import java.io.FileWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,7 +36,7 @@ import org.cipres.treebase.domain.study.StudyService; import org.cipres.treebase.domain.study.Submission; import org.cipres.treebase.web.util.ControllerUtil; -import org.cipres.treebase.web.util.WebUtil; +//import org.cipres.treebase.web.util.WebUtil; /** * @author madhu @@ -46,7 +46,7 @@ * Modified on April 28, 2008 * */ -public class DownloadANexusRCTFileController implements Controller { +public class DownloadANexusRCTFileController extends AbstractDownloadController implements Controller { Logger LOGGER = Logger.getLogger(DownloadANexusRCTFileController.class); @@ -78,52 +78,91 @@ Long study_id = ControllerUtil.getStudyId(pRequest); Study study = mStudyService.findByID(study_id); Submission submission = study.getSubmission(); + generateAFileDynamically(pRequest, pResponse, submission.getId()); + return null; - String fileName = pRequest.getParameter("nexusfile"); +// String fileName = pRequest.getParameter("nexusfile"); +// if (fileName == null) { +// return null; +// } +// +// String nexFileName = (fileName + "rct").replaceAll(TreebaseUtil.ANEMPTYSPACE, "_"); +// +// if (LOGGER.isInfoEnabled()) { +// LOGGER.info("Submission id: " + submission.getId() + ", FileName: " + nexFileName); +// } + +// String downloadDir = pRequest.getSession().getServletContext().getRealPath( +// TreebaseUtil.FILESEP + "NexusFileDownload") +// + TreebaseUtil.FILESEP + pRequest.getRemoteUser(); +// String downloadDir = getDownloadDir(pRequest); +// +// long start = System.currentTimeMillis(); +// +// File dirPath = new File(downloadDir); +// if (!dirPath.exists()) { +// dirPath.mkdirs(); +// } +// +// String pathName = downloadDir + TreebaseUtil.FILESEP + nexFileName; +// File file = new File(pathName); +// +// if (LOGGER.isDebugEnabled()) { +// LOGGER.debug("path=" + pathName); +// } +// +// FileWriter fwriter = new FileWriter(file); +// StringBuilder bldr = new StringBuilder("#NEXUS"); +// bldr.append(TreebaseUtil.getLineSeparators(2)); +// bldr +// .append(getStudyService().generateReconstructedNexusFile(submission.getId(), fileName)); +// +// fwriter.write(bldr.toString()); +// fwriter.close(); +// WebUtil.downloadFile(pResponse, downloadDir, nexFileName); +// +// if (LOGGER.isDebugEnabled()) { +// long end = System.currentTimeMillis(); +// LOGGER.debug(" file download time=" + (end-start)); +// } +// +// return null; + } + + @Override + protected String getFileName(long objectId,HttpServletRequest req) { + String fileName = req.getParameter("nexusfile"); if (fileName == null) { return null; } + return (fileName + "rct").replaceAll(TreebaseUtil.ANEMPTYSPACE, "_"); + } - String nexFileName = (fileName + "rct").replaceAll(TreebaseUtil.ANEMPTYSPACE, "_"); + @Override + protected String getFileNamePrefix() { + // Not necessary because we override getFileName + return null; + } - if (LOGGER.isInfoEnabled()) { - LOGGER.info("Submission id: " + submission.getId() + ", FileName: " + nexFileName); + @Override + protected String getFileContent(long submisionId, HttpServletRequest request) { + if ( getFormat(request) == FORMAT_NEXML ) { + Long study_id = ControllerUtil.getStudyId(request); + Study study = mStudyService.findByID(study_id); + return getNexmlService().serialize(study); } - - String downloadDir = pRequest.getSession().getServletContext().getRealPath( - TreebaseUtil.FILESEP + "NexusFileDownload") - + TreebaseUtil.FILESEP + pRequest.getRemoteUser(); - - long start = System.currentTimeMillis(); - - File dirPath = new File(downloadDir); - if (!dirPath.exists()) { - dirPath.mkdirs(); + else { + String fileName = request.getParameter("nexusfile"); + if (fileName == null) { + return null; + } + StringBuilder bldr = new StringBuilder("#NEXUS"); + bldr + .append(TreebaseUtil.getLineSeparators(2)) + .append(getStudyService() + .generateReconstructedNexusFile(submisionId, fileName)); + return bldr.toString(); } - - String pathName = downloadDir + TreebaseUtil.FILESEP + nexFileName; - File file = new File(pathName); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("path=" + pathName); - } - - FileWriter fwriter = new FileWriter(file); - StringBuilder bldr = new StringBuilder("#NEXUS"); - bldr.append(TreebaseUtil.getLineSeparators(2)); - bldr - .append(getStudyService().generateReconstructedNexusFile(submission.getId(), fileName)); - - fwriter.write(bldr.toString()); - fwriter.close(); - WebUtil.downloadFile(pResponse, downloadDir, nexFileName); - - if (LOGGER.isDebugEnabled()) { - long end = System.currentTimeMillis(); - LOGGER.debug(" file download time=" + (end-start)); - } - - return null; } } Added: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAStudyController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAStudyController.java (rev 0) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAStudyController.java 2009-06-23 11:10:17 UTC (rev 82) @@ -0,0 +1,92 @@ +/** + * + */ +package org.cipres.treebase.web.controllers; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.cipres.treebase.TreebaseIDString; +import org.cipres.treebase.TreebaseUtil; +import org.cipres.treebase.domain.matrix.Matrix; +import org.cipres.treebase.domain.study.Study; +import org.cipres.treebase.domain.study.StudyService; +import org.cipres.treebase.domain.taxon.TaxonLabelSet; +import org.cipres.treebase.domain.tree.TreeBlock; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.Controller; + +/** + * @author rvosa + * + */ +public class DownloadAStudyController extends AbstractDownloadController + implements Controller { + private StudyService mStudyService; + + public StudyService getStudyService() { + return mStudyService; + } + + public void setStudyService(StudyService studyService) { + mStudyService = studyService; + } + + /* (non-Javadoc) + * @see org.cipres.treebase.web.controllers.AbstractDownloadController#getFileContent(long, javax.servlet.http.HttpServletRequest) + */ + @Override + protected String getFileContent(long pStudyID, HttpServletRequest request) { + Study study = getStudyService().findByID(pStudyID); + if ( getFormat(request) == FORMAT_NEXML ) { + return getNexmlService().serialize(study); + } + else { + StringBuilder builder = new StringBuilder(); + builder.append("#NEXUS\n\n"); + + // header: + TreebaseUtil.attachStudyHeader(study, builder); + + // taxa: + for ( TaxonLabelSet tls : study.getTaxonLabelSets() ) { + // one taxon label per line, no line number. + tls.buildNexusBlockTaxa(builder, true, false); + } + + // matrices:: + for ( Matrix m : study.getMatrices() ) { + m.generateNexusBlock(builder); + } + + //tree blocks: + for ( TreeBlock tb : study.getTreeBlocks() ) { + tb.generateAFileDynamically(builder); + } + + return builder.toString(); + } + } + + /* (non-Javadoc) + * @see org.cipres.treebase.web.controllers.AbstractDownloadController#getFileNamePrefix() + */ + @Override + protected String getFileNamePrefix() { + return TreebaseIDString.getPrefixForClass(Study.class); + } + + /* (non-Javadoc) + * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + public ModelAndView handleRequest(HttpServletRequest request, + HttpServletResponse response) throws Exception { + if (request.getParameter("id") == null) { + return null; + } + long pStudyId = Long.parseLong(request.getParameter("id")); + generateAFileDynamically(request, response, pStudyId); + return null; + } + +} Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeBlockController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeBlockController.java 2009-06-23 11:08:34 UTC (rev 81) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeBlockController.java 2009-06-23 11:10:17 UTC (rev 82) @@ -20,19 +20,21 @@ package org.cipres.treebase.web.controllers; -import java.io.File; -import java.io.FileWriter; +//import java.io.File; +//import java.io.FileWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; +//import org.cipres.treebase.TreebaseUtil; +import org.cipres.treebase.domain.nexus.NexusDataSet; import org.cipres.treebase.domain.tree.PhyloTreeHome; import org.cipres.treebase.domain.tree.TreeBlock; -import org.cipres.treebase.web.util.WebUtil; +//import org.cipres.treebase.web.util.WebUtil; /** * @@ -40,8 +42,8 @@ * */ -public class DownloadATreeBlockController implements Controller { - private static final Logger LOGGER = Logger.getLogger(DownloadATreeBlockController.class); +public class DownloadATreeBlockController extends AbstractDownloadController implements Controller { +// private static final Logger LOGGER = Logger.getLogger(DownloadATreeBlockController.class); private PhyloTreeHome mPhyloTreeHome; @@ -67,53 +69,66 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { - // TODO: - String sep = System.getProperty("file.separator"); +// String sep = System.getProperty("file.separator"); if (request.getParameter("treeblockid") == null) { return null; } long blockid = Long.parseLong(request.getParameter("treeblockid")); - String fileName = getFileName(blockid); + generateAFileDynamically(request, response, blockid); + return null; + //String fileName = getFileName(blockid,request); - String downloadDir = request.getSession().getServletContext().getRealPath( - "/NexusFileDownload") - + sep + request.getRemoteUser(); +// String downloadDir = request.getSession().getServletContext().getRealPath( +// "/NexusFileDownload") +// + sep + request.getRemoteUser(); +// String downloadDir = getDownloadDir(request); +// +// long start = System.currentTimeMillis(); +// +// TreeBlock treeBlock = getPhyloTreeHome().findTreeBlockById(blockid); +// +// File dirPath = new File(downloadDir); +// if (!dirPath.exists()) { +// dirPath.mkdirs(); +// } +// File file = new File(downloadDir + System.getProperty("file.separator") +// + getFileName(blockid,request)); +// FileWriter fwriter = new FileWriter(file); +// StringBuilder bldr = new StringBuilder("#NEXUS\n\n"); +// treeBlock.generateAFileDynamically(bldr); +// fwriter.write(bldr.toString()); +// fwriter.close(); +// generateAFileDynamically(request, blockid); +// WebUtil.downloadFile(response, downloadDir, getFileName(blockid,request)); +// +// long end = System.currentTimeMillis(); +// +// if (LOGGER.isDebugEnabled()) { +// LOGGER.debug("TIME DIFFERENCE FOR A SINGAL FILE: " + (end - start)); +// } +// +// return null; + } - long start = System.currentTimeMillis(); + @Override + protected String getFileNamePrefix() { + return "TB"; + } - TreeBlock element = getPhyloTreeHome().findTreeBlockById(blockid); - - File dirPath = new File(downloadDir); - if (!dirPath.exists()) { - dirPath.mkdirs(); + @Override + protected String getFileContent(long blockid, HttpServletRequest request) { + TreeBlock treeBlock = getPhyloTreeHome().findTreeBlockById(blockid); + if ( getFormat(request) == FORMAT_NEXML ) { + NexusDataSet nexusDataSet = new NexusDataSet(); + nexusDataSet.getTaxonLabelSets().add(treeBlock.getTaxonLabelSet()); + nexusDataSet.getTreeBlocks().add(treeBlock); + return getNexmlService().serialize(nexusDataSet); } - File file = new File(downloadDir + System.getProperty("file.separator") - + getFileName(blockid)); - FileWriter fwriter = new FileWriter(file); - StringBuilder bldr = new StringBuilder("#NEXUS\n\n"); - element.generateAFileDynamically(bldr); - fwriter.write(bldr.toString()); - fwriter.close(); - WebUtil.downloadFile(response, downloadDir, fileName); - - long end = System.currentTimeMillis(); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("TIME DIFFERENCE FOR A SINGAL FILE: " + (end - start)); + else { + StringBuilder bldr = new StringBuilder("#NEXUS\n\n"); + treeBlock.generateAFileDynamically(bldr); + return bldr.toString(); } - - return null; } - /** - * This method adds "TB" as Prefix to the tree block ID and ".tre" as Suffix. - * - * @param id (TreeBlock id) - * @return returns the file name to be used for down loading - */ - private String getFileName(long id) { - - return "TB" + id + ".tre"; - } - } Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeController.java 2009-06-23 11:08:34 UTC (rev 81) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadATreeController.java 2009-06-23 11:10:17 UTC (rev 82) @@ -20,24 +20,26 @@ package org.cipres.treebase.web.controllers; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +//import java.io.File; +//import java.io.FileWriter; +//import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import org.cipres.treebase.TreebaseUtil; +import org.cipres.treebase.domain.nexus.NexusDataSet; import org.cipres.treebase.domain.study.Study; import org.cipres.treebase.domain.study.StudyService; import org.cipres.treebase.domain.tree.PhyloTree; import org.cipres.treebase.domain.tree.PhyloTreeService; +import org.cipres.treebase.domain.tree.TreeBlock; import org.cipres.treebase.web.util.ControllerUtil; -import org.cipres.treebase.web.util.WebUtil; +//import org.cipres.treebase.web.util.WebUtil; import org.cipres.treebase.domain.taxon.TaxonLabelSet; @@ -53,8 +55,8 @@ * */ -public class DownloadATreeController implements Controller { - private static final Logger LOGGER = Logger.getLogger(DownloadATreeController.class); +public class DownloadATreeController extends AbstractDownloadController implements Controller { +// private static final Logger LOGGER = Logger.getLogger(DownloadATreeController.class); private PhyloTreeService mPhyloTreeService; private StudyService mStudyService; @@ -94,50 +96,42 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { - // TODO: - String sep = System.getProperty("file.separator"); +// String sep = System.getProperty("file.separator"); if (request.getParameter("treeid") == null) { return null; } long treeid = Long.parseLong(request.getParameter("treeid")); - String fileName = getFileName(treeid); - - String downloadDir = request.getSession().getServletContext().getRealPath( - "/NexusFileDownload") - + sep + request.getRemoteUser(); - - long start = System.currentTimeMillis(); - - generateAFileDynamically(request, treeid, downloadDir); - WebUtil.downloadFile(response, downloadDir, fileName); - - long end = System.currentTimeMillis(); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("TIME DIFFERENCE FOR A SINGAL FILE: " + (end - start)); - } - - return null; // new ModelAndView("treeList", Constants.TREE_LIST, phyloTrees); + generateAFileDynamically(request, response, treeid); + return null; +// String fileName = getFileName(treeid,request); +// +// String downloadDir = request.getSession().getServletContext().getRealPath( +// "/NexusFileDownload") +// + sep + request.getRemoteUser(); +// String downloadDir = getDownloadDir(request); +// +// long start = System.currentTimeMillis(); +// +// generateAFileDynamically(request, treeid, downloadDir); +// WebUtil.downloadFile(response, downloadDir, fileName); +// +// long end = System.currentTimeMillis(); +// +// if (LOGGER.isDebugEnabled()) { +// LOGGER.debug("TIME DIFFERENCE FOR A SINGAL FILE: " + (end - start)); +// } +// +// return null; // new ModelAndView("treeList", Constants.TREE_LIST, phyloTrees); } /** - * This method adds "T" as Prefix to the tree ID and ".tre" as Suffix. * - * @param id (Tree id) - * @return returns the file name to be used for downloading - */ - private String getFileName(long id) { - - return "T" + id + ".tre"; - } - - /** - * * @param request * @param pTreeId It is tree id. * @param downloadDirName download directory where files will be created */ + /* private void generateAFileDynamically( HttpServletRequest request, long pTreeId, @@ -145,15 +139,15 @@ Study study = ControllerUtil.findStudy(request, mStudyService); - File dirPath = new File(downloadDirName); - if (!dirPath.exists()) { - dirPath.mkdirs(); - } +// File dirPath = new File(downloadDirName); +// if (!dirPath.exists()) { +// dirPath.mkdirs(); +// } PhyloTree tree = getPhyloTreeService().findByID(pTreeId); TaxonLabelSet tbnlblSet = tree.getTreeBlock().getTaxonLabelSet(); - String tmp = getFileName(pTreeId); +// String tmp = getFileName(pTreeId,request); StringBuilder builder = new StringBuilder(); @@ -168,19 +162,60 @@ //tree block: tree.buildNexusBlock(builder); + + generateAFileDynamically(request, pTreeId); - try { +// try { +// +// File file = new File(downloadDirName + TreebaseUtil.FILESEP + tmp); +// FileWriter out = new FileWriter(file); +// +// out.write(builder.toString()); +// +// out.close(); +// +// } catch (IOException e) { +// e.printStackTrace(); +// } + } +*/ + @Override + protected String getFileNamePrefix() { + return "T"; + } - File file = new File(downloadDirName + TreebaseUtil.FILESEP + tmp); - FileWriter out = new FileWriter(file); - - out.write(builder.toString()); - - out.close(); - - } catch (IOException e) { - e.printStackTrace(); + @Override + protected String getFileContent(long pTreeId, HttpServletRequest request) { + Study study = ControllerUtil.findStudy(request, mStudyService); + PhyloTree tree = getPhyloTreeService().findByID(pTreeId); + tree = getPhyloTreeService().resurrect(tree); + TreeBlock enclosingTreeBlock = getPhyloTreeService().resurrect(tree.getTreeBlock()); + TaxonLabelSet tls = getPhyloTreeService().resurrect(enclosingTreeBlock.getTaxonLabelSet()); + if ( getFormat(request) == FORMAT_NEXML ) { + NexusDataSet nds = new NexusDataSet(); + nds.getTaxonLabelSets().add(tls); + TreeBlock treeBlock = new TreeBlock(); + treeBlock.setTaxonLabelSet(tls); + treeBlock.addPhyloTree(tree); + nds.getTreeBlocks().add(treeBlock); + return getNexmlService().serialize(nds); } + else { + StringBuilder builder = new StringBuilder(); + builder.append("#NEXUS\n\n"); + + // header: + TreebaseUtil.attachStudyHeader(study, builder); + + // taxa: + // one taxon label per line, no line number. + tls.buildNexusBlockTaxa(builder, true, false); + + //tree block: + tree.buildNexusBlock(builder); + + return builder.toString(); + } } } Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAnAnalysisStepController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAnAnalysisStepController.java 2009-06-23 11:08:34 UTC (rev 81) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/DownloadAnAnalysisStepController.java 2009-06-23 11:10:17 UTC (rev 82) @@ -20,9 +20,9 @@ package org.cipres.treebase.web.controllers; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +//import java.io.File; +//import java.io.FileWriter; +//import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -39,7 +39,7 @@ import org.cipres.treebase.domain.taxon.TaxonLabelSet; import org.cipres.treebase.domain.tree.PhyloTree; import org.cipres.treebase.domain.tree.PhyloTreeService; -import org.cipres.treebase.web.util.WebUtil; +//import org.cipres.treebase.web.util.WebUtil; /** * @@ -49,7 +49,7 @@ * @author rvosa * */ -public class DownloadAnAnalysisStepController implements Controller { +public class DownloadAnAnalysisStepController extends AbstractDownloadController implements Controller { private MatrixService mMatrixService; private PhyloTreeService mPhyloTreeService; private AnalysisStepService mAnalysisStepService; @@ -117,14 +117,15 @@ } long analysisId = Long.parseLong(request.getParameter("analysisid")); - String fileName = getFileName(analysisId); +// String fileName = getFileName(analysisId,request); - String downloadDir = request.getSession().getServletContext().getRealPath( - TreebaseUtil.FILESEP + "NexusFileDownload") - + TreebaseUtil.FILESEP + request.getRemoteUser(); +// String downloadDir = request.getSession().getServletContext().getRealPath( +// TreebaseUtil.FILESEP + "NexusFileDownload") +// + TreebaseUtil.FILESEP + request.getRemoteUser(); +// String downloadDir = getDownloadDir(request); - generateAFileDynamically(request, analysisId, downloadDir); - WebUtil.downloadFile(response, downloadDir, fileName); + generateAFileDynamically(request, response, analysisId); +// WebUtil.downloadFile(response, downloadDir, fileName); return null; } @@ -136,6 +137,7 @@ * @param analysisId * @param downloadDirName */ + /* private void generateAFileDynamically(HttpServletRequest request, long analysisId, String downloadDirName) { AnalysisStep step = getAnalysisStepService().findByID(analysisId); @@ -187,7 +189,7 @@ } } - String tmp = getFileName(analysisId); + String tmp = getFileName(analysisId,request); try { File file = new File(downloadDirName + TreebaseUtil.FILESEP + tmp); FileWriter out = new FileWriter(file); @@ -198,9 +200,57 @@ } } - - private String getFileName(long id) { - return "AnalysisStep" + id + ".nex"; + */ + + @Override + protected String getFileNamePrefix() { + return "AnalysisStep"; } + @Override + protected String getFileContent(long analysisId, HttpServletRequest request) { + AnalysisStep step = getAnalysisStepService().findByID(analysisId); + StringBuilder stepContent = new StringBuilder(); + stepContent.append("#NEXUS\n"); + + //header: + TreebaseUtil.attachStudyHeader(step.getAnalysis().getStudy(), stepContent); + stepContent.append("[ The following blocks are input data for analysis step " + analysisId + " ]\n"); + TaxonLabelSet inputLabelSet = step.getInputTaxonLabelSet(); + inputLabelSet.buildNexusBlockTaxa(stepContent, true, false); + for ( AnalyzedData data : step.getDataSetReadOnly() ) { + if ( data.isInputData() ) { + PhyloTree tree = data.getTreeData(); + Matrix matrix = data.getMatrixData(); + if ( tree != null ) { + tree.getTreeBlock().setTaxonLabelSet(inputLabelSet); + tree.buildNexusBlock(stepContent); + } + if ( matrix != null ) { + matrix.setTaxa(inputLabelSet); + matrix.generateNexusBlock(stepContent); + } + } + } + + stepContent.append("[ The following blocks are output data for analysis step " + analysisId + " ]\n"); + TaxonLabelSet outputLabelSet = step.getOutputTaxonLabelSet(); + outputLabelSet.buildNexusBlockTaxa(stepContent, true, false); + for ( AnalyzedData data : step.getDataSetReadOnly() ) { + if ( ! data.isInputData() ) { + PhyloTree tree = data.getTreeData(); + Matrix matrix = data.getMatrixData(); + if ( tree != null ) { + tree.getTreeBlock().setTaxonLabelSet(outputLabelSet); + tree.buildNexusBlock(stepContent); + } + if ( matrix != null ) { + matrix.setTaxa(outputLabelSet); + matrix.generateNexusBlock(stepContent); + } + } + } + return stepContent.toString(); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |