comparison gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DefaultCollection.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-client/src/main/java/org/dive4elements/river/client/shared/model/DefaultCollection.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.shared.model;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9
10 /**
11 * The default implementation of a {@link Collection}.
12 *
13 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
14 */
15 public class DefaultCollection implements Collection {
16
17 /** The uuid of the collection. */
18 protected String uuid;
19
20 /** The name of the collection. */
21 protected String name;
22
23 /** The creation time of this collection. */
24 protected Date creation;
25
26 /**
27 * The time to live of the collection.
28 * If this value is 0, it will never die.
29 */
30 protected long ttl;
31
32 /** The list of artifacts that are managed by this Collection.*/
33 protected List<CollectionItem> items;
34
35 protected List<Recommendation> recommendations;
36
37 /**
38 * ThemeList by outputmode name.
39 */
40 protected Map<String, ThemeList> themeLists;
41
42 /**
43 * Settings by outputmode name.
44 */
45 protected Map<String, Settings> settings;
46
47 /**
48 * Constructor without arguments is necessary for GWT.
49 */
50 public DefaultCollection() {
51 }
52
53
54 public DefaultCollection(String uuid, long ttl, String name) {
55 this.uuid = uuid;
56 this.ttl = ttl;
57 this.name = name;
58 this.items = new ArrayList<CollectionItem>();
59 this.themeLists = new HashMap<String, ThemeList>();
60 this.recommendations = new ArrayList<Recommendation>();
61 this.settings = new HashMap<String, Settings>();
62 }
63
64
65 /**
66 * Creates a new DefaultCollection with a UUID.
67 *
68 * @param uuid The UUID.
69 */
70 public DefaultCollection(
71 String uuid,
72 long ttl,
73 String name,
74 List<Recommendation> recs
75 ) {
76 this(uuid, ttl, name);
77
78 this.recommendations = recs;
79 }
80
81
82 public DefaultCollection(
83 String uuid,
84 long ttl,
85 String name,
86 List<Recommendation> recommendations,
87 Map<String, ThemeList> themeLists)
88 {
89 this(uuid, ttl, name, recommendations);
90 this.themeLists = themeLists;
91 }
92
93
94 public DefaultCollection(
95 String uuid,
96 long ttl,
97 String name,
98 List<Recommendation> recommendations,
99 Map<String, ThemeList> themeLists,
100 Map<String, Settings> settings)
101 {
102 this(uuid, ttl, name, recommendations);
103 this.themeLists = themeLists;
104 this.settings = settings;
105 }
106
107
108 /**
109 * Creates a new DefaultCollection with uuid and name.
110 *
111 * @param uuid The identifier of this collection.
112 * @param name The name of this collection.
113 * @param creation The creation time.
114 */
115 public DefaultCollection(String uuid, long ttl, String name, Date creation){
116 this(uuid, ttl, name);
117
118 this.creation = creation;
119 }
120
121
122 public String identifier() {
123 return uuid;
124 }
125
126
127 public Date getCreationTime() {
128 return creation;
129 }
130
131
132 /**
133 * Returns now.
134 * TODO candidate for removal?
135 */
136 public Date getLastAccess() {
137 return new Date();
138 }
139
140
141 public long getTTL() {
142 return ttl;
143 }
144
145
146 public void setTTL(long ttl) {
147 this.ttl = ttl;
148 }
149
150
151 public String getName() {
152 return name;
153 }
154
155
156 public void setName(String name) {
157 this.name = name;
158 }
159
160
161 public void addItem(CollectionItem item) {
162 if (item != null) {
163 items.add(item);
164 }
165 }
166
167
168 public int getItemLength() {
169 return items.size();
170 }
171
172
173 /** Returns item at index (0-based), or null if out of range. */
174 public CollectionItem getItem(int idx) {
175 if (idx >= getItemLength()) {
176 return null;
177 }
178
179 return items.get(idx);
180 }
181
182
183 /**
184 * Get item whose identifier is the given string.
185 * @param uuid identifier of collection item (artifacts uuid).
186 * @return CollectionItem whose identifier is given String, null if not found.
187 */
188 public CollectionItem getItem(String uuid) {
189 int size = getItemLength();
190 for (int i = 0; i < size; i++) {
191 CollectionItem item = getItem(i);
192 if (item.identifier().equals(uuid)) {
193 return item;
194 }
195 }
196 return null;
197 }
198
199
200 public Map<String, OutputMode> getOutputModes() {
201 Map<String, OutputMode> modes = new HashMap<String, OutputMode>();
202
203 for (CollectionItem item: items) {
204 List<OutputMode> itemModes = item.getOutputModes();
205
206 if (itemModes != null) {
207 for (OutputMode itemMode: itemModes) {
208 String name = itemMode.getName();
209 if (!modes.containsKey(name)) {
210 // we dont want duplicated OutputModes in our result.
211 modes.put(name, itemMode);
212 }
213 }
214 }
215 }
216
217 return modes;
218 }
219
220
221 /**
222 * Returns ThemeList for given output name.
223 */
224 public ThemeList getThemeList(String outName) {
225 if (themeLists != null) {
226 return themeLists.get(outName);
227 }
228
229 return null;
230 }
231
232
233 /**
234 * Returns Settings for given output name.
235 */
236 public Settings getSettings(String outName) {
237 if (settings != null) {
238 return settings.get(outName);
239 }
240
241 return null;
242 }
243
244
245 public void setSettings(Map<String, Settings> settings) {
246 this.settings = settings;
247 }
248
249
250 public void addSettings(String outname, Settings settings) {
251 if (this.settings == null) {
252 this.settings = new HashMap<String, Settings>();
253 }
254 this.settings.put(outname, settings);
255 }
256
257
258 /** Set the outputname to themelist map. */
259 public void setThemeLists(Map<String, ThemeList> map) {
260 this.themeLists = map;
261 }
262
263
264 public List<Recommendation> getRecommendations() {
265 return recommendations;
266 }
267
268
269 public void addRecommendation(Recommendation recommendation) {
270 recommendations.add(recommendation);
271 }
272
273
274 public void addRecommendations(List<Recommendation> recommendations) {
275 this.recommendations.addAll(recommendations);
276 }
277
278
279 public boolean loadedRecommendation(Recommendation recommendation) {
280 String factory = recommendation.getFactory();
281 String dbids = recommendation.getIDs();
282
283 for (Recommendation in: recommendations) {
284 String inFactory = in.getFactory();
285 String inDbids = in.getIDs();
286
287 if (factory.equals(inFactory) && dbids.equals(inDbids)) {
288 return true;
289 }
290 }
291
292 return false;
293 }
294
295
296 @Override
297 public boolean hasItems() {
298 return items.isEmpty();
299 }
300
301 /**
302 * Returns the name of the collection or uuid if no name is set
303 */
304 @Override
305 public String getDisplayName() {
306 if (this.name != null) {
307 return this.name;
308 }
309 return this.uuid;
310 }
311 }
312 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org