blob: 64128357660560ac1835b655c8968705c35d9c1f [file] [log] [blame]
Michael Hanl0b219982015-09-19 21:33:14 +02001package de.ids_mannheim.korap.resources;
2
3import com.fasterxml.jackson.annotation.JsonIgnore;
Michael Hanlcedf7212016-05-28 10:43:09 +02004import com.fasterxml.jackson.databind.JsonNode;
margaretha894a7d72017-11-08 19:24:20 +01005
6import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl9b84eff2016-01-27 17:11:11 +01007import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanl0b219982015-09-19 21:33:14 +02008import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanl9b84eff2016-01-27 17:11:11 +01009import lombok.AccessLevel;
Michael Hanl0b219982015-09-19 21:33:14 +020010import lombok.Getter;
11import lombok.Setter;
12import org.joda.time.DateTime;
13
margaretha894a7d72017-11-08 19:24:20 +010014import java.io.IOException;
Michael Hanl0b219982015-09-19 21:33:14 +020015import java.util.HashMap;
16import java.util.Map;
17
18/**
19 * Created by hanl on 5/21/14.
20 */
21@Getter
22@Setter
23public abstract class KustvaktResource {
24
25 @JsonIgnore
26 private Integer id;
27 private String persistentID;
28 private String name;
29 private String description;
Michael Hanl9b84eff2016-01-27 17:11:11 +010030 // todo: manage creator over policies!
Michael Hanlc4446022016-02-12 18:03:17 +010031
32 // @JsonIgnore
33 // @Deprecated
34 // private Integer owner;
Michael Hanl0b219982015-09-19 21:33:14 +020035 protected long created;
36 @Deprecated
37 private boolean managed;
38 @Deprecated
39 private boolean shared;
40 private String path;
41 // parents persistentid
42 private String parentID;
43
Michael Hanl9b84eff2016-01-27 17:11:11 +010044 @Getter(AccessLevel.PROTECTED)
45 private Map<String, Object> fields;
Michael Hanl0b219982015-09-19 21:33:14 +020046
Michael Hanl8abaf9e2016-05-23 16:46:35 +020047
48 public KustvaktResource () {
Michael Hanl0b219982015-09-19 21:33:14 +020049 this.created = TimeUtils.getNow().getMillis();
50 this.id = -1;
51 this.parentID = null;
Michael Hanl9b84eff2016-01-27 17:11:11 +010052 this.fields = new HashMap<>();
Michael Hanl0b219982015-09-19 21:33:14 +020053 }
54
Michael Hanl8abaf9e2016-05-23 16:46:35 +020055
56 public KustvaktResource (Integer id) {
Michael Hanl0b219982015-09-19 21:33:14 +020057 this.created = TimeUtils.getNow().getMillis();
58 this.id = id;
Michael Hanl0b219982015-09-19 21:33:14 +020059 this.parentID = null;
Michael Hanl9b84eff2016-01-27 17:11:11 +010060 this.fields = new HashMap<>();
Michael Hanl0b219982015-09-19 21:33:14 +020061 }
62
Michael Hanl8abaf9e2016-05-23 16:46:35 +020063
Michael Hanl19390652016-01-16 11:01:24 +010064 // todo: move creator to builder instance for policies
Michael Hanl8abaf9e2016-05-23 16:46:35 +020065 public KustvaktResource (String persistentID) {
Michael Hanl0b219982015-09-19 21:33:14 +020066 this();
Michael Hanl0b219982015-09-19 21:33:14 +020067 this.persistentID = persistentID;
68 }
69
Michael Hanl8abaf9e2016-05-23 16:46:35 +020070
Michael Hanl9b84eff2016-01-27 17:11:11 +010071 // public void setData(int type, Object data) {
72 // this.data = new Object[2];
73 // this.data[0] = type;
74 // this.data[1] = data;
75 // }
76
77 // public int getDataType() {
78 // return this.data != null ? (int) this.data[0] : -1;
79 // }
80
Michael Hanl8abaf9e2016-05-23 16:46:35 +020081 public void addField (String key, Object value) {
Michael Hanl9b84eff2016-01-27 17:11:11 +010082 this.fields.put(key, value);
83 }
84
Michael Hanl8abaf9e2016-05-23 16:46:35 +020085
86 public void setFields (Map<String, Object> fields) {
Michael Hanl9b84eff2016-01-27 17:11:11 +010087 this.fields = fields;
88 }
89
Michael Hanl8abaf9e2016-05-23 16:46:35 +020090
91 public void setFields (String fields) {
margaretha894a7d72017-11-08 19:24:20 +010092 Map s = null;
93 try {
94 s = JsonUtils.convertToClass(fields, Map.class);
95 }
96 catch (KustvaktException e) {
Michael Hanl9b84eff2016-01-27 17:11:11 +010097 throw new RuntimeException(
98 "Fields could not be read for resource '" + persistentID
99 + "'!");
margaretha894a7d72017-11-08 19:24:20 +0100100 }
Michael Hanl9b84eff2016-01-27 17:11:11 +0100101 this.fields = s;
102 }
103
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200104
105 public Object getField (String key) {
Michael Hanl9b84eff2016-01-27 17:11:11 +0100106 return this.fields.get(key);
107 }
108
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200109
Michael Hanlcedf7212016-05-28 10:43:09 +0200110 public JsonNode getData () {
111 return JsonUtils.valueToTree(this.fields);
112 }
113
Michael Hanl33829ec2016-05-28 17:03:38 +0200114
margarethad4796662017-11-09 16:11:40 +0100115 public String getStringData () throws KustvaktException {
Michael Hanl9b84eff2016-01-27 17:11:11 +0100116 return JsonUtils.toJSON(this.fields);
117 }
118
Michael Hanl0b219982015-09-19 21:33:14 +0200119
120 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200121 public boolean equals (Object other) {
122 return other instanceof KustvaktResource
123 && this.id.equals(((KustvaktResource) other).getId());
124 }
125
126
127 @Override
128 public int hashCode () {
Michael Hanl0b219982015-09-19 21:33:14 +0200129 int result = id.hashCode();
130 return result;
131 }
132
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200133
Michael Hanl0b219982015-09-19 21:33:14 +0200134 /**
135 * Merges another resource with this resource instance
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200136 * Every implementation of KorAPResource should override this
137 * method!
138 *
Michael Hanl0b219982015-09-19 21:33:14 +0200139 * @param other
140 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200141 public void merge (KustvaktResource other) {
Michael Hanl0b219982015-09-19 21:33:14 +0200142 if (other == null)
143 return;
144
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200145 this.setId(this.getId() == null || this.getId() == -1 ? other.getId()
146 : other.getId());
147 this.setPersistentID(this.getPersistentID() == null ? other
148 .getPersistentID() : this.getPersistentID());
149 this.setName(this.getName() == null || this.getName().isEmpty() ? other
150 .getName() : this.getName());
151 this.setDescription(this.getDescription() == null
152 || this.getDescription().isEmpty() ? other.getDescription()
153 : this.getDescription());
154 this.setCreated(this.getCreated() < other.getCreated() ? this
155 .getCreated() : other.getCreated());
Michael Hanl0b219982015-09-19 21:33:14 +0200156 this.setPath(this.getPath() == null ? other.getPath() : this.getPath());
Michael Hanlc4446022016-02-12 18:03:17 +0100157 // this.setOwner(
158 // this.getOwner() == null ? other.getOwner() : this.getOwner());
159 // this.setManaged(
160 // !this.isManaged() ? other.isManaged() : this.isManaged());
161 // this.setShared(!this.isShared() ? other.isShared() : this.isShared());
Michael Hanl0b219982015-09-19 21:33:14 +0200162 }
163
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200164
Michael Hanl0b219982015-09-19 21:33:14 +0200165 /**
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200166 * Checks this instance for null parameter and replaces them with
167 * default values.
168 * Every implementation of KorAPResource should override this
169 * method!
170 *
Michael Hanl0b219982015-09-19 21:33:14 +0200171 * @return
172 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200173 public void checkNull () {
174 setCreated(this.getCreated() == 0L ? TimeUtils.getNow().getMillis()
175 : this.getCreated());
Michael Hanl0b219982015-09-19 21:33:14 +0200176 setName(this.getName() == null ? "" : this.getName());
177 }
178
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200179
Michael Hanl0b219982015-09-19 21:33:14 +0200180 /**
181 * this method is used to return field information about the class
182 * All subclasses should override this method
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200183 *
Michael Hanl0b219982015-09-19 21:33:14 +0200184 * @return
185 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200186 public Map toMap () {
Michael Hanl0b219982015-09-19 21:33:14 +0200187 Map m = new HashMap();
188 m.put("id", persistentID);
189 m.put("name", name);
Michael Hanl19390652016-01-16 11:01:24 +0100190 //todo: fix!
Michael Hanl9b84eff2016-01-27 17:11:11 +0100191 // m.put("path", path);
Michael Hanl0b219982015-09-19 21:33:14 +0200192 m.put("description", description);
193 m.put("created", TimeUtils.format(new DateTime(created)));
Michael Hanlcedf7212016-05-28 10:43:09 +0200194 m.put("data", this.fields);
Michael Hanl19390652016-01-16 11:01:24 +0100195 // deprecated
Michael Hanlcedf7212016-05-28 10:43:09 +0200196 // todo:
Michael Hanl9b84eff2016-01-27 17:11:11 +0100197 // m.put("managed", managed);
198 // m.put("shared", shared);
Michael Hanl0b219982015-09-19 21:33:14 +0200199 return m;
200 }
201
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200202
Michael Hanl0b219982015-09-19 21:33:14 +0200203 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200204 public String toString () {
205 return "Resource{" + "id='" + id + '\'' + "persistentID='"
206 + persistentID + '\'' + ", name='" + name + '\'' + ", created="
207 + created + ", path=" + path + '}';
Michael Hanl0b219982015-09-19 21:33:14 +0200208 }
209
Michael Hanl19390652016-01-16 11:01:24 +0100210 //fixme: make private in respective areas
Michael Hanl0b219982015-09-19 21:33:14 +0200211 @Getter
212 public static class Container {
213 private final Class type;
Michael Hanl0b219982015-09-19 21:33:14 +0200214 private final String persistentID;
215 private final boolean set;
216
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200217
218 public Container (String persistentID, Class type) {
Michael Hanl0b219982015-09-19 21:33:14 +0200219 this.type = type;
Michael Hanl0b219982015-09-19 21:33:14 +0200220 this.set = true;
221 this.persistentID = persistentID;
222 }
223
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200224
225 public Container (Class type) {
Michael Hanl0b219982015-09-19 21:33:14 +0200226 this.type = type;
Michael Hanl0b219982015-09-19 21:33:14 +0200227 this.set = true;
228 this.persistentID = null;
229 }
230
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200231
232 public Container () {
Michael Hanl0b219982015-09-19 21:33:14 +0200233 this.set = false;
234 this.type = null;
Michael Hanl0b219982015-09-19 21:33:14 +0200235 this.persistentID = null;
236 }
237
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200238
Michael Hanl0b219982015-09-19 21:33:14 +0200239 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200240 public String toString () {
Michael Hanl0b219982015-09-19 21:33:14 +0200241 return persistentID + "@" + type.getSimpleName();
242 }
243 }
244
245}