1
|
1 package de.mpiwg.itgroup.diva.utils;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.HashMap;
|
|
5 import java.util.List;
|
|
6 import java.util.Map;
|
|
7
|
|
8 import org.apache.commons.lang.StringUtils;
|
|
9 import org.json.JSONException;
|
|
10 import org.json.JSONObject;
|
|
11 import org.mpi.openmind.repository.bo.Attribute;
|
|
12 import org.mpi.openmind.repository.bo.Entity;
|
|
13
|
|
14 public class JSONEntity {
|
|
15
|
|
16 public Long id;
|
|
17 public Map<String, String> attrs = new HashMap<String, String>();
|
|
18
|
|
19 public JSONEntity(JSONObject json, Long id) throws JSONException{
|
|
20 this.id = id;
|
|
21 for(String attName : JSONObject.getNames(json)){
|
|
22 if(!StringUtils.equals(attName, "id")){
|
|
23 this.attrs.put(attName, json.getString(attName));
|
|
24 }
|
|
25 }
|
|
26 }
|
|
27
|
|
28 public Entity updateEntity(Entity ent){
|
|
29
|
|
30 for(String attName : this.attrs.keySet()){
|
|
31
|
|
32 if(ent.getAttributeByName(attName) == null){
|
|
33 //TODO content type ???
|
|
34 ent.addAttribute(new Attribute(attName, "text", attrs.get(attName)));
|
|
35 }else{
|
|
36 ent.getAttributeByName(attName).setValue(attrs.get(attName));
|
|
37 }
|
|
38 }
|
|
39
|
|
40 return ent;
|
|
41 }
|
|
42
|
|
43
|
|
44 public static List<JSONEntity> json2EntityList(JSONObject json) throws JSONException{
|
|
45
|
|
46 List<JSONEntity> rs = new ArrayList<JSONEntity>();
|
|
47
|
|
48 String[] idList = JSONObject.getNames(json);
|
|
49
|
|
50 for(String idString : idList){
|
|
51 Long id = Long.parseLong(idString);
|
|
52 rs.add(new JSONEntity(json.getJSONObject(idString), id));
|
|
53 }
|
|
54
|
|
55 return rs;
|
|
56
|
|
57 }
|
|
58
|
|
59 }
|