package pt.estgp.estgweb.services.urlstat;
import jomm.utils.BytesUtils;
import jomm.utils.DatesUtils;
import jomm.utils.MessageResources;
import jomm.utils.MyCalendar;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import pt.estgp.estgweb.domain.UrlStat;
import pt.estgp.estgweb.domain.UrlStatDay;
import pt.estgp.estgweb.domain.UrlStatMonth;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.utils.Globals;
import pt.utl.ist.berserk.logic.serviceManager.IService;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @author Jorge Machado
* @date 15/Mar/2008
* @time 6:55:38
* @see pt.estgp.estgweb.services.urlstat
*/
public class ChartBuilderService
implements IService
{
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);
public static final long LINK_CHART_EXPIRE_INTERVAL = Globals.
URL_STAT_CHART_EXPIRE_INTERVAL;
public static final 1.5.0/docs/api/java/lang/String.html">String LINK_CHART_TMP_DIR = Globals.
URL_STAT_CHART_TMP_DIR;
/**
* Return file image location
*
* @param relativePaths to analysis
* @param request to get user language
* @param type of chart
* @return image location
* @throws ServiceException error executing
*/
public 1.5.0/docs/api/java/lang/String.html">String run
(HttpServletRequest request, List
<String
> relativePaths, ChartTypeEnum type
) throws ServiceException
{
MyCalendar c =
new MyCalendar
();
int day = c.
getDay();
int month = c.
getMonth();
int year = c.
getYear();
if(type == ChartTypeEnum.
weekDays)
return build
(request,type.
getMessageKey(), relativePaths, year,month,day,7,type
);
else if(type == ChartTypeEnum.
monthDays)
return build
(request,type.
getMessageKey(), relativePaths, year,month,day,31,type
);
else if(type == ChartTypeEnum.
trimestreDays)
return build
(request,type.
getMessageKey(), relativePaths, year,month,day,90,type
);
else if(type == ChartTypeEnum.
semestreDays)
return build
(request,type.
getMessageKey(), relativePaths, year,month,day,180,type
);
else if(type == ChartTypeEnum.
yearDays)
return build
(request,type.
getMessageKey(), relativePaths, year,month,day,365,type
);
else if(type == ChartTypeEnum.
trimestreMonths)
return build
(request,type.
getMessageKey(), relativePaths, year,month,3,type
);
else if(type == ChartTypeEnum.
semestreMonths)
return build
(request,type.
getMessageKey(), relativePaths, year,month,6,type
);
else if(type == ChartTypeEnum.
yearMonths)
return build
(request,type.
getMessageKey(), relativePaths, year,month,12,type
);
else if(type == ChartTypeEnum.
twoYearsMonths)
return build
(request,type.
getMessageKey(), relativePaths, year,month,24,type
);
return null;
}
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
)
{
5+0%2Fdocs%2Fapi+Date">Date startDate = DatesUtils.
getStartDate(untilYear,untilMonth,untilDay,days
);
MyCalendar c =
new MyCalendar
(untilYear,untilMonth, untilDay
);
5+0%2Fdocs%2Fapi+Date">Date endDate = c.
getTime();
return build
(request,messageTitle,relativePaths,startDate,endDate,type
);
}
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
)
{
5+0%2Fdocs%2Fapi+Date">Date startDate = DatesUtils.
getStartDate(untilYear,untilMonth,months
);
MyCalendar c =
new MyCalendar
(untilYear,untilMonth, 1
);
5+0%2Fdocs%2Fapi+Date">Date endDate = c.
getTime();
return build
(request,messageTitle,relativePaths,startDate,endDate,type
);
}
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
)
{
1.5.0/docs/api/java/lang/String.html">String location = linkToFileNameEncode
(relativePaths,startDate,endDate,type.
getScale(),
true);
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
);
if(img.
exists() && ((1.5.0/docs/api/java/lang/System.html">System.
currentTimeMillis() - img.
lastModified()) < LINK_CHART_EXPIRE_INTERVAL
))
{
return location
;
}
img.
getParentFile().
mkdirs();
TimeSeriesCollection dataset =
new TimeSeriesCollection
();
for(1.5.0/docs/api/java/lang/String.html">String relativePath: relativePaths
)
{
try
{
relativePath = BytesUtils.
getMD5(URIUtil.
encodeWithinQuery(relativePath,
"ISO-8859-1"));
}
catch (1.5.0/docs/api/javax/print/URIException.html">URIException e
)
{
logger.
error(e,e
);
return null;
}
UrlStat urlStat = DaoFactory.
getUrlStatDaoImpl().
loadOrNull(relativePath
);
if(urlStat
!=
null)
{
TimeSeries s =
new TimeSeries
(urlStat.
getName(), type.
getScale().
getRegularTimePeriodClass());
if(type.
getScale() == TimeSeriesScaleEnum.
days)
{
List
<UrlStatDay
> urlStatDays = DaoFactory.
getUrlStatDayDaoImpl().
loadDays(relativePath,startDate,endDate
);
for(UrlStatDay urlStatDay: urlStatDays
)
{
s.
add(new Day
(
urlStatDay.
getDate()),
urlStatDay.
getTotalClicks());
}
}
else if(type.
getScale() == TimeSeriesScaleEnum.
months)
{
List
<UrlStatMonth
> urlStatMonths = DaoFactory.
getUrlStatMonthDaoImpl().
loadByMonth(relativePath,startDate,endDate
);
for(UrlStatMonth urlStatMonth: urlStatMonths
)
{
s.
add(new Month
(
urlStatMonth.
getDate()),
urlStatMonth.
getTotalClicks());
}
}
dataset.
addSeries(s
);
}
}
// dataset.setDomainIsPointsInTime(true);
JFreeChart chart = ChartFactory.
createTimeSeriesChart(
MessageResources.
getMessage(request,messageTitle
),
MessageResources.
getMessage(request,
"url.stat.days"),
MessageResources.
getMessage(request,
"url.stat.access.clicks"),
dataset,
true,
true,
false
);
chart.
setBackgroundImage(null);
XYPlot plot =
(XYPlot
) chart.
getPlot();
DateAxis axis =
(DateAxis
) plot.
getDomainAxis();
axis.
setDateFormatOverride(type.
getSimpleDateFormat());
plot.
setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.
lightGray);
plot.
setDomainGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.
white);
plot.
setDomainGridlinesVisible(true);
plot.
setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.
white);
try
{
ChartUtilities.
saveChartAsPNG(img, chart, 800, 300
);
return location
;
}
catch (1.5.0/docs/api/java/io/IOException.html">IOException e
)
{
logger.
error(e,e
);
}
return null;
}
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
)
{
return linkToFileNameEncode
(relativePaths,startDate,endDate,seriesScaleEnum,
false);
}
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
)
{
try
{
1.5.0/docs/api/java/lang/String.html">String relativePathName =
"";
for(1.5.0/docs/api/java/lang/String.html">String relativePath: relativePaths
)
{
relativePathName += relativePath +
"_";
}
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");
1.5.0/docs/api/java/lang/String.html">String dates = sp.
format(startDate
) +
"_UNTIL_" + sp.
format(endDate
) +
"_" + seriesScaleEnum.
getType();
1.5.0/docs/api/java/lang/String.html">String path
;
if(encode
)
path = URIUtil.
encodeWithinQuery(relativePathName.
replaceAll("/",
"_"),
"ISO-8859-1");
else
path = relativePathName.
replaceAll("/",
"_");
return LINK_CHART_TMP_DIR +
"/" + path +
"_" + dates +
".png";
}
catch (1.5.0/docs/api/javax/print/URIException.html">URIException e
)
{
logger.
fatal(e,e
);
return null;
}
}
/**
* Simple test to create link clicks from 2003 until now (PS: call first: UrlStatService.main) and generate charts of all types
* @param args
* @throws ServiceException
*/
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
{
// UrlStatService.main(null);
// AbstractDao.getCurrentSession().beginTransaction();
// List<String> relativePaths = new ArrayList<String>();
// relativePaths.add("/public/announcements/getFlashNews.jsp?sid=0.9998549583612295");
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")));
// relativePaths.add("/teste2");
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.weekDays));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.monthDays));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.trimestreDays));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.semestreDays));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.yearDays));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.trimestreMonths));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.semestreMonths));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.yearMonths));
// System.out.println(new ChartBuilderService().run(new TestHttpServletRequest(),relativePaths, ChartTypeEnum.twoYearsMonths));
// AbstractDao.getCurrentSession().getTransaction().commit();
}
}