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.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Scanner;
import java.util.Set;
import org.hibernate.criterion.Restrictions;
+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.dao.som.ScenarioDAO;
import org.splat.dal.dao.som.StudyDAO;
import org.splat.dal.dao.som.ValidationCycleDAO;
+import org.splat.exception.IncompatibleDataException;
import org.splat.exception.InvalidParameterException;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MissedPropertyException;
import org.splat.som.Revision;
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.
return study.removeAttribute(study.getAttribute(DescriptionAttribute.class));
}
+ /**
+ *
+ * {@inheritDoc}
+ * @see org.splat.service.StudyService#compare(java.util.List)
+ */
@Override
- public String compare (final List<DocToCompareDTO> docsList) {
- return "Result file path";
+ public String compare (final List<DocToCompareDTO> docsList, final String userName) throws IncompatibleDataException{
+
+ String axis1Name = "";
+ String axis2Name = "";
+ 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());
+
+ //read the file and get points information.
+ try {
+ Scanner input = new Scanner(compDocFile);
+
+ //get the name of the axis.
+ if (input.hasNext()) {
+ String[] tokens = input.nextLine().split(",");
+ 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)) {
+ continue;
+ }
+ else {
+ String[] tokens = currentString.split(" ");
+ series.add(Double.valueOf(tokens[0]), Double.valueOf(tokens[1]));
+ }
+
+ } //while
+
+ dataset.addSeries(series);
+
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return "ERROR";
+ }
+ } //for
+
+ JFreeChart chart = ChartFactory.createXYLineChart(
+ "Comparision of Studies Results", // 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) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (DocumentException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return resultPath;
}
/**