7479bdd135d500d4fa1595f73d79ec2ba55cee28
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / state / StateManager.java
1 package com.indexdata.pz2utils4jsf.pazpar2.state;\r
2 \r
3 import java.util.HashMap;\r
4 import java.util.Map;\r
5 \r
6 import org.apache.log4j.Logger;\r
7 \r
8 import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command;\r
9 \r
10 public class StateManager {\r
11   \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
16   \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
23     }\r
24 \r
25   }\r
26   \r
27   /**\r
28    * Registers a Pazpar2 command for execution.\r
29    * \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
33    * \r
34    * Any command that is created or modified must be checked in\r
35    * like this to come into effect.\r
36    * \r
37    * @param command\r
38    */\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
45     } else {\r
46       logger.debug("Command " + command.getName() + " not found to change the state [" + command.getEncodedQueryString() + "]");\r
47     }\r
48   }\r
49   \r
50   /**\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
54    * \r
55    * @param commandName\r
56    * @return Copy this state's instance of the given command\r
57    */\r
58   public Pazpar2Command checkOut (String commandName) {\r
59     return getCurrentState().getCommand(commandName).copy();\r
60   }\r
61   \r
62   public Pazpar2State getCurrentState () {\r
63     return states.get(currentKey);\r
64   }\r
65   \r
66   /**\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
69    * \r
70    * @param key\r
71    */\r
72   public void setCurrentStateKey(String key) {    \r
73     if (currentKey.equals(key)) {\r
74       logger.debug("setCurrentStateKey: no key change detected");\r
75     } else {\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
79       } else {\r
80         hasPendingStateChange("search",true);\r
81       }\r
82       if (states.get(key).getCommand("record").equals(states.get(currentKey).getCommand("record"))) {\r
83         logger.debug("No record change detected");\r
84       } else {\r
85         hasPendingStateChange("record",true);\r
86       }\r
87       currentKey = key;\r
88     }\r
89   }\r
90 \r
91   /**\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
95    * \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
99    * \r
100    * @param command\r
101    * @param bool\r
102    */\r
103   public void hasPendingStateChange(String command, boolean bool) {\r
104     pendingStateChanges.put(command, new Boolean(bool));\r
105   }\r
106   \r
107   /**\r
108    * \r
109    * @param command\r
110    * @return true if there is a non-executed command change in this state\r
111    */\r
112   public boolean hasPendingStateChange (String command) {\r
113     return pendingStateChanges.get(command).booleanValue();\r
114   }\r
115 \r
116 }\r