package org.gcube.dataharvest.harvester.sobigdata; import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.SortedSet; import org.apache.commons.lang.Validate; import org.gcube.common.homelibrary.home.Home; import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.homelibrary.home.HomeManager; import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; import org.gcube.common.homelibrary.home.workspace.accounting.AccountingEntry; import org.gcube.common.homelibrary.jcr.repository.JCRRepository; import org.gcube.common.homelibrary.jcr.workspace.JCRWorkspace; import org.gcube.common.homelibrary.jcr.workspace.JCRWorkspaceItem; import org.gcube.dataharvest.datamodel.HarvestedData; import org.gcube.dataharvest.datamodel.HarvestedDataKey; import org.gcube.dataharvest.utils.DateUtils; import org.gcube.dataharvest.utils.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Eric Perrone (ISTI - CNR) * @author Luca Frosini (ISTI - CNR) */ public class DataMethodDownloadHarvester extends SoBigDataHarvester { private static Logger logger = LoggerFactory.getLogger(DataMethodDownloadHarvester.class); private int count = 0; public DataMethodDownloadHarvester(Date start, Date end, String catalogueContext, SortedSet contexts) throws ParseException { super(start, end, catalogueContext, contexts); } @Override public List getData() throws Exception { String defaultContext = Utils.getCurrentContext(); logger.debug("The context is: "+defaultContext); try { String vreName = getVRENameToHL(defaultContext); logger.debug("Getting VRE Name to HL from context/scope returns: "+vreName); String user = vreName + "-Manager"; logger.debug("Using user '"+user+"' to getHome from HL"); //ISTANCING HL AND GETTING HOME FOR VRE MANAGER HomeManager manager = HomeLibrary.getHomeManagerFactory().getHomeManager(); @SuppressWarnings("deprecation") Home home = manager.getHome(user); JCRWorkspace ws = (JCRWorkspace) home.getWorkspace(); // String path = "/Workspace/MySpecialFolders/" + vreName; logger.debug("Getting item by Path: "+path); JCRWorkspaceItem item = (JCRWorkspaceItem) ws.getItemByPath(path); // logger.info("Analyzing " + defaultContext + " in the period [" + startDate.toString() + " to " + endDate.toString() +"] starting from root: "+item.getName()); HarvestedData defaultHarvesteData = new HarvestedData(HarvestedDataKey.DATA_METHOD_DOWNLOAD, defaultContext, count); List data = new ArrayList(); for (WorkspaceItem children: item.getChildren()) { count = 0; //resettings the counter HarvestedData harvestedData; //Getting statistics for folder if(children.isFolder()){ logger.info("Getting statistics for folder: "+children.getName()); getStats(children, startDate, endDate); String normalizedName = children.getName().replaceAll("[^A-Za-z0-9]",""); String scope = mapWsFolderNameToVRE.get(normalizedName); //Checking if it is a VRE name to right accouning... if(scope!=null && !scope.isEmpty()){ logger.info("Found scope '" + scope + "' matching with normalized VRE name: "+normalizedName); harvestedData = new HarvestedData(HarvestedDataKey.DATA_METHOD_DOWNLOAD, scope, count); data.add(harvestedData); logger.info("Added data: "+harvestedData); }else{ logger.info("No scope found matching the folder name: "+normalizedName +", accounting its stats in the default context: "+defaultContext); //INCREASING THE DEFAULT CONTEXT COUNTER... defaultHarvesteData.setMeasure(defaultHarvesteData.getMeasure()+count); logger.info("Increased default context stats: "+defaultHarvesteData); } } } //ADDING DEFAULT ACCOUNTING data.add(defaultHarvesteData); logger.info("In the period [from "+startDate+" to "+endDate+ "] returning workspace accouting data:"); for (HarvestedData harvestedData : data) { logger.info(harvestedData.toString()); } return data; } catch(Exception e) { throw e; } } /** * Gets the stats. * * @param baseItem the base item * @param start the start * @param end the end * @return the stats * @throws InternalErrorException the internal error exception */ private void getStats(WorkspaceItem baseItem, Date start, Date end) throws InternalErrorException { List children; if(baseItem.isFolder()) { children = baseItem.getChildren(); for(WorkspaceItem child : children) getStats(child, start, end); } else { try { List accounting = baseItem.getAccounting(); for(AccountingEntry entry : accounting) { switch(entry.getEntryType()) { case CREATE: case UPDATE: case READ: Calendar calendar = entry.getDate(); if(calendar.after(DateUtils.dateToCalendar(start)) && calendar.before(DateUtils.dateToCalendar(end))) { count++; } break; default: break; } } } catch(Exception e) { logger.error("DataMethodDownloadHarvester: " + e.getLocalizedMessage()); throw new InternalErrorException(e.getLocalizedMessage()); } } } /** * Gets the VRE name to HL. * * @param vre the vre * @return the VRE name to HL */ private static String getVRENameToHL(String vre) { Validate.notNull(vre, "scope must be not null"); String newName; if(vre.startsWith(JCRRepository.PATH_SEPARATOR)) newName = vre.replace(JCRRepository.PATH_SEPARATOR, "-").substring(1); else newName = vre.replace(JCRRepository.PATH_SEPARATOR, "-"); return newName; } }