1 package com.indexdata.pz2utils4jsf.pazpar2.state;
\r
3 import java.util.HashMap;
\r
4 import java.util.Map;
\r
6 import org.apache.log4j.Logger;
\r
8 import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command;
\r
10 public class StateManager {
\r
12 Map<String, Pazpar2State> states = new HashMap<String, Pazpar2State>();
\r
13 String currentKey = "";
\r
14 Map<String,Boolean> pendingStateChanges = new HashMap<String,Boolean>();
\r
15 private static Logger logger = Logger.getLogger(StateManager.class);
\r
17 public StateManager () {
\r
18 Pazpar2State initialState = new Pazpar2State();
\r
19 states.put(initialState.getKey(), initialState);
\r
20 currentKey = initialState.getKey();
\r
21 for (String command : Pazpar2Command.allCommands) {
\r
22 pendingStateChanges.put(command, new Boolean(false));
\r
28 * Registers a Pazpar2 command for execution.
\r
30 * The state manager will update current state and flag that
\r
31 * a request change was made but that it was not yet carried
\r
32 * out against Pazpar2.
\r
34 * Any command that is created or modified must be checked in
\r
35 * like this to come into effect.
\r
39 public void checkIn(Pazpar2Command command) {
\r
40 if (getCurrentState().stateMutating(command)) {
\r
41 Pazpar2State state = new Pazpar2State(getCurrentState(),command);
\r
42 states.put(state.getKey(), state);
\r
43 currentKey = state.getKey();
\r
44 hasPendingStateChange(command.getName(),new Boolean(true));
\r
46 logger.debug("Command " + command.getName() + " not found to change the state [" + command.getEncodedQueryString() + "]");
\r
51 * Gets a detached copy of a command. For the change manager
\r
52 * to become aware of any changes to the copy it must be
\r
53 * checked back in with 'checkIn(Pazpar2Command)'
\r
55 * @param commandName
\r
56 * @return Copy this state's instance of the given command
\r
58 public Pazpar2Command checkOut (String commandName) {
\r
59 return getCurrentState().getCommand(commandName).copy();
\r
62 public Pazpar2State getCurrentState () {
\r
63 return states.get(currentKey);
\r
67 * Changes the current state key. Invoked from the UI to have the state
\r
68 * manager switch to another state than the current one.
\r
72 public void setCurrentStateKey(String key) {
\r
73 if (currentKey.equals(key)) {
\r
74 logger.debug("setCurrentStateKey: no key change detected");
\r
76 logger.debug("State key change. Was: [" + currentKey + "]. Will be ["+key+"]");
\r
77 if (states.get(key).getCommand("search").equals(states.get(currentKey).getCommand("search"))) {
\r
78 logger.debug("No search change detected");
\r
80 hasPendingStateChange("search",true);
\r
82 if (states.get(key).getCommand("record").equals(states.get(currentKey).getCommand("record"))) {
\r
83 logger.debug("No record change detected");
\r
85 hasPendingStateChange("record",true);
\r
92 * Sets a pending-state-change flag for the given command. Used by
\r
93 * the beans to decide whether, say, a search should be executed before
\r
94 * doing the next show.
\r
96 * It is up to the client to set and reset this flag since the state
\r
97 * manager is not otherwise informed about actual request activities
\r
98 * (only about the definition of commands to be executed)
\r
103 public void hasPendingStateChange(String command, boolean bool) {
\r
104 pendingStateChanges.put(command, new Boolean(bool));
\r
110 * @return true if there is a non-executed command change in this state
\r
112 public boolean hasPendingStateChange (String command) {
\r
113 return pendingStateChanges.get(command).booleanValue();
\r