From b29ca93776971f22be340f158c53b9e8cdce997b Mon Sep 17 00:00:00 2001 From: rkv Date: Wed, 14 Nov 2012 12:37:31 +0000 Subject: [PATCH] ScenarioService.getScenarioInfo method is added. --- .../org/splat/service/ScenarioService.java | 15 +- .../splat/service/ScenarioServiceImpl.java | 34 +++ .../org/splat/service/dto/DocumentDTO.java | 107 ++++++++ .../src/org/splat/service/dto/FileDTO.java | 123 +++++++++ .../src/org/splat/service/dto/StepDTO.java | 118 +++++++++ .../technical/ProjectSettingsService.java | 20 ++ .../technical/ProjectSettingsServiceImpl.java | 37 +-- .../splat/service/TestScenarioService.java | 244 ++++++++++++++++++ 8 files changed, 678 insertions(+), 20 deletions(-) create mode 100644 Workspace/Siman-Common/src/org/splat/service/dto/DocumentDTO.java create mode 100644 Workspace/Siman-Common/src/org/splat/service/dto/FileDTO.java create mode 100644 Workspace/Siman-Common/src/org/splat/service/dto/StepDTO.java create mode 100644 Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java index 84bf9c7..e93d1c0 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java @@ -9,6 +9,8 @@ package org.splat.service; +import java.util.List; + import org.splat.dal.bo.kernel.User; import org.splat.dal.bo.som.KnowledgeElement; import org.splat.dal.bo.som.Scenario; @@ -17,6 +19,7 @@ import org.splat.dal.bo.som.Study; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MissedPropertyException; import org.splat.kernel.MultiplyDefinedException; +import org.splat.service.dto.StepDTO; import org.splat.som.Step; /** @@ -26,6 +29,15 @@ import org.splat.som.Step; */ public interface ScenarioService { + /** + * Get lists of scenario steps, documents and files for building siman-salome.conf file. + * + * @param scenarioId + * scenario id + * @return list of step DTOs + */ + List getScenarioInfo(long scenarioId); + /** * Create a new study with one scenario and "product" simulation context. * @@ -132,6 +144,5 @@ public interface ScenarioService { * the knowledge element to remove * @return true if removal succeeded */ - boolean removeKnowledgeElement(Scenario scenario, - KnowledgeElement kelm); + boolean removeKnowledgeElement(Scenario scenario, KnowledgeElement kelm); } diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java index 79c424d..d8f560d 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java @@ -10,12 +10,16 @@ package org.splat.service; import java.io.IOException; +import java.util.ArrayList; import java.util.Calendar; import java.util.Iterator; import java.util.List; import org.apache.log4j.Logger; +import org.splat.dal.bo.kernel.Relation; import org.splat.dal.bo.kernel.User; +import org.splat.dal.bo.som.ConvertsRelation; +import org.splat.dal.bo.som.File; import org.splat.dal.bo.som.KnowledgeElement; import org.splat.dal.bo.som.KnowledgeElementType; import org.splat.dal.bo.som.Publication; @@ -30,8 +34,11 @@ import org.splat.dal.dao.som.StudyDAO; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MissedPropertyException; import org.splat.kernel.MultiplyDefinedException; +import org.splat.service.dto.DocumentDTO; +import org.splat.service.dto.StepDTO; import org.splat.service.technical.IndexService; import org.splat.som.Step; +import org.splat.util.BeanHelper; import org.springframework.transaction.annotation.Transactional; /** @@ -164,6 +171,33 @@ public class ScenarioServiceImpl implements ScenarioService { _stepService = stepService; } + /** + * {@inheritDoc} + * @see org.splat.service.ScenarioService#getScenarioInfo(long) + */ + @Transactional + public List getScenarioInfo(final long scenarioId) { + List res = new ArrayList(); + Scenario scen = getScenarioDAO().get(scenarioId); + Step[] steps = getProjectElementService().getSteps(scen); + StepDTO stepDTO; + DocumentDTO docDTO; + for (Step step: steps) { + stepDTO = BeanHelper.copyBean(step.getStep(), StepDTO.class); + res.add(stepDTO); + for (Publication tag: step.getDocuments()) { + docDTO = stepDTO.addDoc(tag.value().getIndex(), tag.value().getTitle()); + char aState = tag.getIsnew(); + docDTO.addFile(tag.value().getFile().getRelativePath(), aState); + for(Relation rel: tag.value().getRelations(ConvertsRelation.class)) { + File aFile = ((ConvertsRelation)rel).getTo(); + docDTO.addFile(aFile.getRelativePath(), aState); + } + } + } + return res; + } + /** * Create a new study with one scenario and "product" simulation context. * diff --git a/Workspace/Siman-Common/src/org/splat/service/dto/DocumentDTO.java b/Workspace/Siman-Common/src/org/splat/service/dto/DocumentDTO.java new file mode 100644 index 0000000..6838cd5 --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/service/dto/DocumentDTO.java @@ -0,0 +1,107 @@ +/***************************************************************************** + * Company OPEN CASCADE + * Application SIMAN + * File $Id$ + * Creation date 14.11.2012 + * @author $Author$ + * @version $Revision$ + * @copyright OPEN CASCADE 2012 + *****************************************************************************/ + +package org.splat.service.dto; + +import java.util.ArrayList; +import java.util.List; + +/** + * Document DTO. This is a container of document files. + */ +public class DocumentDTO { + /** + * Document persistent id. + */ + private Long _id; + /** + * Document title. + */ + private String _title; + /** + * List of document files. + */ + private final List _files = new ArrayList(); // RKV: NOPMD: Access to the collection via getter + + /** + * Constructor with initialization. + * + * @param index + * the document persistent id + * @param title + * the document title + */ + public DocumentDTO(final long index, final String title) { + _id = index; + _title = title; + } + + /** + * Get the files. + * + * @return the files + */ + public List getFiles() { + return _files; + } + + /** + * Get the id. + * + * @return the id + */ + public Long getId() { + return _id; + } + + /** + * Set the id. + * + * @param id + * the id to set + */ + public void setId(final Long id) { + _id = id; + } + + /** + * Get the title. + * + * @return the title + */ + public String getTitle() { + return _title; + } + + /** + * Set the title. + * + * @param title + * the title to set + */ + public void setTitle(final String title) { + _title = title; + } + + /** + * Add a new file DTO. + * + * @param relativePath + * relative file path + * @param state + * file state + * @return the added file DTO + */ + public FileDTO addFile(final String relativePath, final char state) { + FileDTO fileDTO = new FileDTO(relativePath, state); + _files.add(fileDTO); + return fileDTO; + } +} diff --git a/Workspace/Siman-Common/src/org/splat/service/dto/FileDTO.java b/Workspace/Siman-Common/src/org/splat/service/dto/FileDTO.java new file mode 100644 index 0000000..4c0cd8c --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/service/dto/FileDTO.java @@ -0,0 +1,123 @@ +/***************************************************************************** + * Company OPEN CASCADE + * Application SIMAN + * File $Id$ + * Creation date 14.11.2012 + * @author $Author$ + * @version $Revision$ + * @copyright OPEN CASCADE 2012 + *****************************************************************************/ + +package org.splat.service.dto; + +/** + * Document's file DTO. + */ +public class FileDTO { + + /** + * True if the file is a result file for a document. + */ + private boolean _isResult; // RKV: NOPMD: isResult method is used as getter + /** + * File state: "Y" - actual, "O" - outdated, "N" - taken from other study. + */ + private char _state; + /** + * Automatic processing instruction: "file-import", "file-download", etc. + */ + private String _processing; + /** + * File path from the repository root. + */ + private String _path; + + /** + * Constructor with initialization. + * + * @param relativePath + * relative file path + * @param state + * file state + */ + public FileDTO(final String relativePath, final char state) { + _path = relativePath; + _state = state; + } + + /** + * Get the isResult. + * + * @return the isResult + */ + public boolean isResult() { + return _isResult; + } + + /** + * Set the isResult. + * + * @param isResult + * the isResult to set + */ + public void setResult(final boolean isResult) { + _isResult = isResult; + } + + /** + * Get the state. + * + * @return the state + */ + public char getState() { + return _state; + } + + /** + * Set the state. + * + * @param state + * the state to set + */ + public void setState(final char state) { + _state = state; + } + + /** + * Get the processing. + * + * @return the processing + */ + public String getProcessing() { + return _processing; + } + + /** + * Set the processing. + * + * @param processing + * the processing to set + */ + public void setProcessing(final String processing) { + _processing = processing; + } + + /** + * Get the path. + * + * @return the path + */ + public String getPath() { + return _path; + } + + /** + * Set the path. + * + * @param path + * the path to set + */ + public void setPath(final String path) { + _path = path; + } +} diff --git a/Workspace/Siman-Common/src/org/splat/service/dto/StepDTO.java b/Workspace/Siman-Common/src/org/splat/service/dto/StepDTO.java new file mode 100644 index 0000000..93b89e2 --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/service/dto/StepDTO.java @@ -0,0 +1,118 @@ +/***************************************************************************** + * Company OPEN CASCADE + * Application SIMAN + * File $Id$ + * Creation date 14.11.2012 + * @author $Author$ + * @version $Revision$ + * @copyright OPEN CASCADE 2012 + *****************************************************************************/ + +package org.splat.service.dto; + +import java.util.ArrayList; +import java.util.List; + +/** + * Study activity (step) DTO. This is a container of step documents. + */ +public class StepDTO { + /** + * The step's key name. + */ + private String _key; + /** + * The sequential number of the step. + */ + private int _number; + /** + * Executable module for this step. + */ + private String _module; + /** + * Documents of the step. + */ + private List _docs = new ArrayList(); + + /** + * Get the docs. + * + * @return the docs + */ + public List getDocs() { + return _docs; + } + + /** + * Set the docs. + * + * @param docs + * the docs to set + */ + public void setDocs(final List docs) { + _docs = docs; + } + + /** + * Add a document DTO to the step DTO. + * + * @param index + * the document persistent id + * @param title + * the document title + * @return the added document DTO + */ + public DocumentDTO addDoc(final long index, final String title) { + DocumentDTO doc = new DocumentDTO(index, title); + _docs.add(doc); + return doc; + } + + /** + * Get the key. + * @return the key + */ + public String getKey() { + return _key; + } + + /** + * Set the key. + * @param key the key to set + */ + public void setKey(final String key) { + _key = key; + } + + /** + * Get the number. + * @return the number + */ + public int getNumber() { + return _number; + } + + /** + * Set the number. + * @param number the number to set + */ + public void setNumber(final int number) { + _number = number; + } + + /** + * Get the module. + * @return the module + */ + public String getModule() { + return _module; + } + + /** + * Set the module. + * @param module the module to set + */ + public void setModule(final String module) { + _module = module; + } +} diff --git a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java index 524c556..bdb7273 100644 --- a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java +++ b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java @@ -32,6 +32,10 @@ public interface ProjectSettingsService { * The sequential number of the step. */ private int _number; + /** + * The step's key name. + */ + private String _key; /** * The owner of the step: study or scenario. @@ -158,6 +162,22 @@ public interface ProjectSettingsService { public void setModule(final String module) { _module = module; } + + /** + * Get the key. + * @return the key + */ + public String getKey() { + return _key; + } + + /** + * Set the key. + * @param key the key to set + */ + public void setKey(final String key) { + _key = key; + } } /** diff --git a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java index d8da972..cb7c193 100644 --- a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java @@ -434,12 +434,14 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService { final List resultype) { int res = snum; if ("step".equals(node.getNodeName())) { + + String name = ((Element)node).getAttribute("name"); HashMap tags = XDOM.getNamedChildNodes(node); NamedNodeMap natr = tags.get("storage").getAttributes(); ProjectSettingsService.Step step = new ProjectSettingsService.Step( - snum, ownerClass, natr.getNamedItem("path") - .getNodeValue()); + snum, ownerClass, natr.getNamedItem("path").getNodeValue()); + step.setKey(name); // Keeping flow and classification information for eventual later use natr = tags.get("flow").getAttributes(); @@ -459,7 +461,8 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService { if (natr.getNamedItem("contents").getNodeValue() .equals("knowledge")) { if (Study.class.equals(ownerClass)) { - LOG.error("Error: knowledges must be attached to scenarios."); + LOG + .error("Error: knowledges must be attached to scenarios."); } else { // TODO In a given scenario, only one step must contain knowledges step._contents.add(KnowledgeElement.class); @@ -467,8 +470,8 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService { } else { step._contents.add(Document.class); } - - Element module = (Element)tags.get("module"); + + Element module = (Element) tags.get("module"); if (module != null) { step.setModule(module.getAttribute("name")); } @@ -641,30 +644,28 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService { int snum = 0; for (Iterator i = _sclass.iterator(); i.hasNext(); snum++) { NamedNodeMap clatr = i.next(); - if (clatr == null) { - continue; - } - - String[] clist = clatr.getNamedItem("context").getNodeValue() - .split(","); - for (int j = 0; j < clist.length; j++) { - mapstep.put(clist[j], _steps.get(snum)); + if (clatr != null) { + String[] clist = clatr.getNamedItem("context").getNodeValue() + .split(","); + for (int j = 0; j < clist.length; j++) { + mapstep.put(clist[j], _steps.get(snum)); + } } } try { SimulationContextType tctex = null; for (Iterator i = _context.iterator(); i.hasNext();) { String type = i.next(); - if (!mapstep.containsKey(type)) { + if (mapstep.containsKey(type)) { + tctex = getSimulationContextTypeService().createType(type, + mapstep.get(type)); // Creation of Simulation Context Types + getSimulationContextTypeService().approve(tctex); + } else { LOG .warn("Could not find \"" + type + "\" classification. Simulation Context type ignored."); - continue; } - tctex = getSimulationContextTypeService().createType(type, - mapstep.get(type)); // Creation of Simulation Context Types - getSimulationContextTypeService().approve(tctex); } } catch (Exception error) { LOG.warn("Error creating context types, reason:", error); // Should not happen diff --git a/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java new file mode 100644 index 0000000..3dee9cc --- /dev/null +++ b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java @@ -0,0 +1,244 @@ +/***************************************************************************** + * Company OPEN CASCADE + * Application SIMAN + * File $Id$ + * Creation date 12 Oct 2012 + * @author $Author$ + * @version $Revision$ + *****************************************************************************/ +package test.splat.service; + +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import org.splat.dal.bo.kernel.User; +import org.splat.dal.bo.som.Document; +import org.splat.dal.bo.som.DocumentType; +import org.splat.dal.bo.som.KnowledgeElement; +import org.splat.dal.bo.som.Publication; +import org.splat.dal.bo.som.Scenario; +import org.splat.dal.bo.som.Study; +import org.splat.kernel.InvalidPropertyException; +import org.splat.kernel.MissedPropertyException; +import org.splat.kernel.MultiplyDefinedException; +import org.splat.log.AppLogger; +import org.splat.service.DocumentService; +import org.splat.service.DocumentTypeService; +import org.splat.service.ProjectElementService; +import org.splat.service.PublicationService; +import org.splat.service.ScenarioService; +import org.splat.service.StepService; +import org.splat.service.dto.StepDTO; +import org.splat.som.Step; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.orm.hibernate3.HibernateTemplate; +import org.testng.Assert; +import org.testng.annotations.Test; + +import test.splat.common.BaseTest; + +/** + * Test class for KnowledgeElementDAO. + * + * @author Roman Kozlov (RKV) + * + */ +public class TestScenarioService extends BaseTest { + + /** + * Logger for the class. + */ + private static final AppLogger LOG = AppLogger + .getLogger(TestScenarioService.class); + + /** + * The tested ScenarioService. Later injected by Spring. + */ + @Autowired + @Qualifier("scenarioService") + private transient ScenarioService _scenarioService; + + /** + * The DocumentService. Later injected by Spring. + */ + @Autowired + @Qualifier("documentService") + private transient DocumentService _documentService; + + /** + * The PublicationService. Later injected by Spring. + */ + @Autowired + @Qualifier("publicationService") + private transient PublicationService _publicationService; + + /** + * The StepService. Later injected by Spring. + */ + @Autowired + @Qualifier("stepService") + private transient StepService _stepService; + + /** + * The ProjectElementService. Later injected by Spring. + */ + @Autowired + @Qualifier("projectElementService") + private transient ProjectElementService _projectElementService; + + /** + * The DocumentTypeService. Later injected by Spring. + */ + @Autowired + @Qualifier("documentTypeService") + private transient DocumentTypeService _documentTypeService; + + /** + * Test of getting a scenario content for building siman-salome.conf.
+ * Description :
+ * Create a scenario try to get an info for it.
+ * Action :
+ * 1. call the method for an existing scenario id.
+ * 2. call the method for a not existing scenario id.
+ * Test data :
+ * no input parameters
+ * no input parameters
+ * + * Outcome results:
+ * + *
    + *
  • result DTO must contain list of all documents and files
    + *
  • + *
  • Exception is thrown
    + *
  • + *
+ *
+ * + * @throws InvalidPropertyException + * if an invalid property is used when creating objects + * @throws MultiplyDefinedException + * when trying to create an object with already existing id + * @throws MissedPropertyException + * if a mandatory property is not defined for an object to be created + * @throws IOException + * if scenario creation is failed + * + */ + @Test + public void testGetScenarioInfo() throws InvalidPropertyException, + MissedPropertyException, MultiplyDefinedException, IOException { + LOG.debug(">>>>> BEGIN testGetScenarioInfo()"); + long scenarioId = createScenario(); + // Call DAO's create method for a good transient knowledge element. + List steps = _scenarioService.getScenarioInfo(scenarioId); + Assert.assertNotNull(steps, "List of steps must not be null."); + Assert.assertTrue(steps.size() > 0, "No steps are read."); + + // Call DAO's get method for a not existing id. + try { + steps = _scenarioService.getScenarioInfo(-1L); + // getHibernateTemplate().flush(); + Assert + .fail("Getting an object with not existing id must be failed."); + } catch (Exception e) { + LOG.debug("Expected exception is thrown: " + + e.getClass().getSimpleName() + ": " + e.getMessage()); + } + LOG.debug(">>>>> END testGetScenarioInfo()"); + } + + /** + * Create a transient KnowledgeElement for tests. + * + * @return a transient KnowledgeElement + * @throws InvalidPropertyException + * if an invalid property is used when creating objects + * @throws MultiplyDefinedException + * when trying to create an object with already existing id + * @throws MissedPropertyException + * if a mandatory property is not defined for an object to be created + * @throws IOException + * if document creation is failed + */ + private long createScenario() throws InvalidPropertyException, + MissedPropertyException, MultiplyDefinedException, IOException { + // Create a scenario for tests + HibernateTemplate ht = getHibernateTemplate(); + + // Create a test user + User.Properties uprop = new User.Properties(); + uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser") + .setFirstName("TST_FirstName").setDisplayName("TST_test.user") + .addRole("TST_user").setMailAddress( + "noreply@salome-platform.org"); + uprop.disableCheck(); + User anAuthor = new User(uprop); + ht.update(anAuthor); + + // Create a test study + Study.Properties stprops = new Study.Properties().setReference( + "TST_SID_01").setTitle("TST_Study").setManager(anAuthor); + Study aStudy = new Study(stprops); + ht.saveOrUpdate(aStudy); + + // Create a test scenario + Scenario.Properties sprops = new Scenario.Properties().setTitle( + "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy); + Scenario aScenario = new Scenario(sprops); + aStudy.getScenariiList().add(aScenario); + ht.saveOrUpdate(anAuthor); + ht.update(aStudy); + ht.saveOrUpdate(aScenario); + + Document.Properties dprop = new Document.Properties().setAuthor( + anAuthor).setDate(new Date()); + Step[] steps = _projectElementService.getSteps(aScenario); + Assert.assertTrue(steps.length > 0, "No steps are created."); + int i = 0; + for (Step step : steps) { + List dtypes = _documentTypeService.selectTypesOf(step + .getStep()); + dprop.setName("document" + i++).setType(dtypes.get(0)); + // Create a document published in the scenario + Publication pub = _stepService.createDocument(step, dprop); + // Attach a file + _publicationService.attach(pub, "brep"); // TODO: test for different formats + } + + return aScenario.getIndex(); + } + + /** + * Check that given objects are equal. + * + * @param anActual + * the object to check + * @param anExpected + * the expected object + */ + private void compareObjects(final KnowledgeElement anActual, + final KnowledgeElement anExpected) { + Assert.assertNotNull(anActual, + "Created object is not found in the database."); + Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(), + "Knowledge Element author is not saved."); + Assert.assertEquals(anActual.getDate(), anExpected.getDate(), + "Knowledge Element date is not saved."); + Assert + .assertEquals(anActual.getOwnerScenario(), anExpected + .getOwnerScenario(), + "Knowledge Element scenario is not saved."); + Assert.assertEquals(anActual.getProgressState(), anExpected + .getProgressState(), "Knowledge Element state is not saved."); + Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(), + "Knowledge Element title is not saved."); + Assert.assertEquals(anActual.getType(), anExpected.getType(), + "Knowledge Element type is not saved."); + Assert.assertEquals(anActual.getValue(), anExpected.getValue(), + "Knowledge Element value is not saved."); + Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(), + "Knowledge Element index is not saved."); + } +} -- 2.39.2