PrintedCardRestView.java
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package fi.codecrew.moya.rest.meta;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import fi.codecrew.moya.beans.CardTemplateBeanLocal;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.utilities.JsonUtils;
@RequestScoped
@Path("/meta/printedcard")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
public class PrintedCardRestView {
@EJB
CardTemplateBeanLocal cardTemplateBeanLocal;
/**
* Convert List<PathSegment> to List<String>
*
* @param path
* List<PathSegment>
* @return List<String>
*/
public List<String> pathToStrings(List<PathSegment> path) {
ArrayList<String> stringList = new ArrayList<String>(path.size());
for (PathSegment s : path) {
stringList.add(s.getPath());
}
return stringList;
}
/**
* Metadata for entity.
*
* @param id
* @param keys
* The path segments after card id
* @return
*/
@GET
@Path("/{id}/{keys:.*}")
@Produces(MediaType.APPLICATION_JSON)
public String getPrintedCardMeta(@PathParam("id") Integer id,
@PathParam("keys") List<PathSegment> keys) {
// Get the card's metadata
PrintedCard printedCard = cardTemplateBeanLocal.findCard(id);
JsonObject meta = printedCard.getMeta();
// Must be something
if (meta == null) {
meta = Json.createObjectBuilder().build();
}
// Remove lone empty string
if (keys.size() == 1 && keys.get(0).getPath().isEmpty()) {
keys = new ArrayList<PathSegment>();
}
// Get the subobject
JsonObject subObject = JsonUtils
.getSubObject(meta, pathToStrings(keys));
return subObject.toString() + "\n";
}
/**
* Alter metadata in one subproperty of the object.
*
* @param id
* @param keys
* @return
*/
@POST
@Path("/{id}/{keys:.*}")
@Consumes(MediaType.APPLICATION_JSON)
public String setPrintedCardMeta(@PathParam("id") Integer id,
@PathParam("keys") List<PathSegment> keys, String jsonData) {
// Get PrintedCard and it's metadata
PrintedCard printedCard = cardTemplateBeanLocal.findCard(id);
JsonObject meta = printedCard.getMeta();
// If no metadata yet, make empty metadata object
if (meta == null) {
meta = Json.createObjectBuilder().build();
}
// Parse the new json data to be inserted
JsonReader jsonReader = Json.createReader(new StringReader(jsonData));
JsonObject newData = jsonReader.readObject();
// Merge the new json data into existing metadata object
JsonObject alteredMeta = JsonUtils.alterSubObject(meta,
pathToStrings(keys), newData);
// Save the changed meta back to database
printedCard.setMeta(alteredMeta);
cardTemplateBeanLocal.saveCard(printedCard);
return null;
}
}