Skip to content

Commit 5dac4e7

Browse files
committed
inherit from ChartServlet
1 parent 5a65cb7 commit 5dac4e7

File tree

5 files changed

+177
-263
lines changed

5 files changed

+177
-263
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package com.logicaldoc.web;
2+
3+
import java.awt.Color;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.text.DateFormat;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Calendar;
9+
import java.util.Locale;
10+
11+
import javax.servlet.ServletException;
12+
import javax.servlet.http.HttpServlet;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
16+
import org.jfree.chart.ChartFactory;
17+
import org.jfree.chart.ChartUtilities;
18+
import org.jfree.chart.JFreeChart;
19+
import org.jfree.chart.axis.CategoryAxis;
20+
import org.jfree.chart.axis.CategoryLabelPositions;
21+
import org.jfree.chart.axis.NumberAxis;
22+
import org.jfree.chart.plot.CategoryPlot;
23+
import org.jfree.chart.plot.PlotOrientation;
24+
import org.jfree.chart.renderer.category.BarRenderer;
25+
import org.jfree.data.category.DefaultCategoryDataset;
26+
import org.jfree.ui.HorizontalAlignment;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import com.logicaldoc.core.PersistenceException;
31+
import com.logicaldoc.core.security.Tenant;
32+
import com.logicaldoc.core.sequence.SequenceDAO;
33+
import com.logicaldoc.i18n.I18N;
34+
import com.logicaldoc.util.Context;
35+
import com.logicaldoc.util.LocaleUtil;
36+
import com.logicaldoc.util.io.FileUtil;
37+
import com.logicaldoc.web.util.ServletUtil;
38+
39+
/**
40+
* Base for those servlets that produce a graph to display some kind of
41+
* statistics.
42+
*
43+
* @author Marco Meschieri - LogicalDOC
44+
* @since 9.2
45+
*/
46+
public abstract class ChartServlet extends HttpServlet {
47+
48+
private static final long serialVersionUID = 1;
49+
50+
private static final Logger log = LoggerFactory.getLogger(ChartServlet.class);
51+
52+
protected String prefix = "prefix";
53+
54+
protected String rowKey = "key";
55+
56+
/**
57+
* Constructor of the object.
58+
*/
59+
public ChartServlet() {
60+
super();
61+
}
62+
63+
/**
64+
* The doGet method of the servlet.
65+
*
66+
* This method is called when a form has its tag value method equals to get.
67+
*
68+
* @param request the request send by the client to the server
69+
* @param response the response send by the server to the client
70+
*
71+
* @throws ServletException if an error occurred
72+
* @throws IOException if an error occurred
73+
*/
74+
@Override
75+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
76+
File chartFile = null;
77+
try {
78+
ServletUtil.validateSession(request);
79+
80+
int width = 1200;
81+
if (request.getParameter("width") != null)
82+
width = Integer.parseInt(request.getParameter("width"));
83+
84+
int height = 450;
85+
if (request.getParameter("height") != null)
86+
height = Integer.parseInt(request.getParameter("height"));
87+
88+
Locale locale = Locale.ENGLISH;
89+
if (request.getParameter("locale") != null)
90+
locale = LocaleUtil.toLocale(request.getParameter("locale"));
91+
92+
chartFile = FileUtil.createTempFile("chart", ".png");
93+
94+
drawChart(chartFile, width, height, locale, getObjectId(request), getTenantId(request));
95+
96+
ServletUtil.downloadFile(request, response, chartFile, "chart.png");
97+
} catch (Exception e) {
98+
log.error(e.getMessage(), e);
99+
} finally {
100+
FileUtil.delete(chartFile);
101+
}
102+
}
103+
104+
protected long getObjectId(HttpServletRequest request) throws IOException, PersistenceException {
105+
return 0L;
106+
}
107+
108+
protected long getTenantId(HttpServletRequest request) throws IOException, PersistenceException {
109+
long tenantId = Tenant.SYSTEM_ID;
110+
if (request.getParameter("tenantId") != null)
111+
tenantId = Long.parseLong(request.getParameter("tenantId"));
112+
return tenantId;
113+
}
114+
115+
protected void drawChart(File chartFile, int width, int height, Locale locale, long objectId, long tenantId)
116+
throws IOException {
117+
/**
118+
* Retrieve the sequences and order them by date
119+
*/
120+
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
121+
SequenceDAO dao = Context.get(SequenceDAO.class);
122+
123+
DateFormat dfName = new SimpleDateFormat("MMM yyyy", locale);
124+
DateFormat dfNumber = new SimpleDateFormat("yyyyMM");
125+
for (int i = 0; i < 36; i++) {
126+
Calendar cal = Calendar.getInstance();
127+
cal.add(Calendar.MONTH, -i);
128+
String month = dfNumber.format(cal.getTime());
129+
String monthName = dfName.format(cal.getTime());
130+
dataset.addValue(dao.getCurrentValue(prefix + "-" + month, objectId, tenantId),
131+
I18N.message(rowKey, locale), monthName);
132+
}
133+
134+
JFreeChart chart = ChartFactory.createBarChart("", null, null, dataset, PlotOrientation.VERTICAL, false, false,
135+
false);
136+
137+
CategoryPlot catPlot = chart.getCategoryPlot();
138+
catPlot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
139+
BarRenderer r = (BarRenderer) catPlot.getRenderer();
140+
r.setSeriesPaint(0, Color.BLUE);
141+
142+
chart.getTitle().setHorizontalAlignment(HorizontalAlignment.LEFT);
143+
chart.getTitle().setPaint(Color.BLUE);
144+
145+
CategoryAxis domainAxis = catPlot.getDomainAxis();
146+
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
147+
148+
ChartUtilities.saveChartAsPNG(chartFile, chart, width, height);
149+
}
150+
}

logicaldoc-webservice/pom.xml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
<name>logicaldoc-webservice</name>
1212
<packaging>jar</packaging>
1313
<dependencies>
14+
<dependency>
15+
<groupId>com.logicaldoc</groupId>
16+
<artifactId>logicaldoc-webapp</artifactId>
17+
<version>${project.version}</version>
18+
<classifier>api</classifier>
19+
<scope>provided</scope>
20+
<exclusions>
21+
<exclusion>
22+
<groupId>xml-apis</groupId>
23+
<artifactId>xml-apis</artifactId>
24+
</exclusion>
25+
<exclusion>
26+
<groupId>xml-apis</groupId>
27+
<artifactId>xml-apis-ext</artifactId>
28+
</exclusion>
29+
</exclusions>
30+
</dependency>
1431
<dependency>
1532
<groupId>com.logicaldoc</groupId>
1633
<artifactId>logicaldoc-core</artifactId>
@@ -271,11 +288,11 @@
271288
<artifactId>calcite-core</artifactId>
272289
<scope>test</scope>
273290
</dependency>
274-
291+
275292
<dependency>
276-
<groupId>org.mockito</groupId>
277-
<artifactId>mockito-core</artifactId>
278-
<scope>test</scope>
293+
<groupId>org.mockito</groupId>
294+
<artifactId>mockito-core</artifactId>
295+
<scope>test</scope>
279296
</dependency>
280297
<dependency>
281298
<groupId>org.eclipse.jetty.http2</groupId>
@@ -292,7 +309,7 @@
292309
<artifactId>log4j-slf4j-impl</artifactId>
293310
<version>2.24.3</version>
294311
<scope>test</scope>
295-
</dependency>
312+
</dependency>
296313
</dependencies>
297314
<build>
298315
<plugins>
Lines changed: 4 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,23 @@
11
package com.logicaldoc.webservice;
22

3-
import java.awt.Color;
4-
import java.io.File;
5-
import java.io.IOException;
6-
import java.text.DateFormat;
7-
import java.text.SimpleDateFormat;
8-
import java.util.Calendar;
9-
import java.util.Locale;
10-
11-
import javax.servlet.ServletException;
12-
import javax.servlet.http.HttpServlet;
13-
import javax.servlet.http.HttpServletRequest;
14-
import javax.servlet.http.HttpServletResponse;
15-
16-
import org.jfree.chart.ChartFactory;
17-
import org.jfree.chart.ChartUtilities;
18-
import org.jfree.chart.JFreeChart;
19-
import org.jfree.chart.axis.CategoryAxis;
20-
import org.jfree.chart.axis.CategoryLabelPositions;
21-
import org.jfree.chart.axis.NumberAxis;
22-
import org.jfree.chart.plot.CategoryPlot;
23-
import org.jfree.chart.plot.PlotOrientation;
24-
import org.jfree.chart.renderer.category.BarRenderer;
25-
import org.jfree.data.category.DefaultCategoryDataset;
26-
import org.jfree.ui.HorizontalAlignment;
27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
29-
30-
import com.logicaldoc.core.security.Tenant;
31-
import com.logicaldoc.core.sequence.SequenceDAO;
32-
import com.logicaldoc.util.Context;
33-
import com.logicaldoc.util.LocaleUtil;
34-
import com.logicaldoc.util.io.FileUtil;
3+
import com.logicaldoc.web.ChartServlet;
354

365
/**
376
* This servlet provides the chart of the calls to the API
387
*
398
* @author Marco Meschieri - LogicalDOC
409
* @since 8.6.1
4110
*/
42-
public class WebserviceChartServlet extends HttpServlet {
43-
44-
public static final String USER_ID = "userId";
11+
public class WebserviceChartServlet extends ChartServlet {
4512

4613
private static final long serialVersionUID = -6956612970433309888L;
4714

48-
private static final Logger log = LoggerFactory.getLogger(WebserviceChartServlet.class);
49-
5015
/**
5116
* Constructor of the object.
5217
*/
5318
public WebserviceChartServlet() {
5419
super();
55-
}
56-
57-
/**
58-
* The doGet method of the servlet. <br>
59-
*
60-
* This method is called when a form has its tag value method equals to get.
61-
*
62-
* @param request the request send by the client to the server
63-
* @param response the response send by the server to the client
64-
* @throws ServletException if an error occurred
65-
* @throws IOException if an error occurred
66-
*/
67-
@Override
68-
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69-
File chartFile = null;
70-
try {
71-
WebserviceServletUtil.validateSession(request);
72-
73-
int width = 1200;
74-
if (request.getParameter("width") != null)
75-
width = Integer.parseInt(request.getParameter("width"));
76-
77-
int height = 450;
78-
if (request.getParameter("height") != null)
79-
height = Integer.parseInt(request.getParameter("height"));
80-
81-
Locale locale = Locale.ENGLISH;
82-
if (request.getParameter("locale") != null)
83-
locale = LocaleUtil.toLocale(request.getParameter("locale"));
84-
85-
long tenantId = Tenant.SYSTEM_ID;
86-
if (request.getParameter("tenantId") != null)
87-
tenantId = Long.parseLong(request.getParameter("tenantId"));
88-
89-
chartFile = FileUtil.createTempFile("chart", ".png");
90-
91-
/**
92-
* Retrieve the sequences and order them by date
93-
*/
94-
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
95-
SequenceDAO dao = Context.get(SequenceDAO.class);
96-
97-
DateFormat dfName = new SimpleDateFormat("MMM yyyy", locale);
98-
DateFormat dfNumber = new SimpleDateFormat("yyyyMM");
99-
for (int i = 0; i < 36; i++) {
100-
Calendar cal = Calendar.getInstance();
101-
cal.add(Calendar.MONTH, -i);
102-
String month = dfNumber.format(cal.getTime());
103-
String monthName = dfName.format(cal.getTime());
104-
dataset.addValue(dao.getCurrentValue(WebserviceInterceptor.WSCALL_HYPHEN + month, 0L, tenantId),
105-
"calls", monthName);
106-
}
107-
108-
JFreeChart chart = ChartFactory.createBarChart("", null, null, dataset, PlotOrientation.VERTICAL, false,
109-
false, false);
110-
111-
CategoryPlot catPlot = chart.getCategoryPlot();
112-
catPlot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
113-
BarRenderer r = (BarRenderer) catPlot.getRenderer();
114-
r.setSeriesPaint(0, Color.BLUE);
115-
116-
chart.getTitle().setHorizontalAlignment(HorizontalAlignment.LEFT);
117-
chart.getTitle().setPaint(Color.BLUE);
118-
119-
CategoryAxis domainAxis = catPlot.getDomainAxis();
120-
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
121-
122-
ChartUtilities.saveChartAsPNG(chartFile, chart, width, height);
123-
WebserviceServletUtil.downloadFile(request, response, chartFile, "wschart.png");
124-
} catch (Exception e) {
125-
log.error(e.getMessage(), e);
126-
} finally {
127-
FileUtil.delete(chartFile);
128-
}
20+
super.prefix = WebserviceInterceptor.WSCALL;
21+
super.rowKey = "calls";
12922
}
13023
}

logicaldoc-webservice/src/main/java/com/logicaldoc/webservice/WebserviceInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class WebserviceInterceptor extends AbstractPhaseInterceptor<Message> {
5353

5454
public static final String THREADPOOL_CALL_STORE = "WebserviceCallStore";
5555

56-
private static final String WSCALL = "wscall";
56+
public static final String WSCALL = "wscall";
5757

5858
public static final String WSCALL_HYPHEN = WSCALL + "-";
5959

0 commit comments

Comments
 (0)