2018-07-05 18:29:38 +02:00
|
|
|
package org.gcube.portlets.user.accountingdashboard.server.accounting;
|
|
|
|
|
2018-07-06 17:48:46 +02:00
|
|
|
import java.util.ArrayList;
|
2018-07-09 19:08:52 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
2018-07-06 17:48:46 +02:00
|
|
|
|
2018-07-09 15:00:51 +02:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
2018-07-05 18:29:38 +02:00
|
|
|
import org.gcube.data.access.accounting.summary.access.AccountingDao;
|
2018-07-09 19:08:52 +02:00
|
|
|
import org.gcube.data.access.accounting.summary.access.model.MeasureResolution;
|
|
|
|
import org.gcube.data.access.accounting.summary.access.model.Report;
|
2018-07-05 18:29:38 +02:00
|
|
|
import org.gcube.data.access.accounting.summary.access.model.ScopeDescriptor;
|
2018-07-09 19:08:52 +02:00
|
|
|
import org.gcube.portlets.user.accountingdashboard.shared.data.ReportData;
|
|
|
|
import org.gcube.portlets.user.accountingdashboard.shared.data.RequestReportData;
|
2018-07-06 17:48:46 +02:00
|
|
|
import org.gcube.portlets.user.accountingdashboard.shared.data.ScopeData;
|
|
|
|
import org.gcube.portlets.user.accountingdashboard.shared.exception.ServiceException;
|
2018-07-05 18:29:38 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Giancarlo Panichi
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class AccountingService {
|
2018-07-06 17:48:46 +02:00
|
|
|
|
2018-07-05 18:29:38 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(AccountingService.class);
|
|
|
|
|
|
|
|
private AccountingDao dao;
|
|
|
|
|
2018-07-06 17:48:46 +02:00
|
|
|
public AccountingService(AccountingServiceType accountingServiceType) throws ServiceException {
|
|
|
|
try {
|
|
|
|
if (accountingServiceType == null) {
|
|
|
|
logger.error("Invalid AccountingServiceType requested: null");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (accountingServiceType) {
|
|
|
|
case CurrentScope:
|
|
|
|
logger.debug("AccountingService: CurrentScope");
|
|
|
|
dao = AccountingDao.get();
|
|
|
|
break;
|
|
|
|
case PortalContex:
|
|
|
|
logger.debug("AccountingService: PortalContext");
|
|
|
|
dao = AccountingDao.get(new PortalContextTreeProvider());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logger.debug("AccountingService: CurrentScope");
|
|
|
|
dao = AccountingDao.get();
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error("Error retrieving Tree: " + e.getLocalizedMessage(), e);
|
|
|
|
throw new ServiceException("Error retrieving Tree: " + e.getLocalizedMessage(), e);
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:29:38 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 15:00:51 +02:00
|
|
|
public ScopeData getTree(HttpServletRequest httpServletRequest) throws ServiceException {
|
2018-07-05 18:29:38 +02:00
|
|
|
try {
|
|
|
|
logger.debug("AccountingService GetTree()");
|
2018-07-09 15:00:51 +02:00
|
|
|
ScopeDescriptor scopeDescriptor = dao.getTree(httpServletRequest);
|
2018-07-06 17:48:46 +02:00
|
|
|
logger.debug("ScopeDescriptor: " + scopeDescriptor);
|
|
|
|
ScopeData scopeData = getScopeData(scopeDescriptor);
|
|
|
|
return scopeData;
|
|
|
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error("Error retrieving Tree: " + e.getLocalizedMessage(), e);
|
|
|
|
throw new ServiceException("Error retrieving Tree: " + e.getLocalizedMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ScopeData getScopeData(ScopeDescriptor scopeDescriptor) {
|
|
|
|
ScopeData scopeData = null;
|
|
|
|
if (scopeDescriptor != null) {
|
|
|
|
if (scopeDescriptor.hasChildren()) {
|
|
|
|
ArrayList<ScopeData> childs = new ArrayList<>();
|
|
|
|
for (ScopeDescriptor sd : scopeDescriptor.getChildren()) {
|
|
|
|
childs.add(getScopeData(sd));
|
|
|
|
}
|
|
|
|
scopeData = new ScopeData(scopeDescriptor.getId(), scopeDescriptor.getName(), childs);
|
|
|
|
} else {
|
|
|
|
scopeData = new ScopeData(scopeDescriptor.getId(), scopeDescriptor.getName(), null);
|
|
|
|
}
|
2018-07-05 18:29:38 +02:00
|
|
|
}
|
2018-07-06 17:48:46 +02:00
|
|
|
return scopeData;
|
|
|
|
|
2018-07-05 18:29:38 +02:00
|
|
|
}
|
2018-07-09 19:08:52 +02:00
|
|
|
|
|
|
|
public ReportData getReport(RequestReportData requestReportData) throws ServiceException {
|
|
|
|
try {
|
|
|
|
logger.debug("AccountingService GetReport()");
|
|
|
|
|
|
|
|
ScopeDescriptor scopeDescriptor=searchScopeDescriptor(requestReportData.getScopeData());
|
|
|
|
|
|
|
|
int yearFrom=Integer.parseInt(requestReportData.getYearFrom());
|
|
|
|
int monthFrom=Integer.parseInt(requestReportData.getMonthFrom());
|
|
|
|
|
|
|
|
int yearTo=Integer.parseInt(requestReportData.getYearTo());
|
|
|
|
int monthTo=Integer.parseInt(requestReportData.getMonthTo());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date from=new GregorianCalendar(yearFrom, monthFrom, 1).getTime();
|
|
|
|
Date to=new GregorianCalendar(yearTo, monthTo, 1).getTime();
|
|
|
|
|
|
|
|
|
|
|
|
Report report = dao.getReportByScope(scopeDescriptor, from, to,MeasureResolution.MONTHLY);
|
|
|
|
|
|
|
|
logger.debug("Report: " + report);
|
|
|
|
ReportData reportData=new ReportData();
|
|
|
|
|
|
|
|
return reportData;
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error("Error retrieving Tree: " + e.getLocalizedMessage(), e);
|
|
|
|
throw new ServiceException("Error retrieving Tree: " + e.getLocalizedMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ScopeDescriptor searchScopeDescriptor(ScopeData scopeData){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:48:46 +02:00
|
|
|
|
2018-07-05 18:29:38 +02:00
|
|
|
}
|