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