KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > DocumentImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.commonimpl;
17
18 import org.outerj.daisy.repository.*;
19 import org.outerx.daisy.x10.DocumentDocument;
20
21 import java.util.*;
22
23 /**
24  * Implementation of the Document interface.
25  *
26  * <p>This document implementation depends on a {@link DocumentStrategy} which allows
27  * the persistence logic for the document to be pluggable.
28  *
29  * <p>Please note that all methods in this class that are not present in the Document interface,
30  * including public methods, are considered to be <b>*for internal use only*</b> and hence should
31  * never be called by "end users".
32  */

33 public class DocumentImpl implements Document, DocumentWrapper {
34     private DocumentStrategy documentStrategy;
35     private CommonRepository repository;
36     private AuthenticatedUser currentUser;
37     private long id = -1;
38     private Date lastModified;
39     private long lastModifier = -1;
40     private Date created;
41     private long owner;
42     private boolean _private = false;
43     private boolean readOnly = false;
44     /** Tracks if any properties of the document have been changed (not of the document variant) */
45     private boolean changes = false;
46     private long updateCount = 0;
47     private DocumentVariantImpl variant;
48     private IntimateAccess intimateAccess = new IntimateAccess();
49
50     public static final String JavaDoc ERROR_ACCESSING_REPOSITORY_SCHEMA = "Error accessing repository schema information.";
51     private static final String JavaDoc READ_ONLY_MESSAGE = "This document object is read-only.";
52
53     public DocumentImpl(DocumentStrategy documentStrategy, CommonRepository repository, AuthenticatedUser currentUser, long documentTypeId, long branchId, long languageId) {
54         this.documentStrategy = documentStrategy;
55         this.repository = repository;
56         this.currentUser = currentUser;
57         this.variant = new DocumentVariantImpl(this, documentStrategy, repository.getRepositorySchema(), currentUser, documentTypeId, branchId, languageId);
58         this.owner = currentUser.getId(); // initialiase in case of a new document, otherwise will be overwritten later
59
}
60
61     public IntimateAccess getIntimateAccess(DocumentStrategy documentStrategy) {
62         if (this.documentStrategy == documentStrategy)
63             return intimateAccess;
64         else
65             return null;
66     }
67
68     public DocumentImpl getWrappedDocument(DocumentStrategy strategy) {
69         return this;
70     }
71
72     public boolean canReadLiveOnly() {
73         return false;
74     }
75
76     public long getId() {
77         return id;
78     }
79
80     public boolean isNew() {
81         return id == -1;
82     }
83
84     public long getBranchId() {
85         return variant.getBranchId();
86     }
87
88     public long getLanguageId() {
89         return variant.getLanguageId();
90     }
91
92     public VariantKey getVariantKey() {
93         return new VariantKey(getId(), getBranchId(), getLanguageId());
94     }
95
96     public boolean isVariantNew() {
97         return variant.isNew();
98     }
99
100     public AvailableVariants getAvailableVariants() throws RepositoryException {
101         if (this.id != -1) {
102             return repository.getAvailableVariants(this.id, currentUser);
103         }
104         return new AvailableVariantsImpl(new AvailableVariantImpl[0]);
105     }
106
107     public long getDocumentTypeId() {
108         return variant.getDocumentTypeId();
109     }
110
111     public void changeDocumentType(long documentTypeId) throws RepositoryException {
112         variant.changeDocumentType(documentTypeId);
113     }
114
115     public void changeDocumentType(String JavaDoc documentTypeName) throws RepositoryException {
116         variant.changeDocumentType(documentTypeName);
117     }
118
119     public long getOwner() {
120         return owner;
121     }
122
123     public void setOwner(long userId) {
124         if (readOnly)
125             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
126
127         if (userId == owner)
128             return;
129
130         if (!currentUser.isInAdministratorRole() && currentUser.getId() != owner)
131             throw new RepositoryRuntimeException("The owner of a document can only be changed by the current owner or users acting in the Administrator role.");
132
133         this.owner = userId;
134         this.changes = true;
135     }
136
137     public boolean isPrivate() {
138         return _private;
139     }
140
141     public void setPrivate(boolean _private) {
142         if (readOnly)
143             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
144
145         this._private = _private;
146         this.changes = true;
147     }
148
149     public Field getField(String JavaDoc name) throws FieldNotFoundException {
150         return variant.getField(name);
151     }
152
153     public Field getField(long fieldTypeId) throws FieldNotFoundException {
154         return variant.getField(fieldTypeId);
155     }
156
157     public boolean hasField(long fieldTypeId) {
158         return variant.hasField(fieldTypeId);
159     }
160
161     public boolean hasField(String JavaDoc fieldTypeName) {
162         return variant.hasField(fieldTypeName);
163     }
164
165     public Fields getFields() {
166         return variant.getFields();
167     }
168
169     public Fields getFieldsInOrder() {
170         return variant.getFieldsInOrder();
171     }
172
173     public void setField(String JavaDoc name, Object JavaDoc value) throws DocumentTypeInconsistencyException {
174         variant.setField(name, value);
175     }
176
177     public void setField(long fieldTypeId, Object JavaDoc value) throws DocumentTypeInconsistencyException {
178         variant.setField(fieldTypeId, value);
179     }
180
181     public void deleteField(String JavaDoc name) {
182         variant.deleteField(name);
183     }
184
185     public void deleteField(long fieldTypeId) {
186         variant.deleteField(fieldTypeId);
187     }
188
189     public LockInfo getLockInfo(boolean fresh) throws RepositoryException {
190         return variant.getLockInfo(fresh);
191     }
192
193     // This method is by purpose not in the Document interface. It is used to clear lock
194
// info in cached document objects. If it would be in the interface, then the method
195
// getLockInfo should cover the case where lockInfo is null and id is -1.
196
public void clearLockInfo() {
197         variant.clearLockInfo();
198     }
199
200     public boolean lock(long duration, LockType lockType) throws RepositoryException {
201         return variant.lock(duration, lockType);
202     }
203
204     public boolean releaseLock() throws RepositoryException {
205         return variant.releaseLock();
206     }
207
208     public DocumentDocument getXml() throws RepositoryException {
209         DocumentDocument documentDocument = getXmlWithoutVariant();
210         DocumentDocument.Document documentXml = documentDocument.getDocument();
211         variant.addXml(documentXml);
212         return documentDocument;
213     }
214
215     public DocumentDocument getXml(long versionId) throws RepositoryException {
216         DocumentDocument documentDocument = getXmlWithoutVariant();
217         DocumentDocument.Document documentXml = documentDocument.getDocument();
218         variant.addXml(documentXml, versionId);
219         return documentDocument;
220     }
221
222     public DocumentDocument getXmlWithoutVersionedData() throws RepositoryException {
223         DocumentDocument documentDocument = getXmlWithoutVariant();
224         DocumentDocument.Document documentXml = documentDocument.getDocument();
225         variant.addNonVersionedDataToXml(documentXml);
226         return documentDocument;
227     }
228
229     public DocumentDocument getXmlWithoutVariant() {
230         DocumentDocument documentDocument = DocumentDocument.Factory.newInstance();
231         DocumentDocument.Document documentXml = documentDocument.addNewDocument();
232
233         if (id != -1) {
234             documentXml.setId(id);
235             GregorianCalendar lastModified = new GregorianCalendar();
236             lastModified.setTime(this.lastModified);
237             documentXml.setLastModified(lastModified);
238             documentXml.setLastModifier(lastModifier);
239             GregorianCalendar created = new GregorianCalendar();
240             created.setTime(this.created);
241             documentXml.setCreated(created);
242         }
243
244         documentXml.setOwner(owner);
245         documentXml.setPrivate(_private);
246         documentXml.setUpdateCount(updateCount);
247
248         return documentDocument;
249     }
250
251     public void setName(String JavaDoc name) {
252         variant.setName(name);
253     }
254
255     public String JavaDoc getName() {
256         return variant.getName();
257     }
258
259     public void setPart(String JavaDoc partTypeName, String JavaDoc mimeType, byte[] data) throws DocumentTypeInconsistencyException {
260         variant.setPart(partTypeName, mimeType, data);
261     }
262
263     public void setPart(long partTypeId, String JavaDoc mimeType, byte[] data) throws DocumentTypeInconsistencyException {
264         variant.setPart(partTypeId, mimeType, data);
265     }
266
267     public void setPart(String JavaDoc partTypeName, String JavaDoc mimeType, PartDataSource partDataSource) throws DocumentTypeInconsistencyException {
268         variant.setPart(partTypeName, mimeType, partDataSource);
269     }
270
271     public void setPart(long partTypeId, String JavaDoc mimeType, PartDataSource partDataSource) throws DocumentTypeInconsistencyException {
272         variant.setPart(partTypeId, mimeType, partDataSource);
273     }
274
275     public void setPartFileName(String JavaDoc partTypeName, String JavaDoc fileName) {
276         variant.setPartFileName(partTypeName, fileName);
277     }
278
279     public void setPartFileName(long partTypeId, String JavaDoc fileName) {
280         variant.setPartFileName(partTypeId, fileName);
281     }
282
283     public void setPartMimeType(String JavaDoc partTypeName, String JavaDoc mimeType) {
284         variant.setPartMimeType(partTypeName, mimeType);
285     }
286
287     public void setPartMimeType(long partTypeId, String JavaDoc mimeType) {
288         variant.setPartMimeType(partTypeId, mimeType);
289     }
290
291     public Parts getParts() {
292         return variant.getParts();
293     }
294
295     public Parts getPartsInOrder() {
296         return variant.getPartsInOrder();
297     }
298
299     public void deletePart(long partTypeId) {
300         variant.deletePart(partTypeId);
301     }
302
303     public void deletePart(String JavaDoc name) {
304         variant.deletePart(name);
305     }
306
307     public Part getPart(long partTypeId) throws PartNotFoundException {
308         return variant.getPart(partTypeId);
309     }
310
311     public Part getPart(String JavaDoc name) throws PartNotFoundException {
312         return variant.getPart(name);
313     }
314
315     public boolean hasPart(long partTypeId) {
316         return variant.hasPart(partTypeId);
317     }
318
319     public boolean hasPart(String JavaDoc name) {
320         return variant.hasPart(name);
321     }
322
323     public void setCustomField(String JavaDoc name, String JavaDoc value) {
324         variant.setCustomField(name, value);
325     }
326
327     public void deleteCustomField(String JavaDoc name) {
328         variant.deleteCustomField(name);
329     }
330
331     public void clearCustomFields() {
332         variant.clearCustomFields();
333     }
334     
335     public void clearCollections() {
336         variant.clearCollections();
337     }
338
339     public Map getCustomFields() {
340         return variant.getCustomFields();
341     }
342
343     public String JavaDoc getCustomField(String JavaDoc name) {
344         return variant.getCustomField(name);
345     }
346
347     public boolean hasCustomField(String JavaDoc name) {
348         return variant.hasCustomField(name);
349     }
350
351     public Links getLinks() {
352         return variant.getLinks();
353     }
354
355     public void addLink(String JavaDoc title, String JavaDoc target) {
356         variant.addLink(title, target);
357     }
358
359     public void deleteLink(int index) {
360         variant.deleteLink(index);
361     }
362
363     public void clearLinks() {
364         variant.clearLinks();
365     }
366
367     public void save() throws RepositoryException, DocumentTypeInconsistencyException {
368         save(true);
369     }
370
371     public void save(boolean validate) throws RepositoryException {
372         if (readOnly)
373             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
374
375         // first check if the document needs saving at all
376
if (!isNew() && !needsSaving() && !variant.isNew() && !variant.needsSaving())
377             return;
378
379         if (validate)
380             validate();
381
382         variant.setValidateOnSave(validate);
383         documentStrategy.store(this);
384     }
385
386     public void validate() throws DocumentTypeInconsistencyException {
387         variant.validate();
388     }
389
390     public void setNewVersionState(VersionState versionState) {
391         variant.setNewVersionState(versionState);
392     }
393
394     public VersionState getNewVersionState() {
395         return variant.getNewVersionState();
396     }
397
398     public Version getVersion(long versionId) throws RepositoryException {
399         return variant.getVersion(versionId);
400     }
401
402     public Version getLastVersion() throws RepositoryException {
403         return variant.getLastVersion();
404     }
405
406     public Version getLiveVersion() throws RepositoryException {
407         return variant.getLiveVersion();
408     }
409
410     public long getLiveVersionId() {
411         return variant.getLiveVersionId();
412     }
413
414     public Versions getVersions() throws RepositoryException {
415         return variant.getVersions();
416     }
417
418     public long getLastVersionId() {
419         return variant.getLastVersionId();
420     }
421
422     public Date getLastModified() {
423         if (lastModified != null)
424             return (Date)lastModified.clone();
425         return lastModified;
426     }
427
428     public long getLastModifier() {
429         return lastModifier;
430     }
431
432     public Date getVariantLastModified() {
433         return variant.getLastModified();
434     }
435
436     public long getVariantLastModifier() {
437         return variant.getLastModifier();
438     }
439
440     public Date getCreated() {
441         return created;
442     }
443
444     public boolean isRetired() {
445         return variant.isRetired();
446     }
447
448     public void setRetired(boolean retired) {
449         variant.setRetired(retired);
450     }
451
452     public DocumentCollections getCollections() {
453         return variant.getCollections();
454     }
455
456     public boolean inCollection(DocumentCollection collection) {
457         return variant.inCollection(collection);
458     }
459
460     public boolean inCollection(long collectionId) {
461         return variant.inCollection(collectionId);
462     }
463
464     public void addToCollection(DocumentCollection collection) {
465         variant.addToCollection(collection);
466     }
467
468     public void removeFromCollection(DocumentCollection collection) {
469         variant.removeFromCollection(collection);
470     }
471
472     public String JavaDoc getSummary() {
473         return variant.getSummary();
474     }
475
476     public long getVariantCreatedFromBranchId() {
477         return variant.getCreatedFromBranchId();
478     }
479
480     public long getVariantCreatedFromLanguageId() {
481         return variant.getCreatedFromLanguageId();
482     }
483
484     public long getVariantCreatedFromVersionId() {
485         return variant.getCreatedFromVersionId();
486     }
487
488     public long getUpdateCount() {
489         return updateCount;
490     }
491
492     public long getVariantUpdateCount() {
493         return variant.getUpdateCount();
494     }
495
496     public void makeReadOnly() {
497         this.readOnly = true;
498     }
499
500     public boolean isReadOnly() {
501         return this.readOnly;
502     }
503
504     public boolean needsSaving() {
505         return changes;
506     }
507
508     public String JavaDoc toString() {
509         return getVariantKey().toString();
510     }
511
512     public class IntimateAccess {
513
514         private IntimateAccess() {
515         }
516
517         /**
518          * Updates the state of this Document object after saving it, also resets
519          * all 'dirty' flags.
520          *
521          * @param created can be null if not modified (only required after first save)
522          */

523         public void saved(long documentId, Date lastModified, Date created, long updateCount) {
524             DocumentImpl.this.id = documentId;
525             DocumentImpl.this.lastModified = lastModified;
526             DocumentImpl.this.lastModifier = currentUser.getId();
527             DocumentImpl.this.updateCount = updateCount;
528             DocumentImpl.this.created = created;
529             DocumentImpl.this.changes = false;
530         }
531
532
533         public void setCreated(Date created) {
534             DocumentImpl.this.created = created;
535         }
536
537         public AuthenticatedUser getCurrentUser() {
538             return currentUser;
539         }
540
541         public DocumentImpl getDocument() {
542             return DocumentImpl.this;
543         }
544
545         public DocumentVariantImpl getVariant() {
546             return variant;
547         }
548
549         /**
550          * Intialises the document object as when loading an existing document.
551          */

552         public void load(long id, Date lastModified, long lastModifier, Date created, long owner, boolean _private, long updateCount) {
553             DocumentImpl.this.id = id;
554             DocumentImpl.this.lastModified = lastModified;
555             DocumentImpl.this.lastModifier = lastModifier;
556             DocumentImpl.this.created = created;
557             DocumentImpl.this.owner = owner;
558             DocumentImpl.this._private = _private;
559             DocumentImpl.this.updateCount = updateCount;
560         }
561     }
562
563
564 }
565
Popular Tags