From: mka Date: Wed, 6 Mar 2013 14:04:54 +0000 (+0000) Subject: First version of the Compare the Studies results method. X-Git-Tag: Root_Delivery2_2013_04_22~119 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=00c4e2b12552671ffb26230ca84b7ade29bde3e6;p=tools%2Fsiman.git First version of the Compare the Studies results method. --- diff --git a/Workspace/Siman-Common/lib/iText-2.1.5.jar b/Workspace/Siman-Common/lib/iText-2.1.5.jar new file mode 100644 index 0000000..f28be38 Binary files /dev/null and b/Workspace/Siman-Common/lib/iText-2.1.5.jar differ diff --git a/Workspace/Siman-Common/lib/jcommon-1.0.17.jar b/Workspace/Siman-Common/lib/jcommon-1.0.17.jar new file mode 100644 index 0000000..f1bd165 Binary files /dev/null and b/Workspace/Siman-Common/lib/jcommon-1.0.17.jar differ diff --git a/Workspace/Siman-Common/lib/jfreechart-1.0.14.jar b/Workspace/Siman-Common/lib/jfreechart-1.0.14.jar new file mode 100644 index 0000000..0e4d020 Binary files /dev/null and b/Workspace/Siman-Common/lib/jfreechart-1.0.14.jar differ diff --git a/Workspace/Siman-Common/src/conf/log-messages.properties b/Workspace/Siman-Common/src/conf/log-messages.properties index f947e08..5ee7cdb 100644 --- a/Workspace/Siman-Common/src/conf/log-messages.properties +++ b/Workspace/Siman-Common/src/conf/log-messages.properties @@ -19,3 +19,4 @@ DCT-000001=Document type "{0}" already exists DCT-000002=Can not delete the document "{0}" because it is used by other documents. DCT-000003=Can not save a document in {0} state. Check the validation cycle. PRM-000001=Parameter {0} is invalid with value: {1} +IDT-000001=Incompatible data: X-units or Y-units are not the same for all comparable studies. \ No newline at end of file diff --git a/Workspace/Siman-Common/src/conf/log-messages_en.properties b/Workspace/Siman-Common/src/conf/log-messages_en.properties index f1b20c6..5ee7cdb 100644 --- a/Workspace/Siman-Common/src/conf/log-messages_en.properties +++ b/Workspace/Siman-Common/src/conf/log-messages_en.properties @@ -18,4 +18,5 @@ SCT-000001=Simulation context type "{0}" already exists DCT-000001=Document type "{0}" already exists DCT-000002=Can not delete the document "{0}" because it is used by other documents. DCT-000003=Can not save a document in {0} state. Check the validation cycle. -PRM-000001=Parameter {0} is invalid with value: {1} \ No newline at end of file +PRM-000001=Parameter {0} is invalid with value: {1} +IDT-000001=Incompatible data: X-units or Y-units are not the same for all comparable studies. \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java b/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java index eaea30f..1b88747 100644 --- a/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java +++ b/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java @@ -93,10 +93,14 @@ public enum MessageKeyEnum { * DCT-000003=Can not save a document in {0} state. Check the validation cycle. */ DCT_000003("DCT-000003"), - /** + /** * Parameter {0} is invalid with value: {1}. */ - PRM_000001("PRM-000001"); + PRM_000001("PRM-000001"), + /** + * Incompatible data: X-units or Y-units are not the same for all comparable studies. + */ + IDT_000001("IDT-000001"); /** * Value. */ diff --git a/Workspace/Siman-Common/src/org/splat/exception/IncompatibleDataException.java b/Workspace/Siman-Common/src/org/splat/exception/IncompatibleDataException.java new file mode 100644 index 0000000..59a41c5 --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/exception/IncompatibleDataException.java @@ -0,0 +1,33 @@ +/***************************************************************************** + * Company EURIWARE + * Application SIMAN + * File $Id$ + * Creation date 06.03.2013 + * @author Author: Maria KRUCHININA + * @version Revision: + *****************************************************************************/ + +package org.splat.exception; + +/** + * Exception thrown when data is incompatible for "Compare the studies" functionality. + * @author Maria KRUCHININA + * + */ +public class IncompatibleDataException extends BusinessException { + + /** + * Version id for serialization. + */ + private static final long serialVersionUID = -4596111071538834057L; + + /** + * Create a IncompatibleDataException. + * + * @param message + * the error message. + */ + public IncompatibleDataException(final String message) { + super(message); + } +} diff --git a/Workspace/Siman-Common/src/org/splat/service/StudyService.java b/Workspace/Siman-Common/src/org/splat/service/StudyService.java index aea1958..9726ddb 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StudyService.java +++ b/Workspace/Siman-Common/src/org/splat/service/StudyService.java @@ -19,6 +19,7 @@ import org.splat.dal.bo.som.SimulationContext; import org.splat.dal.bo.som.Study; import org.splat.dal.bo.som.Study.Properties; import org.splat.dal.bo.som.ValidationCycle; +import org.splat.exception.IncompatibleDataException; import org.splat.exception.InvalidParameterException; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MissedPropertyException; @@ -308,9 +309,12 @@ public interface StudyService { /** * Compare the studies and generate the file that contains the result chart. * @param docsList the list of dtos each contains information: - * StudyTitle, ScenarioTitle, PathToFile in vault. + * StudyTitle, ScenarioTitle, PathToFile in vault. + * @param userName the name of the user who compare the results. + * @throws IncompatibleDataException + * if data is incompatible for "Compare the studies" functionality. * * @return path to result file in the vault. */ - String compare (List docsList); + String compare (List docsList, final String userName) throws IncompatibleDataException; } diff --git a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java index 7cc8ace..211f2d9 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java @@ -9,6 +9,10 @@ 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; @@ -18,9 +22,16 @@ import java.util.Date; 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; @@ -47,6 +58,7 @@ import org.splat.dal.dao.som.PublicationDAO; 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; @@ -60,6 +72,14 @@ import org.splat.service.technical.RepositoryService; 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. @@ -1129,9 +1149,119 @@ public class StudyServiceImpl implements StudyService { return study.removeAttribute(study.getAttribute(DescriptionAttribute.class)); } + /** + * + * {@inheritDoc} + * @see org.splat.service.StudyService#compare(java.util.List) + */ @Override - public String compare (final List docsList) { - return "Result file path"; + public String compare (final List docsList, final String userName) throws IncompatibleDataException{ + + String axis1Name = ""; + String axis2Name = ""; + String resultPath = ""; + + XYSeriesCollection dataset = new XYSeriesCollection(); + + Iterator 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; } /**