* @copyright OPEN CASCADE 2012
*/
-import java.text.DecimalFormat;
-import java.text.SimpleDateFormat;
import java.util.Arrays;
-import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
-import org.hibernate.Hibernate;
-import org.hibernate.Session;
-
import org.splat.dal.bo.kernel.Persistent;
import org.splat.dal.bo.kernel.Relation;
import org.splat.dal.bo.kernel.User;
import org.splat.dal.bo.som.Timestamp.ComparatorByDate;
-import org.splat.dal.dao.som.Database;
-import org.splat.kernel.NotApplicableException;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MissedPropertyException;
import org.splat.kernel.MultiplyDefinedException;
-import org.splat.manox.Reader;
-import org.splat.manox.Toolbox;
-import org.splat.service.StudyService;
import org.splat.service.technical.ProjectSettingsService;
import org.splat.service.technical.ProjectSettingsServiceImpl;
-import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
import org.splat.som.Revision;
import org.splat.som.Step;
+/**
+ * Document persistent class.
+ */
public class Document extends Entity {
// Persistent fields
private int history;
private User author;
private Date lasdate;
- private ProjectSettingsService _projectSettingsService;
- private StudyService _studyService;
// Transient fields
public static String suformat = "00"; // Format of the suffix number of document did and file name
// Construction
// ==============================================================================================================================
- // Fields initialization class
+ /**
+ * Fields initialization class.
+ */
public static class Properties extends Persistent.Properties {
// ------------------------------------------------------------
private DocumentType type = null;
return (history > 0);
}
- // ==============================================================================================================================
- // Public services
- // ==============================================================================================================================
-
- public static DocumentType createType(DocumentType.Properties tprop)
- throws MissedPropertyException, InvalidPropertyException,
- MultiplyDefinedException, RuntimeException {
- // ---------------------------------------------------------------------
- // TODO: Check for duplicate definition
- DocumentType type = new DocumentType(tprop);
- Session session = Database.getSession();
- session.save(type);
-
- return type;
- }
-
- public static Properties extractProperties(java.io.File file) {
- // --------------------------------------------------------------
- Properties fprop = new Properties();
- Reader tool = Toolbox.getReader(file);
- String value;
- if (tool != null)
- try {
- value = tool.extractProperty("title");
- if (value != null)
- fprop.setName(value);
-
- value = tool.extractProperty("reference");
- if (value != null)
- fprop.setReference(value);
- } catch (Exception e) {
- }
- return fprop;
- }
-
- @SuppressWarnings("unchecked")
- public static List<DocumentType> selectAllTypes() {
- // --------------------------------------------------
- String query = "from DocumentType";
-
- List<DocumentType> types = Database.getSession().createQuery(query)
- .list();
- for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
- Hibernate.initialize(i.next()); // Supposed fetching document types
- }
- return types;
- }
-
- @SuppressWarnings("unchecked")
- public static List<DocumentType> selectResultTypes() {
- // -----------------------------------------------------
- String query = "from DocumentType where result is not null order by result asc";
-
- return Database.getSession().createQuery(query).list();
- }
-
- public static DocumentType selectType(String name) {
- // ---------------------------------------------------
- String query = new StringBuffer("from DocumentType where name='")
- .append(name).append("'").toString();
-
- return (DocumentType) Database.getSession().createQuery(query)
- .uniqueResult();
- }
-
- public static DocumentType selectType(long index) {
- // -------------------------------------------------
- String query = new StringBuffer("from DocumentType where rid='")
- .append(index).append("'").toString();
-
- return (DocumentType) Database.getSession().createQuery(query)
- .uniqueResult();
- }
-
- @SuppressWarnings("unchecked")
- public static List<DocumentType> selectTypesOf(
- ProjectSettingsService.Step step) {
- // --------------------------------------------------------------------------
- Integer number = step.getNumber();
- String query = new StringBuffer("from DocumentType").append(
- " where step like '%-").append(number).append("-%'").toString();
-
- List<DocumentType> types = Database.getSession().createQuery(query)
- .list();
- for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
- Hibernate.initialize(i.next()); // For fetching document types
- }
- return types;
- }
-
- // ==============================================================================================================================
- // Protected services
- // ==============================================================================================================================
-
- protected ConvertsRelation attach(String format) {
- // -------------------------------------------------
- return attach(format, null);
- }
-
- protected ConvertsRelation attach(String format, String description) {
- // ---------------------------------------------------------------------
- String path = this.getRelativePath();
- File export = new File(path + "." + format);
- ConvertsRelation attach = new ConvertsRelation(this, export,
- description);
- Session session = Database.getSession();
-
- session.save(export);
- session.save(attach);
-
- this.addRelation(attach); // Updates this
-
- return attach;
- }
-
- public boolean buildReferenceFrom(ProjectElement scope, Document lineage) {
- // -----------------------------------------------------------------------------
- if (state != ProgressState.inWORK)
- return false;
- Study owner = null;
- Scenario context = null;
- if (scope instanceof Study)
- owner = (Study) scope;
- else {
- context = ((Scenario) scope);
- owner = context.getOwnerStudy();
- }
- did = lineage.did;
- if (context != null && (lineage.isVersioned() || owner.shares(lineage))) {
- version = new Revision(version).setBranch(context.getReference())
- .toString();
- }
- return true;
- }
-
- public boolean buildReferenceFrom(Study scope) {
- // --------------------------------------------------
- if (state != ProgressState.inWORK && state != ProgressState.EXTERN)
- return false;
- DecimalFormat tostring = new DecimalFormat(suformat);
-
- did = did.replace("%" + suformat, tostring.format(scope
- .getLastLocalIndex()));
- return true;
- }
-
- public boolean demote() {
- // ---------------------------
- ValidationStep torem;
-
- if (state == ProgressState.inCHECK) {
- state = ProgressState.inDRAFT;
- torem = ValidationStep.REVIEW;
- // This operation must not change the version number of documents.
- // Consequently, inDRAFT documents may have a minor version number equal to zero.
- } else if (state == ProgressState.inDRAFT) {
- state = ProgressState.inWORK;
- torem = ValidationStep.PROMOTION;
- } else {
- return false;
- }
- for (Iterator<Relation> i = this.getAllRelations().iterator(); i
- .hasNext();) {
- Relation link = i.next();
- if (!(link instanceof StampRelation))
- continue;
- if (((StampRelation) link).getStampType() != torem)
- continue;
- i.remove();
- break;
- }
- Database.getSession().update(this);
- return true;
- }
-
- /**
- * Increments the reference count of this document following its publication into a Study step.
- *
- * @see #release()
- */
- public void hold() {
- // ----------------------
- countag += 1;
- if (this.isSaved())
- Database.getSession().update(this);
- }
-
- public boolean promote(Timestamp stamp) {
- // -------------------------------------------
- ProgressState newstate = null;
-
- if (state == ProgressState.inWORK) {
- newstate = ProgressState.inDRAFT; // Promotion to being reviewed
- } else if (state == ProgressState.inDRAFT) {
- newstate = ProgressState.inCHECK; // Promotion to approval
- Revision myvers = new Revision(version);
- if (myvers.isMinor()) {
- version = myvers.incrementAs(newstate).toString();
- // TODO: If my physical file is programatically editable, update its (property) version number
- // ISSUE: What about attached files such as PDF if exist, should we remove them ?
- }
- } else if (state == ProgressState.inCHECK) {
- newstate = ProgressState.APPROVED;
- }
- this.state = newstate;
- if (stamp != null)
- this.addRelation(stamp.getContext());
- Database.getSession().update(this);
- return true;
- }
-
- /**
- * Decrements the reference count of this document following the removal of a Publication from a Study step.
- *
- * @see #hold()
- */
- public void release() {
- // -------------------------
- countag -= 1;
- if (this.isSaved())
- Database.getSession().update(this);
- }
-
- protected void rename(String title) throws InvalidPropertyException {
- // ------------------------------------
- if (title.length() == 0)
- throw new InvalidPropertyException("name");
-
- Calendar current = Calendar.getInstance();
- this.name = title;
- this.lasdate = current.getTime(); // Today
- Database.getSession().update(this);
- }
-
- public void updateAs(Revision newvers) {
- // ------------------------------------------
- version = newvers.setBranch(version).toString(); // Branch names are propagated by the versionning
- ProgressState newstate = ProgressState.inCHECK;
- if (newvers.isMinor())
- newstate = ProgressState.inWORK;
- state = null; // Just to tell updateAs(sate) to not increment the version number
- updateAs(newstate);
- }
-
- public void updateAs(ProgressState state) {
- // ---------------------------------------------
- Document previous = null;
-
- // Set of version number
- if (state == ProgressState.EXTERN) {
- if (this.state != ProgressState.EXTERN)
- this.version = null; // Strange use-case...
- } else {
- Revision myvers = new Revision(version);
- if (!myvers.isNull()) { // Versionning context
- for (Iterator<Relation> i = getAllRelations().iterator(); i
- .hasNext();) {
- Relation link = i.next();
- if (!link.getClass().equals(VersionsRelation.class))
- continue;
- previous = (Document) link.getTo(); // Versioned document
- break;
- }
- }
- if (this.state != null)
- myvers.incrementAs(state); // Incrementation if the reversion number is not imposed
- this.version = myvers.toString();
- }
- // Update this document and the previous version, if exit
- Session session = Database.getSession();
- if (previous != null) {
- previous.history += 1;
- session.update(previous);
- }
- this.state = state;
- session.update(this);
- }
-
- // protected void upgrade () {
- // -------------------------
- // if (this.state != ProgressState.inWORK) return;
- //
- // Calendar current = Calendar.getInstance();
- // for (Iterator<Relation> i=getAllRelations().iterator(); i.hasNext();) {
- // Relation link = i.next();
- // if (!link.getClass().equals(UsesRelation.class)) continue;
- //
- // Document used = (Document)link.getTo();
- // if (!used.isVersioned()) continue;
- // TODO: Update the uses relation
- // }
- // this.promote();
- // this.lasdate = current.getTime(); // Today
- // Database.getSession().update(this);
- //
- // TODO: Promote documents using this one
- // }
-
- /**
- * @return
- */
- private ProjectSettingsService getProjectSettingsService() {
- return _projectSettingsService;
- }
-
- public void setProjectSettingsService(
- ProjectSettingsService projectSettingsService) {
- _projectSettingsService = projectSettingsService;
- }
-
- /**
- * @return
- */
- public StudyService getStudyService() {
- return _studyService;
- }
-
- public void setStudyService(StudyService studyService) {
- _studyService = studyService;
- }
-
/**
* Get the step.
* @return the step
public void setHistory(int history) {
this.history = history;
}
+
+ /**
+ * Set the version.
+ * @param version the version to set
+ */
+ public void setVersion(String version) {
+ this.version = version;
+ }
+
+ /**
+ * Set the state.
+ * @param state the state to set
+ */
+ public void setProgressState(ProgressState state) {
+ this.state = state;
+ }
+
+ /**
+ * Get the countag.
+ * @return the countag
+ */
+ public int getCountag() {
+ return countag;
+ }
+
+ /**
+ * Set the countag.
+ * @param countag the countag to set
+ */
+ public void setCountag(int countag) {
+ this.countag = countag;
+ }
}
\ No newline at end of file