* @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.Entity;
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;
private String did = null; // Only for searching from a given reference
private ProjectElement owner = null; // Only for constructing a document
// - Public services
+ @Override
public void clear() {
super.clear();
type = null;
// - Property setters
- public Properties setAuthor(User user) {
+ public Properties setAuthor(final User user) {
this.author = user;
return this;
}
- public Properties setDate(Date date) {
+ public Properties setDate(final Date date) {
this.date = date;
return this;
}
/**
* Get the date.
+ *
* @return the date
*/
public Date getDate() {
return date;
}
- public Properties setDescription(String summary)
+ public Properties setDescription(final String summary)
throws InvalidPropertyException {
- if (summary.length() == 0)
+ if (summary.length() == 0) {
throw new InvalidPropertyException("description");
+ }
this.summary = summary;
return this;
}
- public Properties setDocument(Document base) {
+ /**
+ * Copy base properties from the given original document for versioning.
+ *
+ * @param base
+ * the original document
+ * @param aStep
+ * study step
+ * @return document properties
+ */
+ public Properties setDocument(final Document base,
+ final ProjectSettingsService.Step aStep) {
type = base.type;
- step = ProjectSettingsServiceImpl.getStep(base.step);
+ step = aStep;
name = base.name;
format = base.getFormat();
state = ProgressState.inWORK; // For incrementing the version number at save time
return this;
}
- public Properties setExternReference(String ref)
+ public Properties setExternReference(final String ref)
throws InvalidPropertyException {
- if (ref.length() == 0)
+ if (ref.length() == 0) {
throw new InvalidPropertyException("reference");
- if (ref.equals(new Revision().toString()))
+ }
+ if (ref.equals(new Revision().toString())) {
throw new InvalidPropertyException("reference"); // Internal version number
+ }
this.version = ref;
return this;
}
- public Properties setFormat(String format)
+ public Properties setFormat(final String format)
throws InvalidPropertyException {
- if (format.length() == 0)
+ if (format.length() == 0) {
throw new InvalidPropertyException("format");
+ }
this.format = format;
return this;
}
// Required only for passing search arguments
- public Properties setLocalPath(String path)
+ public Properties setLocalPath(final String path)
throws InvalidPropertyException {
- if (path.length() == 0)
+ if (path.length() == 0) {
throw new InvalidPropertyException("path");
+ }
this.path = path;
return this;
}
- public Properties setName(String name) throws InvalidPropertyException {
- if (name.length() == 0)
+ public Properties setName(final String name)
+ throws InvalidPropertyException {
+ if (name.length() == 0) {
throw new InvalidPropertyException("name");
+ }
this.name = name;
return this;
}
-
+
public String getName() {
return this.name;
}
- public Properties setOwner(ProjectElement owner) {
+ public Properties setOwner(final ProjectElement owner) {
this.owner = owner;
return this;
}
public ProjectElement getOwner() {
return this.owner;
}
+
// Required only for passing search arguments
- public Properties setReference(String did)
+ public Properties setReference(final String did)
throws InvalidPropertyException {
- if (did.length() == 0)
+ if (did.length() == 0) {
throw new InvalidPropertyException("reference");
+ }
this.did = did;
return this;
}
- public Properties setState(ProgressState state)
+ public Properties setState(final ProgressState state)
throws InvalidPropertyException {
if (state == ProgressState.inPROGRESS
- || state == ProgressState.TEMPLATE)
+ || state == ProgressState.TEMPLATE) {
throw new InvalidPropertyException("state"); // Non document states
+ }
this.state = state;
return this;
}
- public Properties setStep(ProjectSettingsService.Step step) {
+ public Properties setStep(final ProjectSettingsService.Step step) {
this.step = step;
return this;
}
- public Properties setType(DocumentType type) {
+ public Properties setType(final DocumentType type) {
this.type = type;
return this;
}
public void checkValidity() throws MissedPropertyException,
InvalidPropertyException, MultiplyDefinedException {
- if (type == null)
+ if (type == null) {
throw new MissedPropertyException("type");
- if (owner == null)
+ }
+ if (owner == null) {
throw new MissedPropertyException("owner");
- if (step == null)
+ }
+ if (step == null) {
throw new MissedPropertyException("step");
- if (author == null)
+ }
+ if (author == null) {
throw new MissedPropertyException("author");
- if (format == null)
+ }
+ if (format == null) {
throw new MissedPropertyException("format");
- if (owner instanceof Study && !step.appliesTo(Study.class))
+ }
+ if (owner instanceof Study && !step.appliesTo(Study.class)) {
throw new InvalidPropertyException("step");
- if (!type.isContentInto(step))
+ }
+ if (!type.isContentInto(step)) {
throw new InvalidPropertyException("step");
+ }
if (state != null && state != ProgressState.EXTERN) {
// inDRAFT, inCHECK or APPROVED + version = imposed version (future use)
// inWORK + version = base version incremented at save time (used for versioning)
- if (version == null)
+ if (version == null) {
throw new InvalidPropertyException("state");
+ }
}
if (version != null) {
- if (state == null)
+ if (state == null) {
state = ProgressState.EXTERN;
+ }
}
}
+
+ /**
+ * Get the format.
+ *
+ * @return the format
+ */
+ public String getFormat() {
+ return format;
+ }
}
- // Database fetch constructor
+ /**
+ * Database fetch constructor.
+ */
protected Document() {
+ super();
}
- // Internal constructor
- public Document(Properties dprop) throws MissedPropertyException,
+ /**
+ * Initialization constructor.
+ *
+ * @param dprop
+ * @throws MissedPropertyException
+ * @throws InvalidPropertyException
+ * @throws MultiplyDefinedException
+ */
+ public Document(final Properties dprop) throws MissedPropertyException,
InvalidPropertyException, MultiplyDefinedException {
- // -------------------------------------
super(dprop); // Throws one of the above exception if not valid
- myfile = new File(null, dprop.format, dprop.date); // The path is initialized below
+ myfile = new File(dprop.getLocalPath(), dprop.format, dprop.date); // The path is initialized below
type = dprop.type;
step = dprop.step.getNumber();
name = dprop.name;
// Public member functions
// ==============================================================================================================================
- public File getAttachedFile(String format) {
- // -------------------------------------------
+ /**
+ * Get the attached file of the given format.
+ *
+ * @param format
+ * the file format
+ * @return the attached file or null if not found
+ */
+ public File getAttachedFile(final String format) {
List<Relation> exports = getRelations(ConvertsRelation.class);
-
- for (Iterator<Relation> i = exports.iterator(); i.hasNext();) {
- File export = (File) i.next().getTo();
- if (export.getFormat().equals(format))
- return export;
+ File res = null;
+ for (Relation rel : exports) {
+ File export = (File) rel.getTo();
+ if (export.getFormat().equals(format)) {
+ res = export;
+ break;
+ }
}
- return null;
+ return res;
}
public User getAuthor() {
- // ------------------------
return author;
}
public Date getCreationDate() {
- // ------------------------------
return myfile.getDate();
}
public Date getLastModificationDate() {
- // --------------------------------------
return lasdate;
}
-
- public void setLastModificationDate(Date aDate) {
+
+ public void setLastModificationDate(final Date aDate) {
lasdate = aDate;
}
public String getFormat() {
- // --------------------------
return myfile.getFormat();
}
public Document getPreviousVersion() {
- // -------------------------------------
Relation previous = getFirstRelation(VersionsRelation.class);
- if (previous != null)
- return (Document) previous.getTo();
- else
+ if (previous == null) {
return null;
+ } else {
+ return (Document) previous.getTo();
+ }
}
public ProgressState getProgressState() {
- // ----------------------------------------
return state;
}
* @return the path of the document
*/
public String getRelativePath() {
- // --------------------------------
String[] table = myfile.getRelativePath().split("\\x2E");
StringBuffer path = new StringBuffer(table[0]);
- for (int i = 1; i < table.length - 1; i++)
+ for (int i = 1; i < table.length - 1; i++) {
path.append('.').append(table[i]);
+ }
return path.toString();
}
* @return the document reference
*/
public String getReference() {
- // -----------------------------
return did;
}
public File getSourceFile() {
- // ----------------------------
return myfile;
}
* @return the stamps of the document in ascending order of dates, or an empty array if no stamp exist.
*/
public Timestamp[] getStamps() {
- // -------------------------------
Vector<Timestamp> stamps = new Vector<Timestamp>();
for (Iterator<Relation> i = this.getAllRelations().iterator(); i
.hasNext();) {
Relation link = i.next();
- if (link instanceof StampRelation)
+ if (link instanceof StampRelation) {
stamps.add(((StampRelation) link).getTo());
+ }
}
Timestamp[] result = stamps.toArray(new Timestamp[stamps.size()]);
ComparatorByDate bydate = new Timestamp.ComparatorByDate();
*/
public String getTitle() {
// -------------------------
- if (this.isUndefined())
+ if (this.isUndefined()) {
return "";
- else
+ } else {
return name;
+ }
}
-
+
/**
* Set document title.
- * @param aTitle document title to set
+ *
+ * @param aTitle
+ * document title to set
*/
- public void setTitle(String aTitle) {
+ public void setTitle(final String aTitle) {
this.name = aTitle;
}
public DocumentType getType() {
- // ------------------------------
return type;
}
* @see #isUndefined()
*/
public String getVersion() {
- // ---------------------------
return version;
}
* @see #initialize(Properties)
*/
public boolean isUndefined() {
- // -----------------------------
return (history == -1);
}
- public boolean isInto(Step container) {
- // --------------------------------------
+ public boolean isInto(final Step container) {
return (step == container.getNumber());
}
public boolean isPublished() {
- // -----------------------------
return (countag > 0);
}
public boolean isShared() {
- // --------------------------
return (countag + history > 1);
}
public boolean isVersioned() {
- // -----------------------------
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 int getStep() {
/**
* Set the step.
- * @param step the step to set
+ *
+ * @param step
+ * the step to set
*/
- public void setStep(int step) {
+ public void setStep(final int step) {
this.step = step;
}
/**
* Get the did.
+ *
* @return the did
*/
public String getDid() {
/**
* Set the did.
- * @param did the did to set
+ *
+ * @param did
+ * the did to set
*/
- public void setDid(String did) {
+ public void setDid(final String did) {
this.did = did;
}
/**
* Get the myfile.
+ *
* @return the myfile
*/
public File getFile() {
/**
* Set the myfile.
- * @param myfile the myfile to set
+ *
+ * @param myfile
+ * the myfile to set
*/
- public void setFile(File myfile) {
+ public void setFile(final File myfile) {
this.myfile = myfile;
}
/**
* Get the history.
+ *
* @return the history
*/
public int getHistory() {
/**
* Set the history.
- * @param history the history to set
+ *
+ * @param history
+ * the history to set
*/
- public void setHistory(int history) {
+ public void setHistory(final int history) {
this.history = history;
}
+
+ /**
+ * Set the version.
+ *
+ * @param version
+ * the version to set
+ */
+ public void setVersion(final String version) {
+ this.version = version;
+ }
+
+ /**
+ * Set the state.
+ *
+ * @param state
+ * the state to set
+ */
+ public void setProgressState(final 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(final int countag) {
+ this.countag = countag;
+ }
}
\ No newline at end of file