KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Date JavaDoc;
18 import java.util.GregorianCalendar JavaDoc;
19
20 import org.outerj.daisy.repository.DocumentCollection;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerx.daisy.x10.CollectionDocument;
23
24 /**
25  * A <b>user aware</b> Collection implementation.
26  * In order to use this awareness, a client needs of course
27  * to be aware of the fact that the Collections being used
28  * are in fact CollectionImpl object.
29  *
30  * The extra methods in this interface that are not present
31  * in the Collection interface are meant for internal use
32  * and <b>not for client use</b>!
33  */

34 public class DocumentCollectionImpl implements DocumentCollection, Comparable JavaDoc {
35     private final AuthenticatedUser requestingUser;
36     private String JavaDoc name;
37     private Date JavaDoc lastModified;
38     private long lastModifier=-1;
39     private boolean readOnly = false;
40     private long updateCount = 0;
41     private static final String JavaDoc READ_ONLY_MESSAGE = "This collection is read-only.";
42
43     /* Initialize on -1 until the save method is called.
44      * As soon as the Collection is persisted, this id
45      * can be updated with the actual value. This allows
46      * a user of this class to distinguish between
47      * persisted and not yet persisted Collection objects.
48      *
49      * After the CollectionStrategy has been invoked
50      * in order to persist the Collection, this
51      * CollectionStrategy itself calls a method of the
52      * Object that was used to invoke the CollectionStrategy
53      * in the first place. Although this concept isn't
54      * formalized yet, this is the general idea.
55      * Also the 'user awareness' will probably only
56      * be used by this CollectionStrategy.
57      *
58      */

59     private long id = -1;
60     private CollectionStrategy collectionStrategy;
61     private IntimateAccess intimateAccess = new IntimateAccess();
62     
63     /**
64      * creates a new CollectionImpl object which is aware
65      * of the user that requested the collection.
66      *
67      * @param collectionStrategy the strategy used to load and store data
68      * @param name the name of the Collection
69      * @param requestingUser the User requesting the collection
70      */

71     public DocumentCollectionImpl(CollectionStrategy collectionStrategy, String JavaDoc name, AuthenticatedUser requestingUser) {
72         this.requestingUser = requestingUser;
73         this.name = name;
74         this.collectionStrategy = collectionStrategy;
75     }
76     
77     public long getId() {
78         return id;
79     }
80
81     public String JavaDoc getName() {
82         return name;
83     }
84
85     public void setName(String JavaDoc collectionName) {
86         if (readOnly)
87             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
88
89         if (collectionName == null)
90             throw new NullPointerException JavaDoc("Collection name should not be null.");
91         
92         name = collectionName;
93     }
94
95     public void save() throws RepositoryException {
96         if (readOnly)
97             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
98
99         collectionStrategy.store(this);
100     }
101
102     /* (non-Javadoc)
103      * @see org.outerj.daisy.repository.Collection#getXml()
104      */

105     public CollectionDocument getXml() {
106         CollectionDocument collectionDocument = CollectionDocument.Factory.newInstance();
107         CollectionDocument.Collection collectionXml = collectionDocument.addNewCollection();
108         collectionXml.setName(name);
109         collectionXml.setUpdatecount(updateCount);
110
111         if (id != -1) {
112             collectionXml.setId(id);
113             GregorianCalendar JavaDoc lastModifiedCalendar = new GregorianCalendar JavaDoc();
114             lastModifiedCalendar.setTime(lastModified);
115             collectionXml.setLastModified(lastModifiedCalendar);
116             collectionXml.setLastModifier(lastModifier);
117         }
118         
119         return collectionDocument;
120     }
121
122     public Date JavaDoc getLastModified() {
123         if (lastModified != null)
124             return (Date JavaDoc)lastModified.clone();
125         else
126             return null;
127     }
128
129     public long getLastModifier() {
130         return lastModifier;
131     }
132     
133     /**
134      * @param strategy the CollectionStrategy requesting intimate access
135      * @return an IntimateAccess object if the CollectionStrategy is the same as the
136      * one supplied when creating the DocumentCollectionImpl object, null if it is
137      * another one.
138      */

139     public IntimateAccess getIntimateAccess(CollectionStrategy strategy) {
140         if (this.collectionStrategy == strategy) {
141             return intimateAccess;
142         }
143         else
144             return null;
145     }
146
147     public long getUpdateCount() {
148         return updateCount;
149     }
150
151     public void makeReadOnly() {
152         this.readOnly = true;
153     }
154
155     public int compareTo(Object JavaDoc o) {
156         DocumentCollection otherCollection = (DocumentCollection)o;
157         return getName().compareTo(otherCollection.getName());
158     }
159
160     /**
161      * a class that provides intimate access to the DocumentCollectionImpl.
162      *
163      * <p>The purpose is to grant setter access to all fields for certain
164      * classes, whilst other (all) classes only have setter access to a limited amount
165      * of fields.
166      */

167     public class IntimateAccess {
168         private IntimateAccess() {}
169
170         public void setId(long id) {
171             if (readOnly)
172                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
173             DocumentCollectionImpl.this.id=id;
174         }
175
176         public void setLastModified(Date JavaDoc d) {
177             if (readOnly)
178                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
179             DocumentCollectionImpl.this.lastModified=d;
180         }
181
182         public void setLastModifier(long lastModifier) {
183             if (readOnly)
184                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
185             DocumentCollectionImpl.this.lastModifier=lastModifier;
186         }
187
188         public AuthenticatedUser getCurrentUser() {
189             return DocumentCollectionImpl.this.requestingUser;
190         }
191
192         public void setUpdateCount(long updateCount) {
193             if (readOnly)
194                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
195             DocumentCollectionImpl.this.updateCount = updateCount;
196         }
197
198         /**
199          * updates the state of the current object after it has been persisted.
200          *
201          * <p>The CollectionStrategy uses this method to update the state of the
202          * current object after it has been persisted.
203          */

204         public void saved (long id, String JavaDoc name, Date JavaDoc lastModified, long lastModifier, long updateCount) {
205             if (readOnly)
206                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
207
208             DocumentCollectionImpl.this.id = id;
209             DocumentCollectionImpl.this.name = name;
210             DocumentCollectionImpl.this.lastModified = lastModified;
211             DocumentCollectionImpl.this.lastModifier = lastModifier;
212             DocumentCollectionImpl.this.updateCount = updateCount;
213         }
214     }
215 }
216
Popular Tags