1 package org.splat.dal.bo.som;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.util.Calendar;
9 import java.util.Collections;
10 import java.util.Date;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.LinkedList;
17 import java.util.Vector;
19 import org.hibernate.Session;
20 import org.splat.dal.bo.kernel.Persistent;
21 import org.splat.dal.bo.kernel.Relation;
22 import org.splat.dal.bo.kernel.User;
23 import org.splat.dal.dao.som.Database;
24 import org.splat.kernel.MultiplyDefinedException;
25 import org.splat.kernel.InvalidPropertyException;
26 import org.splat.kernel.MissedPropertyException;
27 import org.splat.kernel.UserDirectory;
28 import org.splat.service.StepService;
29 import org.splat.service.technical.IndexServiceImpl;
30 import org.splat.service.technical.ProjectSettingsServiceImpl;
31 import org.splat.service.technical.ProjectSettingsServiceImpl.ProjectSettingsValidationCycle;
32 import org.splat.som.Revision;
35 public class Study extends ProjectElement {
38 private String sid; // External unique reference in a format conform to the configuration pattern
39 private int docount; // Total number of documents of this study, including versions
40 private ProgressState state;
41 private Visibility visibility;
42 private List<Scenario> scenarii;
43 private String version;
44 private int history; // Number of studies versioning this one, if any
47 private List<User> contributor; // Shortcut to contributors
48 private HashMap<String,ValidationCycle> validactor; // Shortcut to validation cycles
49 private Set<User> actor; // Summary of above actors
50 private StepService _stepService;
52 // ==============================================================================================================================
54 // ==============================================================================================================================
56 // Fields initialization class
57 public static class Properties extends Persistent.Properties {
58 // ------------------------------------------------------------
59 private String sid = null; // Search criterion only
60 private String title = null;
61 private String summary = null;
62 private User manager = null;
63 private User actor = null; // Search criterion only
64 private Visibility visibility = null; // Search criterion only
65 private ProgressState state = null; // Search criterion only
66 private Date date = null;
67 private List<SimulationContext> context = new Vector<SimulationContext>(); // Search criterion only
71 public void clear () {
81 context = new Vector<SimulationContext>(); // as clear() may generate side effects
83 public Properties copy () {
84 Properties copy = new Properties();
86 copy.title = this.title;
87 copy.summary = this.summary;
88 copy.manager = this.manager;
89 copy.actor = this.actor;
90 copy.visibility = this.visibility;
91 copy.state = this.state;
92 copy.date = this.date;
93 copy.context = this.context;
96 // - Protected services
98 public User getActor () {
101 public User getManager () {
104 public ProgressState getProgressState () {
107 public String getReference () {
110 public List<SimulationContext> getSimulationContexts () {
113 public String getTitle () {
116 public String getSummary () {
119 public Visibility getVisibility () {
122 // - Property setters
124 // For building a search query
125 public Properties setActor (User actor)
130 public Properties setDate (Date date)
135 public Properties setDescription (String summary)
137 if (summary.length() > 0) this.summary = summary;
140 public Properties setManager (User user)
145 // For building a search query
146 public Properties setReference (String sid) throws InvalidPropertyException
148 if (sid.length() == 0) throw new InvalidPropertyException("reference");
152 // For building a search query
153 public Properties setSimulationContexts (List<SimulationContext> context) {
154 this.context = context;
157 // For building a search query
158 public Properties setState (ProgressState state)
163 public Properties setTitle (String title) throws InvalidPropertyException
165 if (title.length() == 0) throw new InvalidPropertyException("title");
169 // For building a search query
170 public Properties setVisibility (Visibility area)
172 this.visibility = area;
175 // - Global validity check
177 public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
179 if (title == null) throw new MissedPropertyException("title");
180 if (manager == null) throw new MissedPropertyException("manager");
183 // Database fetch constructor
185 // ------------------
190 // Internal constructor
191 public Study (Properties sprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
192 // ----------------------------------
193 super(sprop); // Throws one of the above exception if not valid
194 sid = sprop.sid; // Reset after save
195 title = sprop.title; // Inherited attribute
196 manager = sprop.manager;
199 scenarii = new LinkedList<Scenario>();
200 visibility = Visibility.PRIVATE;
201 state = ProgressState.inWORK;
203 credate = sprop.date; // Inherited attribute
204 if (credate == null) {
205 Calendar current = Calendar.getInstance();
206 credate = current.getTime(); // Today
208 lasdate = credate; // Inherited attribute
209 version = new Revision().incrementAs(state).toString();
211 if (sprop.summary != null) this.setAttribute( new DescriptionAttribute(this, sprop.summary) );
219 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
221 * @return the actors of this study
222 * @see #hasActor(User)
224 public Set<User> getActors () {
225 // -----------------------------
226 if (actor == null) setShortCuts();
227 return Collections.unmodifiableSet(actor);
231 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
233 * @return the actors of this study
234 * @see #hasActor(User)
236 public Set<User> getModifiableActors () {
237 // -----------------------------
238 if (actor == null) setShortCuts();
242 public List<User> getContributors () {
243 // ------------------------------------
244 if (contributor == null) setShortCuts();
245 return Collections.unmodifiableList(contributor); // May be empty
248 public List<User> getModifiableContributors () {
249 if (contributor == null) setShortCuts();
250 return contributor; // May be empty
253 public ProgressState getProgressState () {
254 // ----------------------------------------
259 * Returns the global unique reference of this study.
260 * The study reference is common to all versions of the study (versioning a study does not change its reference).
261 * The form of this study reference is defined in the configuration of the application server - see the SOM XML customization
264 public String getReference () {
268 public void setReference (String aReference) {
272 public Scenario[] getScenarii () {
273 // --------------------------------
274 return scenarii.toArray(new Scenario[scenarii.size()]);
277 public List<Scenario> getScenariiList () {
278 // --------------------------------
283 * Returns the validation cycle of the given document type.
285 * @param doc the document type being subject of validation
286 * @return the validation cycle of the document, or null if not defined.
288 public ValidationCycle getValidationCycleOf (DocumentType type) {
289 // ---------------------------------------------------------------
290 if (validactor == null) setShortCuts();
291 ValidationCycle result = validactor.get(type.getName());
292 if (result == null) {
293 if (type.isStepResult()) result = validactor.get("default"); // "default" validation cycle defined in the configuration, if exist
294 if (result == null) result = validactor.get("built-in");
299 public String getVersion () {
300 // ---------------------------
304 public Visibility getVisibility () {
305 // ----------------------------------
310 * Checks if the given user is actor of this study.
311 * Actors include contributors, reviewers and approvers.
313 * @return true if the given user is actor of this study.
316 public boolean hasActor (User user) {
317 // -----------------------------------
318 if (user == null) return false;
319 for (Iterator<User> i=this.getActors().iterator(); i.hasNext(); ) {
320 User involved = i.next();
321 if (involved.equals(user)) return true;
327 * Checks whether this study is in the Public or the Reference area of the repository.
329 * @return true if the study is public.
330 * @see #moveToPublic()
331 * @see #moveToReference()
333 public boolean isPublic () {
334 // --------------------------
335 return (visibility != Visibility.PRIVATE);
338 * Checks if the given user participates to this study.
339 * The Study staff includes the author and contributors.
341 * @return true if the given user is actor of this study.
342 * @see #getContributors()
344 public boolean isStaffedBy (User user) {
345 // --------------------------------------
346 if (user == null) return false;
347 if (manager.equals(user)) return true;
348 for (Iterator<User> i=getContributors().iterator(); i.hasNext();) {
349 if (i.next().equals(user)) return true;
354 public boolean isVersioned () {
355 // -----------------------------
356 return (history > 0);
359 public boolean shares (Document doc) {
360 // ------------------------------------
361 Scenario[] scene = this.getScenarii(); // If shared from within the study, the document is shared by the scenarios
364 for (int i=0; i<scene.length; i++) {
365 if (!scene[i].publishes(doc)) continue;
366 if (counter == 1) return true;
372 public int getLastLocalIndex () {
373 // ----------------------------------
377 public void loadWorkflow () {
378 // ------------------------------
382 public void setShortCuts () {
383 // ----------------------------
384 contributor = new Vector<User>();
385 validactor = new HashMap<String,ValidationCycle>();
386 actor = new HashSet<User>();
388 // Get the contributors
389 for (Iterator<Relation> i=getRelations(ContributorRelation.class).iterator(); i.hasNext(); ) {
390 ContributorRelation link = (ContributorRelation)i.next();
391 contributor.add(link.getTo());
393 // Get the validation cycles specific to this study
394 for (Iterator<Relation> i=getRelations(ValidationCycleRelation.class).iterator(); i.hasNext(); ) {
395 ValidationCycleRelation link = (ValidationCycleRelation)i.next();
396 validactor.put(link.getDocumentType().getName(), link.getTo()); // The associated document type is necessarily not null in this context
398 // Get the validation cycles coming from the configured workflow and not overridden in this study
399 for (Iterator<ProjectSettingsServiceImpl.ProjectSettingsValidationCycle> i=ProjectSettingsServiceImpl.getAllValidationCycles().iterator(); i.hasNext(); ) {
400 ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle = i.next();
401 String type = cycle.getName();
402 if (!validactor.containsKey(type)) validactor.put(type, new ValidationCycle(this, cycle));
404 // Get all corresponding actors
405 for (Iterator<ValidationCycle> i=validactor.values().iterator(); i.hasNext(); ) {
406 ValidationCycle cycle = i.next();
407 User[] user = cycle.getAllActors();
408 for (int j=0; j<user.length; j++) actor.add(user[j]);
410 // Get all other actors
411 for (Iterator<Relation> i=this.getAllRelations().iterator(); i.hasNext(); ) {
412 Relation link = i.next();
413 Class<?> kindof = link.getClass().getSuperclass();
414 if (!kindof.equals(ActorRelation.class)) continue;
415 actor.add( ((ActorRelation)link).getTo() );
419 * @param aVisibility a study visibility to set
421 public void setVisibility(Visibility aVisibility) {
422 visibility = aVisibility;
425 * @param aState a study progress state to set
427 public void setProgressState(ProgressState aState) {
433 public void setVersion(String aVersion) {
439 public void setLastLocalIndex(int anIndex) {
445 public HashMap<String, ValidationCycle> getValidationCycles() {