ingo@219: /*
ingo@219: * Copyright (c) 2010, 2011 by Intevation GmbH
ingo@219: *
ingo@219: * This program is free software under the LGPL (>=v2.1)
ingo@219: * Read the file LGPL.txt coming with the software for details
ingo@219: * or visit http://www.gnu.org/licenses/ if it does not exist.
ingo@219: */
ingo@219: package de.intevation.artifactdatabase;
ingo@219:
ingo@331: import java.util.LinkedList;
ingo@331:
ingo@219: import org.apache.log4j.Logger;
ingo@219:
ingo@219: import de.intevation.artifacts.CallMeta;
ingo@331: import de.intevation.artifacts.Message;
ingo@219:
ingo@219: import de.intevation.artifactdatabase.Backend.PersistentArtifact;
ingo@219:
ingo@219:
ingo@219: /**
ingo@219: * Class that implements the call context handed to the methods calls
ingo@219: * describe(), feed(), etc. of the artifact.
ingo@219: *
ingo@219: * @author Sascha L. Teichmann
ingo@219: */
ingo@219: public class ArtifactCallContext extends AbstractCallContext {
ingo@219:
ingo@219: private static Logger logger = Logger.getLogger(ArtifactCallContext.class);
ingo@219:
ingo@219:
ingo@219: /**
ingo@219: * Error message issued if an artifact wants to translate itself
ingo@219: * into a none valid persistent state.
ingo@219: */
ingo@219: public static final String INVALID_CALL_STATE = "Invalid after call state";
ingo@219:
ingo@219: /**
ingo@219: * Error message issued if one tries to remove a requested artifact
ingo@219: * from the list of artifacts running in background which is
ingo@219: * not in this list.
ingo@219: */
ingo@219: public static final String NOT_IN_BACKGROUND = "Not in background";
ingo@219:
ingo@219:
ingo@219: /**
ingo@219: * The persistence wrapper around the living artifact
ingo@219: */
ingo@219: protected PersistentArtifact artifact;
ingo@219:
ingo@219:
ingo@219: public ArtifactCallContext(
ingo@219: ArtifactDatabaseImpl artifactDatabase,
ingo@219: int action,
ingo@219: CallMeta callMeta,
ingo@219: PersistentArtifact artifact)
ingo@219: {
sascha@246: super(artifactDatabase, action, callMeta);
ingo@219:
ingo@219: this.artifact = artifact;
ingo@219: }
ingo@219:
ingo@219:
ingo@219: public void afterCall(int action) {
ingo@219: this.action = action;
ingo@219: if (action == BACKGROUND) {
ingo@219: database.addIdToBackground(artifact.getId());
ingo@219: }
ingo@219: }
ingo@219:
ingo@219:
ingo@219: public void afterBackground(int action) {
ingo@219: if (this.action != BACKGROUND) {
ingo@219: throw new IllegalStateException(NOT_IN_BACKGROUND);
ingo@219: }
ingo@219: database.fromBackground(artifact, action);
ingo@219: }
ingo@219:
ingo@219:
ingo@330: public boolean isInBackground() {
ingo@330: return database.getLockedIds().contains(artifact.getId());
ingo@330: }
ingo@330:
ingo@330:
ingo@331: public void addBackgroundMessage(Message msg) {
ingo@331: database.addBackgroundMessage(artifact.getArtifact().identifier(), msg);
ingo@331: }
ingo@331:
ingo@331:
ingo@331: public LinkedList getBackgroundMessages() {
ingo@331: return database.getBackgroundMessages(
ingo@331: artifact.getArtifact().identifier());
ingo@331: }
ingo@331:
ingo@331:
ingo@219: public Long getTimeToLive() {
ingo@219: return artifact.getTTL();
ingo@219: }
ingo@219:
ingo@219:
ingo@219: /**
ingo@219: * Dispatches and executes the persistence action after
ingo@219: * the return of the concrete artifact call.
ingo@219: */
ingo@219: public void postCall() {
sascha@246: try {
sascha@246: switch (action) {
sascha@246: case NOTHING:
sascha@246: break;
sascha@246: case TOUCH:
sascha@246: artifact.touch();
sascha@246: break;
sascha@246: case STORE:
sascha@246: artifact.store();
sascha@246: break;
sascha@246: case BACKGROUND:
sascha@246: logger.warn(
sascha@246: "BACKGROUND processing is not fully implemented, yet!");
sascha@246: artifact.store();
sascha@246: break;
sascha@246: default:
sascha@246: logger.error(INVALID_CALL_STATE + ": " + action);
sascha@246: throw new IllegalStateException(INVALID_CALL_STATE);
sascha@246: }
sascha@246: }
sascha@246: finally {
sascha@246: super.postCall();
ingo@219: }
ingo@219: }
ingo@219: }
ingo@219: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :