1 package com.indexdata.mkjsf.pazpar2.state;
\r
3 import java.util.HashMap;
\r
4 import java.util.Map;
\r
6 import com.indexdata.mkjsf.pazpar2.commands.BytargetCommand;
\r
7 import com.indexdata.mkjsf.pazpar2.commands.InitCommand;
\r
8 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;
\r
9 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;
\r
10 import com.indexdata.mkjsf.pazpar2.commands.PingCommand;
\r
11 import com.indexdata.mkjsf.pazpar2.commands.RecordCommand;
\r
12 import com.indexdata.mkjsf.pazpar2.commands.SearchCommand;
\r
13 import com.indexdata.mkjsf.pazpar2.commands.SettingsCommand;
\r
14 import com.indexdata.mkjsf.pazpar2.commands.ShowCommand;
\r
15 import com.indexdata.mkjsf.pazpar2.commands.StatCommand;
\r
16 import com.indexdata.mkjsf.pazpar2.commands.TermlistCommand;
\r
17 import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;
\r
18 import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommands;
\r
21 * Holds a 'pazpar2 state', understood as a full set of pazpar2 commands and
\r
22 * all their parameter settings at a given point in time.
\r
24 * @author Niels Erik
\r
27 public class Pazpar2State {
\r
30 Map<String,Pazpar2Command> commands = new HashMap<String,Pazpar2Command>();;
\r
32 public Pazpar2State (StateManager mgr) {
\r
33 commands.put(Pazpar2Commands.INIT, new InitCommand(mgr));
\r
34 commands.put(Pazpar2Commands.PING, new PingCommand(mgr));
\r
35 commands.put(Pazpar2Commands.SETTINGS, new SettingsCommand(mgr));
\r
36 commands.put(Pazpar2Commands.SEARCH, new SearchCommand(mgr));
\r
37 commands.put(Pazpar2Commands.STAT, new StatCommand(mgr));
\r
38 commands.put(Pazpar2Commands.SHOW, new ShowCommand(mgr));
\r
39 commands.put(Pazpar2Commands.RECORD, new RecordCommand(mgr));
\r
40 commands.put(Pazpar2Commands.TERMLIST, new TermlistCommand(mgr));
\r
41 commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr));
\r
43 commands.put(ServiceProxyCommands.AUTH, new AuthCommand(mgr));
\r
48 * Creates new state by cloning all commands of the provided state and
\r
49 * then overriding one of them with the provided state changing command.
\r
51 * @param previousState
\r
54 public Pazpar2State (Pazpar2State previousState, Pazpar2Command newCommand) {
\r
55 for (String commandName : previousState.commands.keySet()) {
\r
56 this.commands.put(commandName, previousState.commands.get(commandName).copy());
\r
58 this.commands.put(newCommand.getCommandName(),newCommand);
\r
59 this.key = getKey();
\r
63 * Generates a state key that can be used by the browser to pick
\r
64 * up this state again at a later point in time.
\r
68 public String getKey() {
\r
70 StringBuilder querystatebuilder = new StringBuilder("");
\r
71 for (Pazpar2Command command : commands.values()) {
\r
72 if (command.hasParameters()) {
\r
73 querystatebuilder.append("||"+command.getCommandName()+"::");
\r
74 querystatebuilder.append(command.getValueWithExpressions());
\r
77 key = "#"+querystatebuilder.toString();
\r
85 * Checks if a command represents a change of this state
\r
88 * @return true if the command causes a change of state
\r
90 public boolean stateMutating (Pazpar2Command command) {
\r
91 if (command == null) {
\r
93 } else if (commands.get(command.getCommandName()) == null) {
\r
95 } else if ((command.equals(commands.get(command.getCommandName())))) {
\r
103 * Returns a command from this state
\r
108 public Pazpar2Command getCommand(String name) {
\r
109 return commands.get(name);
\r