Commit e5f29427 by Patryk Czarnik

PDFWriter - obsługa pojedynczych

parent 4e016afe
...@@ -2,11 +2,14 @@ package sklep.rest; ...@@ -2,11 +2,14 @@ package sklep.rest;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import sklep.db.DBConnection; import sklep.db.DBConnection;
import sklep.db.DBException; import sklep.db.DBException;
import sklep.db.ProductDAO; import sklep.db.ProductDAO;
import sklep.db.RecordNotFound;
import sklep.model.Product;
import sklep.model.ProductList; import sklep.model.ProductList;
@Path("/products.pdf") @Path("/products.pdf")
...@@ -20,4 +23,13 @@ public class RProductsPDF { ...@@ -20,4 +23,13 @@ public class RProductsPDF {
return new ProductList(productDAO.readAll()); return new ProductList(productDAO.readAll());
} }
} }
@GET
@Path("/{id}")
public Product readOne(@PathParam("id") int productId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
return productDAO.findById(productId);
}
}
} }
...@@ -13,10 +13,11 @@ import javax.ws.rs.core.MultivaluedMap; ...@@ -13,10 +13,11 @@ import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
import sklep.model.Product;
import sklep.model.ProductList; import sklep.model.ProductList;
@Provider @Provider
public class PDFWriter implements MessageBodyWriter<ProductList> { public class PDFWriter implements MessageBodyWriter<Object> {
private static final MediaType PDF_TYPE = new MediaType("application", "pdf"); private static final MediaType PDF_TYPE = new MediaType("application", "pdf");
@Context @Context
...@@ -24,15 +25,15 @@ public class PDFWriter implements MessageBodyWriter<ProductList> { ...@@ -24,15 +25,15 @@ public class PDFWriter implements MessageBodyWriter<ProductList> {
@Override @Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return (type == ProductList.class) && PDF_TYPE.isCompatible(mediaType); return (type == ProductList.class || type == Product.class) && PDF_TYPE.isCompatible(mediaType);
} }
@Override @Override
public void writeTo(ProductList lista, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream output) MultivaluedMap<String, Object> httpHeaders, OutputStream output)
throws IOException, WebApplicationException { throws IOException, WebApplicationException {
ObslugaXSL obslugaXSL = new ObslugaXSL(servletContext); ObslugaXSL obslugaXSL = new ObslugaXSL(servletContext);
obslugaXSL.wypiszPDF(lista, output); obslugaXSL.wypiszPDF(obj, output);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment