teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.shared.model; ingo@3: ingo@17: import java.util.ArrayList; ingo@3: import java.util.Date; ingo@67: import java.util.HashMap; ingo@17: import java.util.List; ingo@67: import java.util.Map; ingo@3: ingo@3: ingo@3: /** ingo@3: * The default implementation of a {@link Collection}. ingo@3: * ingo@3: * @author Ingo Weinzierl ingo@3: */ ingo@3: public class DefaultCollection implements Collection { ingo@3: ingo@3: /** The uuid of the collection. */ ingo@3: protected String uuid; ingo@3: felix@1430: /** The name of the collection. */ ingo@95: protected String name; ingo@95: felix@1430: /** The creation time of this collection. */ ingo@95: protected Date creation; ingo@95: felix@1430: /** felix@1430: * The time to live of the collection. felix@1430: * If this value is 0, it will never die. felix@1430: */ ingo@587: protected long ttl; ingo@587: ingo@17: /** The list of artifacts that are managed by this Collection.*/ ingo@67: protected List items; ingo@67: ingo@809: protected List recommendations; ingo@809: felix@1430: /** felix@1430: * ThemeList by outputmode name. felix@1430: */ ingo@524: protected Map themeLists; ingo@524: raimund@1439: /** raimund@1439: * Settings by outputmode name. raimund@1439: */ raimund@1439: protected Map settings; ingo@67: ingo@67: /** ingo@67: * Constructor without arguments is necessary for GWT. ingo@67: */ ingo@67: public DefaultCollection() { ingo@67: } ingo@17: ingo@3: ingo@856: public DefaultCollection(String uuid, long ttl, String name) { felix@1430: this.uuid = uuid; felix@1430: this.ttl = ttl; felix@1430: this.name = name; felix@1430: this.items = new ArrayList(); felix@1430: this.themeLists = new HashMap(); felix@840: this.recommendations = new ArrayList(); raimund@1439: this.settings = new HashMap(); ingo@524: } ingo@524: ingo@524: ingo@809: /** ingo@809: * Creates a new DefaultCollection with a UUID. ingo@809: * ingo@809: * @param uuid The UUID. ingo@809: */ ingo@856: public DefaultCollection( ingo@856: String uuid, ingo@856: long ttl, ingo@856: String name, ingo@856: List recs ingo@856: ) { ingo@856: this(uuid, ttl, name); ingo@809: ingo@809: this.recommendations = recs; ingo@809: } ingo@809: ingo@809: ingo@587: public DefaultCollection( ingo@587: String uuid, ingo@587: long ttl, ingo@856: String name, ingo@809: List recommendations, ingo@587: Map themeLists) ingo@587: { ingo@856: this(uuid, ttl, name, recommendations); felix@840: this.themeLists = themeLists; ingo@3: } ingo@3: ingo@3: raimund@1439: public DefaultCollection( raimund@1439: String uuid, raimund@1439: long ttl, raimund@1439: String name, raimund@1439: List recommendations, raimund@1439: Map themeLists, raimund@1439: Map settings) raimund@1439: { raimund@1439: this(uuid, ttl, name, recommendations); raimund@1439: this.themeLists = themeLists; raimund@1439: this.settings = settings; raimund@1439: } raimund@1439: felix@1544: ingo@95: /** ingo@95: * Creates a new DefaultCollection with uuid and name. ingo@95: * ingo@95: * @param uuid The identifier of this collection. ingo@95: * @param name The name of this collection. ingo@95: * @param creation The creation time. ingo@95: */ ingo@587: public DefaultCollection(String uuid, long ttl, String name, Date creation){ ingo@587: this(uuid, ttl, name); ingo@95: ingo@95: this.creation = creation; ingo@95: } ingo@95: ingo@95: ingo@5: public String identifier() { ingo@5: return uuid; ingo@5: } ingo@5: ingo@5: ingo@95: public Date getCreationTime() { ingo@95: return creation; ingo@95: } ingo@95: ingo@95: felix@1430: /** felix@1430: * Returns now. felix@1430: * TODO candidate for removal? felix@1430: */ ingo@3: public Date getLastAccess() { ingo@3: return new Date(); ingo@3: } ingo@3: ingo@3: ingo@587: public long getTTL() { ingo@587: return ttl; ingo@587: } ingo@587: ingo@587: ingo@597: public void setTTL(long ttl) { ingo@597: this.ttl = ttl; ingo@597: } ingo@597: ingo@597: ingo@3: public String getName() { ingo@95: return name; ingo@3: } ingo@17: ingo@17: ingo@597: public void setName(String name) { ingo@597: this.name = name; ingo@597: } ingo@597: ingo@597: ingo@67: public void addItem(CollectionItem item) { ingo@67: if (item != null) { ingo@67: items.add(item); ingo@17: } ingo@17: } ingo@17: ingo@17: ingo@67: public int getItemLength() { ingo@67: return items.size(); ingo@17: } ingo@17: ingo@17: felix@1544: /** Returns item at index (0-based), or null if out of range. */ ingo@67: public CollectionItem getItem(int idx) { ingo@67: if (idx >= getItemLength()) { ingo@67: return null; ingo@67: } ingo@67: ingo@67: return items.get(idx); ingo@67: } ingo@67: ingo@67: felix@1544: /** felix@1544: * Get item whose identifier is the given string. felix@1544: * @param uuid identifier of collection item (artifacts uuid). felix@1544: * @return CollectionItem whose identifier is given String, null if not found. felix@1544: */ felix@1544: public CollectionItem getItem(String uuid) { felix@1544: int size = getItemLength(); felix@1544: for (int i = 0; i < size; i++) { felix@1544: CollectionItem item = getItem(i); felix@1544: if (item.identifier().equals(uuid)) { felix@1544: return item; felix@1544: } felix@1544: } felix@1544: return null; felix@1544: } felix@1544: felix@1544: ingo@67: public Map getOutputModes() { ingo@67: Map modes = new HashMap(); ingo@67: ingo@67: for (CollectionItem item: items) { ingo@67: List itemModes = item.getOutputModes(); ingo@67: ingo@67: if (itemModes != null) { ingo@67: for (OutputMode itemMode: itemModes) { ingo@67: String name = itemMode.getName(); ingo@67: if (!modes.containsKey(name)) { ingo@67: // we dont want duplicated OutputModes in our result. ingo@67: modes.put(name, itemMode); ingo@67: } ingo@67: } ingo@67: } ingo@67: } ingo@67: ingo@67: return modes; ingo@17: } ingo@524: ingo@524: felix@1430: /** felix@1430: * Returns ThemeList for given output name. felix@1430: */ ingo@524: public ThemeList getThemeList(String outName) { ingo@524: if (themeLists != null) { ingo@524: return themeLists.get(outName); ingo@524: } ingo@524: ingo@524: return null; ingo@524: } ingo@809: ingo@809: raimund@1439: /** raimund@1439: * Returns Settings for given output name. raimund@1439: */ raimund@1439: public Settings getSettings(String outName) { raimund@1439: if (settings != null) { raimund@1439: return settings.get(outName); raimund@1439: } raimund@1439: raimund@1439: return null; raimund@1439: } raimund@1439: raimund@1439: raimund@1439: public void setSettings(Map settings) { raimund@1439: this.settings = settings; raimund@1439: } raimund@1439: raimund@1439: raimund@1439: public void addSettings(String outname, Settings settings) { raimund@1464: if (this.settings == null) { raimund@1464: this.settings = new HashMap(); raimund@1464: } raimund@1439: this.settings.put(outname, settings); raimund@1439: } raimund@1439: raimund@1439: felix@1435: /** Set the outputname to themelist map. */ felix@1435: public void setThemeLists(Map map) { felix@1435: this.themeLists = map; felix@1435: } felix@1435: felix@1435: ingo@809: public List getRecommendations() { ingo@809: return recommendations; ingo@809: } ingo@809: ingo@809: ingo@809: public void addRecommendation(Recommendation recommendation) { ingo@809: recommendations.add(recommendation); ingo@809: } ingo@809: ingo@809: ingo@809: public void addRecommendations(List recommendations) { ingo@809: this.recommendations.addAll(recommendations); ingo@809: } ingo@809: ingo@809: felix@6540: /** felix@6540: * Returns true if a recommendation with given factory and id felix@6540: * is already member of this collection. felix@6540: */ ingo@809: public boolean loadedRecommendation(Recommendation recommendation) { ingo@809: String factory = recommendation.getFactory(); ingo@809: String dbids = recommendation.getIDs(); ingo@809: ingo@809: for (Recommendation in: recommendations) { ingo@809: String inFactory = in.getFactory(); ingo@809: String inDbids = in.getIDs(); ingo@809: ingo@809: if (factory.equals(inFactory) && dbids.equals(inDbids)) { ingo@809: return true; ingo@809: } ingo@809: } ingo@809: ingo@809: return false; ingo@809: } bjoern@4660: bjoern@4660: bjoern@4660: @Override bjoern@4660: public boolean hasItems() { bjoern@4660: return items.isEmpty(); bjoern@4660: } bjoern@4689: bjoern@4689: /** bjoern@4689: * Returns the name of the collection or uuid if no name is set bjoern@4689: */ bjoern@4689: @Override bjoern@4689: public String getDisplayName() { bjoern@4689: if (this.name != null) { bjoern@4689: return this.name; bjoern@4689: } bjoern@4689: return this.uuid; bjoern@4689: } ingo@3: } ingo@3: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :