Commit f028befe by Tuukka Kivilahti

Merge branch 'feature/more-meta' into 'master'

Metatukea eventusereille

Vilkuili läpi @tkfftk
2 parents 3cb7ca22 72eae1db
......@@ -7,6 +7,7 @@ import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import javax.json.JsonValue.ValueType;
public class JsonUtils {
......@@ -18,15 +19,22 @@ public class JsonUtils {
* @param path
* @return
*/
public static JsonObject getSubObject(JsonObject jsonObject,
public static JsonValue getSubObject(JsonObject jsonObject,
List<String> path) {
JsonObject sub = jsonObject;
JsonValue sub = jsonObject;
// Burrow into object hierarchy
for (String s : path) {
sub = sub.getJsonObject(s);
if (sub == null)
break;
if (sub.getValueType() == ValueType.OBJECT) {
JsonObject subObject = (JsonObject) sub;
sub = subObject.get(s);
} else {
// Trying to get sub-object of something not an object. Bad.
return null;
}
}
// If this thing does not exist, return an empty object.
if (sub == null) {
sub = Json.createObjectBuilder().build();
}
......
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
......@@ -23,5 +23,11 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="MoyaUtilitiesTest">
<wb-resource deploy-path="/" source-path="/src"/>
<wb-resource deploy-path="/" source-path="/src/test/java"/>
</wb-module>
</project-modules>
......@@ -4,7 +4,6 @@
<artifactId>moya-utils-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
......
......@@ -6,11 +6,14 @@ import java.util.ArrayList;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import fi.codecrew.moya.utilities.JsonUtils;
public class JsonUtilsTest {
private JsonObject jsonObject(String jsonText) {
......@@ -27,12 +30,11 @@ public class JsonUtilsTest {
@Test
public final void testGetSubObject() {
JsonObject meta = jsonObject("{\"foo\":\"bar\",\"baz\":{\"quuz\":\"plop\"}}");
JsonObject expected = jsonObject("{\"quuz\":\"plop\"}");
ArrayList<String> path = new ArrayList<String>();
path.add("baz");
JsonObject actual = JsonUtils.getSubObject(meta, path);
JsonValue expected = jsonObject("{\"quuz\":\"plop\"}");
JsonValue actual = JsonUtils.getSubObject(meta, path);
Assert.assertEquals(expected.toString(), actual.toString());
}
......
package fi.codecrew.moya.rest.meta.v1;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;
import fi.codecrew.moya.model.EntityMeta;
import fi.codecrew.moya.utilities.JsonUtils;
public abstract class AbstractRestViewV1 {
/**
* 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) {
String segmentString = s.getPath();
if (segmentString != null && !segmentString.isEmpty()) {
stringList.add(segmentString);
}
}
return stringList;
}
public JsonObject parseJson(String jsonString) {
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
JsonObject newData = jsonReader.readObject();
return newData;
}
protected void setEntityMeta(EntityMeta entity, List<PathSegment> keys, String jsonString) {
if (entity == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
JsonObject meta = entity.getMeta();
if (meta == null) {
meta = Json.createObjectBuilder().build();
}
JsonObject alteredMeta = JsonUtils.alterSubObject(meta, pathToStrings(keys), parseJson(jsonString));
if (alteredMeta != null) {
entity.setMeta(alteredMeta);
}
}
protected String getEntityMeta(EntityMeta entity, List<PathSegment> keys) {
if (entity == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
JsonObject meta = entity.getMeta();
if (meta == null) {
meta = Json.createObjectBuilder().build();
}
JsonValue subValue = JsonUtils.getSubObject(meta, pathToStrings(keys));
if (subValue == null) {
subValue = Json.createObjectBuilder().build();
}
return subValue.toString() + "\n";
}
}
package fi.codecrew.moya.rest.meta.v1;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
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.UserBeanLocal;
import fi.codecrew.moya.model.EventUser;
@RequestScoped
@Path("/meta/v1/eventuser")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
public class EventUserRestViewV1 extends AbstractRestViewV1 {
@EJB
UserBeanLocal userBean;
@GET
@Path("/{id}/{path:.*}")
public String getMeta(@PathParam("id") Integer id,
@PathParam("path") List<PathSegment> path) {
EventUser eventUser = userBean.findByEventUserId(id);
return getEntityMeta(eventUser, path);
}
@PUT
@Path("/{id}/{path:.*}")
public void putMeta(@PathParam("id") Integer id,
@PathParam("path") List<PathSegment> path, String jsonString) {
EventUser eventUser = userBean.findByEventUserId(id);
setEntityMeta(eventUser, path, jsonString);
userBean.mergeChanges(eventUser);
}
@POST
@Path("/{id}/{path:.*}")
public void postMeta(@PathParam("id") Integer id,
@PathParam("path") List<PathSegment> path, String jsonString) {
EventUser eventUser = userBean.findByEventUserId(id);
setEntityMeta(eventUser, path, jsonString);
userBean.mergeChanges(eventUser);
}
}
package fi.codecrew.moya.rest.meta.v1;
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.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
......@@ -20,101 +16,42 @@ 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/v1/printedcard")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
public class PrintedCardRestViewV1 {
public class PrintedCardRestViewV1 extends AbstractRestViewV1 {
@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) {
@Path("/{id}/{path:.*}")
public String getMeta(@PathParam("id") Integer id,
@PathParam("path") List<PathSegment> path) {
// 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>();
}
return getEntityMeta(printedCard, path);
}
// Get the subobject
JsonObject subObject = JsonUtils
.getSubObject(meta, pathToStrings(keys));
@PUT
@Path("/{id}/{path:.*}")
public void putMeta(@PathParam("id") Integer id,
@PathParam("path") List<PathSegment> path, String jsonString) {
return subObject.toString() + "\n";
PrintedCard printedCard = cardTemplateBeanLocal.findCard(id);
setEntityMeta(printedCard, path, jsonString);
cardTemplateBeanLocal.saveCard(printedCard);
}
/**
* 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) {
@Path("/{id}/{path:.*}")
public void postMeta(@PathParam("id") Integer id,
@PathParam("path") List<PathSegment> path, String jsonString) {
// 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);
setEntityMeta(printedCard, path, jsonString);
cardTemplateBeanLocal.saveCard(printedCard);
return null;
}
}
package fi.codecrew.moya.rest.meta.v1;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
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.UserBeanLocal;
import fi.codecrew.moya.model.User;
@RequestScoped
@Path("/meta/v1/user")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
public class UserRestViewV1 extends AbstractRestViewV1 {
@EJB
UserBeanLocal userBean;
@GET
@Path("/{id}/{path:.*}")
public String getMeta(@PathParam("id") Integer id,
@PathParam("path") List<PathSegment> path) {
User user = userBean.getUser(id);
return getEntityMeta(user, path);
}
// There is no way to merge/save User entity through the UserBean.
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!