ingo@119: /*
ingo@119:  * Copyright (c) 2011 by Intevation GmbH
ingo@119:  *
ingo@119:  * This program is free software under the LGPL (>=v2.1)
ingo@119:  * Read the file LGPL.txt coming with the software for details
ingo@119:  * or visit http://www.gnu.org/licenses/ if it does not exist.
ingo@119:  */
ingo@119: package de.intevation.artifactdatabase;
ingo@119: 
ingo@119: import java.io.IOException;
ingo@119: import java.io.OutputStream;
ingo@119: import java.util.ArrayList;
ingo@119: import java.util.Date;
ingo@119: import java.util.HashMap;
ingo@119: import java.util.List;
ingo@119: import java.util.Map;
ingo@119: 
ingo@119: import org.apache.log4j.Logger;
ingo@119: 
ingo@119: import org.w3c.dom.Document;
ingo@119: 
ingo@119: import de.intevation.artifacts.Artifact;
ingo@119: import de.intevation.artifacts.ArtifactCollection;
ingo@119: import de.intevation.artifacts.ArtifactCollectionFactory;
ingo@119: import de.intevation.artifacts.CallContext;
ingo@119: import de.intevation.artifacts.User;
ingo@119: 
ingo@119: 
ingo@119: /**
ingo@119:  * Trivial implementation of an artifact collection. Useful to be subclassed.
ingo@119:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@119:  */
ingo@119: public class DefaultArtifactCollection
ingo@119: implements   ArtifactCollection
ingo@119: {
ingo@119:     /** The logger used in this class.*/
ingo@119:     private static Logger logger =
ingo@119:         Logger.getLogger(DefaultArtifactCollection.class);
ingo@119: 
ingo@119:     /**
ingo@119:      * The identifier of the collection.
ingo@119:      */
ingo@119:     protected String identifier;
ingo@119: 
ingo@119:     /**
sascha@159:      * The identifier of the collection.
sascha@159:      */
sascha@159:     protected String name;
sascha@159: 
sascha@159:     /**
ingo@119:      * The owner of this collection.
ingo@119:      */
ingo@119:     protected User user;
ingo@119: 
ingo@119:     /**
ingo@119:      * The artifacts stored in this collection.
ingo@119:      */
ingo@119:     protected List<Artifact> artifacts;
ingo@119: 
ingo@119:     /**
ingo@119:      * The attributes used for the artifacts stored in this collection. The key
ingo@119:      * of this map represents the identifier of the artifact which the attribute
ingo@119:      * belong to.
ingo@119:      */
ingo@119:     protected Map<String, Document> attributes;
ingo@119: 
ingo@119:     /** The creation time of this collection.*/
ingo@119:     protected Date creationTime;
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Default constructor.
ingo@119:      */
ingo@119:     public DefaultArtifactCollection() {
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * When created by a factory this method is called to
ingo@119:      * initialize the collection.
ingo@119:      * @param identifier The identifier from collection database
ingo@119:      * @param factory    The factory which created this collection.
ingo@119:      * @param context    The global context of the runtime system.
ingo@119:      * @param data       The data which can be use to setup a collection with
ingo@119:      *                   more details.
ingo@119:      */
ingo@119:     public void setup(
ingo@119:         String                    identifier,
sascha@170:         String                    name,
sascha@170:         Date                      creationTime,
ingo@119:         ArtifactCollectionFactory factory,
ingo@119:         Object                    context,
ingo@119:         Document                  data)
ingo@119:     {
ingo@119:         logger.debug("DefaultArtifactCollection.setup: " + identifier);
ingo@119: 
ingo@119:         artifacts  = new ArrayList<Artifact>();
ingo@119:         attributes = new HashMap<String, Document>();
ingo@119: 
ingo@119:         setIdentifier(identifier);
sascha@170:         setName(name);
sascha@170:         setCreationTime(creationTime);
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Set a new identifier for this collection.
ingo@119:      * @param identifier New identifier for this collection.
ingo@119:      */
ingo@119:     public void setIdentifier(String identifier) {
ingo@119:         this.identifier = identifier;
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Identify this collection.
ingo@119:      * @return Returns unique string to identify this collection globally.
ingo@119:      */
ingo@119:     public String identifier() {
ingo@119:         return identifier;
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
sascha@159:      * Name of this collection.
sascha@159:      * @return Returns the name of this collection
sascha@159:      */
sascha@159:     public String getName() {
sascha@159:         return name;
sascha@159:     }
sascha@159: 
sascha@159:     /**
sascha@159:      * Name of this collection.
sascha@159:      * @return Returns the name of this collection
sascha@159:      */
sascha@159:     public void setName(String name) {
sascha@159:         this.name = name;
sascha@159:     }
sascha@159: 
sascha@159: 
sascha@159:     /**
ingo@119:      * Set a new owner of this collection.
ingo@119:      * @param user New owner for this collection.
ingo@119:      */
ingo@119:     public void setUser(User user) {
ingo@119:         this.user = user;
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Identify the owner of the collection.
ingo@119:      * @return Returns owner of the collection.
ingo@119:      */
ingo@119:     public User getUser() {
ingo@119:         return user;
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Returns the creation time of the collection.
ingo@119:      *
ingo@119:      * @return the creation time of the collection.
ingo@119:      */
ingo@119:     public Date getCreationTime() {
ingo@119:         return creationTime;
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Sets the creation time of the collection.
ingo@119:      *
ingo@119:      * @param creationTime The new creation time.
ingo@119:      */
ingo@119:     public void setCreationTime(Date creationTime) {
ingo@119:         this.creationTime = creationTime;
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Called from artifact database when an artifact is
ingo@119:      * going to be removed from system.
ingo@119:      * @param context The global context of the runtime system.
ingo@119:      */
ingo@119:     public void endOfLife(Object context) {
ingo@119:         logger.debug("DefaultArtifactCollection.endOfLife");
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Internal hash of this collection.
ingo@119:      * @return Returns hash that should stay the same if the internal
ingo@119:      *         value has not changed. Useful for caching
ingo@119:      */
ingo@119:     public String hash() {
ingo@119:         logger.debug("DefaultArtifactCollection.hash");
ingo@119: 
ingo@119:         return String.valueOf(hashCode());
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Called from artifact database before an artifact is
ingo@119:      * going to be exported as xml document.
ingo@119:      * @param context The global context of the runtime system.
ingo@119:      */
ingo@119:     public void cleanup(Object context) {
ingo@119:         logger.debug("DefaultArtifactCollection.cleanup");
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Adds a new artifact to this collection.
ingo@119:      *
ingo@119:      * @param artifact The new artifact.
ingo@119:      * @param attribute The attributes used for this artifact.
ingo@119:      * @param context The CallContext.
ingo@119:      */
ingo@119:     public void addArtifact(
ingo@119:         Artifact    artifact,
ingo@119:         Document    attribute,
ingo@119:         CallContext context)
ingo@119:     {
ingo@119:         logger.debug("DefaultArtifactCollection.addArtifact");
ingo@119: 
ingo@119:         artifacts.add(artifact);
ingo@119:         attributes.put(artifact.identifier(), attribute);
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Removes the given artifact from this collection.
ingo@119:      *
ingo@119:      * @param artifact The artifact that should be removed.
ingo@119:      * @param context The CallContext.
ingo@119:      */
ingo@119:     public void removeArtifact(Artifact artifact, CallContext context) {
ingo@119:         logger.debug("DefaultArtifactCollection.removeArtifact");
ingo@119: 
ingo@119:         if (artifact == null) {
ingo@119:             return;
ingo@119:         }
ingo@119: 
ingo@119:         artifacts.remove(artifact);
ingo@119:         attributes.remove(artifact.identifier());
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Returns a list of artifacts that are stored in this collection.
ingo@119:      *
ingo@119:      * @param context The CallContext.
ingo@119:      *
ingo@119:      * @return the list of artifacts stored in this collection.
ingo@119:      */
ingo@119:     public Artifact[] getArtifacts(CallContext context) {
ingo@119:         logger.debug("DefaultArtifactCollection.getArtifacts");
ingo@119: 
ingo@119:         return (Artifact[]) artifacts.toArray();
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Returns the attribute document for the given artifact.
ingo@119:      *
ingo@119:      * @param artifact The artifact.
ingo@119:      * @param context The CallContext.
ingo@119:      *
ingo@119:      * @return a document that contains the attributes of the artifact.
ingo@119:      */
ingo@119:     public Document getAttribute(Artifact artifact, CallContext context) {
ingo@119:         logger.debug("DefaultArtifactCollection.getAttribute");
ingo@119: 
ingo@119:         return attributes.get(artifact.identifier());
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Set the attribute for the given artifact.
ingo@119:      *
ingo@119:      * @param artifact The artifact of the attribute.
ingo@119:      * @param document The new attribute of the artifact.
ingo@119:      * @param context The CallContext.
ingo@119:      */
ingo@119:     public void setAttribute(
ingo@119:         Artifact    artifact,
ingo@119:         Document    document,
ingo@119:         CallContext context)
ingo@119:     {
ingo@119:         logger.debug("DefaultArtifactCollection.setAttribute");
ingo@119: 
ingo@119:         attributes.put(artifact.identifier(), document);
ingo@119:     }
ingo@119: 
ingo@119: 
ingo@119:     /**
ingo@119:      * Produce output for this collection.
ingo@119:      * @param format Specifies the format of the output.
ingo@119:      * @param out Stream to write the result data to.
ingo@119:      * @param context The global context of the runtime system.
ingo@119:      * @throws IOException Thrown if an I/O occurs.
ingo@119:      */
ingo@119:     public void out(Document format, OutputStream out, CallContext context)
ingo@119:     throws IOException
ingo@119:     {
ingo@119:         logger.debug("DefaultArtifactCollection.out");
ingo@119:     }
ingo@119: }
ingo@119: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :