|
| 1 | +package at.gmi.nordborglab.widgets.geneviewer.client.datasource; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import com.google.gwt.core.client.JavaScriptObject; |
| 7 | +import com.google.gwt.core.client.JsArray; |
| 8 | +import com.google.gwt.core.client.JsonUtils; |
| 9 | +import com.google.gwt.json.client.JSONArray; |
| 10 | +import com.google.gwt.json.client.JSONObject; |
| 11 | +import com.google.gwt.json.client.JSONValue; |
| 12 | +import com.google.gwt.storage.client.Storage; |
| 13 | +import com.google.gwt.visualization.client.DataTable; |
| 14 | + |
| 15 | +public class LocalStorageImpl implements Cache{ |
| 16 | + |
| 17 | + public static enum TYPE {LOCAL,SESSION} |
| 18 | + private TYPE type; |
| 19 | + private Storage cacheStorage = null; |
| 20 | + |
| 21 | + public LocalStorageImpl(TYPE type) throws Exception { |
| 22 | + this.type = type; |
| 23 | + if (type == TYPE.LOCAL) { |
| 24 | + cacheStorage = Storage.getLocalStorageIfSupported(); |
| 25 | + } |
| 26 | + else { |
| 27 | + cacheStorage = Storage.getSessionStorageIfSupported(); |
| 28 | + } |
| 29 | + if (cacheStorage == null) { |
| 30 | + throw new Exception("LocalStorage not supported"); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + @Override |
| 36 | + public void put(Object key, Object value) { |
| 37 | + if (key == null) { |
| 38 | + throw new NullPointerException("key is null"); |
| 39 | + } |
| 40 | + if (value == null) { |
| 41 | + throw new NullPointerException("value is null"); |
| 42 | + } |
| 43 | + String jsonData = null; |
| 44 | + if (value instanceof List) { |
| 45 | + JSONArray array = new JSONArray(); |
| 46 | + int index = 0; |
| 47 | + for (Object val:(List)value) { |
| 48 | + array.set(index,new JSONObject((JavaScriptObject)val)); |
| 49 | + index = index +1; |
| 50 | + } |
| 51 | + jsonData = array.toString(); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + jsonData = ((CustomDataTable)value).toJSON(); |
| 56 | + } |
| 57 | + cacheStorage.setItem(key.toString(), jsonData); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Object get(Object key) { |
| 62 | + if (key == null) { |
| 63 | + throw new NullPointerException("key is null"); |
| 64 | + } |
| 65 | + String jsonDataString = cacheStorage.getItem(key.toString()); |
| 66 | + if (jsonDataString == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + Object data = null; |
| 70 | + JavaScriptObject jsonData = JsonUtils.safeEval(jsonDataString); |
| 71 | + if (!key.equals("genomestatslist")) |
| 72 | + data = DataTable.create((JavaScriptObject)jsonData); |
| 73 | + else if (jsonData instanceof JsArray){ |
| 74 | + JsArray<GenomeStat> jsonStats = (JsArray<GenomeStat>)jsonData; |
| 75 | + List<GenomeStat> stats = new ArrayList<GenomeStat>(); |
| 76 | + for (int i = 0;i<jsonStats.length();i++) { |
| 77 | + stats.add(jsonStats.get(i)); |
| 78 | + } |
| 79 | + data = (Object)stats; |
| 80 | + } |
| 81 | + return data; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void remove(Object key) { |
| 86 | + cacheStorage.removeItem(key.toString()); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void clear() { |
| 91 | + cacheStorage.clear(); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + public TYPE getType() { |
| 96 | + return type; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments