package org.gcube.dataharvest; import java.io.IOException; import java.io.InputStream; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Properties; import org.gcube.dataharvest.dao.DatabaseManager; import org.gcube.dataharvest.datamodel.Harvest; import org.gcube.dataharvest.harvester.DataMethodDownloadHarvester; import org.gcube.dataharvest.harvester.MethodInvocationHarvester; import org.gcube.dataharvest.harvester.ResourceCatalogueHarvester; import org.gcube.dataharvest.harvester.SocialHarvester; import org.gcube.dataharvest.harvester.VreUsersHarvester; import org.gcube.dataharvest.utils.ContextAuthorization; import org.gcube.vremanagement.executor.plugin.Plugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AccountingDataHarvesterPlugin extends Plugin { private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterPlugin.class); private static final String PROPERTY_FILENAME = "config.properties"; public static final String PARAMETER_FROM = "from"; public static final String PARAMETER_TO = "to"; public static final String TEST = "test"; private boolean testMode = false, updateFlag = false; private Date dateFrom, dateTo; private Properties properties; private void getConfigParameters() throws IOException { properties = new Properties(); InputStream input = AccountingDataHarvesterPlugin.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME); properties.load(input); } public AccountingDataHarvesterPlugin(DataHarvestPluginDeclaration pluginDeclaration) { super(pluginDeclaration); } /** {@inheritDoc} */ @Override public void launch(Map inputs) throws Exception { logger.debug("DataHarvestPlugin: launch()"); try { getConfigParameters(); }catch (Exception e) { logger.warn("Unable to load {} file containing configuration properties. AccountingDataHarvesterPlugin will use defaults", PROPERTY_FILENAME); } ContextAuthorization contextAuthorization = new ContextAuthorization(properties); String message = ""; DatabaseManager dbaseManager = new DatabaseManager(testMode); if (inputs == null) { message = "Invalid input parameter (null)"; logger.error(message); throw new Exception(message, null); } Object from = inputs.get(PARAMETER_FROM); if (!checkDate(from)) { message = "Missing or invalid input parameter: " + PARAMETER_FROM; logger.error(message); throw new Exception(message, null); } dateFrom = (Date)from; Object to = inputs.get(PARAMETER_TO); if (!checkDate(to)) { message = "Missing or invalid input parameter: " + PARAMETER_TO; logger.error(message); throw new Exception(message, null); } dateTo = (Date)to; testMode = (inputs.get(TEST) == null) ? false : (boolean) inputs.get(TEST); logger.debug("TEST mode is " + testMode); setTimePeriod(); logger.debug("Time period: " + dateFrom.toString() + " -> " + dateTo.toString()); try { // collecting info on VRE users VreUsersHarvester vreUsersHarvester = new VreUsersHarvester(dateFrom, dateTo); List users = vreUsersHarvester.getData(); if(!testMode) { dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, users, updateFlag); } } catch (Exception x) { logger.error(x.getLocalizedMessage()); } try { // collecting info on Res. Catalogue (Dataset, Application, Deliverables, Methods) ResourceCatalogueHarvester resourceCatalogueHarvester = new ResourceCatalogueHarvester(dateFrom, dateTo); List res = resourceCatalogueHarvester.getData(); if(!testMode) { dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, res, updateFlag); } } catch (Exception x) { logger.error(x.getLocalizedMessage()); } try { // collecting info on Data/Method download DataMethodDownloadHarvester dataMethodDownloadHarvester = new DataMethodDownloadHarvester(dateFrom, dateTo); List res = dataMethodDownloadHarvester.getData(); if(!testMode) { dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, res, updateFlag); } } catch (Exception x) { logger.error(x.getLocalizedMessage()); } try { // collecting info on social (posts, replies and likes) SocialHarvester socialHarvester = new SocialHarvester(dateFrom, dateTo); List res = socialHarvester.getData(); if(!testMode) { dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, res, updateFlag); } } catch (Exception x) { logger.error(x.getLocalizedMessage()); } try { // collecting info on method invocation MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(dateFrom, dateTo); List res = methodInvocationHarvester.getData(); if(!testMode) { dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, res, updateFlag); } } catch (Exception x) { logger.error(x.getLocalizedMessage()); } } /** {@inheritDoc} */ @Override protected void onStop() throws Exception { logger.debug("DataHarvestPlugin: onStop()"); } private boolean checkDate(Object date) { if (date == null) return false; return date instanceof java.util.Date; } // private void insertMonthlyData(Date from, Date to, List data) { // Dao dao = null; // try { // dao = dbConnect(); // dao.insertMonthlyMeasure(data, from, to, false); // } catch (Exception e) { // logger.error(e.getLocalizedMessage()); // } finally { // if (dao != null) { // try { // dao.disconnect(); // } catch (DaoException e) { // logger.error(e.getLocalizedMessage()); // } // } // } // } // // private Dao dbConnect() throws DaoException { // DatabaseDataExplorer dde = new DatabaseDataExplorer(); // dde.setTestMode(testMode); // DatabaseConnectionData dcd = dde.retrieveDatabaseInfo(); // Dao dao = new Dao(); // dao.init(); // dao.connect(dcd.getUrl(), dcd.getUser(), dcd.getPassword()); // return dao; // } private void setTimePeriod() { Calendar from = Calendar.getInstance(); from.setTime(dateFrom); from.set(Calendar.HOUR_OF_DAY, 0); from.set(Calendar.MINUTE, 0); from.set(Calendar.SECOND, 0); dateFrom = from.getTime(); Calendar to = Calendar.getInstance(); to.setTime(dateTo); to.set(Calendar.HOUR_OF_DAY, 23); to.set(Calendar.MINUTE, 59); to.set(Calendar.SECOND, 59); dateTo = to.getTime(); } }