Commit 4d7544b3 by Patryk Czarnik

PDF - Obsługa też pojedynczych produktów

parent b4470cb8
...@@ -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,16 +25,23 @@ public class PDFWriter implements MessageBodyWriter<ProductList> { ...@@ -24,16 +25,23 @@ 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 obj, 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 out) MultivaluedMap<String, Object> httpHeaders, OutputStream output)
throws IOException, WebApplicationException { throws IOException, WebApplicationException {
String fileName = "products.pdf";
if (obj instanceof Product) {
Product product = (Product) obj;
fileName = product.getProductName().replace(' ', '_') + ".pdf";
}
// httpHeaders.add("Content-Disposition", "attachment;filename=" + fileName);
httpHeaders.add("Content-Disposition", "inline;filename=" + fileName);
ObslugaXSL obslugaXSL = new ObslugaXSL(servletContext); ObslugaXSL obslugaXSL = new ObslugaXSL(servletContext);
obslugaXSL.wypiszPDF(obj, out); obslugaXSL.wypiszPDF(obj, output);
} }
} }
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
border-color="#2233AA"> border-color="#2233AA">
<fo:block font-weight="bold" font-size="14pt" <fo:block font-weight="bold" font-size="14pt"
margin-bottom="1em" color="#FF2244"> margin-bottom="1em" color="#FF2244">
<xsl:apply-templates select="name" /> <xsl:apply-templates select="product-name" />
</fo:block> </fo:block>
<fo:block font-weight="bold" color="green"> <fo:block font-weight="bold" color="green">
<xsl:text>Cena: </xsl:text> <xsl:text>Cena: </xsl:text>
......
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