Rev 144 | Rev 438 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
18 | jmachado | 1 | package pt.estgp.estgweb.services.urlstat; |
2 | |||
3 | import jomm.utils.MessageResources; |
||
4 | import jomm.utils.MyCalendar; |
||
5 | import org.apache.log4j.Logger; |
||
6 | import org.apache.commons.httpclient.util.URIUtil; |
||
7 | import org.apache.commons.httpclient.URIException; |
||
8 | import org.jfree.chart.ChartFactory; |
||
9 | import org.jfree.chart.ChartUtilities; |
||
10 | import org.jfree.chart.JFreeChart; |
||
11 | import org.jfree.chart.axis.DateAxis; |
||
12 | import org.jfree.chart.plot.XYPlot; |
||
13 | import org.jfree.data.time.Day; |
||
14 | import org.jfree.data.time.TimeSeries; |
||
15 | import org.jfree.data.time.TimeSeriesCollection; |
||
16 | import org.jfree.data.time.Month; |
||
17 | import pt.estgp.estgweb.domain.UrlStat; |
||
18 | import pt.estgp.estgweb.domain.UrlStatDay; |
||
19 | import pt.estgp.estgweb.domain.UrlStatMonth; |
||
20 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
21 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
||
22 | import pt.estgp.estgweb.Globals; |
||
23 | import jomm.utils.DatesUtils; |
||
24 | import jomm.dao.impl.AbstractDao; |
||
25 | import jomm.web.utils.TestHttpServletRequest; |
||
26 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
27 | |||
28 | import javax.servlet.http.HttpServletRequest; |
||
29 | import java.awt.*; |
||
30 | import java.io.File; |
||
31 | import java.io.IOException; |
||
32 | import java.util.Date; |
||
33 | import java.util.List; |
||
34 | import java.util.ArrayList; |
||
35 | import java.text.SimpleDateFormat; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * @author Jorge Machado |
||
40 | * @date 15/Mar/2008 |
||
41 | * @time 6:55:38 |
||
42 | * @see pt.estgp.estgweb.services.urlstat |
||
43 | */ |
||
44 | public class ChartBuilderService implements IService |
||
45 | { |
||
46 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(ChartBuilderService.class); |
||
47 | |||
144 | jmachado | 48 | public static final long LINK_CHART_EXPIRE_INTERVAL = Globals.URL_STAT_CHART_EXPIRE_INTERVAL; |
49 | public static final 1.5.0/docs/api/java/lang/String.html">String LINK_CHART_TMP_DIR = Globals.URL_STAT_CHART_TMP_DIR; |
||
18 | jmachado | 50 | |
51 | /** |
||
52 | * Return file image location |
||
53 | * |
||
54 | * @param relativePaths to analysis |
||
55 | * @param request to get user language |
||
56 | * @param type of chart |
||
57 | * @return image location |
||
58 | * @throws ServiceException error executing |
||
59 | |||
60 | */ |
||
61 | public 1.5.0/docs/api/java/lang/String.html">String run(HttpServletRequest request, List<String> relativePaths, ChartTypeEnum type) throws ServiceException |
||
62 | { |
||
63 | MyCalendar c = new MyCalendar(); |
||
64 | int day = c.getDay(); |
||
65 | int month = c.getMonth(); |
||
66 | int year = c.getYear(); |
||
67 | if(type == ChartTypeEnum.weekDays) |
||
68 | return build(request,type.getMessageKey(), relativePaths, year,month,day,7,type); |
||
69 | else if(type == ChartTypeEnum.monthDays) |
||
70 | return build(request,type.getMessageKey(), relativePaths, year,month,day,31,type); |
||
71 | else if(type == ChartTypeEnum.trimestreDays) |
||
72 | return build(request,type.getMessageKey(), relativePaths, year,month,day,90,type); |
||
73 | else if(type == ChartTypeEnum.semestreDays) |
||
74 | return build(request,type.getMessageKey(), relativePaths, year,month,day,180,type); |
||
75 | else if(type == ChartTypeEnum.yearDays) |
||
76 | return build(request,type.getMessageKey(), relativePaths, year,month,day,365,type); |
||
77 | else if(type == ChartTypeEnum.trimestreMonths) |
||
78 | return build(request,type.getMessageKey(), relativePaths, year,month,3,type); |
||
79 | else if(type == ChartTypeEnum.semestreMonths) |
||
80 | return build(request,type.getMessageKey(), relativePaths, year,month,6,type); |
||
81 | else if(type == ChartTypeEnum.yearMonths) |
||
82 | return build(request,type.getMessageKey(), relativePaths, year,month,12,type); |
||
83 | else if(type == ChartTypeEnum.twoYearsMonths) |
||
84 | return build(request,type.getMessageKey(), relativePaths, year,month,24,type); |
||
85 | return null; |
||
86 | } |
||
87 | |||
88 | |||
89 | private 1.5.0/docs/api/java/lang/String.html">String build(HttpServletRequest request, 1.5.0/docs/api/java/lang/String.html">String messageTitle, List<String> relativePaths, int untilYear, int untilMonth, int untilDay, int days, ChartTypeEnum type) |
||
90 | { |
||
91 | 5+0%2Fdocs%2Fapi+Date">Date startDate = DatesUtils.getStartDate(untilYear,untilMonth,untilDay,days); |
||
92 | MyCalendar c = new MyCalendar(untilYear,untilMonth, untilDay); |
||
93 | 5+0%2Fdocs%2Fapi+Date">Date endDate = c.getTime(); |
||
94 | return build(request,messageTitle,relativePaths,startDate,endDate,type); |
||
95 | } |
||
96 | |||
97 | private 1.5.0/docs/api/java/lang/String.html">String build(HttpServletRequest request, 1.5.0/docs/api/java/lang/String.html">String messageTitle, List<String> relativePaths, int untilYear, int untilMonth, int months,ChartTypeEnum type) |
||
98 | { |
||
99 | 5+0%2Fdocs%2Fapi+Date">Date startDate = DatesUtils.getStartDate(untilYear,untilMonth,months); |
||
100 | MyCalendar c = new MyCalendar(untilYear,untilMonth, 1); |
||
101 | 5+0%2Fdocs%2Fapi+Date">Date endDate = c.getTime(); |
||
102 | return build(request,messageTitle,relativePaths,startDate,endDate,type); |
||
103 | } |
||
104 | |||
105 | private 1.5.0/docs/api/java/lang/String.html">String build(HttpServletRequest request, 1.5.0/docs/api/java/lang/String.html">String messageTitle, List<String> relativePaths, 5+0%2Fdocs%2Fapi+Date">Date startDate, 5+0%2Fdocs%2Fapi+Date">Date endDate,ChartTypeEnum type) |
||
106 | { |
||
55 | jmachado | 107 | 1.5.0/docs/api/java/lang/String.html">String location = linkToFileNameEncode(relativePaths,startDate,endDate,type.getScale(),true); |
18 | jmachado | 108 | 1.5.0/docs/api/java/io/File.html">File img = new 1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + location); |
109 | if(img.exists() && ((1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() - img.lastModified()) < LINK_CHART_EXPIRE_INTERVAL)) |
||
110 | { |
||
111 | return location; |
||
112 | } |
||
113 | img.getParentFile().mkdirs(); |
||
114 | |||
115 | TimeSeriesCollection dataset = new TimeSeriesCollection(); |
||
116 | for(1.5.0/docs/api/java/lang/String.html">String relativePath: relativePaths) |
||
117 | { |
||
359 | jmachado | 118 | try |
119 | { |
||
120 | relativePath = URIUtil.encodeWithinQuery(relativePath,"ISO-8859-1"); |
||
121 | } |
||
122 | catch (1.5.0/docs/api/javax/print/URIException.html">URIException e) |
||
123 | { |
||
124 | logger.error(e,e); |
||
125 | return null; |
||
126 | } |
||
18 | jmachado | 127 | UrlStat urlStat = DaoFactory.getUrlStatDaoImpl().loadOrNull(relativePath); |
128 | if(urlStat != null) |
||
129 | { |
||
130 | TimeSeries s = new TimeSeries(urlStat.getName(), type.getScale().getRegularTimePeriodClass()); |
||
131 | if(type.getScale() == TimeSeriesScaleEnum.days) |
||
132 | { |
||
133 | List<UrlStatDay> urlStatDays = DaoFactory.getUrlStatDayDaoImpl().loadDays(relativePath,startDate,endDate); |
||
134 | for(UrlStatDay urlStatDay: urlStatDays) |
||
135 | { |
||
136 | s.add(new Day( |
||
137 | urlStatDay.getDate()), |
||
138 | urlStatDay.getTotalClicks()); |
||
139 | } |
||
140 | } |
||
141 | else if(type.getScale() == TimeSeriesScaleEnum.months) |
||
142 | { |
||
143 | List<UrlStatMonth> urlStatMonths = DaoFactory.getUrlStatMonthDaoImpl().loadByMonth(relativePath,startDate,endDate); |
||
144 | for(UrlStatMonth urlStatMonth: urlStatMonths) |
||
145 | { |
||
146 | s.add(new Month( |
||
147 | urlStatMonth.getDate()), |
||
148 | urlStatMonth.getTotalClicks()); |
||
149 | } |
||
150 | } |
||
151 | dataset.addSeries(s); |
||
152 | } |
||
153 | } |
||
154 | // dataset.setDomainIsPointsInTime(true); |
||
155 | |||
156 | |||
157 | JFreeChart chart = ChartFactory.createTimeSeriesChart( |
||
158 | MessageResources.getMessage(request,messageTitle), |
||
159 | MessageResources.getMessage(request,"url.stat.days"), |
||
160 | MessageResources.getMessage(request,"url.stat.access.clicks"), |
||
161 | dataset, |
||
162 | true, |
||
163 | true, |
||
164 | false |
||
165 | ); |
||
166 | chart.setBackgroundImage(null); |
||
167 | XYPlot plot = (XYPlot) chart.getPlot(); |
||
168 | DateAxis axis = (DateAxis) plot.getDomainAxis(); |
||
169 | axis.setDateFormatOverride(type.getSimpleDateFormat()); |
||
170 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.lightGray); |
||
171 | plot.setDomainGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.white); |
||
172 | plot.setDomainGridlinesVisible(true); |
||
173 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.white); |
||
174 | try |
||
175 | { |
||
176 | ChartUtilities.saveChartAsPNG(img, chart, 800, 300); |
||
177 | return location; |
||
178 | } |
||
179 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
180 | { |
||
181 | logger.error(e,e); |
||
182 | } |
||
183 | return null; |
||
184 | } |
||
185 | |||
186 | |||
187 | |||
188 | private static 1.5.0/docs/api/java/lang/String.html">String linkToFileName(List<String> relativePaths, 5+0%2Fdocs%2Fapi+Date">Date startDate, 5+0%2Fdocs%2Fapi+Date">Date endDate, TimeSeriesScaleEnum seriesScaleEnum) |
||
189 | { |
||
190 | return linkToFileNameEncode(relativePaths,startDate,endDate,seriesScaleEnum,false); |
||
191 | } |
||
192 | |||
193 | private static 1.5.0/docs/api/java/lang/String.html">String linkToFileNameEncode(List<String> relativePaths, 5+0%2Fdocs%2Fapi+Date">Date startDate, 5+0%2Fdocs%2Fapi+Date">Date endDate, TimeSeriesScaleEnum seriesScaleEnum,boolean encode) |
||
194 | { |
||
195 | try |
||
196 | { |
||
197 | 1.5.0/docs/api/java/lang/String.html">String relativePathName = ""; |
||
198 | for(1.5.0/docs/api/java/lang/String.html">String relativePath: relativePaths) |
||
199 | { |
||
200 | relativePathName += relativePath + "_"; |
||
201 | } |
||
202 | 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat sp = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat("yyyy_MM_dd"); |
||
203 | 1.5.0/docs/api/java/lang/String.html">String dates = sp.format(startDate) + "_UNTIL_" + sp.format(endDate) + "_" + seriesScaleEnum.getType(); |
||
204 | 1.5.0/docs/api/java/lang/String.html">String path; |
||
205 | if(encode) |
||
359 | jmachado | 206 | path = URIUtil.encodeWithinQuery(relativePathName.replaceAll("/","_"),"ISO-8859-1"); |
18 | jmachado | 207 | else |
208 | path = relativePathName.replaceAll("/","_"); |
||
209 | return LINK_CHART_TMP_DIR + "/" + path + "_" + dates + ".png"; |
||
210 | } |
||
211 | catch (1.5.0/docs/api/javax/print/URIException.html">URIException e) |
||
212 | { |
||
213 | logger.fatal(e,e); |
||
214 | return null; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Simple test to create link clicks from 2003 until now (PS: call first: UrlStatService.main) and generate charts of all types |
||
221 | * @param args |
||
222 | * @throws ServiceException |
||
223 | */ |
||
224 | |||
359 | jmachado | 225 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException, 1.5.0/docs/api/javax/print/URIException.html">URIException |
18 | jmachado | 226 | { |
227 | // UrlStatService.main(null); |
||
359 | jmachado | 228 | // AbstractDao.getCurrentSession().beginTransaction(); |
229 | // List<String> relativePaths = new ArrayList<String>(); |
||
230 | // relativePaths.add("/public/announcements/getFlashNews.jsp?sid=0.9998549583612295"); |
||
231 | 1.5.0/docs/api/java/lang/System.html">System.out.println(URIUtil.decode(URIUtil.encodeWithinQuery("/public/announcements/getFlashNews.jsp?sid=0.9998549583612295&jorge=j","ISO-8859-1"))); |
||
18 | jmachado | 232 | // relativePaths.add("/teste2"); |
359 | jmachado | 233 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.weekDays)); |
234 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.monthDays)); |
||
235 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.trimestreDays)); |
||
236 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.semestreDays)); |
||
237 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.yearDays)); |
||
238 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.trimestreMonths)); |
||
239 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.semestreMonths)); |
||
240 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.yearMonths)); |
||
241 | // System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.twoYearsMonths)); |
||
242 | // AbstractDao.getCurrentSession().getTransaction().commit(); |
||
18 | jmachado | 243 | } |
244 | |||
245 | } |