--- /dev/null
+package org.splat.dal.bo.som;
+/**
+ *
+ * @author Daniel Brunier-Coulin
+ * @copyright OPEN CASCADE 2012
+ */
+
+import java.util.Calendar;
+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.LinkedList;
+import java.util.Set;
+import java.util.Vector;
+
+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.dao.som.Database;
+import org.splat.kernel.MultiplyDefinedException;
+import org.splat.kernel.InvalidPropertyException;
+import org.splat.kernel.MissedPropertyException;
+import org.splat.kernel.UserDirectory;
+import org.splat.service.StepService;
+import org.splat.service.technical.IndexServiceImpl;
+import org.splat.service.technical.ProjectSettingsServiceImpl;
+import org.splat.service.technical.ProjectSettingsServiceImpl.ProjectSettingsValidationCycle;
+import org.splat.som.Revision;
+
+
+public class Study extends ProjectElement {
+
+// Persistent fields
+ private String sid; // External unique reference in a format conform to the configuration pattern
+ private int docount; // Total number of documents of this study, including versions
+ private ProgressState state;
+ private Visibility visibility;
+ private List<Scenario> scenarii;
+ private String version;
+ private int history; // Number of studies versioning this one, if any
+
+// Transient fields
+ private List<User> contributor; // Shortcut to contributors
+ private HashMap<String,ValidationCycle> validactor; // Shortcut to validation cycles
+ private Set<User> actor; // Summary of above actors
+ private StepService _stepService;
+
+// ==============================================================================================================================
+// Construction
+// ==============================================================================================================================
+
+// Fields initialization class
+ public static class Properties extends Persistent.Properties {
+// ------------------------------------------------------------
+ private String sid = null; // Search criterion only
+ private String title = null;
+ private String summary = null;
+ private User manager = null;
+ private User actor = null; // Search criterion only
+ private Visibility visibility = null; // Search criterion only
+ private ProgressState state = null; // Search criterion only
+ private Date date = null;
+ private List<SimulationContext> context = new Vector<SimulationContext>(); // Search criterion only
+
+// - Public services
+
+ public void clear () {
+ super.clear();
+ sid = null;
+ title = null;
+ summary = null;
+ manager = null;
+ actor = null;
+ visibility = null;
+ state = null;
+ date = null;
+ context = new Vector<SimulationContext>(); // as clear() may generate side effects
+ }
+ public Properties copy () {
+ Properties copy = new Properties();
+ copy.sid = this.sid;
+ copy.title = this.title;
+ copy.summary = this.summary;
+ copy.manager = this.manager;
+ copy.actor = this.actor;
+ copy.visibility = this.visibility;
+ copy.state = this.state;
+ copy.date = this.date;
+ copy.context = this.context;
+ return copy;
+ }
+// - Protected services
+
+ public User getActor () {
+ return actor;
+ }
+ public User getManager () {
+ return manager;
+ }
+ public ProgressState getProgressState () {
+ return state;
+ }
+ public String getReference () {
+ return sid;
+ }
+ public List<SimulationContext> getSimulationContexts () {
+ return context;
+ }
+ public String getTitle () {
+ return title;
+ }
+ public String getSummary () {
+ return summary;
+ }
+ public Visibility getVisibility () {
+ return visibility;
+ }
+// - Property setters
+
+// For building a search query
+ public Properties setActor (User actor)
+ {
+ this.actor = actor;
+ return this;
+ }
+ public Properties setDate (Date date)
+ {
+ this.date = date;
+ return this;
+ }
+ public Properties setDescription (String summary)
+ {
+ if (summary.length() > 0) this.summary = summary;
+ return this;
+ }
+ public Properties setManager (User user)
+ {
+ this.manager = user;
+ return this;
+ }
+// For building a search query
+ public Properties setReference (String sid) throws InvalidPropertyException
+ {
+ if (sid.length() == 0) throw new InvalidPropertyException("reference");
+ this.sid = sid;
+ return this;
+ }
+// For building a search query
+ public Properties setSimulationContexts (List<SimulationContext> context) {
+ this.context = context;
+ return this;
+ }
+// For building a search query
+ public Properties setState (ProgressState state)
+ {
+ this.state = state;
+ return this;
+ }
+ public Properties setTitle (String title) throws InvalidPropertyException
+ {
+ if (title.length() == 0) throw new InvalidPropertyException("title");
+ this.title = title;
+ return this;
+ }
+// For building a search query
+ public Properties setVisibility (Visibility area)
+ {
+ this.visibility = area;
+ return this;
+ }
+// - Global validity check
+
+ public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
+ {
+ if (title == null) throw new MissedPropertyException("title");
+ if (manager == null) throw new MissedPropertyException("manager");
+ }
+ }
+// Database fetch constructor
+ protected Study () {
+// ------------------
+ contributor = null;
+ validactor = null;
+ actor = null;
+ }
+// Internal constructor
+ public Study (Properties sprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
+// ----------------------------------
+ super(sprop); // Throws one of the above exception if not valid
+ sid = sprop.sid; // Reset after save
+ title = sprop.title; // Inherited attribute
+ manager = sprop.manager;
+ docount = 0;
+ history = 0;
+ scenarii = new LinkedList<Scenario>();
+ visibility = Visibility.PRIVATE;
+ state = ProgressState.inWORK;
+
+ credate = sprop.date; // Inherited attribute
+ if (credate == null) {
+ Calendar current = Calendar.getInstance();
+ credate = current.getTime(); // Today
+ }
+ lasdate = credate; // Inherited attribute
+ version = new Revision().incrementAs(state).toString();
+
+ if (sprop.summary != null) this.setAttribute( new DescriptionAttribute(this, sprop.summary) );
+
+ contributor = null;
+ validactor = null;
+ actor = null;
+ }
+
+ /**
+ * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
+ *
+ * @return the actors of this study
+ * @see #hasActor(User)
+ */
+ public Set<User> getActors () {
+ // -----------------------------
+ if (actor == null) setShortCuts();
+ return Collections.unmodifiableSet(actor);
+ }
+
+ /**
+ * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
+ *
+ * @return the actors of this study
+ * @see #hasActor(User)
+ */
+ public Set<User> getModifiableActors () {
+ // -----------------------------
+ if (actor == null) setShortCuts();
+ return actor;
+ }
+
+ public List<User> getContributors () {
+ // ------------------------------------
+ if (contributor == null) setShortCuts();
+ return Collections.unmodifiableList(contributor); // May be empty
+ }
+
+ public List<User> getModifiableContributors () {
+ if (contributor == null) setShortCuts();
+ return contributor; // May be empty
+ }
+
+ public ProgressState getProgressState () {
+// ----------------------------------------
+ return state;
+ }
+
+/**
+ * Returns the global unique reference of this study.
+ * The study reference is common to all versions of the study (versioning a study does not change its reference).
+ * The form of this study reference is defined in the configuration of the application server - see the SOM XML customization
+ * file.
+ */
+ public String getReference () {
+ return sid;
+ }
+
+ public void setReference (String aReference) {
+ sid = aReference;
+ }
+
+ public Scenario[] getScenarii () {
+// --------------------------------
+ return scenarii.toArray(new Scenario[scenarii.size()]);
+ }
+
+ public List<Scenario> getScenariiList () {
+// --------------------------------
+ return scenarii;
+ }
+
+/**
+ * Returns the validation cycle of the given document type.
+ *
+ * @param doc the document type being subject of validation
+ * @return the validation cycle of the document, or null if not defined.
+ */
+ public ValidationCycle getValidationCycleOf (DocumentType type) {
+// ---------------------------------------------------------------
+ if (validactor == null) setShortCuts();
+ ValidationCycle result = validactor.get(type.getName());
+ if (result == null) {
+ if (type.isStepResult()) result = validactor.get("default"); // "default" validation cycle defined in the configuration, if exist
+ if (result == null) result = validactor.get("built-in");
+ }
+ return result;
+ }
+
+ public String getVersion () {
+// ---------------------------
+ return version;
+ }
+
+ public Visibility getVisibility () {
+// ----------------------------------
+ return visibility;
+ }
+
+/**
+ * Checks if the given user is actor of this study.
+ * Actors include contributors, reviewers and approvers.
+ *
+ * @return true if the given user is actor of this study.
+ * @see #getActors()
+ */
+ public boolean hasActor (User user) {
+// -----------------------------------
+ if (user == null) return false;
+ for (Iterator<User> i=this.getActors().iterator(); i.hasNext(); ) {
+ User involved = i.next();
+ if (involved.equals(user)) return true;
+ }
+ return false;
+ }
+
+/**
+ * Checks whether this study is in the Public or the Reference area of the repository.
+ *
+ * @return true if the study is public.
+ * @see #moveToPublic()
+ * @see #moveToReference()
+ */
+ public boolean isPublic () {
+// --------------------------
+ return (visibility != Visibility.PRIVATE);
+ }
+/**
+ * Checks if the given user participates to this study.
+ * The Study staff includes the author and contributors.
+ *
+ * @return true if the given user is actor of this study.
+ * @see #getContributors()
+ */
+ public boolean isStaffedBy (User user) {
+// --------------------------------------
+ if (user == null) return false;
+ if (manager.equals(user)) return true;
+ for (Iterator<User> i=getContributors().iterator(); i.hasNext();) {
+ if (i.next().equals(user)) return true;
+ }
+ return false;
+ }
+
+ public boolean isVersioned () {
+// -----------------------------
+ return (history > 0);
+ }
+
+ public boolean shares (Document doc) {
+// ------------------------------------
+ Scenario[] scene = this.getScenarii(); // If shared from within the study, the document is shared by the scenarios
+ int counter = 0;
+
+ for (int i=0; i<scene.length; i++) {
+ if (!scene[i].publishes(doc)) continue;
+ if (counter == 1) return true;
+ counter += 1;
+ }
+ return false;
+ }
+
+ public int getLastLocalIndex () {
+// ----------------------------------
+ return docount;
+ }
+
+ public void loadWorkflow () {
+// ------------------------------
+ setShortCuts();
+ }
+
+ public void setShortCuts () {
+// ----------------------------
+ contributor = new Vector<User>();
+ validactor = new HashMap<String,ValidationCycle>();
+ actor = new HashSet<User>();
+
+// Get the contributors
+ for (Iterator<Relation> i=getRelations(ContributorRelation.class).iterator(); i.hasNext(); ) {
+ ContributorRelation link = (ContributorRelation)i.next();
+ contributor.add(link.getTo());
+ }
+// Get the validation cycles specific to this study
+ for (Iterator<Relation> i=getRelations(ValidationCycleRelation.class).iterator(); i.hasNext(); ) {
+ ValidationCycleRelation link = (ValidationCycleRelation)i.next();
+ validactor.put(link.getDocumentType().getName(), link.getTo()); // The associated document type is necessarily not null in this context
+ }
+// Get the validation cycles coming from the configured workflow and not overridden in this study
+ for (Iterator<ProjectSettingsServiceImpl.ProjectSettingsValidationCycle> i=ProjectSettingsServiceImpl.getAllValidationCycles().iterator(); i.hasNext(); ) {
+ ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle = i.next();
+ String type = cycle.getName();
+ if (!validactor.containsKey(type)) validactor.put(type, new ValidationCycle(this, cycle));
+ }
+// Get all corresponding actors
+ for (Iterator<ValidationCycle> i=validactor.values().iterator(); i.hasNext(); ) {
+ ValidationCycle cycle = i.next();
+ User[] user = cycle.getAllActors();
+ for (int j=0; j<user.length; j++) actor.add(user[j]);
+ }
+// Get all other actors
+ for (Iterator<Relation> i=this.getAllRelations().iterator(); i.hasNext(); ) {
+ Relation link = i.next();
+ Class<?> kindof = link.getClass().getSuperclass();
+ if (!kindof.equals(ActorRelation.class)) continue;
+ actor.add( ((ActorRelation)link).getTo() );
+ }
+ }
+ /**
+ * @param aVisibility a study visibility to set
+ */
+ public void setVisibility(Visibility aVisibility) {
+ visibility = aVisibility;
+ }
+ /**
+ * @param aState a study progress state to set
+ */
+ public void setProgressState(ProgressState aState) {
+ state = aState;
+ }
+ /**
+ * @param string
+ */
+ public void setVersion(String aVersion) {
+ version = aVersion;
+ }
+ /**
+ * @param i
+ */
+ public void setLastLocalIndex(int anIndex) {
+ docount = anIndex;
+ }
+ /**
+ * @return
+ */
+ public HashMap<String, ValidationCycle> getValidationCycles() {
+ return validactor;
+ }
+}
\ No newline at end of file