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;
sascha@207:
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@218: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@218:
ingo@119:
ingo@119: /**
ingo@119: * Trivial implementation of an artifact collection. Useful to be subclassed.
ingo@119: * @author Ingo Weinzierl
ingo@119: */
ingo@119: public class DefaultArtifactCollection
ingo@119: implements ArtifactCollection
ingo@119: {
felix@338: /** The logger used in this class. */
ingo@119: private static Logger logger =
ingo@119: Logger.getLogger(DefaultArtifactCollection.class);
ingo@119:
felix@338: /** The identifier of the collection. */
ingo@119: protected String identifier;
ingo@119:
felix@338: /** The identifier of the collection. */
sascha@159: protected String name;
sascha@159:
felix@338: /** The owner of this collection. */
ingo@119: protected User user;
ingo@119:
felix@338: /** The attribute of this collection. */
ingo@221: protected Document attribute;
ingo@221:
felix@338: /** The artifacts stored in this collection. */
ingo@119: protected List 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 attributes;
ingo@119:
ingo@119: /** The creation time of this collection.*/
ingo@119: protected Date creationTime;
ingo@119:
ingo@281: protected long ttl;
ingo@281:
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@281: long ttl,
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();
ingo@119: attributes = new HashMap();
ingo@119:
ingo@119: setIdentifier(identifier);
sascha@170: setName(name);
sascha@170: setCreationTime(creationTime);
ingo@281: setTTL(ttl);
ingo@221: setAttribute(data);
ingo@119: }
ingo@119:
ingo@119:
ingo@218: public Document describe(CallContext context) {
ingo@218: logger.debug("DefaultArtifactCollection.describe: " + identifier);
ingo@218:
ingo@218: return XMLUtils.newDocument();
ingo@218: }
ingo@218:
ingo@218:
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@281: public long getTTL() {
ingo@281: return ttl;
ingo@281: }
ingo@281:
ingo@281:
ingo@281: public void setTTL(long ttl) {
ingo@281: this.ttl = ttl;
ingo@281: }
ingo@281:
ingo@281:
ingo@119: /**
ingo@221: * Returns the attribute of the collection.
ingo@221: *
ingo@221: * @return the attribute of the collection.
ingo@221: */
ingo@221: public Document getAttribute() {
ingo@221: return attribute;
ingo@221: }
ingo@221:
ingo@221:
ingo@221: /**
ingo@221: * Sets the attribute of the collection.
ingo@221: *
ingo@221: * @param attribute The attribute of this collection.
ingo@221: */
ingo@221: public void setAttribute(Document attribute) {
ingo@221: this.attribute = attribute;
ingo@221: }
ingo@221:
ingo@221:
ingo@221: /**
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@269: * @param type Specifies the output type.
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@269: public void out(
ingo@269: String type,
ingo@269: Document format,
ingo@269: OutputStream out,
ingo@269: 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 :