X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=Workspace%2FSiman-Common%2Fsrc%2Forg%2Fsplat%2Fservice%2FStudyServiceImpl.java;h=7fd574be5fa406b1c083190e87cf35f9bcc300c4;hb=1ba46da65ce8b7823a8d01870d3c0db1c7ed85ab;hp=425381eea67525a38a7c4a20efd2a2f89c58b525;hpb=1705f95f3921cc48eaf02ff2c608cedb0767277f;p=tools%2Fsiman.git diff --git a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java index 425381e..7fd574b 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java @@ -9,18 +9,35 @@ 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.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; +import java.util.HashMap; 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.Hibernate; 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; @@ -28,32 +45,51 @@ import org.splat.dal.bo.som.ContributorRelation; import org.splat.dal.bo.som.DescriptionAttribute; import org.splat.dal.bo.som.DocumentType; 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.Scenario; 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.dal.bo.som.ValidationCycle.Actor; import org.splat.dal.bo.som.ValidationCycleRelation; import org.splat.dal.bo.som.ValidationStep; import org.splat.dal.bo.som.Visibility; +import org.splat.dal.bo.som.Study.Properties; +import org.splat.dal.bo.som.ValidationCycle.Actor; +import org.splat.dal.dao.som.DescriptionAttributeDAO; +import org.splat.dal.dao.som.DocumentDAO; import org.splat.dal.dao.som.IDBuilderDAO; +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.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.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.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. * @@ -118,11 +154,32 @@ public class StudyServiceImpl implements StudyService { */ private UserService _userService; + /** + * Injected publication DAO. + */ + private PublicationDAO _publicationDAO; + + /** + * Injected repository service. + */ + private RepositoryService _repositoryService; + + /** + * Injected document DAO. + */ + private DocumentDAO _documentDAO; + + /** + * Injected description attribute DAO. + */ + private DescriptionAttributeDAO _descriptionAttributeDAO; + /** * {@inheritDoc} * * @see org.splat.service.StudyService#selectStudy(long) */ + @Override @Transactional public Study selectStudy(final long index) { Study result = getStudyDAO().get(index); @@ -130,6 +187,18 @@ public class StudyServiceImpl implements StudyService { return result; } + /** + * {@inheritDoc} + * @see org.splat.service.StudyService#removeStudy(long) + */ + @Transactional + public void removeStudy(final long index) { + Study study = getStudyDAO().get(index); + if (study != null) { + getStudyDAO().delete(study); + } + } + /** * Get study by its reference. * @@ -150,6 +219,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#createStudy(org.splat.dal.bo.som.Study.Properties) */ + @Override @Transactional public Study createStudy(final Study.Properties sprop) throws MissedPropertyException, InvalidPropertyException, @@ -159,14 +229,14 @@ public class StudyServiceImpl implements StudyService { buildReference(study); getStudyDAO().create(study); - try { - IndexService lucin = getIndex(); - lucin.add(study); - } catch (IOException error) { - LOG.error("Unable to index the study '" + study.getIndex() - + "', reason:", error); - // Continue and try to index later - } +// try { +// IndexService lucin = getIndex(); +// lucin.add(study); +// } catch (IOException error) { +// LOG.error("Unable to index the study '" + study.getIndex() +// + "', reason:", error); +// // Continue and try to index later +// } return study; } @@ -175,6 +245,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext.Properties) */ + @Override @Transactional public SimulationContext addProjectContext(final Study aStudy, final SimulationContext.Properties cprop) @@ -191,6 +262,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext) */ + @Override @Transactional public SimulationContext addProjectContext(final Study aStudy, final SimulationContext context) { @@ -205,6 +277,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#addContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User) */ + @Override public boolean addContributor(final Study aStudy, final User user) { List contributor = getModifiableContributors(aStudy); // Initializes contributor for (Iterator i = contributor.iterator(); i.hasNext();) { @@ -234,6 +307,7 @@ public class StudyServiceImpl implements StudyService { * @see #isPublic() * @see Publication#approve(Date) */ + @Override public boolean moveToReference(final Study aStudy) { if (aStudy.getProgressState() != ProgressState.APPROVED) { return false; @@ -254,6 +328,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#update(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Study.Properties) */ + @Override public boolean update(final Study aStudy, final Properties sprop) throws InvalidPropertyException { if (sprop.getTitle() != null) { @@ -293,6 +368,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#removeContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User[]) */ + @Override public boolean removeContributor(final Study aStudy, final User... users) { List contributor = getModifiableContributors(aStudy); // Initializes contributor Boolean done = false; @@ -321,6 +397,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#removeProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext) */ + @Override public boolean removeProjectContext(final Study aStudy, final SimulationContext context) { boolean done = getStepService().removeSimulationContext( @@ -335,6 +412,7 @@ public class StudyServiceImpl implements StudyService { * @see org.splat.service.StudyService#setValidationCycle(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.DocumentType, * org.splat.dal.bo.som.ValidationCycle.Properties) */ + @Override @Transactional public void setValidationCycle(final Study aStudyDTO, final DocumentType type, final ValidationCycle.Properties vprop) { @@ -379,6 +457,7 @@ public class StudyServiceImpl implements StudyService { * a study to demote * @return true if the demotion succeeded. */ + @Override public boolean demote(final Study aStudy) { if (aStudy.getProgressState() == ProgressState.inCHECK) { aStudy.setProgressState(ProgressState.inDRAFT); @@ -395,6 +474,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#generateLocalIndex(org.splat.dal.bo.som.Study) */ + @Override @Transactional public int generateLocalIndex(final Study aStudy) { aStudy.setLastLocalIndex(aStudy.getLastLocalIndex() + 1); @@ -409,6 +489,8 @@ public class StudyServiceImpl implements StudyService { * a study to promote * @return true if the demotion succeeded. */ + @Override + @Transactional public boolean promote(final Study aStudy) { if (aStudy.getProgressState() == ProgressState.inWORK) { aStudy.setProgressState(ProgressState.inDRAFT); @@ -436,6 +518,7 @@ public class StudyServiceImpl implements StudyService { * @return true if the move succeeded. * @see #isPublic() */ + @Override @Transactional public boolean moveToPublic(final Study aStudy) { boolean isOk = false; @@ -455,6 +538,7 @@ public class StudyServiceImpl implements StudyService { * a study to move * @return true if the move succeeded. */ + @Override @Transactional public boolean moveToPrivate(final Study aStudy) { boolean isOk = false; @@ -480,7 +564,7 @@ public class StudyServiceImpl implements StudyService { try { getStudyDAO().merge(aStudy); // Update of relational base setShortCuts(aStudy); // RKV: initialize transient actors set - getIndex().update(aStudy); // Update of Lucene index + //RKV: getIndex().update(aStudy); // Update of Lucene index isOk = true; } catch (Exception e) { LOG.error("STD-000001", e, aStudy.getIndex(), e.getMessage()); @@ -628,25 +712,26 @@ public class StudyServiceImpl implements StudyService { * @return true if reindexing succeeded */ private boolean updateKnowledgeElementsIndex(final Study aStudy) { - boolean isOk = false; - try { - IndexService lucin = getIndex(); - - for (Iterator i = aStudy.getScenariiList().iterator(); i - .hasNext();) { - Scenario scene = i.next(); - for (Iterator j = scene - .getAllKnowledgeElements().iterator(); j.hasNext();) { - KnowledgeElement kelm = j.next(); - lucin.update(kelm); - } - } - isOk = true; - } catch (Exception error) { - LOG.error("Unable to re-index Knowledge Elements, reason:", - error); - } - return isOk; +// boolean isOk = false; +// try { +// IndexService lucin = getIndex(); +// +// for (Iterator i = aStudy.getScenariiList().iterator(); i +// .hasNext();) { +// Scenario scene = i.next(); +// for (Iterator j = scene +// .getAllKnowledgeElements().iterator(); j.hasNext();) { +// KnowledgeElement kelm = j.next(); +// lucin.update(kelm); +// } +// } +// isOk = true; +// } catch (Exception error) { +// LOG.error("Unable to re-index Knowledge Elements, reason:", +// error); +// } +// return isOk; + return true; } /** @@ -658,6 +743,11 @@ public class StudyServiceImpl implements StudyService { */ private IndexService getIndex() throws IOException { IndexService lucin = getIndexService(); + if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService() + .getRepositoryIndexDirectory()))) { + IndexWriter.unlock(FSDirectory.open(getRepositoryService() + .getRepositoryIndexDirectory())); + } if (!lucin.exists()) { lucin.create(); // Happens when re-indexing all studies } @@ -830,6 +920,7 @@ public class StudyServiceImpl implements StudyService { * the study * @return the unmodifiable not null transient list of contributors of this study */ + @Override public List getContributors(final Study aStudy) { if (aStudy.getContributor() == null) { setShortCuts(aStudy); @@ -860,6 +951,7 @@ public class StudyServiceImpl implements StudyService { * the document type being subject of validation * @return the validation cycle of the document, or null if not defined. */ + @Override public ValidationCycle getValidationCycleOf(final Study aStudy, final DocumentType type) { if (aStudy.getValidationCycles() == null || aStudy.getValidationCycles().isEmpty()) { @@ -888,6 +980,7 @@ public class StudyServiceImpl implements StudyService { * @return true if the given user is actor of this study. * @see #getActors() */ + @Override public boolean hasActor(final Study aStudy, final User user) { if (user == null) { return false; @@ -911,6 +1004,7 @@ public class StudyServiceImpl implements StudyService { * @return true if the given user is actor of this study. * @see #getContributors() */ + @Override public boolean isStaffedBy(final Study aStudy, final User user) { if (user == null) { return false; @@ -938,6 +1032,7 @@ public class StudyServiceImpl implements StudyService { * @param aStudy * the study */ + @Override public void loadWorkflow(final Study aStudy) { setShortCuts(aStudy); } @@ -1026,7 +1121,181 @@ public class StudyServiceImpl implements StudyService { 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("studyId", "null"); + } + Study study = _studyDAO.get(studyId); + if(study == null) { + throw new InvalidParameterException("studyId", studyId.toString()); + } + return study.getDescription(); + } + + + /** + * {@inheritDoc} + * @see org.splat.service.StudyService#setDescription(java.lang.Long, java.lang.String) + */ + @Override + @Transactional + public void setDescription(final Long studyId, final String descriptionText) + throws InvalidParameterException { + if(studyId == null) { + throw new InvalidParameterException("studyId", "null"); + } + Study study = _studyDAO.get(studyId); + if(study == null) { + throw new InvalidParameterException("studyId", studyId.toString()); + } + study.setAttribute(new DescriptionAttribute(study, descriptionText)); + } + + /** + * {@inheritDoc} + * @see org.splat.service.StudyService#removeStudyDescription(java.lang.Long) + */ + @Override + @Transactional + public boolean removeDescription(final Long studyId) throws InvalidParameterException { + if(studyId == null) { + throw new InvalidParameterException("studyId", String.valueOf(studyId)); + } + Study study = _studyDAO.get(studyId); + if(study == null) { + throw new InvalidParameterException("studyId", 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 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 (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)) { + 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; + } + /** * Get project settings. * @@ -1220,4 +1489,167 @@ public class StudyServiceImpl implements StudyService { public void setUserService(final UserService userService) { _userService = userService; } + + /** + * {@inheritDoc} + * + * @see org.splat.service.StudyService#getComparableStudies() + */ + @Transactional(readOnly = true) + public List getComparableStudies(final long userId) throws MismatchException { + //retrieve the number of the "Analyze the results" step + List 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 publications = _publicationDAO.getFilteredList("mydoc", + Restrictions.eq("step", Integer.valueOf(theAnalyzeStep.getNumber()))); + + //split retrieved publications into groups by studies and scenarios + Map> studyMap = new HashMap>(); + Map> scenarioMap = + new HashMap>(); + + 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()); + } + + if(!studyMap.get(study).contains(scenario)) { + studyMap.get(study).add(scenario); + } + + if(!scenarioMap.containsKey(scenario)) { + scenarioMap.put(scenario, new ArrayList()); + } + scenarioMap.get(scenario).add(publication); + } + + //Create the result DTOs + List result = new ArrayList(); + for(Study study : studyMap.keySet()) { + + StudyFacadeDTO studyDTO = new StudyFacadeDTO(); + studyDTO.setName(study.getTitle()); + studyDTO.setScenarios(new ArrayList()); + result.add(studyDTO); + + for(ProjectElement scenario : studyMap.get(study)) { + + StudyFacadeDTO.ScenarioDTO scenarioDTO = new StudyFacadeDTO.ScenarioDTO(); + scenarioDTO.setName(scenario.getTitle()); + scenarioDTO.setDocs(new ArrayList()); + 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. + * @return the publicationDAO + */ + public PublicationDAO getPublicationDAO() { + return _publicationDAO; + } + + /** + * Set the publicationDAO. + * @param publicationDAO the publicationDAO to set + */ + public void setPublicationDAO(final PublicationDAO publicationDAO) { + _publicationDAO = publicationDAO; + } + + /** + * Get the repositoryService. + * @return the repositoryService + */ + public RepositoryService getRepositoryService() { + return _repositoryService; + } + + /** + * Set the repositoryService. + * @param repositoryService the repositoryService to set + */ + public void setRepositoryService(final RepositoryService repositoryService) { + _repositoryService = repositoryService; + } + + /** + * Get the documentDAO. + * @return the documentDAO + */ + public DocumentDAO getDocumentDAO() { + return _documentDAO; + } + + /** + * Set the documentDAO. + * @param documentDAO the documentDAO to set + */ + public void setDocumentDAO(final DocumentDAO documentDAO) { + _documentDAO = documentDAO; + } + + /** + * Get the descriptionAttributeDAO. + * @return the descriptionAttributeDAO + */ + public DescriptionAttributeDAO getDescriptionAttributeDAO() { + return _descriptionAttributeDAO; + } + + /** + * Set the descriptionAttributeDAO. + * @param descriptionAttributeDAO the descriptionAttributeDAO to set + */ + public void setDescriptionAttributeDAO( + final DescriptionAttributeDAO descriptionAttributeDAO) { + _descriptionAttributeDAO = descriptionAttributeDAO; + } }