package org.gcube.portlets.admin.accountingmanager.server.amservice; import java.util.List; import org.gcube.accounting.analytics.Info; import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery; import org.gcube.accounting.analytics.persistence.AccountingPersistenceQueryFactory; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQuery; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQuery4Job; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQuery4Portlet; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQuery4Service; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQuery4Storage; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQuery4Task; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQueryBuilder; import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQueryDirector; import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4Job; import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4Portlet; import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4Service; import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4Storage; import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4Task; import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponseBuilder; import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponseDirector; import org.gcube.portlets.admin.accountingmanager.shared.data.AccountingType; import org.gcube.portlets.admin.accountingmanager.shared.data.query.SeriesRequest; import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesResponse; import org.gcube.portlets.admin.accountingmanager.shared.exception.AccountingManagerServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author giancarlo email: g.panichi@isti.cnr.it * */ public class AccountingCaller { private static Logger logger = LoggerFactory .getLogger(AccountingCaller.class); public AccountingCaller() { } public SeriesResponse getSeries(AccountingType accountingType, SeriesRequest seriesRequest) throws AccountingManagerServiceException { try { logger.debug("getSeries(): [AccountingType="+accountingType+" , seriesRequest=" + seriesRequest+"]"); AccountingPersistenceQuery apq = AccountingPersistenceQueryFactory .getInstance(); AccountingQueryBuilder queryBuilder = getAccountQueryBuilder( accountingType, seriesRequest); AccountingQueryDirector director = new AccountingQueryDirector(); director.setAccountingQueryBuilder(queryBuilder); director.constructAccountingQuery(); AccountingQuery query = director.getAccountingQuery(); if (query == null) { throw new AccountingManagerServiceException( "Error in invocation: Operation not supported"); } List infos = apq.query(query.getType(), query.getTemporalConstraint(), null); if (infos == null) { throw new AccountingManagerServiceException( "Error retrieving list of info: list is null!"); } logger.debug("Retrived Infos"); SeriesResponseBuilder seriesResponseBuilder = getSeriesResponseBuilder( accountingType, infos); SeriesResponseDirector seriesResponseDirector = new SeriesResponseDirector(); seriesResponseDirector .setSeriesResponseBuilder(seriesResponseBuilder); seriesResponseDirector.constructSeriesResponse(); SeriesResponse seriesResponse = seriesResponseDirector .getSeriesResponse(); if (seriesResponse == null) { throw new AccountingManagerServiceException( "Error creating series response!"); } return seriesResponse; } catch (Throwable e) { logger.error("Error in GetSeries(): " + e.getLocalizedMessage()); e.printStackTrace(); throw new AccountingManagerServiceException(e.getLocalizedMessage()); } } private AccountingQueryBuilder getAccountQueryBuilder( AccountingType accountingType, SeriesRequest seriesRequest) throws AccountingManagerServiceException { if (accountingType == null) { throw new AccountingManagerServiceException( "Error accounting type is null"); } switch (accountingType) { case JOB: return new AccountingQuery4Job(seriesRequest); case PORTLET: return new AccountingQuery4Portlet(seriesRequest); case SERVICE: return new AccountingQuery4Service(seriesRequest); case STORAGE: return new AccountingQuery4Storage(seriesRequest); case TASK: return new AccountingQuery4Task(seriesRequest); default: throw new AccountingManagerServiceException( "Error request type is unknow!"); } } private SeriesResponseBuilder getSeriesResponseBuilder( AccountingType accountingType, List infos) throws AccountingManagerServiceException { if (accountingType == null) { throw new AccountingManagerServiceException( "Error accounting type is null"); } switch (accountingType) { case JOB: return new SeriesResponse4Job(infos); case PORTLET: return new SeriesResponse4Portlet(infos); case SERVICE: return new SeriesResponse4Service(infos); case STORAGE: return new SeriesResponse4Storage(infos); case TASK: return new SeriesResponse4Task(infos); default: throw new AccountingManagerServiceException( "Error request type is unknow!"); } } }