public Set<Publication> getDocums() {
return docums;
}
+
+ /**
+ * Return the study of the project element.
+ *
+ * @return the project element study
+ */
+ public abstract Study getOwnerStudy();
}
\ No newline at end of file
package org.splat.dal.bo.som;
+
/**
* Publication objects are the way to reference document versions from a Project Element.
* As such, a Document version is added (or published) to a Project Element through a Publication object.
import org.splat.dal.bo.kernel.Relation;
import org.splat.som.Step;
-
+/**
+ * Persistent class representing a tag of a document published in a study/scenario activity.
+ */
public class Publication extends Persistent {
-// Persistent fields
- private Document mydoc;
- private ProjectElement owner; // Either Study or Scenario, depending on the step involved by the publication
- private char isnew; // True if this references a document version new for the owner project element
+ // Persistent fields
+ /**
+ * Persistent field. The published document.
+ */
+ private Document mydoc;
+ /**
+ * Persistent field. Either Study or Scenario, depending on the step involved by the publication.
+ */
+ private ProjectElement owner;
+ /**
+ * Persistent field. True if this references a document version new for the owner project element.
+ */
+ private char isnew;
-// Transient fields
- private Step mystep;
+ // Transient fields
+ /**
+ * Transient field. The step where the document is published.
+ */
+ private Step mystep;
-// ==============================================================================================================================
-// Construction
-// ==============================================================================================================================
+ // ==============================================================================================================================
+ // Construction
+ // ==============================================================================================================================
-/**
+ /**
* Get the mystep.
+ *
* @return the mystep
*/
public Step getStep() {
return mystep;
}
+
/**
* Set the mystep.
- * @param aStep the mystep to set
+ *
+ * @param aStep
+ * the mystep to set
*/
public void setStep(final Step aStep) {
this.mystep = aStep;
}
- // Database fetch constructor
- public Publication () {
-// ------------------------
- mystep = null;
- }
-// Internal constructors
- public Publication (final Document doc, final ProjectElement publisher) {
-// --------------------------------------------------------------
- mydoc = doc;
- mystep = null;
- owner = publisher;
- isnew = 'Y';
- }
-// ==============================================================================================================================
-// Member functions
-// ==============================================================================================================================
-
- public Relation addDependency (final Publication to) {
-// ----------------------------------------------
- return this.addDependency(to.value());
- }
-
- public Relation addDependency (final Document to) {
-// -------------------------------------------
- if (to == null) {
- return null;
- } else {
- return mydoc.addRelation( new UsesRelation(mydoc, to) );
- }
- }
-/**
- * Returns either the Study Scenario or the Study itself to which this publication belongs, depending on the Study Step into
- * which the referenced document has been published.<br/>
- * If this publication belongs to a Study, the Project Element returned is the Study returned by getOwnerStudy().
- *
- * @return the Study Scenario or the Study to which this publication belongs to
- * @see #getOwnerStudy()
- */
- public ProjectElement getOwner () {
-// ---------------------------------
- return owner;
- }
+ /**
+ * Database fetch constructor.
+ */
+ public Publication() {
+ super();
+ mystep = null;
+ }
- /**
+ /**
+ * Internal constructors.
+ *
+ * @param doc
+ * the published document
+ * @param publisher
+ * the project element where the document is published
+ */
+ public Publication(final Document doc, final ProjectElement publisher) {
+ super();
+ mydoc = doc;
+ mystep = null;
+ owner = publisher;
+ isnew = 'Y';
+ }
+
+ // ==============================================================================================================================
+ // Member functions
+ // ==============================================================================================================================
+
+ public Relation addDependency(final Publication to) {
+ return this.addDependency(to.value());
+ }
+
+ public Relation addDependency(final Document to) {
+ Relation res = null;
+ if (to != null) {
+ res = mydoc.addRelation(new UsesRelation(mydoc, to));
+ }
+ return res;
+ }
+
+ /**
+ * Returns either the Study Scenario or the Study itself to which this publication belongs, depending on the Study Step into which the
+ * referenced document has been published.<br/> If this publication belongs to a Study, the Project Element returned is the Study
+ * returned by getOwnerStudy().
+ *
+ * @return the Study Scenario or the Study to which this publication belongs to
+ * @see #getOwnerStudy()
+ */
+ public ProjectElement getOwner() {
+ return owner;
+ }
+
+ /**
* Set the owner.
- * @param owner the owner to set
+ *
+ * @param owner
+ * the owner to set
*/
public void setOwner(final ProjectElement owner) {
this.owner = owner;
}
- public Study getOwnerStudy () {
-// -----------------------------
- if (owner instanceof Study) {
- return (Study)owner;
- } else {
- return ((Scenario)owner).getOwnerStudy();
+ /**
+ * Returns the study where the document is published.
+ *
+ * @return the owner study
+ */
+ public Study getOwnerStudy() {
+ return owner.getOwnerStudy();
}
- }
-/**
- * Returns the state of this published document.
- * It is the same than the state of the referenced document, unless this publication is out-of-date, in which case it is
- * In-Work state.
- *
- * @see #outdate()
- * @see #isOutdated()
- */
- public ProgressState getProgressState () {
-// ----------------------------------------
- if (this.isOutdated()) {
- return ProgressState.inWORK; // Overrides the document state
- } else {
- return mydoc.getProgressState();
- }
- }
-
- public List<Publication> getRelations (final Class<? extends Relation> type) {
-// ----------------------------------------------------------------------
- if (type == null) {
- return null;
- }
-
- List<Publication> result = new ArrayList<Publication>();
- List<Relation> relist = mydoc.getRelations(type);
- for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
- Relation relation = i.next();
- Document relatedoc = (Document)relation.getTo();
- Publication related = owner.getPublication(relatedoc);
- if (related != null) {
- result.add(related);
- } else if (owner instanceof Scenario) { // The relation may cross steps belonging to a scenario and its owner study
- related = ((Scenario)owner).getOwnerStudy().getPublication(relatedoc);
- if (related != null) {
- result.add(related);
- }
- }
- }
- return result;
- }
-
- public File getSourceFile () {
-// ----------------------------
- return mydoc.getSourceFile();
- }
-
- public boolean isNewForOwner () {
-// -------------------------------
- return (isnew == 'Y');
- }
-
- public boolean isOutdated () {
-// ----------------------------
- return (isnew == 'O');
- }
-
- /**
+ /**
+ * Returns the state of this published document. It is the same than the state of the referenced document, unless this publication is
+ * out-of-date, in which case it is In-Work state.
+ *
+ * @see #outdate()
+ * @see #isOutdated()
+ * @return the document progress state
+ */
+ public ProgressState getProgressState() {
+ ProgressState res;
+ if (this.isOutdated()) {
+ res = ProgressState.inWORK; // Overrides the document state
+ } else {
+ res = mydoc.getProgressState();
+ }
+ return res;
+ }
+
+ public List<Publication> getRelations(final Class<? extends Relation> type) {
+ if (type == null) {
+ return null;
+ }
+
+ List<Publication> result = new ArrayList<Publication>();
+ List<Relation> relist = mydoc.getRelations(type);
+ for (Iterator<Relation> i = relist.iterator(); i.hasNext();) {
+ Relation relation = i.next();
+ Document relatedoc = (Document) relation.getTo();
+ Publication related = owner.getPublication(relatedoc);
+ if (related != null) {
+ result.add(related);
+ } else if (owner instanceof Scenario) { // The relation may cross steps belonging to a scenario and its owner study
+ related = owner.getOwnerStudy().getPublication(relatedoc);
+ if (related != null) {
+ result.add(related);
+ }
+ }
+ }
+ return result;
+ }
+
+ public File getSourceFile() {
+ return mydoc.getSourceFile();
+ }
+
+ public boolean isNewForOwner() {
+ return (isnew == 'Y');
+ }
+
+ public boolean isOutdated() {
+ return (isnew == 'O');
+ }
+
+ /**
* Get the isnew.
+ *
* @return the isnew
*/
public char getIsnew() {
return isnew;
}
+
/**
* Set the isnew.
- * @param isnew the isnew to set
+ *
+ * @param isnew
+ * the isnew to set
*/
public void setIsnew(final char isnew) {
this.isnew = isnew;
}
-
-/**
- * Returns the document version referenced by this Publication.
- */
- public Document value () {
-// ------------------------
- return mydoc;
- }
+
+ /**
+ * Returns the document version referenced by this Publication.
+ */
+ public Document value() {
+ return mydoc;
+ }
+
/**
* Set the mydoc.
- * @param mydoc the mydoc to set
+ *
+ * @param mydoc
+ * the mydoc to set
*/
- public void setValue (final Document aDoc) {
+ public void setValue(final Document aDoc) {
this.mydoc = aDoc;
}
}
\ No newline at end of file
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
-import java.util.HashSet;
import java.util.Vector;
import org.splat.dal.bo.kernel.Persistent;
/**
* The persistent set of scenario knowledge elements.
*/
- private Set<KnowledgeElement> kelms = new HashSet<KnowledgeElement>();
+ private final Set<KnowledgeElement> kelms = new HashSet<KnowledgeElement>();
// Transient fields
/**
* The transient map of knowledge elements grouped by types.
*/
- transient private HashMap<Long, List<KnowledgeElement>> known;
+ transient private final HashMap<Long, List<KnowledgeElement>> known;
/**
* The scenario transient list of knowledge elements.
*/
// - Public services
+ @Override
public void clear() {
super.clear();
owner = null;
return manager;
}
- public Properties setOwnerStudy(Study owner) {
+ public Properties setOwnerStudy(final Study owner) {
this.owner = owner;
return this;
}
// - Setters of Scenario properties
- public Properties setBaseStep(Step base)
+ public Properties setBaseStep(final Step base)
throws InvalidPropertyException {
- if (!(base.getOwner() instanceof Scenario))
+ if (!(base.getOwner() instanceof Scenario)) {
throw new InvalidPropertyException("step");
+ }
this.base = base;
return this;
}
- public Properties setDate(Date date) {
+ public Properties setDate(final Date date) {
this.date = date;
return this;
}
- public Properties setDescription(String summary) {
- if (summary.length() > 0)
+ public Properties setDescription(final String summary) {
+ if (summary.length() > 0) {
this.summary = summary;
+ }
return this;
}
- public Properties setInsertAfter(Scenario previous) {
+ public Properties setInsertAfter(final Scenario previous) {
this.previous = previous;
return this;
}
- public Properties setManager(User user) {
+ public Properties setManager(final User user) {
this.manager = user;
return this;
}
- public Properties setTitle(String title)
+ public Properties setTitle(final String title)
throws InvalidPropertyException {
- if (title.length() == 0)
+ if (title.length() == 0) {
throw new InvalidPropertyException("title");
+ }
this.title = title;
return this;
}
public void checkValidity() throws MissedPropertyException,
InvalidPropertyException, MultiplyDefinedException {
- if (owner == null)
+ if (owner == null) {
throw new MissedPropertyException("owner");
- if (title == null)
+ }
+ if (title == null) {
throw new MissedPropertyException("title");
- if (manager == null)
+ }
+ if (manager == null) {
throw new MissedPropertyException("manager");
+ }
}
}
}
// Internal constructor
- public Scenario(Properties sprop) throws MissedPropertyException,
+ public Scenario(final Properties sprop) throws MissedPropertyException,
InvalidPropertyException, MultiplyDefinedException {
super(sprop); // Throws one of the above exception if not valid
owner = sprop.owner;
}
lasdate = credate; // Inherited attribute
- if (sprop.summary != null)
+ if (sprop.summary != null) {
this.setAttribute(new DescriptionAttribute(this, sprop.summary));
+ }
Scenario[] scene = owner.getScenarii();
- for (int i = 0; i < scene.length; i++)
- if (scene[i].sid > this.sid)
+ for (int i = 0; i < scene.length; i++) {
+ if (scene[i].sid > this.sid) {
this.sid = scene[i].sid;
+ }
+ }
sid += 1;
}
knowl = new Vector<KnowledgeElement>(kelms.size());
for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
KnowledgeElement kelm = i.next();
- if (kelm.getType().equals("usecase"))
+ if (kelm.getType().equals("usecase")) {
ucase = kelm;
- else
+ } else {
knowl.add(kelm);
+ }
}
}
return Collections.unmodifiableList(knowl);
}
- public KnowledgeElement getKnowledgeElement(long l) {
+ public KnowledgeElement getKnowledgeElement(final long l) {
for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
KnowledgeElement mykelm = i.next();
if (l == mykelm.getIndex()) {
return null;
}
-/* RKV: Not used
- * public List<KnowledgeElement> getKnowledgeElementsOf(
- KnowledgeElementType type) {
- if (known == null)
- known = new HashMap<Long, List<KnowledgeElement>>();
-
- long numtype = type.getIndex();
- List<KnowledgeElement> listype = known.get(numtype);
- if (listype == null) {
- listype = new Vector<KnowledgeElement>();
- for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
- KnowledgeElement kelm = i.next();
- if (kelm.getType().getIndex() == numtype)
- listype.add(kelm);
- }
- known.put(numtype, listype);
- }
- return listype; // No protection against this object corruption as it would not corrupt the database
- }
-*/
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.dal.bo.som.ProjectElement#getOwnerStudy()
+ */
+ @Override
public Study getOwnerStudy() {
- // -----------------------------
return owner;
}
* Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
*/
public String getReference() {
- // -----------------------------
return String.valueOf(sid);
}
return cuser; // Null if the scenario has not been checked-out
}
- public void setUser(User aUser) {
+ public void setUser(final User aUser) {
cuser = aUser; // Null if the scenario has not been checked-out
}
public boolean isCheckedout() {
- // ------------------------------
return (cuser != null);
}
* @param kelm
* the scenario transient "use case" knowledge element
*/
- public void setUcase(KnowledgeElement kelm) {
+ public void setUcase(final KnowledgeElement kelm) {
ucase = kelm;
}
public KnowledgeElement getUcase() {
return ucase;
}
-
- /**
- * Get the known.
- *
- * @return the known
- */
-/* RKV: Not used
- * public HashMap<Long, List<KnowledgeElement>> getKnowledgeByType() {
- return known;
- }
-*/}
\ No newline at end of file
+}
\ No newline at end of file
/**
* Transient map of document types to validation cycles.
*/
- private transient final Map<String, ValidationCycle> validactor = new HashMap<String, ValidationCycle>();
+ private transient final Map<String, ValidationCycle> validactor = new HashMap<String, ValidationCycle>();
/**
* Transient set of all actors of the study, i.d. contributors and validation cycles participants.
*/
return res;
}
+ /**
+ * Get the last numerical index of study documents.
+ *
+ * @return the last value of the index part of study documents reference
+ */
public int getLastLocalIndex() {
return docount;
}
/**
+ * Set visibility of the study.
+ *
* @param aVisibility
* a study visibility to set
*/
}
/**
+ * Set the progress state for the study.
+ *
* @param aState
* a study progress state to set
*/
}
/**
+ * Set the study version.
+ *
* @param aVersion
+ * the version
*/
public void setVersion(final String aVersion) {
version = aVersion;
public Set<User> getActor() {
return actor;
}
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.dal.bo.som.ProjectElement#getOwnerStudy()
+ */
+ @Override
+ public Study getOwnerStudy() {
+ return this;
+ }
}
\ No newline at end of file
*/
@Transactional
public void generateDocumentId(final Document aDoc, final Properties dprop) {
- Study owner = null;
- if (dprop.getOwner() instanceof Study) {
- owner = (Study) dprop.getOwner();
- } else {
- owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
- }
+ Study owner = dprop.getOwner().getOwnerStudy();
// Synchronize the object with the current Hibernate session.
- owner = getStudyDAO().get(owner.getIndex());
+ //owner = getStudyDAO().merge(owner);
- SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); //RKV: NOPMD: TODO: Use locale here?
+ SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); // RKV: NOPMD: TODO: Use locale here?
String year = tostring.format(owner.getDate());
String filename = generateEncodedName(aDoc, owner);
String path = owner.getReference();
.append(aDoc.getFile().getFormat()) // File name and extension
.toString();
aDoc.getFile().changePath(path);
+// owner = getStudyDAO().merge(owner);
+// getStudyDAO().update(owner);
}
/**
*/
public boolean buildReferenceFrom(final Document aDoc,
final ProjectElement scope, final Document lineage) {
- if (aDoc.getProgressState() != ProgressState.inWORK) {
- return false;
- }
- Study owner = null;
- Scenario context = null;
- if (scope instanceof Study) {
- owner = (Study) scope;
- } else {
- context = ((Scenario) scope);
- owner = context.getOwnerStudy();
- }
- aDoc.setDid(lineage.getDid());
- if (context != null && (lineage.isVersioned() || owner.shares(lineage))) {
- aDoc.setVersion(new Revision(aDoc.getVersion()).setBranch(
- context.getReference()).toString());
+ boolean res = (aDoc.getProgressState() == ProgressState.inWORK);
+ if (res) {
+ Study owner = scope.getOwnerStudy();
+ Scenario context = null;
+ if (scope instanceof Scenario) {
+ context = ((Scenario) scope);
+ }
+ aDoc.setDid(lineage.getDid());
+ if (context != null
+ && (lineage.isVersioned() || owner.shares(lineage))) {
+ aDoc.setVersion(new Revision(aDoc.getVersion()).setBranch(
+ context.getReference()).toString());
+ }
}
- return true;
+ return res;
}
/**
getDocumentDAO().merge(previous);
}
aDoc.setProgressState(state);
- //RKV: getDocumentDAO().update(aDoc);
+ // RKV: getDocumentDAO().update(aDoc);
}
// protected void upgrade () {
table[table.length - 1]).setAuthor(user));
updir = addoc.getSourceFile().asFile();
if (LOG.isInfoEnabled()) {
- LOG.info("Moving \"" + upfile.getName() + "\" to \""
+ LOG.info("Moving \"" + upfile.getAbsolutePath() + "\" to \""
+ updir.getPath() + "\".");
}
upfile.renameTo(updir);
"Cannot save a Publication object refering an undefined Document");
}
if (!aPublication.value().getSourceFile().exists()) {
- throw new FileNotFoundException();
+ throw new FileNotFoundException(aPublication.value().getSourceFile().asFile().getAbsolutePath());
}
if (state == ProgressState.inWORK || state == ProgressState.EXTERN) {
public Publication createDocument(final Step aStep,
final Document.Properties dprop) throws MissedPropertyException,
InvalidPropertyException, MultiplyDefinedException, IOException {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Local index before: "
+ + aStep.getOwnerStudy().getLastLocalIndex());
+ }
Document newdoc = new Document(dprop.setOwner(aStep.getOwner())
.setStep(aStep.getStep()));
getDocumentService().generateDocumentId(newdoc, dprop);
}
// Identification and save
+ aStep.getOwnerStudy().setLastLocalIndex(
+ dprop.getOwner().getOwnerStudy().getLastLocalIndex());
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Local index after: "
+ + dprop.getOwner().getOwnerStudy().getLastLocalIndex());
+ }
getDocumentService().buildReferenceFrom(newdoc, aStep.getOwnerStudy());
getDocumentDAO().create(newdoc);
using.add((Document) link.getTo());
}
}
-// value.getAllRelations().removeAll(converts);
+ // value.getAllRelations().removeAll(converts);
// Remove relations from depending documents
if (LOG.isDebugEnabled()) {
LOG.debug("Remove " + using.size() + " UsedByRelation(s).");
@Transactional
public int generateLocalIndex(final Study aStudy) {
aStudy.setLastLocalIndex(aStudy.getLastLocalIndex() + 1);
- getStudyDAO().update(aStudy);
return aStudy.getLastLocalIndex();
}
if (!mydoc.value().isInto(this)) {
continue;
}
- mydoc.setStep(this); //RKV
+ mydoc.setStep(this); // RKV
this._docums.add(mydoc);
}
}
}
public Study getOwnerStudy() {
- if (_owner instanceof Study) {
- return (Study) _owner;
- } else {
- return ((Scenario) _owner).getOwnerStudy();
- }
+ return _owner.getOwnerStudy();
}
public String getPath() {
import org.splat.dal.bo.som.Study;
import org.splat.dal.bo.som.Document.Properties;
import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.DocumentDAO;
import org.splat.dal.dao.som.StudyDAO;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MissedPropertyException;
import org.splat.service.DocumentTypeService;
import org.splat.service.PublicationService;
import org.splat.service.StepService;
+import org.splat.service.StudyService;
import org.splat.service.technical.ProjectSettingsService;
import org.splat.service.technical.ProjectSettingsService.Step;
import org.springframework.beans.factory.annotation.Autowired;
import test.splat.common.BaseTest;
/**
- * Test class for KnowledgeElementDAO.
+ * Test class for StudyDAO.
*
* @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
*
@Qualifier("documentTypeService")
private transient DocumentTypeService _documentTypeService;
+ /**
+ * The DocumentDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("documentDAO")
+ private transient DocumentDAO _documentDAO;
+
+ /**
+ * The StudyService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("studyService")
+ private transient StudyService _studyService;
+
/**
* Test creation of a study.<BR>
* <B>Description :</B> <BR>
getHibernateTemplate().clear();
Study aStudyFound = _studyDAO.get(id);
+ Assert.assertNotNull(aStudyFound);
+
// Call DAO's get method for a not existing id.
aStudyFound = _studyDAO.get(-1L);
getHibernateTemplate().flush();
Study aStudy = createStudy();
// Call DAO's create method for a good transient study.
- Long id = aStudy.getIndex();//_studyDAO.create(aStudy);
+ Long id = aStudy.getIndex();// _studyDAO.create(aStudy);
Assert.assertNotNull(id,
"Create method returns null instead of a new id.");
Assert.assertTrue(id > 0, "The new id is not a positive number.");
return pub;
}
+
+ /**
+ * Test of getting a study.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create a study and try to get it from the database.</i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. call DAO's read method for an existing id.</i><BR>
+ * <i>2. call DAO's read method for a not existing id.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>Object is found in the database successfully<BR>
+ * </li>
+ * <li>Result of search is null<BR>
+ * </li>
+ * </ul>
+ * </i>
+ *
+ * @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 SQLException
+ * @throws IOException
+ *
+ */
+ @Test
+ public void testIsReferenced() throws InvalidPropertyException,
+ MissedPropertyException, MultiplyDefinedException, IOException,
+ SQLException {
+ LOG.debug(">>>>> BEGIN testIsReferenced()");
+ startNestedTransaction();
+
+ HibernateTemplate ht = getHibernateTemplate();
+ Study aStudy = createStudy();
+ // Call DAO's create method for a good transient study.
+ Long id = aStudy.getIndex();
+ Assert.assertNotNull(id,
+ "Create method returns null instead of a new id.");
+ Assert.assertTrue(id > 0, "The new id is not a positive number.");
+
+ // Call DAO's get method for an existing id.
+ _studyDAO.flush();
+ getHibernateTemplate().evict(aStudy);
+ getHibernateTemplate().clear();
+ Study aStudyFound = _studyDAO.get(id);
+
+ ProjectElement projElem;
+ Step step;
+ long docId;
+ Document doc;
+ for (int i = _projectSettings.getAllSteps().size(); i > 0; i--) {
+ LOG.debug("Remove documents from the step " + i);
+ step = _projectSettings.getStep(i);
+ if (step.appliesTo(Study.class)) {
+ projElem = aStudyFound;
+ } else {
+ projElem = aStudyFound.getScenarii()[0];
+ }
+
+ org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
+
+ if (aScStep.getDocuments().size() > 0) {
+ doc = aScStep.getDocuments().get(0).value();
+ docId = doc.getIndex();
+/* Assert.assertEquals(_documentDAO.isReferenced(doc), (ht.find(
+ "from UsesRelation where refer=" + docId).size() + ht
+ .find("from UsedByRelation where refer=" + docId)
+ .size()) > 0);
+*/ }
+ }
+
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testIsReferenced()");
+ }
}
// ////// Load good workflow customization
getHibernateTemplate().clear();
_projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+ Database.getInstance().reset();
try {
_projectSettings.configure(ClassLoader.getSystemResource(
"test/som.xml").getPath());
Assert.fail("Can't find configuration file: ", e);
}
+ getHibernateTemplate().flush();
List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
Assert.assertTrue(steps.size() > 0, "No steps are created.");
KnowledgeElementType ucase = _knowledgeElementTypeService.selectType("usecase");
--- /dev/null
+/*****************************************************************************
+ * Company OPEN CASCADE
+ * Application SIMAN
+ * File $Id$
+ * Creation date 12 Oct 2012
+ * @author $Author$
+ * @version $Revision$
+ *****************************************************************************/
+package test.splat.service;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.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.Study;
+import org.splat.dal.bo.som.Document.Properties;
+import org.splat.dal.dao.som.Database;
+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.kernel.NotApplicableException;
+import org.splat.log.AppLogger;
+import org.splat.service.DocumentTypeService;
+import org.splat.service.ProjectElementService;
+import org.splat.service.PublicationService;
+import org.splat.service.StepService;
+import org.splat.service.StudyService;
+import org.splat.service.technical.ProjectSettingsService;
+import org.splat.service.technical.RepositoryService;
+import org.splat.service.technical.ProjectSettingsService.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 PublicationService.
+ *
+ * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
+ *
+ */
+public class TestPublicationService extends BaseTest {
+
+ /**
+ * Logger for the class.
+ */
+ private static final AppLogger LOG = AppLogger
+ .getLogger(TestPublicationService.class);
+
+ /**
+ * The StudyDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("studyDAO")
+ private transient StudyDAO _studyDAO;
+
+ /**
+ * The PublicationService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("publicationService")
+ private transient PublicationService _publicationService;
+
+ /**
+ * The ProjectElementService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("projectElementService")
+ private transient ProjectElementService _projectElementService;
+
+ /**
+ * The StepService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("stepService")
+ private transient StepService _stepService;
+
+ /**
+ * The ProjectSettingsService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("projectSettings")
+ private transient ProjectSettingsService _projectSettings;
+
+ /**
+ * The DocumentTypeService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("documentTypeService")
+ private transient DocumentTypeService _documentTypeService;
+
+ /**
+ * The StudyService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("studyService")
+ private transient StudyService _studyService;
+
+ /**
+ * The RepositoryService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("repositoryService")
+ private transient RepositoryService _repositoryService;
+
+ /**
+ * Create a persistent scenario for tests.
+ *
+ * @return a persistent scenario
+ * @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
+ * @throws SQLException
+ * if project settings loading is failed
+ */
+ private Study createStudy() throws InvalidPropertyException,
+ MissedPropertyException, MultiplyDefinedException, IOException,
+ SQLException {
+ // Create a scenario for tests
+ HibernateTemplate ht = getHibernateTemplate();
+
+ Database.getInstance().reset();
+ _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+ // Load workflow customization
+ try {
+ _projectSettings.configure(ClassLoader.getSystemResource(
+ "test/som.xml").getPath());
+ } catch (FileNotFoundException e) {
+ Assert.fail("Can't find som.xml: ", e);
+ }
+ List<Step> steps = _projectSettings.getAllSteps();
+ Assert.assertTrue(steps.size() > 0, "No steps are created.");
+
+ // 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.saveOrUpdate(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_Scenario").setManager(anAuthor).setOwnerStudy(aStudy);
+ Scenario aScenario = new Scenario(sprops);
+ aStudy.getScenariiList().add(aScenario);
+ ht.saveOrUpdate(anAuthor);
+ ht.saveOrUpdate(aStudy);
+ ht.saveOrUpdate(aScenario);
+
+ // Create documents for each scenario step
+ Document.Properties dprop = new Document.Properties().setAuthor(
+ anAuthor).setDate(new Date());
+ int i = 0;
+ Publication usedPub = null;
+ Map<Long, Long> usedMap = new HashMap<Long, Long>();
+ for (int stepNum = 1; stepNum <= steps.size(); stepNum++) {
+ Step step = _projectSettings.getStep(stepNum);
+ LOG.debug("Create scenario step: " + stepNum);
+ ProjectElement projElem;
+
+ if (step.appliesTo(Study.class)) {
+ projElem = aStudy;
+ } else {
+ projElem = aScenario;
+ }
+ org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
+ List<DocumentType> dtypes = _documentTypeService
+ .selectTypesOf(step);
+ if (dtypes.size() > 0) {
+ DocumentType dtype = dtypes.get(0);
+ // Create a document published in the scenario
+ // document<i>: document type[0] - first type used on the step
+ // <source-file>.brep
+ // <attached-file>.med
+ i++;
+ dprop.setName("document" + stepNum).setType(dtype);
+ if (step.getNumber() > 3) {
+ dprop.setFormat("med");
+ } else {
+ dprop.setFormat("py");
+ }
+ Publication pub = createDoc(projElem, aScStep, dprop, "med",
+ false);
+ if (usedPub != null) {
+ pub.addDependency(usedPub);
+ LOG.debug("Add dependency: " + pub.value().getTitle()
+ + " from " + usedPub.value().getTitle());
+ ht.saveOrUpdate(pub.value());
+ ht.flush();
+
+ usedMap.put(pub.getIndex(), usedPub.getIndex());
+ }
+ usedPub = pub;
+ }
+ if (dtypes.size() <= 0) {
+ LOG.debug("No document types are found for scenario step " + i);
+ }
+ }
+
+ // Check that the scenario and its documents have been created correctly.
+
+ Assert.assertNotNull(ht.find("from Document"),
+ "No documents in the database.");
+ Assert.assertTrue(ht.find("from Document").size() > 0,
+ "No documents in the database.");
+
+ Assert.assertNotNull(ht.find("from Publication where owner="
+ + aScenario.getIndex()), "No publications in the database.");
+ Assert.assertTrue(
+ ht.find("from Publication where owner=" + aScenario.getIndex())
+ .size() > 0, "No publications in the database.");
+
+ for (Publication p : (List<Publication>) ht
+ .find("from Publication where owner=" + aScenario.getIndex())) {
+ LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
+ + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
+ + "]");
+ Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
+ "The publication was not attached to the scenario.");
+ }
+
+ // Remove the scenario from the current hibernate session.
+ ht.evict(aScenario);
+ // Check that the scenario is created in the database.
+ Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
+ Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
+ Assert.assertTrue(aScen.getDocums().size() > 0,
+ "No publications in the scenario.");
+
+ Assert.assertTrue(i > 0,
+ "More then one document must be in the database");
+
+ // Check created uses relations
+ Assert
+ .assertTrue(usedMap.size() > 0,
+ "Uses relations must be created.");
+ boolean foundAny = false;
+ for (Long usingId : usedMap.keySet()) {
+ for (Publication pub : aScen.getDocums()) {
+ if (pub.getIndex() == usingId) {
+ boolean found = false;
+ for (Publication used : aScen.getDocums()) {
+ found = (used.getIndex() == usedMap.get(usingId));
+ if (found) {
+ break;
+ }
+ }
+ if (!found) {
+ for (Publication used : aStudy.getDocums()) {
+ found = (used.getIndex() == usedMap.get(usingId));
+ if (found) {
+ break;
+ }
+ }
+ }
+ Assert.assertTrue(found,
+ "Uses relation was not created in the database.");
+ foundAny = foundAny || found;
+ }
+ }
+ }
+ Assert.assertTrue(foundAny,
+ "No Uses relation was created in the database.");
+
+ return aScenario.getOwnerStudy();
+ }
+
+ /**
+ * Create a document published in the scenario. <BR>
+ * document:<BR>
+ * document type - type used on the step <BR>
+ * <source-file>.brep <BR>
+ * <attached-file>.med
+ *
+ * @param aScenario
+ * the scenario to add the document to
+ * @param aScStep
+ * scenario step where the document to be published
+ * @param dprop
+ * document properties
+ * @param attachedFileExt
+ * extension of the secon attached (exported) file
+ * @param isOutdated
+ * outdated document flag
+ * @return the publication of the created document
+ * @throws IOException
+ * @throws MultiplyDefinedException
+ * @throws InvalidPropertyException
+ * @throws MissedPropertyException
+ */
+ private Publication createDoc(final ProjectElement aScenario,
+ final org.splat.som.Step aScStep, final Properties dprop,
+ final String attachedFileExt, final boolean isOutdated)
+ throws MissedPropertyException, InvalidPropertyException,
+ MultiplyDefinedException, IOException {
+ // Create a document published in the scenario
+ // document<i>: document type - type used on the step
+ // <source-file>.brep
+ // <attached-file>.med
+ Publication pub = _stepService.createDocument(aScStep, dprop);
+ Assert.assertNotNull(pub.getOwner(),
+ "The publication must be attached to the scenario.");
+ Assert.assertEquals(pub.getOwner().getIndex(), aScenario.getIndex(),
+ "The publication was not attached to the scenario.");
+
+ if (isOutdated) {
+ pub.setIsnew('O');
+ }
+ aScenario.add(pub);
+ HibernateTemplate ht = getHibernateTemplate();
+ ht.saveOrUpdate(pub);
+
+ // Attach a file
+ ht.save(pub.value());
+ ht.flush();
+ ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));
+
+ return pub;
+ }
+
+ /**
+ * Test of generating a study document index.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create a study and try to generate the next document index.</i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. call DAO's read method for an existing id.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>The new index must be equal to the incremented old one and saved into the database<BR>
+ * </li>
+ * </ul>
+ * </i>
+ *
+ * @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 SQLException
+ * if test study creation is failed
+ * @throws IOException
+ * if test study creation is failed
+ * @throws ParseException
+ * @throws InterruptedException
+ * @throws NotApplicableException
+ *
+ */
+ @Test
+ public void testCreateDoc() throws InvalidPropertyException,
+ MissedPropertyException, MultiplyDefinedException, IOException,
+ SQLException, NotApplicableException, InterruptedException,
+ ParseException {
+ LOG.debug(">>>>> BEGIN testCreateDoc()");
+ startNestedTransaction();
+
+ HibernateTemplate ht = getHibernateTemplate();
+ Study aStudy = createStudy();
+ // Call DAO's create method for a good transient study.
+ Long id = aStudy.getIndex();
+ Assert.assertNotNull(id,
+ "Create method returns null instead of a new id.");
+ Assert.assertTrue(id > 0, "The new id is not a positive number.");
+
+ // Call DAO's get method for an existing id.
+ _studyDAO.flush();
+ getHibernateTemplate().evict(aStudy);
+ getHibernateTemplate().clear();
+ Study aStudyFound = _studyDAO.get(id);
+ ht.evict(aStudyFound);
+
+ long userId = aStudyFound.getAuthor().getIndex();
+ org.splat.som.Step[] studySteps = _projectElementService
+ .getSteps(aStudyFound);
+ org.splat.som.Step[] scSteps = _projectElementService
+ .getSteps(aStudyFound.getScenarii()[0]);
+
+ List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
+ steps.addAll(Arrays.asList(studySteps));
+ steps.addAll(Arrays.asList(scSteps));
+ steps.addAll(Arrays.asList(studySteps));
+ for (org.splat.som.Step step: steps) {
+ LOG.debug("Create a document for the step " + step.getNumber());
+
+ List<DocumentType> dtypes = _documentTypeService
+ .selectTypesOf(step.getStep());
+ if (dtypes.size() > 0) {
+ DocumentType dtype = dtypes.get(0);
+ int oldInd = ht.get(Study.class, aStudyFound.getIndex())
+ .getLastLocalIndex();
+
+ // Create a file in the download directory
+ String fname = "filename" + step.getNumber() + ".py";
+ String filePath = getDownloadPath(aStudyFound.getAuthor())
+ + fname;
+ LOG.debug(step.getStep().getKey() + ": "
+ + step.getOwner().getClass().getSimpleName()
+ + ": Create test file: " + filePath);
+ FileWriter fw = new FileWriter(filePath);
+ fw
+ .write("Simulation of filename.py file for creating a new document at "
+ + new Date());
+ fw.close();
+
+ Publication addoc = _publicationService.createDoc(aStudyFound
+ .getIndex(), step, dtype.getIndex(), userId, fname,
+ "tstdoc", ProgressState.inWORK, "", "", new Date(),
+ null);
+
+ _studyDAO.flush();
+ ht.flush();
+
+ int ind = ht.get(Study.class, aStudyFound.getIndex())
+ .getLastLocalIndex();
+
+ LOG.debug("Previous index: " + oldInd + ", new index: " + ind);
+ Assert.assertEquals(ht.get(Study.class, aStudyFound.getIndex())
+ .getLastLocalIndex(), oldInd + 1,
+ "Incremented index must be saved in the database.");
+ aStudy = (Study) ht.find(
+ "from Study where rid = " + aStudyFound.getIndex())
+ .get(0);
+ Assert.assertEquals(aStudy.getLastLocalIndex(), oldInd + 1,
+ "Incremented index must be saved in the database.");
+ }
+ }
+
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testCreateDoc()");
+ }
+
+ /**
+ * Get path to the user's downloads directory. The directory is created if it is not exist yet.
+ *
+ * @param user
+ * user
+ * @return absolute path to downloads directory followed by slash
+ */
+ private String getDownloadPath(final User user) {
+ // Prepare download directory
+ File tmpDir = _repositoryService.getDownloadDirectory(user);
+ if (!tmpDir.exists()) {
+ Assert.assertTrue(tmpDir.mkdir(),
+ "Can't create temporary directory: "
+ + tmpDir.getAbsolutePath());
+ }
+
+ return tmpDir.getAbsolutePath() + "/";
+ }
+
+}
--- /dev/null
+/*****************************************************************************
+ * Company OPEN CASCADE
+ * Application SIMAN
+ * File $Id$
+ * Creation date 12 Oct 2012
+ * @author $Author$
+ * @version $Revision$
+ *****************************************************************************/
+package test.splat.service;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.ProjectElement;
+import org.splat.dal.bo.som.Publication;
+import org.splat.dal.bo.som.Scenario;
+import org.splat.dal.bo.som.Study;
+import org.splat.dal.bo.som.Document.Properties;
+import org.splat.dal.dao.som.Database;
+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.log.AppLogger;
+import org.splat.service.DocumentTypeService;
+import org.splat.service.PublicationService;
+import org.splat.service.StepService;
+import org.splat.service.StudyService;
+import org.splat.service.technical.ProjectSettingsService;
+import org.splat.service.technical.ProjectSettingsService.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 StudyService.
+ *
+ * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
+ *
+ */
+public class TestStudyService extends BaseTest {
+
+ /**
+ * Logger for the class.
+ */
+ private static final AppLogger LOG = AppLogger
+ .getLogger(TestStudyService.class);
+
+ /**
+ * The StudyDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("studyDAO")
+ private transient StudyDAO _studyDAO;
+
+ /**
+ * 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 ProjectSettingsService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("projectSettings")
+ private transient ProjectSettingsService _projectSettings;
+
+ /**
+ * The DocumentTypeService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("documentTypeService")
+ private transient DocumentTypeService _documentTypeService;
+
+ /**
+ * The StudyService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("studyService")
+ private transient StudyService _studyService;
+
+ /**
+ * Create a persistent scenario for tests.
+ *
+ * @return a persistent scenario
+ * @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
+ * @throws SQLException
+ * if project settings loading is failed
+ */
+ private Study createStudy() throws InvalidPropertyException,
+ MissedPropertyException, MultiplyDefinedException, IOException,
+ SQLException {
+ // Create a scenario for tests
+ HibernateTemplate ht = getHibernateTemplate();
+
+ Database.getInstance().reset();
+ _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+ // Load workflow customization
+ try {
+ _projectSettings.configure(ClassLoader.getSystemResource(
+ "test/som.xml").getPath());
+ } catch (FileNotFoundException e) {
+ Assert.fail("Can't find som.xml: ", e);
+ }
+ List<Step> steps = _projectSettings.getAllSteps();
+ Assert.assertTrue(steps.size() > 0, "No steps are created.");
+
+ // 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.saveOrUpdate(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_Scenario").setManager(anAuthor).setOwnerStudy(aStudy);
+ Scenario aScenario = new Scenario(sprops);
+ aStudy.getScenariiList().add(aScenario);
+ ht.saveOrUpdate(anAuthor);
+ ht.saveOrUpdate(aStudy);
+ ht.saveOrUpdate(aScenario);
+
+ // Create documents for each scenario step
+ Document.Properties dprop = new Document.Properties().setAuthor(
+ anAuthor).setDate(new Date());
+ int i = 0;
+ Publication usedPub = null;
+ Map<Long, Long> usedMap = new HashMap<Long, Long>();
+ for (int stepNum = 1; stepNum <= steps.size(); stepNum++) {
+ Step step = _projectSettings.getStep(stepNum);
+ LOG.debug("Create scenario step: " + stepNum);
+ ProjectElement projElem;
+
+ if (step.appliesTo(Study.class)) {
+ projElem = aStudy;
+ } else {
+ projElem = aScenario;
+ }
+ org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
+ List<DocumentType> dtypes = _documentTypeService
+ .selectTypesOf(step);
+ if (dtypes.size() > 0) {
+ DocumentType dtype = dtypes.get(0);
+ // Create a document published in the scenario
+ // document<i>: document type[0] - first type used on the step
+ // <source-file>.brep
+ // <attached-file>.med
+ i++;
+ dprop.setName("document" + stepNum).setType(dtype);
+ if (step.getNumber() > 3) {
+ dprop.setFormat("med");
+ } else {
+ dprop.setFormat("py");
+ }
+ Publication pub = createDoc(projElem, aScStep, dprop, "med",
+ false);
+ if (usedPub != null) {
+ pub.addDependency(usedPub);
+ LOG.debug("Add dependency: " + pub.value().getTitle()
+ + " from " + usedPub.value().getTitle());
+ ht.saveOrUpdate(pub.value());
+ ht.flush();
+
+ usedMap.put(pub.getIndex(), usedPub.getIndex());
+ }
+ usedPub = pub;
+ }
+ if (dtypes.size() <= 0) {
+ LOG.debug("No document types are found for scenario step " + i);
+ }
+ }
+
+ // Check that the scenario and its documents have been created correctly.
+
+ Assert.assertNotNull(ht.find("from Document"),
+ "No documents in the database.");
+ Assert.assertTrue(ht.find("from Document").size() > 0,
+ "No documents in the database.");
+
+ Assert.assertNotNull(ht.find("from Publication where owner="
+ + aScenario.getIndex()), "No publications in the database.");
+ Assert.assertTrue(
+ ht.find("from Publication where owner=" + aScenario.getIndex())
+ .size() > 0, "No publications in the database.");
+
+ for (Publication p : (List<Publication>) ht
+ .find("from Publication where owner=" + aScenario.getIndex())) {
+ LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
+ + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
+ + "]");
+ Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
+ "The publication was not attached to the scenario.");
+ }
+
+ // Remove the scenario from the current hibernate session.
+ ht.evict(aScenario);
+ // Check that the scenario is created in the database.
+ Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
+ Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
+ Assert.assertTrue(aScen.getDocums().size() > 0,
+ "No publications in the scenario.");
+
+ Assert.assertTrue(i > 0,
+ "More then one document must be in the database");
+
+ // Check created uses relations
+ Assert
+ .assertTrue(usedMap.size() > 0,
+ "Uses relations must be created.");
+ boolean foundAny = false;
+ for (Long usingId : usedMap.keySet()) {
+ for (Publication pub : aScen.getDocums()) {
+ if (pub.getIndex() == usingId) {
+ boolean found = false;
+ for (Publication used : aScen.getDocums()) {
+ found = (used.getIndex() == usedMap.get(usingId));
+ if (found) {
+ break;
+ }
+ }
+ if (!found) {
+ for (Publication used : aStudy.getDocums()) {
+ found = (used.getIndex() == usedMap.get(usingId));
+ if (found) {
+ break;
+ }
+ }
+ }
+ Assert.assertTrue(found,
+ "Uses relation was not created in the database.");
+ foundAny = foundAny || found;
+ }
+ }
+ }
+ Assert.assertTrue(foundAny,
+ "No Uses relation was created in the database.");
+
+ return aScenario.getOwnerStudy();
+ }
+
+ /**
+ * Create a document published in the scenario. <BR>
+ * document:<BR>
+ * document type - type used on the step <BR>
+ * <source-file>.brep <BR>
+ * <attached-file>.med
+ *
+ * @param aScenario
+ * the scenario to add the document to
+ * @param aScStep
+ * scenario step where the document to be published
+ * @param dprop
+ * document properties
+ * @param attachedFileExt
+ * extension of the secon attached (exported) file
+ * @param isOutdated
+ * outdated document flag
+ * @return the publication of the created document
+ * @throws IOException
+ * @throws MultiplyDefinedException
+ * @throws InvalidPropertyException
+ * @throws MissedPropertyException
+ */
+ private Publication createDoc(final ProjectElement aScenario,
+ final org.splat.som.Step aScStep, final Properties dprop,
+ final String attachedFileExt, final boolean isOutdated)
+ throws MissedPropertyException, InvalidPropertyException,
+ MultiplyDefinedException, IOException {
+ // Create a document published in the scenario
+ // document<i>: document type - type used on the step
+ // <source-file>.brep
+ // <attached-file>.med
+ Publication pub = _stepService.createDocument(aScStep, dprop);
+ Assert.assertNotNull(pub.getOwner(),
+ "The publication must be attached to the scenario.");
+ Assert.assertEquals(pub.getOwner().getIndex(), aScenario.getIndex(),
+ "The publication was not attached to the scenario.");
+
+ if (isOutdated) {
+ pub.setIsnew('O');
+ }
+ aScenario.add(pub);
+ HibernateTemplate ht = getHibernateTemplate();
+ ht.saveOrUpdate(pub);
+
+ // Attach a file
+ ht.save(pub.value());
+ ht.flush();
+ ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));
+
+ return pub;
+ }
+
+ /**
+ * Test of generating a study document index.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create a study and try to generate the next document index.</i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. call DAO's read method for an existing id.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>The new index must be equal to the incremented old one and saved into the database<BR>
+ * </li>
+ * </ul>
+ * </i>
+ *
+ * @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 SQLException
+ * if test study creation is failed
+ * @throws IOException
+ * if test study creation is failed
+ *
+ */
+ @Test
+ public void testGenerateLocalIndex() throws InvalidPropertyException,
+ MissedPropertyException, MultiplyDefinedException, IOException,
+ SQLException {
+ LOG.debug(">>>>> BEGIN testGenerateLocalIndex()");
+ startNestedTransaction();
+
+ HibernateTemplate ht = getHibernateTemplate();
+ Study aStudy = createStudy();
+ // Call DAO's create method for a good transient study.
+ Long id = aStudy.getIndex();
+ Assert.assertNotNull(id,
+ "Create method returns null instead of a new id.");
+ Assert.assertTrue(id > 0, "The new id is not a positive number.");
+
+ // Call DAO's get method for an existing id.
+ _studyDAO.flush();
+ getHibernateTemplate().evict(aStudy);
+ getHibernateTemplate().clear();
+ Study aStudyFound = _studyDAO.get(id);
+
+ int oldInd = aStudyFound.getLastLocalIndex();
+ int ind = _studyService.generateLocalIndex(aStudyFound);
+
+ _studyDAO.flush();
+ Assert.assertEquals(ind, oldInd + 1, "Index must be incremented.");
+ Assert.assertEquals(ind, aStudyFound.getLastLocalIndex(),
+ "Index must be incremented.");
+ Assert.assertEquals(ind, ht.get(Study.class, aStudyFound.getIndex())
+ .getLastLocalIndex(),
+ "Incremented index must be saved in the database.");
+ aStudy = (Study)ht.find(
+ "from Study where rid = " + aStudyFound.getIndex()).get(0);
+ Assert.assertEquals(ind, aStudy.getLastLocalIndex(),
+ "Incremented index must be saved in the database.");
+
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testGenerateLocalIndex()");
+ }
+}