KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerj.daisy.repository.schema.PartType;
20 import org.outerx.daisy.x10.VersionDocument;
21
22 import java.util.Date JavaDoc;
23 import java.util.GregorianCalendar JavaDoc;
24
25 public class VersionImpl implements Version {
26     private long id;
27     private Date JavaDoc created;
28     private long creator=-1;
29     private String JavaDoc documentName;
30     private PartImpl[] parts;
31     private FieldImpl[] fields;
32     private LinkImpl[] links;
33     private VersionState state;
34     private long totalSizeOfParts;
35     private long stateLastModifier=-1;
36     private Date JavaDoc stateLastModified;
37     private DocumentVariantImpl.IntimateAccess ownerVariantInt;
38     private IntimateAccess intimateAccess = new IntimateAccess();
39
40     public VersionImpl(DocumentVariantImpl.IntimateAccess ownerVariantInt, long id, String JavaDoc documentName, Date JavaDoc created,
41             long creator, VersionState versionState, Date JavaDoc versionStateLastModified, long versionStateLastModifier,
42             long totalSizeOfParts) {
43         this.ownerVariantInt = ownerVariantInt;
44         this.id = id;
45         this.documentName = documentName;
46         this.created = created;
47         this.creator = creator;
48         this.state = versionState;
49         this.totalSizeOfParts = totalSizeOfParts;
50         this.stateLastModified = versionStateLastModified;
51         this.stateLastModifier = versionStateLastModifier;
52     }
53
54     public VersionImpl(DocumentVariantImpl.IntimateAccess ownerVariantInt, long id, Date JavaDoc created, long creator,
55             String JavaDoc documentName, PartImpl[] parts, FieldImpl[] fields, LinkImpl[] links,
56             VersionState versionState, Date JavaDoc versionStateLastModified, long versionStateLastModifier, long totalSizeOfParts) {
57         this.ownerVariantInt = ownerVariantInt;
58         this.id = id;
59         this.documentName = documentName;
60         this.parts = parts;
61         this.fields = fields;
62         this.links = links;
63         this.created = created;
64         this.creator = creator;
65         this.state = versionState;
66         this.stateLastModified = versionStateLastModified;
67         this.stateLastModifier = versionStateLastModifier;
68         this.totalSizeOfParts = totalSizeOfParts;
69     }
70
71     public VersionImpl.IntimateAccess getIntimateAccess(DocumentStrategy documentStrategy) {
72         if (this.ownerVariantInt.getDocumentStrategy() == documentStrategy)
73             return intimateAccess;
74         else
75             return null;
76     }
77
78     public long getId() {
79         return id;
80     }
81
82     public String JavaDoc getDocumentName() {
83         return documentName;
84     }
85
86     public Date JavaDoc getCreated() {
87         return (Date JavaDoc)created.clone();
88     }
89
90     public long getCreator() {
91         return creator;
92     }
93
94     public Parts getParts() throws RepositoryException {
95         lazyLoadCheck();
96         return new PartsImpl(parts);
97     }
98
99     public Parts getPartsInOrder() {
100         lazyLoadCheck();
101         return new PartsImpl(ownerVariantInt.orderParts(parts));
102     }
103
104     public Part getPart(long typeId) {
105         lazyLoadCheck();
106         for (int i = 0; i < parts.length; i++) {
107             if (parts[i].getTypeId() == typeId)
108                 return parts[i];
109         }
110         throw new PartNotFoundException(typeId);
111     }
112
113     public Part getPart(String JavaDoc typeName) {
114         PartType partType = null;
115         try {
116             partType = ownerVariantInt.getRepositorySchema().getPartTypeByName(typeName, false, ownerVariantInt.getCurrentUser());
117         } catch (RepositoryException e) {
118             throw new RuntimeException JavaDoc(DocumentImpl.ERROR_ACCESSING_REPOSITORY_SCHEMA, e);
119         }
120         return getPart(partType.getId());
121     }
122
123     public boolean hasPart(long typeId) {
124         lazyLoadCheck();
125         for (int i = 0; i < parts.length; i++) {
126             if (parts[i].getTypeId() == typeId)
127                 return true;
128         }
129         return false;
130     }
131
132     public boolean hasPart(String JavaDoc typeName) {
133         lazyLoadCheck();
134         long partTypeId;
135         try {
136             partTypeId = ownerVariantInt.getRepositorySchema().getPartTypeByName(typeName, false, ownerVariantInt.getCurrentUser()).getId();
137         } catch (RepositoryException e) {
138             throw new RuntimeException JavaDoc(DocumentImpl.ERROR_ACCESSING_REPOSITORY_SCHEMA, e);
139         }
140         return hasPart(partTypeId);
141     }
142
143     public Fields getFields() {
144         lazyLoadCheck();
145         return new FieldsImpl(fields);
146     }
147
148     public Fields getFieldsInOrder() throws RepositoryException {
149         lazyLoadCheck();
150         return new FieldsImpl(ownerVariantInt.orderFields(fields));
151     }
152
153     public Field getField(long fieldTypeId) throws FieldNotFoundException {
154         lazyLoadCheck();
155         for (int i = 0; i < fields.length; i++) {
156             if (fields[i].getTypeId() == fieldTypeId)
157                 return fields[i];
158         }
159         throw new FieldNotFoundException(fieldTypeId);
160     }
161
162     public boolean hasField(long fieldTypeId) {
163         lazyLoadCheck();
164         for (int i = 0; i < fields.length; i++) {
165             if (fields[i].getTypeId() == fieldTypeId)
166                 return true;
167         }
168         return false;
169     }
170
171     public Links getLinks() {
172         lazyLoadCheck();
173         return new LinksImpl(links);
174     }
175
176     private final synchronized void lazyLoadCheck() {
177         if (parts != null)
178             return;
179
180         try {
181             ownerVariantInt.getDocumentStrategy().completeVersion(ownerVariantInt.getVariant(), this);
182         } catch (Exception JavaDoc e) {
183             throw new RuntimeException JavaDoc("Error lazy-loading version data for version " + id + " of document " + ownerVariantInt.getDocument().getId() + ".");
184         }
185     }
186
187     public VersionDocument getShallowXml() {
188         VersionDocument versionDocument = VersionDocument.Factory.newInstance();
189         VersionDocument.Version versionXml = versionDocument.addNewVersion();
190         versionXml.setId(id);
191         versionXml.setDocumentName(documentName);
192         GregorianCalendar JavaDoc createdCalendar = new GregorianCalendar JavaDoc();
193         createdCalendar.setTime(created);
194         versionXml.setCreated(createdCalendar);
195         versionXml.setCreator(creator);
196         versionXml.setState(state.toString());
197         GregorianCalendar JavaDoc stateLastModifiedCalendar = new GregorianCalendar JavaDoc();
198         stateLastModifiedCalendar.setTime(stateLastModified);
199         versionXml.setStateLastModified(stateLastModifiedCalendar);
200         versionXml.setStateLastModifier(stateLastModifier);
201         versionXml.setTotalSizeOfParts(totalSizeOfParts);
202         return versionDocument;
203     }
204
205     public VersionDocument getXml() throws RepositoryException {
206         lazyLoadCheck();
207
208         VersionDocument versionDocument = getShallowXml();
209         VersionDocument.Version versionXml = versionDocument.getVersion();
210
211         versionXml.setFields(getFieldsInOrder().getXml().getFields());
212         versionXml.setParts(getPartsInOrder().getXml().getParts());
213         versionXml.setLinks(getLinks().getXml().getLinks());
214
215         return versionDocument;
216     }
217
218     public void setState(VersionState state) throws RepositoryException {
219         if (ownerVariantInt.getDocument().isReadOnly())
220             throw new RuntimeException JavaDoc("This Version object is read-only.");
221
222         if (this.state == state)
223             return;
224
225         ownerVariantInt.getDocumentStrategy().setVersionState(ownerVariantInt.getDocument(), this, state);
226     }
227
228     public VersionState getState() {
229         return state;
230     }
231
232     public long getStateLastModifier() {
233         return stateLastModifier;
234     }
235
236     public Date JavaDoc getStateLastModified() {
237         return (Date JavaDoc)stateLastModified.clone();
238     }
239
240     public long getTotalSizeOfParts() {
241         return totalSizeOfParts;
242     }
243
244     public class IntimateAccess {
245         private IntimateAccess() {
246         }
247
248         public void stateChanged(VersionState versionState, Date JavaDoc lastModfied, long lastModifier) {
249             VersionImpl.this.state = versionState;
250             VersionImpl.this.stateLastModified = lastModfied;
251             VersionImpl.this.stateLastModifier = lastModifier;
252         }
253
254         public void setParts(PartImpl[] parts) {
255             VersionImpl.this.parts = parts;
256         }
257
258         public void setFields(FieldImpl[] fields) {
259             VersionImpl.this.fields = fields;
260         }
261
262         public void setLinks(LinkImpl[] links) {
263             VersionImpl.this.links = links;
264         }
265
266         public PartImpl[] getPartImpls() {
267             lazyLoadCheck();
268             return parts;
269         }
270     }
271 }
272
Popular Tags