3 * Class providing services for checking the rights related to operations on a given document.
4 * These rights are partially driven by the validation cycle associated to documents.
7 * @author Daniel Brunier-Coulin
8 * @copyright OPEN CASCADE 2012
10 //TODO: Review this rights in the following contexts:
11 // - Document shared by several scenarios
12 // - Document out-dated following a modification of a document it uses
14 import java.util.Iterator;
15 import java.util.List;
17 import org.splat.kernel.Relation;
18 import org.splat.kernel.User;
21 public class DocumentRights {
24 private Publication operand;
25 private ValidationCycle cycle;
26 private boolean isauthor; // True if the user is author of the document
28 // ==============================================================================================================================
30 // ==============================================================================================================================
32 public DocumentRights (User user, Publication tag) {
33 // --------------------------------------------------
36 this.cycle = operand.getOwnerStudy().getValidationCycleOf(operand.value().getType());
37 this.isauthor = operand.value().getAuthor().equals(user);
38 //TODO: all contributors of the given document (when supported) must also behave as author
40 protected DocumentRights (Publication tag) {
41 // ------------------------------------------
42 this.user = operand.value().getAuthor();
44 this.cycle = operand.getOwnerStudy().getValidationCycleOf(operand.value().getType());
45 this.isauthor = true; // In order to ignore the author state in the context of any user
46 //TODO: all contributors of the given document (when supported) must also behave as author
49 // ==============================================================================================================================
50 // Public member functions
51 // ==============================================================================================================================
54 * Checks if the user has right to accept the modifications of documents depending on the selected document.
55 * This operation applies to out-dated documents following a modification of documents they use.
56 * Only the author of the document has such right.
58 * @return true if the user has right to accept the modifications of dependencies of the document.
59 * @see Publication#accept()
61 public boolean canAccept () {
62 // ---------------------------
63 if (!isauthor) return false;
64 return operand.isOutdated();
68 * Checks if the user has right to approve the selected document.
69 * Only the approver of the type of selected document has such right, providing that the document is candidate for approval and
70 * all document dependencies have already been approved.
72 * @return true if the user has right to approve the document.
73 * @see Publication#approve()
74 * @see ValidationCycle
76 public boolean canApprove () {
77 // ----------------------------
78 User approver = cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
80 if (!user.equals(approver)) return false;
81 if (operand.getProgressState() != ProgressState.inCHECK) return false;
83 List<Relation> use = operand.value().getRelations(UsesRelation.class);
84 for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
85 Document depend = (Document)i.next().getTo();
86 ProgressState state = depend.getProgressState();
87 if (state == ProgressState.EXTERN) continue; // External documents do not follow this progress state
88 if (state != ProgressState.APPROVED) return false;
94 * Checks if the user has right to attach a file to the selected document.
95 * Both, the author, during the elaboration of the document, and the reviewer of the document, during the review process,
98 * @return true if the user has right to attach a file to the document.
99 * @see Publication#attach(String)
100 * @see Publication#attach(String, String)
102 public boolean canAttach () {
103 // ---------------------------
104 User manager = operand.getOwnerStudy().getAuthor();
105 User reviewer = cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
106 ProgressState state = operand.value().getProgressState();
108 if (state == ProgressState.inWORK) return (isauthor);
109 else return (isauthor || user.equals(manager) || user.equals(reviewer));
113 * Checks if the user has right to demote the selected document.
114 * A document can be demoted providing that it is In-Draft or In-Check and all documents using it have previously been demoted.
115 * In-Draft documents can be demoted by default by both, the author of the document and the responsible of study, while
116 * documents in approval process can be demoted by their approver only.
118 * @return true if the user has right to demote the document.
119 * @see #canInvalidate()
121 * @see Publication#demote()
122 * @see ValidationCycle
124 public boolean canDemote () {
125 // ---------------------------
126 User manager = operand.getOwnerStudy().getAuthor();
127 User publisher = cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
128 User approver = cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
129 ProgressState mystate = operand.value().getProgressState();
131 if (mystate == ProgressState.inDRAFT) {
132 if (publisher == null) { if (!isauthor && !user.equals(manager)) return false;
133 } else if (!user.equals(publisher)) return false;
135 if (mystate == ProgressState.inCHECK) {
136 if (!user.equals(approver)) return false;
139 List<Relation> use = operand.value().getRelations(UsedByRelation.class);
140 for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
141 Document depend = (Document)i.next().getTo();
142 ProgressState state = depend.getProgressState();
143 if (mystate == ProgressState.inDRAFT && state != ProgressState.inWORK) return false;
144 if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) return false;
150 * Checks if the user has right to check-out the selected document for editing.
151 * In-Work documents can be checked-out by both, the author of the document and the responsible of study, while
152 * documents In-Draft can be checked-out by the reviewer only.
154 * @return true if the user has right to edit the document.
156 public boolean canEdit () {
157 // -------------------------
158 User manager = operand.getOwnerStudy().getAuthor();
159 User reviewer = cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
160 ProgressState state = operand.value().getProgressState();
162 //TODO: Should be restricted by the application if no editor available
163 if (state == ProgressState.inWORK) {
164 if (isauthor || user.equals(manager)) return true;
166 if (state == ProgressState.inDRAFT) {
167 if (user.equals(reviewer)) return true;
173 * Checks if the user has right to promote the selected document.
174 * A document can be promoted providing that it is In-Work and all its dependencies have previously been promoted.
175 * By default, both the author of the document and the responsible of study has right to promote such document. Otherwise,
176 * only the user involved in the Promotion step of validation cycle of the selected document has such right.
178 * @return true if the user has right to promote the document.
180 * @see Publication#promote()
181 * @see ValidationCycle
183 public boolean canPromote () {
184 // ----------------------------
185 User manager = operand.getOwnerStudy().getAuthor();
186 User publisher = cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
188 if (operand.getProgressState() != ProgressState.inWORK) return false;
189 if (publisher == null) { if (!isauthor && !user.equals(manager)) return false;
190 } else { if (!user.equals(publisher)) return false;
192 List<Relation> use = operand.value().getRelations(UsesRelation.class);
193 for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
194 Document depend = (Document)i.next().getTo();
195 ProgressState state = depend.getProgressState();
196 if (state == ProgressState.EXTERN) continue; // External documents do not follow this progress state
197 if (state == ProgressState.inWORK) return false;
203 * Checks if the user has right to remove the history of the selected document, if exists.
204 * Only the responsible of the study have such right.
206 * @return true if the user has right to purge the document.
208 public boolean canPurge () {
209 // --------------------------
210 User manager = operand.getOwnerStudy().getAuthor();
211 Document doc = operand.value();
213 if (!user.equals(manager)) return false;
214 if (doc.isShared()) return false;
215 if (doc.getFirstRelation(VersionsRelation.class) == null) return false;
220 * Checks if the user has right to remove the selected document from the study.
221 * Both, the author of the document and the responsible of the study, have such right, providing that:
222 * - the document is neither in review or in approval process
223 * - the document is not used by any other document
225 * @return true if the user has right to remove the document.
226 * @see Step#removeDocument(Publication)
228 public boolean canRemove () {
229 // ---------------------------
230 User manager = operand.getOwnerStudy().getAuthor();
231 ProgressState state = operand.getProgressState();
233 if (!isauthor && !user.equals(manager)) return false;
234 if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
236 List<Publication> using = operand.getRelations(UsedByRelation.class);
237 return (using.size() == 0);
241 * Checks if the user has right to rename the selected document.
242 * Only the author of the document has such right, providing that the document is neither in review nor in approval process.
244 * @return true if the user has right to rename the document.
245 * @see Publication#rename(String)
247 public boolean canRename () {
248 // ---------------------------
249 ProgressState state = operand.getProgressState();
251 if (!isauthor) return false; // In case of external document, the author is the one who has imported the document.
252 if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
253 return (!operand.value().isShared());
257 * Checks if the user has right to replace the source file of the selected document.
258 * Both, the author of the document and the responsible of study has such right, providing that the document is neither in review
259 * nor in approval process.
261 * @return true if the user has right to replace the document.
263 public boolean canReplace () {
264 // ----------------------------
265 User manager = operand.getOwnerStudy().getAuthor();
266 ProgressState state = operand.getProgressState();
268 if (!isauthor && !user.equals(manager)) return false; // Supposed to work also in case of external document.
269 if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
270 return !operand.value().isShared();
274 * Checks if the user has right to validate the selected document.
275 * Only the reviewer of the type of selected document has such right, providing that the document is being reviewed and
276 * all document dependencies have already been validated.
278 * @return true if the user has right to validate the document
279 * @see #canUnvalidate()
280 * @see Publication#review()
281 * @see ValidationCycle
283 public boolean canReview () {
284 // ---------------------------
285 User reviewer = cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
287 if (!user.equals(reviewer)) return false;
288 if (operand.getProgressState() != ProgressState.inDRAFT) return false;
290 List<Relation> use = operand.value().getRelations(UsesRelation.class);
291 for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
292 Document depend = (Document)i.next().getTo();
293 ProgressState state = depend.getProgressState();
294 if (state == ProgressState.EXTERN) continue; // External documents do not follow this progress state
295 if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) return false;
301 * Checks if the user has right to undo the validation operation of the selected document.
302 * Both, the author and the reviewer of a validated document, have such right.
304 * @return true if the user has right to undo the validation operation
307 * @see Publication#invalidate()
308 * @see ValidationCycle
310 public boolean canInvalidate () {
311 // -------------------------------
312 User reviewer = cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
313 ProgressState mystate = operand.value().getProgressState();
315 if (mystate != ProgressState.inCHECK) return false;
316 if (!isauthor && !user.equals(reviewer)) return false;
318 List<Relation> use = operand.value().getRelations(UsedByRelation.class);
319 for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
320 Document depend = (Document)i.next().getTo();
321 ProgressState state = depend.getProgressState();
322 if (mystate == ProgressState.inDRAFT && state != ProgressState.inWORK) return false;
323 if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) return false;
329 * Checks if the user has right to version the selected document.
330 * In-Work documents can be versioned by both, the author of the document and the responsible of study, while
331 * documents In-Draft can be versioned by the reviewer only.
332 * Additionally, Approved documents can also be versioned by their author in order to enter in a new modification validation cycle.
334 * @return true if the user has right to version the document.
335 * @see Step#versionDocument(Publication)
336 * @see Step#versionDocument(Publication, Document.Properties)
337 * @see Step#versionDocument(Publication, String)
339 public boolean canVersion () {
340 // ----------------------------
341 User manager = operand.getOwnerStudy().getAuthor();
342 User reviewer = cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
343 ProgressState state = operand.value().getProgressState();
345 if (state == ProgressState.inWORK) {
346 if (isauthor || user.equals(manager)) return true;
348 if (state == ProgressState.inDRAFT) {
349 if (user.equals(reviewer)) return true;
351 if (state == ProgressState.APPROVED) {
352 if (isauthor) return true;
357 // ==============================================================================================================================
359 // ==============================================================================================================================
362 * Returns the document subject of checks according to this user rights.
364 * @return the document subject of checks.
366 public Document getOperand () {
367 // -----------------------------
368 return operand.value();