package eu.smartlm.abs.portal.view;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.VerticalPositionMark;
import eu.smartlm.abs.portal.util.StringUtil;
import eu.smartlm.abs.portal.view.event.EventData;
import eu.smartlm.abs.portal.view.portlet.QueryPortlet;
import eu.smartlm.schemas.x2009.x06.urec.LicenseResourceType;
import eu.smartlm.schemas.x2009.x06.urec.SmartLMUsageRecordType;
/**
* Servlet that creates the PDF output of the accounting or billing query
* @author David García Pérez - CESGA
*
*/
public class PDFOutput extends HttpServlet {
private static final long serialVersionUID = 8806162452520858258L;
private static final Font BOLD_16 = new Font(Font.HELVETICA, 16, Font.BOLD);
private static final Font BOLD_12 = new Font(Font.HELVETICA, 12, Font.BOLD);
private static final Font BOLD_10 = new Font(Font.HELVETICA, 8, Font.BOLD);
private static final Font NORMAL_10 = new Font(Font.HELVETICA, 8, Font.NORMAL);
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get Data to create the PDF
HttpSession session = request.getSession();
EventData data = (EventData) session.getAttribute("eventpdf");
// Setting the type of data
String type = "";
if(data.getType().equals(QueryPortlet.TYPE_ACCOUNTING))
type = "Accounting";
else if (data.getType().equals(QueryPortlet.TYPE_BILLING))
type = "Billing";
try {
// Creation of the document
Document document = new Document(PageSize.A4/*.rotate()*/);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
// HEADER
document.add(new Paragraph(type + " Results", BOLD_16));
//document.add(new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2));
// QUERY PART
document.add(Chunk.NEWLINE);
document.add(new Paragraph("Query:", BOLD_12));
document.add(Chunk.NEWLINE);
//ADD IMAGE:
document.add(Image.getInstance((java.awt.Image) data.getGraph().createBufferedImage(400, 400), null));
// USAGE RECORDS:
addUsageRecordsToDocument(document, data);
// Document ready...
document.close();
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
}
catch(DocumentException e) {
throw new IOException(e.getMessage());
}
}
private void addUsageRecordsToDocument(Document document, EventData eventData) throws DocumentException {
// Format options...
Chunk tab1 = new Chunk(new VerticalPositionMark(), 60, true);
Chunk tab2 = new Chunk(new VerticalPositionMark(), 160, true);
Chunk tab3 = new Chunk(new VerticalPositionMark(), 300, true);
Chunk tab4 = new Chunk(new VerticalPositionMark(), 430, true);
//Chunk tab5 = new Chunk(new VerticalPositionMark(), 610, true);
//Chunk tab6 = new Chunk(new VerticalPositionMark(), 660, true);
//Chunk tab7 = new Chunk(new VerticalPositionMark(), 710, true);
//Chunk tab8 = new Chunk(new VerticalPositionMark(), 760, true);
// Table of results:
Paragraph line1 = new Paragraph();
line1.add(new Chunk("Users:", BOLD_10));
line1.add(tab1);
line1.add(new Chunk("Product:", BOLD_10));
line1.add(tab2);
line1.add(new Chunk("Start Date:", BOLD_10));
line1.add(tab3);
line1.add(new Chunk("End Date:", BOLD_10));
line1.add(tab4);
line1.add(new Chunk("Accounting Group:", BOLD_10));
// line1.add(tab5);
// line1.add(new Chunk("Project:", BOLD_10));
// line1.add(tab6);
// line1.add(new Chunk("Host:", BOLD_10));
// line1.add(tab7);
// line1.add(new Chunk("SubmitHost:", BOLD_10));
document.add(line1);
// We retrieve the usage records
Set<SmartLMUsageRecordType> usageRecords = eventData.getUsageRecords();
for(SmartLMUsageRecordType usageRecord: usageRecords) {
String user = StringUtil.removeSpaces(usageRecord.getUserIdentityArray(0).getLocalUserId());
//String host = usageRecord.getHostArray(0).getStringValue();
//String submitHost = usageRecord.getSubmitHostArray(0).getStringValue();
//String project = usageRecord.getProjectNameArray(0).getStringValue();
LicenseResourceType[] licenseResources = usageRecord.getLicenseResourceArray();
for(int i = 0; i < licenseResources.length; i++) {
LicenseResourceType licenseResource = licenseResources[i];
String product = StringUtil.removeSpaces(licenseResource.getLicense().getProduct().getName());
String accountingGroup = StringUtil.removeSpaces(licenseResource.getAccountingGroup());
String start = StringUtil.removeSpaces(licenseResource.getAccountingPeriod().getStartTime().toString());
String end = StringUtil.removeSpaces(licenseResource.getAccountingPeriod().getEndTime().toString());
Paragraph line = new Paragraph();
line.add(new Chunk(user, NORMAL_10));
line.add(tab1);
line.add(new Chunk(product, NORMAL_10));
line.add(tab2);
line.add(new Chunk(start, NORMAL_10));
line.add(tab3);
line.add(new Chunk(end, NORMAL_10));
line.add(tab4);
line.add(new Chunk(accountingGroup, NORMAL_10));
//line.add(tab5);
//line.add(new Chunk(project, NORMAL_10));
//line.add(tab6);
//line.add(new Chunk(host, NORMAL_10));
//line.add(tab7);
//line.add(new Chunk(submitHost, NORMAL_10));
document.add(line);
}
}
}
}