• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.common;
2 
3 
4 import com.google.gwt.json.client.JSONObject;
5 import com.google.gwt.json.client.JSONValue;
6 import com.google.gwt.json.client.JSONArray;
7 
8 import java.util.HashMap;
9 
10 /**
11  * A singleton class to manage a set of static data, such as the list of users.
12  * The data will most likely be retrieved once at the beginning of program
13  * execution.  Other classes can then retrieve the data from this shared
14  * storage.
15  */
16 public class StaticDataRepository {
17     public interface FinishedCallback {
onFinished()18         public void onFinished();
19     }
20     // singleton
21     public static final StaticDataRepository theInstance = new StaticDataRepository();
22 
23     protected JSONObject dataObject = null;
24     protected HashMap<Double, String> priorityMap = null;
25 
StaticDataRepository()26     private StaticDataRepository() {}
27 
getRepository()28     public static StaticDataRepository getRepository() {
29         return theInstance;
30     }
31 
32     /**
33      * Update the local copy of the static data from the server.
34      * @param finished callback to be notified once data has been retrieved
35      */
refresh(final FinishedCallback finished)36     public void refresh(final FinishedCallback finished) {
37         JsonRpcProxy.getProxy().rpcCall("get_static_data", null,
38                                         new JsonRpcCallback() {
39             @Override
40             public void onSuccess(JSONValue result) {
41                 dataObject = result.isObject();
42                 priorityMap = new HashMap<Double, String>();
43                 populatePriorities(dataObject.get("priorities").isArray());
44                 finished.onFinished();
45             }
46         });
47     }
48 
populatePriorities(JSONArray priorities)49     private void populatePriorities(JSONArray priorities) {
50         for(int i = 0; i < priorities.size(); i++) {
51             JSONArray priorityData = priorities.get(i).isArray();
52             String priority = priorityData.get(1).isString().stringValue();
53             Double priority_value = priorityData.get(0).isNumber().getValue();
54             priorityMap.put(priority_value, priority);
55         }
56     }
57 
58     /**
59      * Get a value from the static data object.
60      */
getData(String key)61     public JSONValue getData(String key) {
62         return dataObject.get(key);
63     }
64 
65     /**
66      * Set a value in the repository.
67      */
setData(String key, JSONValue data)68     public void setData(String key, JSONValue data) {
69         dataObject.put(key, data);
70     }
71 
getCurrentUserLogin()72     public String getCurrentUserLogin() {
73         return Utils.jsonToString(dataObject.get("current_user").isObject().get("login"));
74     }
75 
getPriorityName(Double value)76     public String getPriorityName(Double value) {
77         if (priorityMap == null) {
78             return "Unknown";
79         }
80 
81         String priorityName = priorityMap.get(value);
82         if (priorityName == null) {
83             priorityName = value.toString();
84         }
85 
86         return priorityName;
87     }
88 }
89