forked from slifty/OpenHMIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryService.java
104 lines (87 loc) · 4.23 KB
/
InventoryService.java
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
package org.openhmis.webservice;
import java.io.IOException;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
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.QueryParam;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import org.openhmis.dto.InventoryDTO;
import org.openhmis.exception.AccessDeniedException;
import org.openhmis.manager.InventoryManager;
import org.openhmis.util.Authentication;
import org.openhmis.util.DateParser;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
@Path("/inventories")
public class InventoryService {
private static final Logger log = Logger.getLogger(InventoryService.class);
public InventoryService() {}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<InventoryDTO> getInventories(@HeaderParam("Authorization") String authorization, @QueryParam("updatedSince") String updatedSince) throws JsonProcessingException {
if(!Authentication.googleAuthenticate(authorization, Authentication.READ))
throw new AccessDeniedException();
List<InventoryDTO> inventoryDTOs;
// If the user specified no updatedSince parameter, return everything
if(updatedSince == null) {
inventoryDTOs = InventoryManager.getInventories();
} else {
inventoryDTOs = InventoryManager.getInventories(DateParser.parseDate(updatedSince));
}
log.info("GET /inventories (" + inventoryDTOs.size() + ")");
return inventoryDTOs;
}
@POST
@Path("/")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public InventoryDTO createInventory(@HeaderParam("Authorization") String authorization, InventoryDTO inputDTO) throws JsonParseException, JsonMappingException, IOException {
if(!Authentication.googleAuthenticate(authorization, Authentication.WRITE))
throw new AccessDeniedException();
InventoryDTO outputDTO = InventoryManager.addInventory(inputDTO);
log.info("POST /inventories (" + outputDTO.getId() + ")");
return outputDTO;
}
@GET
@Path("/{inventoryId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public InventoryDTO getInventory(@HeaderParam("Authorization") String authorization, @PathParam("inventoryId") String inventoryId) throws JsonProcessingException {
if(!Authentication.googleAuthenticate(authorization, Authentication.READ))
throw new AccessDeniedException();
InventoryDTO outputDTO = InventoryManager.getInventoryById(inventoryId);
log.info("GET /inventories/" + inventoryId);
return outputDTO;
}
@PUT
@Path("/{inventoryId}")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public InventoryDTO updateInventory(@HeaderParam("Authorization") String authorization, @PathParam("inventoryId") String inventoryId, InventoryDTO inputDTO) throws JsonParseException, JsonMappingException, IOException {
if(!Authentication.googleAuthenticate(authorization, Authentication.WRITE))
throw new AccessDeniedException();
inputDTO.setInventoryId(inventoryId);
InventoryDTO outputDTO = InventoryManager.updateInventory(inputDTO);
log.info("PUT /inventories/" + inventoryId);
return outputDTO;
}
@DELETE
@Path("/{inventoryId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String deleteInventory(@HeaderParam("Authorization") String authorization, @PathParam("inventoryId") String inventoryId) throws JsonParseException, JsonMappingException, IOException {
if(!Authentication.googleAuthenticate(authorization, Authentication.WRITE))
throw new AccessDeniedException();
InventoryManager.deleteInventory(inventoryId);
log.info("DELETE /inventories/" + inventoryId);
return "true";
}
}