1 package crisp.wbwiki.client;
2
3 import com.google.gwt.user.client.rpc.AsyncCallback;
4
5 public class ClientStateManager {
6 /** The last page version loaded from the server */
7 private Page lastLoadedPage = null;
8 private final WikiServiceAsync wikiService;
9 private static boolean loggingEnabled = true;
10
11
12 public ClientStateManager(final WikiServiceAsync wikiService) {
13 this.wikiService = wikiService;
14 }
15
16 public void synchronizeWithServer(final WikiGui gui) {
17 String currentContent = gui.getCurrentContent();
18
19 final Integer lastLoadedVersion;
20 final String contentToSendToServer;
21 if (lastLoadedPage == null) {
22 lastLoadedVersion = null;
23 contentToSendToServer = null;
24 } else {
25 lastLoadedVersion = new Integer(lastLoadedPage.getVersion());
26 if (lastLoadedPage.getContent().equals(currentContent)) {
27 contentToSendToServer = null;
28 } else {
29 log(gui, "Content has changed!\nold: '" + lastLoadedPage.getContent() + "'\nnew: '" + currentContent + "'");
30 contentToSendToServer = currentContent;
31 }
32 }
33 log(gui, "Calling synchronize(" + lastLoadedVersion + ", " + contentToSendToServer + ")");
34 wikiService.synchronize(
35 lastLoadedVersion,
36 contentToSendToServer,
37 new AsyncCallback() {
38 public void onFailure(Throwable err) {
39
40 err.printStackTrace();
41 }
42 public void onSuccess(Object response) {
43 Page updatedPage = (Page) response;
44 log(gui, "Received response: " + updatedPage);
45 if (updatedPage == null) {
46
47 if (contentToSendToServer == null) {
48
49
50
51 } else {
52
53
54 lastLoadedPage = new Page(
55 lastLoadedPage.getVersion() + 1,
56 contentToSendToServer
57 );
58 }
59 } else {
60
61
62
63 lastLoadedPage = updatedPage;
64 gui.setCurrentContent(updatedPage.getContent());
65 }
66 }
67 }
68 );
69 }
70
71 void log(WikiGui gui, String logEntry) {
72 if (loggingEnabled) {
73 gui.log(logEntry);
74 }
75 }
76
77 }