|
| 1 | +package com.guesswork.community.endpoint; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Date; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +import org.json.JSONArray; |
| 12 | +import org.json.JSONObject; |
| 13 | +import org.json.JSONTokener; |
| 14 | + |
| 15 | +import com.google.appengine.api.datastore.Blob; |
| 16 | +import com.google.appengine.api.datastore.Entity; |
| 17 | +import com.google.appengine.api.datastore.Text; |
| 18 | +import com.google.appengine.api.utils.SystemProperty; |
| 19 | + |
| 20 | +public class DatastoreUtil |
| 21 | +{ |
| 22 | + |
| 23 | + private static final Set<Class<?>> WRAPPER_TYPES = getWrapperTypes(); |
| 24 | + |
| 25 | + public static boolean isWrapperType(Class<?> clazz) |
| 26 | + { |
| 27 | + return WRAPPER_TYPES.contains(clazz); |
| 28 | + } |
| 29 | + |
| 30 | + private static Set<Class<?>> getWrapperTypes() |
| 31 | + { |
| 32 | + Set<Class<?>> ret = new HashSet<Class<?>>(); |
| 33 | + ret.add(String.class); |
| 34 | + ret.add(Boolean.class); |
| 35 | + ret.add(Character.class); |
| 36 | + ret.add(Byte.class); |
| 37 | + ret.add(Short.class); |
| 38 | + ret.add(Integer.class); |
| 39 | + ret.add(Long.class); |
| 40 | + ret.add(Float.class); |
| 41 | + ret.add(Double.class); |
| 42 | + ret.add(Void.class); |
| 43 | + ret.add(Date.class); |
| 44 | + return ret; |
| 45 | + } |
| 46 | + |
| 47 | + public static Entity update(JSONObject source, Entity target) throws Exception |
| 48 | + { |
| 49 | + Iterator it = source.keys(); |
| 50 | + while(it.hasNext()) |
| 51 | + { |
| 52 | + String key = (String)it.next(); |
| 53 | + Object val = source.get(key); |
| 54 | + if(val instanceof String) |
| 55 | + { |
| 56 | + String str = (String)val; |
| 57 | + if(str.length() < 500) |
| 58 | + target.setProperty(key, str); |
| 59 | + else |
| 60 | + target.setProperty(key, new Text(str)); |
| 61 | + } |
| 62 | + else if(isWrapperType(val.getClass())) |
| 63 | + { |
| 64 | + target.setProperty(key, val); |
| 65 | + } |
| 66 | + else if(val instanceof JSONArray) |
| 67 | + { |
| 68 | + JSONArray l = (JSONArray)val; |
| 69 | + if(l.length() > 0) |
| 70 | + { |
| 71 | + Object first = l.get(0); |
| 72 | + if(isWrapperType(first.getClass())) |
| 73 | + { |
| 74 | + List uplst = new ArrayList(); |
| 75 | + for(int i = 0; i < l.length(); i++) |
| 76 | + uplst.add(l.get(i)); |
| 77 | + target.setProperty(key, uplst); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + String js = l.toString(); |
| 82 | + target.setProperty(key, new Blob(js.getBytes())); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + else if(val instanceof JSONObject) |
| 87 | + { |
| 88 | + JSONObject m = (JSONObject)val; |
| 89 | + String js = m.toString(); |
| 90 | + target.setProperty(key, new Blob(js.getBytes())); |
| 91 | + } |
| 92 | + } |
| 93 | + return target; |
| 94 | + } |
| 95 | + |
| 96 | + public static JSONObject update(Entity source, JSONObject target) throws Exception |
| 97 | + { |
| 98 | + String sid = source.getKey().getName(); |
| 99 | + if(sid == null) |
| 100 | + target.put("id", Long.toString(source.getKey().getId())); |
| 101 | + else |
| 102 | + target.put("id", sid); |
| 103 | + target.put("kind", source.getKind()); |
| 104 | + Map m = source.getProperties(); |
| 105 | + for(Object ky : m.keySet()) |
| 106 | + { |
| 107 | + String key = (String)ky; |
| 108 | + Object val = m.get(key); |
| 109 | + if(val == null) |
| 110 | + target.put(key, val); |
| 111 | + else if(val instanceof String) |
| 112 | + target.put(key, val); |
| 113 | + else if(isWrapperType(val.getClass())) |
| 114 | + target.put(key, val); |
| 115 | + else if(val instanceof List) |
| 116 | + { |
| 117 | + List vlst = (List)val; |
| 118 | + JSONArray arr = new JSONArray(); |
| 119 | + for(int i = 0; i < vlst.size(); i++) |
| 120 | + arr.put(vlst.get(i)); |
| 121 | + target.put(key, arr); |
| 122 | + } |
| 123 | + else if(val instanceof Text) |
| 124 | + { |
| 125 | + Text txt = (Text)val; |
| 126 | + target.put(key, txt.getValue()); |
| 127 | + } |
| 128 | + else if(val instanceof Blob) |
| 129 | + { |
| 130 | + Blob blb = (Blob)val; |
| 131 | + target.put(key, convert(blb)); |
| 132 | + } |
| 133 | + } |
| 134 | + return target; |
| 135 | + } |
| 136 | + |
| 137 | + public static Object convert(Blob blb) throws Exception |
| 138 | + { |
| 139 | + if(blb == null) |
| 140 | + return null; |
| 141 | + else |
| 142 | + { |
| 143 | + String str = new String(blb.getBytes()); |
| 144 | + JSONTokener tok = new JSONTokener(str); |
| 145 | + if(str.startsWith("[")) |
| 146 | + return new JSONArray(tok); |
| 147 | + else if(str.startsWith("{")) |
| 148 | + return new JSONObject(tok); |
| 149 | + else |
| 150 | + return str; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public String getString(Entity en, String name) |
| 155 | + { |
| 156 | + Object val = en.getProperty(name); |
| 157 | + if(val == null) |
| 158 | + return null; |
| 159 | + else if(val instanceof String) |
| 160 | + return (String)val; |
| 161 | + else if(val instanceof Text) |
| 162 | + return ((Text)val).getValue(); |
| 163 | + else |
| 164 | + return val.toString(); |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + public static boolean isLive() |
| 169 | + { |
| 170 | + return SystemProperty.environment.value()==SystemProperty.Environment.Value.Production; |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments