Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cn.leancloud.ops.Utils Maven / Gradle / Ivy
package cn.leancloud.ops;
import java.util.*;
import cn.leancloud.*;
import cn.leancloud.types.AVGeoPoint;
import cn.leancloud.utils.StringUtil;
import cn.leancloud.codec.Base64;
import com.alibaba.fastjson.JSONObject;
public class Utils {
private static final String typeTag = "__type";
public static Map createPointerArrayOpMap(String key, String op,
Collection objects) {
Map map = new HashMap();
map.put("__op", op);
List> list = new ArrayList>();
for (AVObject obj : objects) {
list.add(mapFromPointerObject(obj));
}
map.put("objects", list);
Map result = new HashMap();
result.put(key, map);
return result;
}
public static Map mapFromPointerObject(AVObject object) {
return mapFromAVObject(object, false);
}
public static Map mapFromGeoPoint(AVGeoPoint point) {
Map result = new HashMap();
result.put(typeTag, "GeoPoint");
result.put("latitude", point.getLatitude());
result.put("longitude", point.getLongitude());
return result;
}
public static AVGeoPoint geoPointFromMap(Map map) {
double la = ((Number) map.get("latitude")).doubleValue();
double lo = ((Number) map.get("longitude")).doubleValue();
AVGeoPoint point = new AVGeoPoint(la, lo);
return point;
}
public static AVObject parseObjectFromMap(Map map) {
AVObject avObject = Transformer.objectFromClassName((String) map.get("className"));
map.remove("__type");
avObject.resetServerData(map);
return avObject;
}
public static byte[] dataFromMap(Map map) {
String value = (String) map.get("base64");
return Base64.decode(value, Base64.NO_WRAP);
}
public static Date dateFromMap(Map map) {
String value = (String) map.get("iso");
return StringUtil.dateFromString(value);
}
public static Map mapFromDate(Date date) {
Map result = new HashMap();
result.put(typeTag, "Date");
result.put("iso", StringUtil.stringFromDate(date));
return result;
}
public static Map mapFromByteArray(byte[] data) {
Map result = new HashMap();
result.put(typeTag, "Bytes");
result.put("base64", Base64.encodeToString(data, Base64.NO_WRAP));
return result;
}
public static Map mapFromFile(AVFile file) {
Map result = new HashMap();
result.put("__type", "_File");
result.put("metaData", file.getMetaData());
result.put("id", file.getName());
return result;
}
public static Map mapFromAVObject(AVObject object, boolean topObject) {
Map result = new HashMap();
result.put("className", object.internalClassName());
if (!StringUtil.isEmpty(object.getObjectId())) {
result.put("objectId", object.getObjectId());
}
if (!topObject) {
result.put("__type", "Pointer");
} else {
result.put("__type", "Object");
Map serverData = getParsedMap(object.getServerData(), false);
if (serverData != null && !serverData.isEmpty()) {
result.putAll(serverData);
}
}
return result;
}
public static Map getParsedMap(Map map) {
return getParsedMap(map, false);
}
public static Map getParsedMap(Map object, boolean topObject) {
Map newMap = new HashMap(object.size());
for (Map.Entry entry : object.entrySet()) {
final String key = entry.getKey();
Object o = entry.getValue();
newMap.put(key, getParsedObject(o, topObject));
}
return newMap;
}
public static List getParsedList(Collection list) {
List newList = new ArrayList(list.size());
for (Object o : list) {
newList.add(getParsedObject(o));
}
return newList;
}
public static List getParsedList(Collection object, boolean topObject) {
if (!topObject) {
return getParsedList(object);
} else {
List newList = new ArrayList(object.size());
for (Object o : object) {
newList.add(getParsedObject(o, true));
}
return newList;
}
}
public static Object getParsedObject(Object object) {
return getParsedObject(object, false);
}
public static Object getParsedObject(Object object, boolean topObject) {
if (object == null) {
return null;
} else if (object instanceof Map) {
return getParsedMap((Map) object, topObject);
} else if (object instanceof Collection) {
return getParsedList((Collection) object, topObject);
} else if (object instanceof AVObject) {
if (!topObject) {
return mapFromPointerObject((AVObject) object);
} else {
return mapFromAVObject((AVObject) object, true);
}
} else if (object instanceof AVGeoPoint) {
return mapFromGeoPoint((AVGeoPoint) object);
} else if (object instanceof Date) {
return mapFromDate((Date) object);
} else if (object instanceof byte[]) {
return mapFromByteArray((byte[]) object);
} else if (object instanceof AVFile) {
return mapFromFile((AVFile) object);
} else {
return object;
}
}
public static Map createArrayOpMap(String key, String op, Collection> objects) {
Map map = new HashMap();
map.put("__op", op);
List array = new ArrayList();
for (Object obj : objects) {
array.add(getParsedObject(obj));
}
map.put("objects", array);
Map ops = new HashMap();
ops.put(key, map);
return ops;
}
public static AVRelation objectFromRelationMap(Map map) {
String className = (String) map.get("className");
return new AVRelation(className);
}
public static AVFile fileFromMap(Map map) {
AVFile file = new AVFile("", "");
file.resetServerData(map);
Object metadata = map.get("metaData");
if (metadata != null && metadata instanceof Map) {
file.getMetaData().putAll((Map) metadata);
}
return file;
}
public static List getObjectFrom(Collection list) {
List newList = new ArrayList();
for (Object obj : list) {
newList.add(getObjectFrom(obj));
}
return newList;
}
public static Object getObjectFrom(Map map) {
Object type = map.get("__type");
if (type == null || !(type instanceof String)) {
if(map.containsKey(ObjectTypeAdapter.KEY_VERSION) && map.containsKey(ObjectTypeAdapter.KEY_SERVERDATA)) {
// support new version jsonString of AVObject.
String className = (String) map.get(AVObject.KEY_CLASSNAME);
AVObject avObject = Transformer.objectFromClassName(className);
Map serverData = (Map) map.get(ObjectTypeAdapter.KEY_SERVERDATA);
Map decodedValues = new HashMap<>();
for(Map.Entry entry: serverData.entrySet()) {
String k = entry.getKey();
Object v = entry.getValue();
if (v instanceof String || v instanceof Number || v instanceof Boolean || v instanceof Byte || v instanceof Character) {
// primitive type
decodedValues.put(k, v);
} else if (v instanceof Map || v instanceof JSONObject) {
decodedValues.put(k, Utils.getObjectFrom(v));
} else if (v instanceof Collection) {
decodedValues.put(k, Utils.getObjectFrom(v));
} else if (null != v) {
decodedValues.put(k, v);
}
}
avObject.resetServerData(decodedValues);
return avObject;
} else {
Map newMap = new HashMap(map.size());
for (Map.Entry entry : map.entrySet()) {
final String key = entry.getKey();
Object o = entry.getValue();
newMap.put(key, getObjectFrom(o));
}
return newMap;
}
}
map.remove("__type");
if (type.equals("Pointer") || type.equals("Object")) {
AVObject avObject = Transformer.objectFromClassName((String) map.get("className"));
// avObject.resetServerData(map);
for (String k : map.keySet()) {
Object v = map.get(k);
if (v instanceof String || v instanceof Number || v instanceof Boolean || v instanceof Byte || v instanceof Character) {
// primitive type
avObject.getServerData().put(k, v);
} else if (v instanceof Map || v instanceof JSONObject) {
avObject.getServerData().put(k, Utils.getObjectFrom(v));
} else if (v instanceof Collection) {
avObject.getServerData().put(k, Utils.getObjectFrom(v));
} else if (null != v) {
avObject.getServerData().put(k, v);
}
}
return avObject;
} else if (type.equals("GeoPoint")) {
return geoPointFromMap(map);
} else if (type.equals("Bytes")) {
return dataFromMap(map);
} else if (type.equals("Date")) {
return dateFromMap(map);
} else if (type.equals("Relation")) {
return objectFromRelationMap(map);
} else if (type.equals("File")) {
return fileFromMap(map);
}
return map;
}
public static Object getObjectFrom(Object obj) {
if (obj instanceof Collection) {
return getObjectFrom((Collection) obj);
} else if (obj instanceof Map || obj instanceof JSONObject) {
return getObjectFrom((Map) obj);
}
return obj;
}
public static Map makeCompletedRequest(String internalId, String path, String method, Map param) {
if (null == param || StringUtil.isEmpty(path) || StringUtil.isEmpty(method)) {
return null;
}
param.put(BaseOperation.KEY_INTERNAL_ID, internalId);
Map topParams = new HashMap();
topParams.put(BaseOperation.KEY_BODY, param);
topParams.put(BaseOperation.KEY_PATH, path);
topParams.put(BaseOperation.KEY_HTTP_METHOD, method);
return topParams;
}
}