+ /**
+ *
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.StudyService#markStudyAsReference(org.splat.dal.bo.som.Study)
+ */
+ @Transactional
+ public void markStudyAsReference(final Study aStudy) {
+
+ aStudy.setMarkreference(1);
+ aStudy.setProgressState(ProgressState.TEMPLATE);
+ getStudyDAO().merge(aStudy);
+ }
+
+ /**
+ *
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.StudyService#removeStudyAsReference(org.splat.dal.bo.som.Study)
+ */
+ @Transactional
+ public void removeStudyAsReference(final Study aStudy) {
+
+ aStudy.setMarkreference(0);
+ aStudy.setProgressState(ProgressState.APPROVED);
+ getStudyDAO().merge(aStudy);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.StudyService#getDescription(java.lang.Long)
+ */
+ @Override
+ @Transactional(readOnly = true)
+ public String getDescription(final Long studyId)
+ throws InvalidParameterException {
+ if (studyId == null) {
+ throw new InvalidParameterException(PARAM_STUDY_ID, "null");
+ }
+ Study study = _studyDAO.get(studyId);
+ if (study == null) {
+ throw new InvalidParameterException(PARAM_STUDY_ID, studyId
+ .toString());
+ }
+ return study.getDescription();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.StudyService#setDescription(java.lang.Long, java.lang.String)
+ */
+ @Transactional
+ public void setDescription(final Long studyId, final String descriptionText)
+ throws InvalidParameterException {
+ if (studyId == null) {
+ throw new InvalidParameterException(PARAM_STUDY_ID, "null");
+ }
+ Study study = _studyDAO.get(studyId);
+ if (study == null) {
+ throw new InvalidParameterException(PARAM_STUDY_ID, studyId
+ .toString());
+ }
+ study.setAttribute(new DescriptionAttribute(study, descriptionText));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.StudyService#removeStudyDescription(java.lang.Long)
+ */
+ @Transactional
+ public boolean removeDescription(final Long studyId)
+ throws InvalidParameterException {
+ if (studyId == null) {
+ throw new InvalidParameterException(PARAM_STUDY_ID, String
+ .valueOf(studyId));
+ }
+ Study study = _studyDAO.get(studyId);
+ if (study == null) {
+ throw new InvalidParameterException(PARAM_STUDY_ID, String
+ .valueOf(studyId));
+ }
+ 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,
+ 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}
+ * @see org.splat.service.StudyService#getReaders(long)
+ */
+ @Transactional(readOnly = true)
+ public List<UserDTO> getReaders(final long studyId) throws InvalidParameterException {
+ Study aStudy = selectStudy(studyId);
+ if(aStudy == null){
+ throw new InvalidParameterException(PARAM_STUDY_ID, String.valueOf(studyId));
+ }
+ List<Relation> relations = aStudy.getRelations(ReaderRelation.class);
+ List<UserDTO> result = new ArrayList<UserDTO>();
+ for(Relation relation : relations){
+ result.add(BeanHelper.copyBean(relation.getTo(), UserDTO.class));
+ }
+ return Collections.unmodifiableList(result);
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.splat.service.StudyService#addReader(long, long)
+ */
+ @Transactional
+ public boolean addReader(final long studyId, final long userId) throws InvalidParameterException {
+ Study aStudy = selectStudy(studyId);
+ if(aStudy == null){
+ throw new InvalidParameterException(PARAM_STUDY_ID, String.valueOf(studyId));
+ }
+ User user = _userService.selectUser(userId);
+ if(user == null){
+ throw new InvalidParameterException("userId", String.valueOf(userId));
+ }
+
+ for(Relation relation : aStudy.getRelations(ReaderRelation.class)) {
+ if(user.equals(relation.getTo())) {
+ return false;
+ }
+ }
+ aStudy.addRelation(new ReaderRelation(aStudy, user));
+ update(aStudy);
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.splat.service.StudyService#removeReader(long, long)
+ */
+ @Transactional
+ public boolean removeReader(final long studyId, final long userId) throws InvalidParameterException {
+ Study aStudy = selectStudy(studyId);
+ if(aStudy == null){
+ throw new InvalidParameterException(PARAM_STUDY_ID, String.valueOf(studyId));
+ }
+ User user = _userService.selectUser(userId);
+ if(user == null){
+ throw new InvalidParameterException("userId", String.valueOf(userId));
+ }
+
+ Relation relation = aStudy.removeRelation(ReaderRelation.class, user);
+ update(aStudy);
+ return relation != null;
+ }
+