Created
September 1, 2015 16:48
-
-
Save leefsmp/013f833d1f9538677423 to your computer and use it in GitHub Desktop.
Become a Java EE developer - Part III Models.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.autodesk.adn.viewanddata.api; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.UUID; | |
import javax.ws.rs.DELETE; | |
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.Response; | |
import org.json.JSONException; | |
import com.google.gson.Gson; | |
@Path("/models") | |
public class Models { | |
//JSON Parser | |
private Gson _gson = new Gson(); | |
static class Model{ | |
public String name; | |
public String urn; | |
public String id; | |
public Model( | |
String _id, | |
String _name, | |
String _urn) { | |
name = _name; | |
urn = _urn; | |
id = _id; | |
} | |
} | |
static HashMap<String, Model> _modelsDb = null; | |
static void initialize() { | |
_modelsDb = new HashMap<String, Model>(); | |
String id1 = UUID.randomUUID().toString(); | |
String id2 = UUID.randomUUID().toString(); | |
String id3 = UUID.randomUUID().toString(); | |
_modelsDb.put(id1, | |
new Model( | |
id1, | |
"Engine", | |
"... base 64 URN ...")); | |
_modelsDb.put(id2, | |
new Model( | |
id2, | |
"Hairdryer", | |
"... base 64 URN ...")); | |
_modelsDb.put(id3, | |
new Model( | |
id3, | |
"Plane Engine", | |
"... base 64 URN ...")); | |
} | |
public Models() { | |
if(Models._modelsDb == null) { | |
Models.initialize(); | |
} | |
} | |
//GET /models/{modelId} | |
@GET | |
@Produces("application/json") | |
public Response getModels() throws JSONException { | |
Collection<Model> models = _modelsDb.values(); | |
String result = _gson.toJson(models); | |
return Response.status(200).entity(result).build(); | |
} | |
// GET /models/{modelId} | |
@GET | |
@Path("{modelId}") | |
@Produces("application/json") | |
public Response getModelById(@PathParam("modelId") String modelId) throws JSONException { | |
if(!_modelsDb.containsKey(modelId)) { | |
//NOT FOUND 404 | |
return Response.status(404).entity("NOT FOUND").build(); | |
} | |
String result = _gson.toJson(_modelsDb.get(modelId)); | |
return Response.status(200).entity(result).build(); | |
} | |
// POST /models | |
@POST | |
@Produces("application/json") | |
public Response addNewModel(String payload) throws JSONException { | |
Model model = _gson.fromJson(payload, Model.class); | |
model.id = UUID.randomUUID().toString(); | |
_modelsDb.put(model.id, model); | |
String result = _gson.toJson(model); | |
return Response.status(200).entity(result).build(); | |
} | |
// PUT /models/{modelId} | |
@PUT | |
@Path("{modelId}") | |
@Produces("application/json") | |
public Response updateModel(@PathParam("modelId") String modelId, String payload) throws JSONException { | |
if(!_modelsDb.containsKey(modelId)) { | |
//NOT FOUND 404 | |
return Response.status(404).entity("NOT FOUND").build(); | |
} | |
Model model = _gson.fromJson(payload, Model.class); | |
model.id = modelId; | |
_modelsDb.put(modelId, model); | |
String result = _gson.toJson(model); | |
return Response.status(200).entity(result).build(); | |
} | |
// DELETE /models/{modelId} | |
@DELETE | |
@Path("{modelId}") | |
@Produces("application/json") | |
public Response deleteModel(@PathParam("modelId") String modelId) throws JSONException { | |
if(!_modelsDb.containsKey(modelId)) { | |
//NOT FOUND 404 | |
return Response.status(404).entity("NOT FOUND").build(); | |
} | |
Model model = _modelsDb.get(modelId); | |
_modelsDb.remove(modelId); | |
String result = _gson.toJson(model); | |
return Response.status(200).entity(result).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment