1 : |
dgarcia |
1 |
package eu.smartlm.abs.portal.view;
|
2 : |
|
|
|
3 : |
|
|
import java.io.ByteArrayOutputStream;
|
4 : |
|
|
import java.io.IOException;
|
5 : |
|
|
import java.io.OutputStream;
|
6 : |
|
|
import java.util.Set;
|
7 : |
|
|
|
8 : |
|
|
import javax.servlet.ServletException;
|
9 : |
|
|
import javax.servlet.http.HttpServlet;
|
10 : |
|
|
import javax.servlet.http.HttpServletRequest;
|
11 : |
|
|
import javax.servlet.http.HttpServletResponse;
|
12 : |
|
|
import javax.servlet.http.HttpSession;
|
13 : |
|
|
|
14 : |
|
|
import com.lowagie.text.Chunk;
|
15 : |
|
|
import com.lowagie.text.Document;
|
16 : |
|
|
import com.lowagie.text.DocumentException;
|
17 : |
|
|
import com.lowagie.text.Font;
|
18 : |
|
|
import com.lowagie.text.Image;
|
19 : |
|
|
import com.lowagie.text.PageSize;
|
20 : |
|
|
import com.lowagie.text.Paragraph;
|
21 : |
|
|
import com.lowagie.text.pdf.PdfWriter;
|
22 : |
|
|
import com.lowagie.text.pdf.draw.VerticalPositionMark;
|
23 : |
|
|
|
24 : |
|
|
import eu.smartlm.abs.portal.util.StringUtil;
|
25 : |
|
|
import eu.smartlm.abs.portal.view.event.EventData;
|
26 : |
|
|
import eu.smartlm.abs.portal.view.portlet.QueryPortlet;
|
27 : |
|
|
import eu.smartlm.schemas.x2009.x06.urec.LicenseResourceType;
|
28 : |
|
|
import eu.smartlm.schemas.x2009.x06.urec.SmartLMUsageRecordType;
|
29 : |
|
|
|
30 : |
|
|
/**
|
31 : |
|
|
* Servlet that creates the PDF output of the accounting or billing query
|
32 : |
|
|
* @author David García Pérez - CESGA
|
33 : |
|
|
*
|
34 : |
|
|
*/
|
35 : |
|
|
public class PDFOutput extends HttpServlet {
|
36 : |
|
|
private static final long serialVersionUID = 8806162452520858258L;
|
37 : |
|
|
|
38 : |
|
|
private static final Font BOLD_16 = new Font(Font.HELVETICA, 16, Font.BOLD);
|
39 : |
|
|
private static final Font BOLD_12 = new Font(Font.HELVETICA, 12, Font.BOLD);
|
40 : |
|
|
private static final Font BOLD_10 = new Font(Font.HELVETICA, 8, Font.BOLD);
|
41 : |
|
|
private static final Font NORMAL_10 = new Font(Font.HELVETICA, 8, Font.NORMAL);
|
42 : |
|
|
|
43 : |
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
44 : |
|
|
// Get Data to create the PDF
|
45 : |
|
|
HttpSession session = request.getSession();
|
46 : |
|
|
EventData data = (EventData) session.getAttribute("eventpdf");
|
47 : |
|
|
|
48 : |
|
|
// Setting the type of data
|
49 : |
|
|
String type = "";
|
50 : |
|
|
if(data.getType().equals(QueryPortlet.TYPE_ACCOUNTING))
|
51 : |
|
|
type = "Accounting";
|
52 : |
|
|
else if (data.getType().equals(QueryPortlet.TYPE_BILLING))
|
53 : |
|
|
type = "Billing";
|
54 : |
|
|
|
55 : |
|
|
try {
|
56 : |
|
|
|
57 : |
|
|
// Creation of the document
|
58 : |
|
|
Document document = new Document(PageSize.A4/*.rotate()*/);
|
59 : |
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
60 : |
|
|
PdfWriter.getInstance(document, baos);
|
61 : |
|
|
document.open();
|
62 : |
|
|
|
63 : |
|
|
// HEADER
|
64 : |
|
|
document.add(new Paragraph(type + " Results", BOLD_16));
|
65 : |
|
|
//document.add(new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2));
|
66 : |
|
|
|
67 : |
|
|
|
68 : |
|
|
// QUERY PART
|
69 : |
|
|
document.add(Chunk.NEWLINE);
|
70 : |
|
|
document.add(new Paragraph("Query:", BOLD_12));
|
71 : |
|
|
|
72 : |
|
|
document.add(Chunk.NEWLINE);
|
73 : |
|
|
|
74 : |
|
|
//ADD IMAGE:
|
75 : |
|
|
document.add(Image.getInstance((java.awt.Image) data.getGraph().createBufferedImage(400, 400), null));
|
76 : |
|
|
|
77 : |
|
|
// USAGE RECORDS:
|
78 : |
|
|
addUsageRecordsToDocument(document, data);
|
79 : |
|
|
|
80 : |
|
|
// Document ready...
|
81 : |
|
|
document.close();
|
82 : |
|
|
|
83 : |
|
|
// setting some response headers
|
84 : |
|
|
response.setHeader("Expires", "0");
|
85 : |
|
|
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
|
86 : |
|
|
response.setHeader("Pragma", "public");
|
87 : |
|
|
// setting the content type
|
88 : |
|
|
response.setContentType("application/pdf");
|
89 : |
|
|
// the contentlength
|
90 : |
|
|
response.setContentLength(baos.size());
|
91 : |
|
|
// write ByteArrayOutputStream to the ServletOutputStream
|
92 : |
|
|
OutputStream os = response.getOutputStream();
|
93 : |
|
|
baos.writeTo(os);
|
94 : |
|
|
os.flush();
|
95 : |
|
|
os.close();
|
96 : |
|
|
}
|
97 : |
|
|
catch(DocumentException e) {
|
98 : |
|
|
throw new IOException(e.getMessage());
|
99 : |
|
|
}
|
100 : |
|
|
}
|
101 : |
|
|
|
102 : |
|
|
private void addUsageRecordsToDocument(Document document, EventData eventData) throws DocumentException {
|
103 : |
|
|
// Format options...
|
104 : |
|
|
Chunk tab1 = new Chunk(new VerticalPositionMark(), 60, true);
|
105 : |
|
|
Chunk tab2 = new Chunk(new VerticalPositionMark(), 160, true);
|
106 : |
|
|
Chunk tab3 = new Chunk(new VerticalPositionMark(), 300, true);
|
107 : |
|
|
Chunk tab4 = new Chunk(new VerticalPositionMark(), 430, true);
|
108 : |
|
|
//Chunk tab5 = new Chunk(new VerticalPositionMark(), 610, true);
|
109 : |
|
|
//Chunk tab6 = new Chunk(new VerticalPositionMark(), 660, true);
|
110 : |
|
|
//Chunk tab7 = new Chunk(new VerticalPositionMark(), 710, true);
|
111 : |
|
|
//Chunk tab8 = new Chunk(new VerticalPositionMark(), 760, true);
|
112 : |
|
|
|
113 : |
|
|
// Table of results:
|
114 : |
|
|
Paragraph line1 = new Paragraph();
|
115 : |
|
|
line1.add(new Chunk("Users:", BOLD_10));
|
116 : |
|
|
line1.add(tab1);
|
117 : |
|
|
line1.add(new Chunk("Product:", BOLD_10));
|
118 : |
|
|
line1.add(tab2);
|
119 : |
|
|
line1.add(new Chunk("Start Date:", BOLD_10));
|
120 : |
|
|
line1.add(tab3);
|
121 : |
|
|
line1.add(new Chunk("End Date:", BOLD_10));
|
122 : |
|
|
line1.add(tab4);
|
123 : |
|
|
line1.add(new Chunk("Accounting Group:", BOLD_10));
|
124 : |
|
|
// line1.add(tab5);
|
125 : |
|
|
// line1.add(new Chunk("Project:", BOLD_10));
|
126 : |
|
|
// line1.add(tab6);
|
127 : |
|
|
// line1.add(new Chunk("Host:", BOLD_10));
|
128 : |
|
|
// line1.add(tab7);
|
129 : |
|
|
// line1.add(new Chunk("SubmitHost:", BOLD_10));
|
130 : |
|
|
document.add(line1);
|
131 : |
|
|
|
132 : |
|
|
// We retrieve the usage records
|
133 : |
|
|
Set<SmartLMUsageRecordType> usageRecords = eventData.getUsageRecords();
|
134 : |
|
|
|
135 : |
|
|
|
136 : |
|
|
for(SmartLMUsageRecordType usageRecord: usageRecords) {
|
137 : |
|
|
String user = StringUtil.removeSpaces(usageRecord.getUserIdentityArray(0).getLocalUserId());
|
138 : |
|
|
//String host = usageRecord.getHostArray(0).getStringValue();
|
139 : |
|
|
//String submitHost = usageRecord.getSubmitHostArray(0).getStringValue();
|
140 : |
|
|
//String project = usageRecord.getProjectNameArray(0).getStringValue();
|
141 : |
|
|
|
142 : |
|
|
LicenseResourceType[] licenseResources = usageRecord.getLicenseResourceArray();
|
143 : |
|
|
|
144 : |
|
|
for(int i = 0; i < licenseResources.length; i++) {
|
145 : |
|
|
LicenseResourceType licenseResource = licenseResources[i];
|
146 : |
|
|
String product = StringUtil.removeSpaces(licenseResource.getLicense().getProduct().getName());
|
147 : |
|
|
String accountingGroup = StringUtil.removeSpaces(licenseResource.getAccountingGroup());
|
148 : |
|
|
String start = StringUtil.removeSpaces(licenseResource.getAccountingPeriod().getStartTime().toString());
|
149 : |
|
|
String end = StringUtil.removeSpaces(licenseResource.getAccountingPeriod().getEndTime().toString());
|
150 : |
|
|
|
151 : |
|
|
Paragraph line = new Paragraph();
|
152 : |
|
|
line.add(new Chunk(user, NORMAL_10));
|
153 : |
|
|
line.add(tab1);
|
154 : |
|
|
line.add(new Chunk(product, NORMAL_10));
|
155 : |
|
|
line.add(tab2);
|
156 : |
|
|
line.add(new Chunk(start, NORMAL_10));
|
157 : |
|
|
line.add(tab3);
|
158 : |
|
|
line.add(new Chunk(end, NORMAL_10));
|
159 : |
|
|
line.add(tab4);
|
160 : |
|
|
line.add(new Chunk(accountingGroup, NORMAL_10));
|
161 : |
|
|
//line.add(tab5);
|
162 : |
|
|
//line.add(new Chunk(project, NORMAL_10));
|
163 : |
|
|
//line.add(tab6);
|
164 : |
|
|
//line.add(new Chunk(host, NORMAL_10));
|
165 : |
|
|
//line.add(tab7);
|
166 : |
|
|
//line.add(new Chunk(submitHost, NORMAL_10));
|
167 : |
|
|
document.add(line);
|
168 : |
|
|
}
|
169 : |
|
|
}
|
170 : |
|
|
}
|
171 : |
|
|
}
|