accounting-dashboard/src/main/java/org/gcube/portlets/user/accountingdashboard/server/accounting/AccountingService.java

85 lines
2.6 KiB
Java

package org.gcube.portlets.user.accountingdashboard.server.accounting;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.gcube.data.access.accounting.summary.access.AccountingDao;
import org.gcube.data.access.accounting.summary.access.model.ScopeDescriptor;
import org.gcube.portlets.user.accountingdashboard.shared.data.ScopeData;
import org.gcube.portlets.user.accountingdashboard.shared.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Giancarlo Panichi
*
*/
public class AccountingService {
private static Logger logger = LoggerFactory.getLogger(AccountingService.class);
private AccountingDao dao;
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);
}
}
public ScopeData getTree(HttpServletRequest httpServletRequest) throws ServiceException {
try {
logger.debug("AccountingService GetTree()");
ScopeDescriptor scopeDescriptor = dao.getTree(httpServletRequest);
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);
}
}
return scopeData;
}
}