diff --git a/rtd_yeastx/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EICMLChromatogramGeneratorServlet.java b/rtd_yeastx/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EICMLChromatogramGeneratorServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..52284791dcc6f6aa8a6d8f9256101ab5e7313e3f --- /dev/null +++ b/rtd_yeastx/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EICMLChromatogramGeneratorServlet.java @@ -0,0 +1,172 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.dss.generic.server; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.openbis.dss.generic.server.AbstractDatasetDownloadServlet; +import ch.systemsx.cisd.yeastx.eicml.ChromatogramDTO; +import ch.systemsx.cisd.yeastx.eicml.EICMLChromatogramImageGenerator; + +/** + * @author Chandrasekhar Ramakrishnan + */ +public class EICMLChromatogramGeneratorServlet extends AbstractDatasetDownloadServlet +{ + + private static final long serialVersionUID = 1L; + + // Required servlet parameters + + public final static String DATASET_CODE_PARAM = "dataset"; + + public final static String CHROMATOGRAM_CODE_PARAM = "chromatogram"; + + // Optional, but recommended servlet parameters + + public final static String IMAGE_WIDTH_PARAM = "w"; + + public final static String IMAGE_HEIGHT_PARAM = "h"; + + /** + * A utility class for dealing with the parameters required to generate an image from a + * chromatogram. + * + * @author Chandrasekhar Ramakrishnan + */ + private static class RequestParams + { + private final String sessionId; + + private final String datasetCode; + + private final int width; + + private final int height; + + public RequestParams(HttpServletRequest request) + { + sessionId = getParam(request, SESSION_ID_PARAM); + datasetCode = getParam(request, DATASET_CODE_PARAM); + width = getIntParam(request, IMAGE_WIDTH_PARAM); + height = getIntParam(request, IMAGE_HEIGHT_PARAM); + } + + private static int getIntParam(HttpServletRequest request, String paramName) + { + String value = getParam(request, paramName); + try + { + return Integer.valueOf(value); + } catch (NumberFormatException e) + { + throw new UserFailureException("parameter " + paramName + + " should be an integer, but is: " + value); + } + } + + private static String getParam(final HttpServletRequest request, String paramName) + { + String value = request.getParameter(paramName); + if (value == null) + { + throw new UserFailureException("no value for the parameter " + paramName + + " found in the URL"); + } + return value; + } + + public String getSessionId() + { + return sessionId; + } + + public String getDatasetCode() + { + return datasetCode; + } + + public int getWidth() + { + return width; + } + + public int getHeight() + { + return height; + } + } + + // private static IEICMSRunDAO createQuery(Properties properties) + // { + // final DatabaseConfigurationContext dbContext = DBUtils.createAndInitDBContext(properties); + // DataSource dataSource = dbContext.getDataSource(); + // return QueryTool.getQuery(dataSource, IEICMSRunDAO.class); + // } + + @Override + protected final void doGet(final HttpServletRequest request, final HttpServletResponse response) + throws ServletException, IOException + { + try + { + // Get the parameters from the request + RequestParams params = new RequestParams(request); + String sessionId = params.getSessionId(); + String datasetCode = params.getDatasetCode(); + int height = params.getHeight(); + int width = params.getWidth(); + + // Get the session and user from the request + HttpSession session = tryGetOrCreateSession(request, sessionId); + if (session == null) + { + printSessionExpired(response); + return; + } + // Check that the user has view access to the chromatogram data + // NOTE: This throws an exception -- it may be nicer to return an image for a + // non-accessible chromatogram... + ensureDatasetAccessible(datasetCode, session, sessionId); + + // TODO Get the chromatogram data + ChromatogramDTO chromatogram = getChromatogramForId(null); + + // Generate a chromatogram image into the stream + EICMLChromatogramImageGenerator generator = + new EICMLChromatogramImageGenerator(chromatogram, response.getOutputStream(), + width, height); + generator.generateImage(); + + } catch (Exception e) + { + printErrorResponse(response, "Error: " + e.getMessage()); + e.printStackTrace(); + } + } + + ChromatogramDTO getChromatogramForId(String id) + { + return null; + } +} diff --git a/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/eicml/EICMLChromatogramImagesReporter.java b/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/eicml/EICMLChromatogramImagesReporter.java new file mode 100644 index 0000000000000000000000000000000000000000..3579a518a659faaeeef885ba9a2bdd4e437944da --- /dev/null +++ b/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/eicml/EICMLChromatogramImagesReporter.java @@ -0,0 +1,147 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.yeastx.eicml; + +import static ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.SimpleTableModelBuilder.asText; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.sql.DataSource; + +import net.lemnik.eodsql.DataIterator; +import net.lemnik.eodsql.QueryTool; + +import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext; +import ch.systemsx.cisd.openbis.dss.generic.server.EICMLChromatogramGeneratorServlet; +import ch.systemsx.cisd.openbis.dss.generic.server.plugins.standard.AbstractDatastorePlugin; +import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.IReportingPluginTask; +import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.SimpleTableModelBuilder; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ISerializableComparable; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ImageTableCell; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.TableModel; +import ch.systemsx.cisd.openbis.generic.shared.dto.DatasetDescription; +import ch.systemsx.cisd.yeastx.db.DBUtils; + +/** + * Reporting plugin which shows images for the chromatograms contained in the specified datasets. + * + * @author Chandrasekhar Ramakrishnan + */ +public class EICMLChromatogramImagesReporter extends AbstractDatastorePlugin implements + IReportingPluginTask +{ + private static final String CHROMATOGRAM_SERVLET = "chromatogram"; + + private static final int THUMBNAIL_SIZE = 60; + + private static final long serialVersionUID = 1L; + + private final IEICMSRunDAO query; + + public EICMLChromatogramImagesReporter(Properties properties, File storeRoot) + { + super(properties, storeRoot); + this.query = createQuery(properties); + } + + private static IEICMSRunDAO createQuery(Properties properties) + { + final DatabaseConfigurationContext dbContext = DBUtils.createAndInitDBContext(properties); + DataSource dataSource = dbContext.getDataSource(); + return QueryTool.getQuery(dataSource, IEICMSRunDAO.class); + } + + public TableModel createReport(List<DatasetDescription> datasets) + { + SimpleTableModelBuilder builder = new SimpleTableModelBuilder(); + addReportHeaders(builder); + List<EICMSRunDTO> runs = fetchRuns(datasets); + for (EICMSRunDTO run : runs) + { + DataIterator<ChromatogramDTO> chromatograms = query.getChromatogramsForRun(run); + addRun(builder, run, chromatograms); + } + return builder.getTableModel(); + } + + private List<EICMSRunDTO> fetchRuns(List<DatasetDescription> datasets) + { + List<EICMSRunDTO> runs = new ArrayList<EICMSRunDTO>(); + for (DatasetDescription dataset : datasets) + { + EICMSRunDTO run = query.getMSRunByDatasetPermId(dataset.getDatasetCode()); + if (run != null) + { + runs.add(run); + } + } + return runs; + } + + private static void addRun(SimpleTableModelBuilder builder, EICMSRunDTO run, + DataIterator<ChromatogramDTO> chromatograms) + { + for (ChromatogramDTO chromatogram : chromatograms) + { + builder.addRow(createRow(builder, run, chromatogram)); + } + } + + private static List<ISerializableComparable> createRow(SimpleTableModelBuilder builder, + EICMSRunDTO run, ChromatogramDTO chromatogram) + { + List<ISerializableComparable> row = new ArrayList<ISerializableComparable>(); + + row.add(asText(chromatogram.getLabel())); + + StringBuffer imageURL = new StringBuffer(); + + imageURL.append(CHROMATOGRAM_SERVLET); + + imageURL.append("?"); + imageURL.append(EICMLChromatogramGeneratorServlet.DATASET_CODE_PARAM); + imageURL.append("="); + imageURL.append(run.getId()); + + imageURL.append("&"); + imageURL.append(EICMLChromatogramGeneratorServlet.CHROMATOGRAM_CODE_PARAM); + imageURL.append("="); + imageURL.append(chromatogram.getId()); + + imageURL.append("&"); + imageURL.append(EICMLChromatogramGeneratorServlet.IMAGE_WIDTH_PARAM); + imageURL.append("="); + imageURL.append(THUMBNAIL_SIZE); + + imageURL.append("&"); + imageURL.append(EICMLChromatogramGeneratorServlet.IMAGE_HEIGHT_PARAM); + imageURL.append("="); + imageURL.append(THUMBNAIL_SIZE); + + row.add(new ImageTableCell(imageURL.toString(), THUMBNAIL_SIZE, THUMBNAIL_SIZE)); + return row; + } + + private static void addReportHeaders(SimpleTableModelBuilder builder) + { + builder.addHeader("Label"); + builder.addHeader("Chromatogram"); + } +}