Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollection.java @ 119:3bb121d5b0b7
Added a default implementation of an ArtifactCollection and a User.
artifacts/trunk@1342 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 01 Mar 2011 15:58:09 +0000 |
parents | |
children | db0d20440b92 |
comparison
equal
deleted
inserted
replaced
118:0e0c27bc0b90 | 119:3bb121d5b0b7 |
---|---|
1 /* | |
2 * Copyright (c) 2011 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 package de.intevation.artifactdatabase; | |
9 | |
10 import java.io.IOException; | |
11 import java.io.OutputStream; | |
12 import java.util.ArrayList; | |
13 import java.util.Date; | |
14 import java.util.HashMap; | |
15 import java.util.List; | |
16 import java.util.Map; | |
17 | |
18 import org.apache.log4j.Logger; | |
19 | |
20 import org.w3c.dom.Document; | |
21 | |
22 import de.intevation.artifacts.Artifact; | |
23 import de.intevation.artifacts.ArtifactCollection; | |
24 import de.intevation.artifacts.ArtifactCollectionFactory; | |
25 import de.intevation.artifacts.CallContext; | |
26 import de.intevation.artifacts.User; | |
27 | |
28 | |
29 /** | |
30 * Trivial implementation of an artifact collection. Useful to be subclassed. | |
31 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
32 */ | |
33 public class DefaultArtifactCollection | |
34 implements ArtifactCollection | |
35 { | |
36 /** The logger used in this class.*/ | |
37 private static Logger logger = | |
38 Logger.getLogger(DefaultArtifactCollection.class); | |
39 | |
40 /** | |
41 * The identifier of the collection. | |
42 */ | |
43 protected String identifier; | |
44 | |
45 /** | |
46 * The owner of this collection. | |
47 */ | |
48 protected User user; | |
49 | |
50 /** | |
51 * The artifacts stored in this collection. | |
52 */ | |
53 protected List<Artifact> artifacts; | |
54 | |
55 /** | |
56 * The attributes used for the artifacts stored in this collection. The key | |
57 * of this map represents the identifier of the artifact which the attribute | |
58 * belong to. | |
59 */ | |
60 protected Map<String, Document> attributes; | |
61 | |
62 /** The creation time of this collection.*/ | |
63 protected Date creationTime; | |
64 | |
65 | |
66 /** | |
67 * Default constructor. | |
68 */ | |
69 public DefaultArtifactCollection() { | |
70 } | |
71 | |
72 | |
73 /** | |
74 * When created by a factory this method is called to | |
75 * initialize the collection. | |
76 * @param identifier The identifier from collection database | |
77 * @param factory The factory which created this collection. | |
78 * @param context The global context of the runtime system. | |
79 * @param data The data which can be use to setup a collection with | |
80 * more details. | |
81 */ | |
82 public void setup( | |
83 String identifier, | |
84 ArtifactCollectionFactory factory, | |
85 Object context, | |
86 Document data) | |
87 { | |
88 logger.debug("DefaultArtifactCollection.setup: " + identifier); | |
89 | |
90 artifacts = new ArrayList<Artifact>(); | |
91 attributes = new HashMap<String, Document>(); | |
92 | |
93 setIdentifier(identifier); | |
94 } | |
95 | |
96 | |
97 /** | |
98 * Set a new identifier for this collection. | |
99 * @param identifier New identifier for this collection. | |
100 */ | |
101 public void setIdentifier(String identifier) { | |
102 this.identifier = identifier; | |
103 } | |
104 | |
105 | |
106 /** | |
107 * Identify this collection. | |
108 * @return Returns unique string to identify this collection globally. | |
109 */ | |
110 public String identifier() { | |
111 return identifier; | |
112 } | |
113 | |
114 | |
115 /** | |
116 * Set a new owner of this collection. | |
117 * @param user New owner for this collection. | |
118 */ | |
119 public void setUser(User user) { | |
120 this.user = user; | |
121 } | |
122 | |
123 | |
124 /** | |
125 * Identify the owner of the collection. | |
126 * @return Returns owner of the collection. | |
127 */ | |
128 public User getUser() { | |
129 return user; | |
130 } | |
131 | |
132 | |
133 /** | |
134 * Returns the creation time of the collection. | |
135 * | |
136 * @return the creation time of the collection. | |
137 */ | |
138 public Date getCreationTime() { | |
139 return creationTime; | |
140 } | |
141 | |
142 | |
143 /** | |
144 * Sets the creation time of the collection. | |
145 * | |
146 * @param creationTime The new creation time. | |
147 */ | |
148 public void setCreationTime(Date creationTime) { | |
149 this.creationTime = creationTime; | |
150 } | |
151 | |
152 | |
153 /** | |
154 * Called from artifact database when an artifact is | |
155 * going to be removed from system. | |
156 * @param context The global context of the runtime system. | |
157 */ | |
158 public void endOfLife(Object context) { | |
159 logger.debug("DefaultArtifactCollection.endOfLife"); | |
160 } | |
161 | |
162 | |
163 /** | |
164 * Internal hash of this collection. | |
165 * @return Returns hash that should stay the same if the internal | |
166 * value has not changed. Useful for caching | |
167 */ | |
168 public String hash() { | |
169 logger.debug("DefaultArtifactCollection.hash"); | |
170 | |
171 return String.valueOf(hashCode()); | |
172 } | |
173 | |
174 | |
175 /** | |
176 * Called from artifact database before an artifact is | |
177 * going to be exported as xml document. | |
178 * @param context The global context of the runtime system. | |
179 */ | |
180 public void cleanup(Object context) { | |
181 logger.debug("DefaultArtifactCollection.cleanup"); | |
182 } | |
183 | |
184 | |
185 /** | |
186 * Adds a new artifact to this collection. | |
187 * | |
188 * @param artifact The new artifact. | |
189 * @param attribute The attributes used for this artifact. | |
190 * @param context The CallContext. | |
191 */ | |
192 public void addArtifact( | |
193 Artifact artifact, | |
194 Document attribute, | |
195 CallContext context) | |
196 { | |
197 logger.debug("DefaultArtifactCollection.addArtifact"); | |
198 | |
199 artifacts.add(artifact); | |
200 attributes.put(artifact.identifier(), attribute); | |
201 } | |
202 | |
203 | |
204 /** | |
205 * Removes the given artifact from this collection. | |
206 * | |
207 * @param artifact The artifact that should be removed. | |
208 * @param context The CallContext. | |
209 */ | |
210 public void removeArtifact(Artifact artifact, CallContext context) { | |
211 logger.debug("DefaultArtifactCollection.removeArtifact"); | |
212 | |
213 if (artifact == null) { | |
214 return; | |
215 } | |
216 | |
217 artifacts.remove(artifact); | |
218 attributes.remove(artifact.identifier()); | |
219 } | |
220 | |
221 | |
222 /** | |
223 * Returns a list of artifacts that are stored in this collection. | |
224 * | |
225 * @param context The CallContext. | |
226 * | |
227 * @return the list of artifacts stored in this collection. | |
228 */ | |
229 public Artifact[] getArtifacts(CallContext context) { | |
230 logger.debug("DefaultArtifactCollection.getArtifacts"); | |
231 | |
232 return (Artifact[]) artifacts.toArray(); | |
233 } | |
234 | |
235 | |
236 /** | |
237 * Returns the attribute document for the given artifact. | |
238 * | |
239 * @param artifact The artifact. | |
240 * @param context The CallContext. | |
241 * | |
242 * @return a document that contains the attributes of the artifact. | |
243 */ | |
244 public Document getAttribute(Artifact artifact, CallContext context) { | |
245 logger.debug("DefaultArtifactCollection.getAttribute"); | |
246 | |
247 return attributes.get(artifact.identifier()); | |
248 } | |
249 | |
250 | |
251 /** | |
252 * Set the attribute for the given artifact. | |
253 * | |
254 * @param artifact The artifact of the attribute. | |
255 * @param document The new attribute of the artifact. | |
256 * @param context The CallContext. | |
257 */ | |
258 public void setAttribute( | |
259 Artifact artifact, | |
260 Document document, | |
261 CallContext context) | |
262 { | |
263 logger.debug("DefaultArtifactCollection.setAttribute"); | |
264 | |
265 attributes.put(artifact.identifier(), document); | |
266 } | |
267 | |
268 | |
269 /** | |
270 * Produce output for this collection. | |
271 * @param format Specifies the format of the output. | |
272 * @param out Stream to write the result data to. | |
273 * @param context The global context of the runtime system. | |
274 * @throws IOException Thrown if an I/O occurs. | |
275 */ | |
276 public void out(Document format, OutputStream out, CallContext context) | |
277 throws IOException | |
278 { | |
279 logger.debug("DefaultArtifactCollection.out"); | |
280 } | |
281 } | |
282 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |