package org.splat.service;
-import java.awt.Graphics2D;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Scanner;
import java.util.Set;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.hibernate.Criteria;
-import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
-import org.hibernate.proxy.HibernateProxy;
-import org.jfree.chart.ChartFactory;
-import org.jfree.chart.JFreeChart;
-import org.jfree.chart.plot.PlotOrientation;
-import org.jfree.data.xy.XYSeries;
-import org.jfree.data.xy.XYSeriesCollection;
-import org.splat.common.properties.MessageKeyEnum;
import org.splat.dal.bo.kernel.Relation;
import org.splat.dal.bo.kernel.User;
import org.splat.dal.bo.som.ActorRelation;
import org.splat.dal.bo.som.IDBuilder;
import org.splat.dal.bo.som.KnowledgeElement;
import org.splat.dal.bo.som.ProgressState;
-import org.splat.dal.bo.som.ProjectElement;
import org.splat.dal.bo.som.Publication;
import org.splat.dal.bo.som.ReaderRelation;
import org.splat.dal.bo.som.Scenario;
import org.splat.dal.dao.som.UsedByRelationDAO;
import org.splat.dal.dao.som.ValidationCycleDAO;
import org.splat.exception.BusinessException;
-import org.splat.exception.IncompatibleDataException;
import org.splat.exception.InvalidParameterException;
import org.splat.kernel.InvalidPropertyException;
-import org.splat.kernel.MismatchException;
import org.splat.kernel.MissedPropertyException;
import org.splat.kernel.MultiplyDefinedException;
import org.splat.log.AppLogger;
-import org.splat.service.dto.DocToCompareDTO;
-import org.splat.service.dto.DocumentDTO;
-import org.splat.service.dto.StudyFacadeDTO;
import org.splat.service.dto.UserDTO;
import org.splat.service.technical.IndexService;
import org.splat.service.technical.ProjectSettingsService;
import org.splat.service.technical.ProjectSettingsServiceImpl;
import org.splat.service.technical.RepositoryService;
-import org.splat.service.technical.ProjectSettingsService.Step;
import org.splat.som.Revision;
import org.splat.util.BeanHelper;
import org.springframework.transaction.annotation.Transactional;
-import com.lowagie.text.Document;
-import com.lowagie.text.DocumentException;
-import com.lowagie.text.Rectangle;
-import com.lowagie.text.pdf.DefaultFontMapper;
-import com.lowagie.text.pdf.PdfContentByte;
-import com.lowagie.text.pdf.PdfTemplate;
-import com.lowagie.text.pdf.PdfWriter;
-
/**
* This class defines all methods for creation, modification the study.
*
.getAttribute(DescriptionAttribute.class));
}
- /**
- *
- * {@inheritDoc}
- *
- * @see org.splat.service.StudyService#compare(java.util.List)
- */
- @Override
- public String compare(final List<DocToCompareDTO> docsList,
- final String userName) throws IncompatibleDataException {
-
- String axis1Name = "";
- String axis2Name = "";
- String chartTitle = "";
- String resultPath = "";
-
- XYSeriesCollection dataset = new XYSeriesCollection();
-
- Iterator<DocToCompareDTO> docListIter = docsList.iterator();
-
- for (; docListIter.hasNext();) {
-
- DocToCompareDTO docDTO = docListIter.next();
- String pathToFile = docDTO.getPathToFile();
- File compDocFile = new File(pathToFile);
-
- resultPath = pathToFile.substring(0, pathToFile.indexOf("vault"))
- + "downloads" + File.separator + userName + File.separator
- + "ComparisonResult.pdf";
-
- XYSeries series = new XYSeries("Study: " + docDTO.getStudyTitle()
- + " Scenario: " + docDTO.getScenarioTitle() + " Document: "
- + docDTO.getDocumentTitle());
-
- // read the file and get points information.
- try {
- Scanner input = new Scanner(compDocFile);
-
- // get the title of the chart.
- if (input.hasNext()) {
- chartTitle = input.nextLine();
- }
-
- // get the name of the axis.
- if (input.hasNext()) {
- String[] tokens = input.nextLine().split(",");
-
- if (tokens.length < 2) {
- throw new IncompatibleDataException(
- MessageKeyEnum.IDT_000001.toString());
- }
-
- if ("".equals(axis1Name)) {
- axis1Name = tokens[0];
- } else if (!axis1Name.equals(tokens[0])) {
- LOG.debug("Axis must be the same for all documents");
- throw new IncompatibleDataException(
- MessageKeyEnum.IDT_000001.toString());
- }
-
- if ("".equals(axis2Name)) {
- axis2Name = tokens[1];
- } else if (!axis2Name.equals(tokens[1])) {
- LOG.debug("Axis must be the same for all documents");
- throw new IncompatibleDataException(
- MessageKeyEnum.IDT_000001.toString());
- }
- }
-
- // Get the XY points series.
- while (input.hasNext()) {
-
- String currentString = input.nextLine();
-
- if (!("".equals(currentString))) {
- String[] tokens = currentString.split(" ");
- series.add(Double.valueOf(tokens[0]), Double
- .valueOf(tokens[1]));
- }
-
- } // while
-
- dataset.addSeries(series);
-
- } catch (FileNotFoundException e) {
- LOG.error("Sorry, the file is not found.", e);
- }
- } // for
-
- JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, // Title
- axis1Name, // x-axis Label
- axis2Name, // y-axis Label
- dataset, // Dataset
- PlotOrientation.VERTICAL, // Plot Orientation
- true, // Show Legend
- true, // Use tooltips
- false // Configure chart to generate URLs?
- );
-
- // export to PDF - file.
- int x = 500;
- int y = 300;
- Rectangle pagesize = new Rectangle(x, y);
- Document document = new Document(pagesize, 50, 50, 50, 50);
- PdfWriter writer;
- try {
- File resFile = new File(resultPath);
- File resFolder = new File(resultPath.substring(0, resultPath
- .lastIndexOf(File.separator)));
- resFolder.mkdirs();
- writer = PdfWriter.getInstance(document, new FileOutputStream(
- resFile));
-
- document.open();
- PdfContentByte cb = writer.getDirectContent();
- PdfTemplate tp = cb.createTemplate(x, y);
- Graphics2D g2 = tp.createGraphics(x, y, new DefaultFontMapper());
- chart.draw(g2, new java.awt.Rectangle(x, y));
- g2.dispose();
- cb.addTemplate(tp, 0, 0);
- document.close();
-
- } catch (FileNotFoundException e) {
- LOG.error("Sorry, the file is not found.", e);
- } catch (DocumentException e) {
- LOG.error("Sorry, the DocumentException is thrown.", e);
- }
-
- return resultPath;
- }
-
/**
* {@inheritDoc}
*
_userService = userService;
}
- /**
- * {@inheritDoc}
- *
- * @see org.splat.service.StudyService#getComparableStudies()
- */
- @Transactional(readOnly = true)
- public List<StudyFacadeDTO> getComparableStudies(final long userId)
- throws MismatchException {
- // retrieve the number of the "Analyze the results" step
- List<Step> allSteps = _projectSettings.getAllSteps();
- Step theAnalyzeStep = null;
- for (Step step : allSteps) {
- if (step.getKey().equals("postprocessing")) {
- theAnalyzeStep = step;
- }
- }
- if (theAnalyzeStep == null) { // TODO: throw some other exception
- throw new MismatchException(
- "no step with key 'postprocessing' found."
- + "Probably, customization settings have been changed.");
- }
-
- List<Publication> publications = _publicationDAO.getFilteredList(
- "mydoc", Restrictions.eq("step", Integer.valueOf(theAnalyzeStep
- .getNumber())));
-
- // split retrieved publications into groups by studies and scenarios
- Map<Study, List<ProjectElement>> studyMap = new HashMap<Study, List<ProjectElement>>();
- Map<ProjectElement, List<Publication>> scenarioMap = new HashMap<ProjectElement, List<Publication>>();
-
- for (Publication publication : publications) {
- // filter out publications corresponding to a document of given step which is not a _result_ document
- if (!publication.value().getType().isResultOf(theAnalyzeStep)
- || !"srd".equals(publication.getSourceFile().getFormat())) {
- continue;
- }
-
- // check the study visibility to the user
- if (!isStaffedBy(publication.getOwnerStudy(), _userService
- .selectUser(userId))
- && Visibility.PUBLIC.equals(publication.getOwnerStudy()
- .getVisibility())) {
- continue;
- }
-
- Study study = publication.getOwnerStudy();
- ProjectElement scenario = publication.getOwner();
-
- Hibernate.initialize(scenario);
- if (scenario instanceof HibernateProxy) {
- scenario = (ProjectElement) ((HibernateProxy) scenario)
- .getHibernateLazyInitializer().getImplementation();
- }
-
- if (!(scenario instanceof Scenario)) {
- throw new MismatchException(
- "publications from postprocessing step are supposed to have owner scenario");
- }
-
- if (!studyMap.containsKey(study)) {
- studyMap.put(study, new ArrayList<ProjectElement>());
- }
-
- if (!studyMap.get(study).contains(scenario)) {
- studyMap.get(study).add(scenario);
- }
-
- if (!scenarioMap.containsKey(scenario)) {
- scenarioMap.put(scenario, new ArrayList<Publication>());
- }
- scenarioMap.get(scenario).add(publication);
- }
-
- // Create the result DTOs
- List<StudyFacadeDTO> result = new ArrayList<StudyFacadeDTO>();
- for (Study study : studyMap.keySet()) {
-
- StudyFacadeDTO studyDTO = new StudyFacadeDTO();
- studyDTO.setName(study.getTitle());
- studyDTO.setScenarios(new ArrayList<StudyFacadeDTO.ScenarioDTO>());
- result.add(studyDTO);
-
- for (ProjectElement scenario : studyMap.get(study)) {
-
- StudyFacadeDTO.ScenarioDTO scenarioDTO = new StudyFacadeDTO.ScenarioDTO();
- scenarioDTO.setName(scenario.getTitle());
- scenarioDTO.setDocs(new ArrayList<DocumentDTO>());
- studyDTO.getScenarios().add(scenarioDTO);
-
- for (Publication publication : scenarioMap.get(scenario)) {
-
- DocumentDTO documentDTO = new DocumentDTO();
- documentDTO.setId(publication.getIndex());
- documentDTO.setTitle(publication.value().getTitle());
-
- scenarioDTO.getDocs().add(documentDTO);
- }
- }
- }
- return result;
- }
-
/**
* Get the publicationDAO.
*