From f054e25f0a1cdb2c820a108d94c86b6e20348abf Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Mon, 15 May 2017 10:59:17 +0000 Subject: [PATCH] WorkspaceUtils class minor fixes git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@148657 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../server/utils/WorkspaceUtils.java | 345 +++++++++--------- 1 file changed, 181 insertions(+), 164 deletions(-) diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/WorkspaceUtils.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/WorkspaceUtils.java index f06079b..15c97d6 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/WorkspaceUtils.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/WorkspaceUtils.java @@ -26,10 +26,176 @@ import com.liferay.portal.kernel.log.LogFactoryUtil; public class WorkspaceUtils { - //private static final org.slf4j.Logger logger = LoggerFactory.getLogger(WorkspaceUtils.class); private static final Log logger = LogFactoryUtil.getLog(WorkspaceUtils.class); private static final String RESOURCES_NAME_SEPARATOR = "_"; + private static final String STRIP_NOT_ALPHANUMERIC = "[^A-Za-z0-9.-_]"; + + /** + * This method receives a folder id within the user's workspace and set the list of resources in the dataset bean to be returned + * @param folderId + * @param owner + * @param bean + * @param userName + * @throws Exception + */ + public static void handleWorkspaceResources(String folderId, String userName, + DatasetBean bean) throws Exception { + + // get workspace + Workspace ws = HomeLibrary + .getHomeManagerFactory() + .getHomeManager() + .getHome().getWorkspace(); + + WorkspaceItem originalFolderOrFile = ws.getItem(folderId); + + logger.debug("Item retrieved is " + originalFolderOrFile); + + if(!originalFolderOrFile.isFolder()){ + + ResourceElementBean resource = new ResourceElementBean(); + resource.setDescription(originalFolderOrFile.getDescription()); + resource.setFolder(false); + resource.setEditableName(originalFolderOrFile.getName()); + resource.setName(originalFolderOrFile.getName()); + resource.setOriginalIdInWorkspace(folderId); + bean.setResourceRoot(resource); + bean.setTitle(originalFolderOrFile.getName().replaceAll(STRIP_NOT_ALPHANUMERIC, " ")); + bean.setDescription(originalFolderOrFile.getDescription()); + + }else{ + + String onlyAlphanumericTitle = originalFolderOrFile.getName().replaceAll(STRIP_NOT_ALPHANUMERIC, " "); + bean.setTitle(onlyAlphanumericTitle); + bean.setDescription(originalFolderOrFile.getDescription()); + + // Create the folder in the catalogue + Map folderItems = getGcubeItemProperties(originalFolderOrFile); + + if(folderItems != null){ + // transform this properties + Map> tempItems = new HashMap>(folderItems.size()); + + Iterator> iterator = folderItems.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = (Map.Entry) iterator + .next(); + tempItems.put(entry.getKey(), Arrays.asList(entry.getValue())); + } + bean.setCustomFields(tempItems); + } + + // set them into the bean + bean.setResourceRoot(WorkspaceUtils.getTreeFromFolder(folderId, ws)); + } + + } + + /** Gets the gcube item properties. + * + * @param item the item + * @return the gcube item properties + */ + public static Map getGcubeItemProperties(WorkspaceItem item) { + + if(item instanceof GCubeItem){ + GCubeItem gItem = (GCubeItem) item; + try { + if(gItem.getProperties()!=null){ + Map map = gItem.getProperties().getProperties(); + HashMap properties = new HashMap(map.size()); //TO PREVENT GWT SERIALIZATION ERROR + for (String key : map.keySet()) + properties.put(key, map.get(key)); + + return properties; + } + } catch (InternalErrorException e) { + logger.error("Error in server getItemProperties: ", e); + } + } + return null; + } + + /** + * Returns a tree object + * @param workspaceFolderId + * @param ws + * @return ResourceElementBean a tree object + * @throws Exception + */ + public static ResourceElementBean getTreeFromFolder(String workspaceFolderId, Workspace ws) throws Exception{ + + ResourceElementBean rootElem = new ResourceElementBean(); + String pathSeparator = ws.getPathSeparator(); + WorkspaceItem initialItem = ws.getItem(workspaceFolderId); + String fullPathBase = initialItem.getPath(); + fullPathBase = fullPathBase.endsWith(ws.getPathSeparator()) ? fullPathBase : fullPathBase + ws.getPathSeparator(); + rootElem.setFolder(initialItem.isFolder()); + rootElem.setFullPath(initialItem.getPath().replace(fullPathBase, "")); + rootElem.setName(initialItem.getName()); + rootElem.setOriginalIdInWorkspace(initialItem.getId()); + rootElem.setDescription(initialItem.getDescription()); + extractEditableNameFromPath(rootElem, pathSeparator); + + // recursive visiting + if(initialItem.isFolder()) + visit(rootElem, initialItem, fullPathBase, pathSeparator); + + logger.debug("Tree that is going to be returned is " + rootElem); + return rootElem; + } + + /** + * Recursive visit of a workspace item + * @param rootElem + * @param initialItemWS + * @throws InternalErrorException + */ + private static void visit(ResourceElementBean parent, WorkspaceItem initialItemWS, String fullPathBase, String pathSeparator) throws InternalErrorException { + List children = initialItemWS.getChildren(); + ArrayList childrenInTree = new ArrayList(children.size()); + for (WorkspaceItem item : children) { + // logger.debug("Path BEFORE REPLACE is " + item.getPath()); + // logger.debug("Path AFTER REPLACE is " + item.getPath().replace(fullPathBase, "")); + // logger.debug("Name is " + item.getName()); + // logger.debug("id is " + item.getId()); + ResourceElementBean elem = new ResourceElementBean(); + elem.setFolder(item.isFolder()); + elem.setOriginalIdInWorkspace(item.getId()); + elem.setFullPath(item.getPath().replace(fullPathBase, "")); + elem.setParent(parent); + elem.setName(item.getName()); + elem.setDescription(item.getDescription()); + extractEditableNameFromPath(elem, pathSeparator); + childrenInTree.add(elem); + logger.trace("Elem is " + elem); + if(item.isFolder()) + visit(elem, item, fullPathBase, pathSeparator); + } + // add these list as child of the rootElem + parent.setChildren(childrenInTree); + } + + /** + * Replaces the "/" char with a custom one and return an editable name for the user + * @param rootElem + * @param pathSeparatorInWs + */ + private static void extractEditableNameFromPath(ResourceElementBean rootElem, String pathSeparatorInWs) { + + if(rootElem == null) + return; + + String elemName = rootElem.getName(); + String fullPath = rootElem.getFullPath(); + logger.info("Element original is " + rootElem); + + int lastIndex = rootElem.getFullPath().lastIndexOf(elemName); + fullPath = rootElem.getFullPath().substring(0, lastIndex); + fullPath = fullPath.replaceAll(pathSeparatorInWs, RESOURCES_NAME_SEPARATOR) + elemName; + rootElem.setEditableName(fullPath); + } /** * Copy into the .catalogue area folder the checked resources. @@ -106,168 +272,19 @@ public class WorkspaceUtils { return resources; } - /** - * This method receives a folder id within the user's workspace and set the list of resources in the dataset bean to be returned - * @param folderId - * @param owner - * @param bean - * @param userName - * @throws Exception - */ - public static void handleWorkspaceResources(String folderId, String userName, - DatasetBean bean) throws Exception { - - // get workspace - Workspace ws = HomeLibrary - .getHomeManagerFactory() - .getHomeManager() - .getHome().getWorkspace(); - - WorkspaceItem originalFolderOrFile = ws.getItem(folderId); - - logger.debug("Item retrieved is " + originalFolderOrFile); - - if(!originalFolderOrFile.isFolder()){ - - ResourceElementBean resource = new ResourceElementBean(); - resource.setDescription(originalFolderOrFile.getDescription()); - resource.setFolder(false); - resource.setEditableName(originalFolderOrFile.getName()); - resource.setName(originalFolderOrFile.getName()); - resource.setOriginalIdInWorkspace(folderId); - bean.setResourceRoot(resource); - bean.setTitle(originalFolderOrFile.getName().replaceAll("[^A-Za-z0-9.-_]", " ")); - bean.setDescription(originalFolderOrFile.getDescription()); - - }else{ - - String onlyAlphanumericTitle = originalFolderOrFile.getName().replaceAll("[^A-Za-z0-9.-_]", " "); // that is, remove characters different than the ones inside - bean.setTitle(onlyAlphanumericTitle); - bean.setDescription(originalFolderOrFile.getDescription()); - - // Create the folder in the catalogue - Map folderItems = getGcubeItemProperties(originalFolderOrFile); - - if(folderItems != null){ - // transform this properties - Map> tempItems = new HashMap>(folderItems.size()); - - Iterator> iterator = folderItems.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = (Map.Entry) iterator - .next(); - tempItems.put(entry.getKey(), Arrays.asList(entry.getValue())); - } - bean.setCustomFields(tempItems); - } - - // set them into the bean - bean.setResourceRoot(WorkspaceUtils.getTreeFromFolder(folderId, ws)); - } - - } - - /** Gets the gcube item properties. - * - * @param item the item - * @return the gcube item properties - */ - public static Map getGcubeItemProperties(WorkspaceItem item) { - - if(item instanceof GCubeItem){ - GCubeItem gItem = (GCubeItem) item; - try { - if(gItem.getProperties()!=null){ - Map map = gItem.getProperties().getProperties(); - HashMap properties = new HashMap(map.size()); //TO PREVENT GWT SERIALIZATION ERROR - for (String key : map.keySet()) - properties.put(key, map.get(key)); - - return properties; - } - } catch (InternalErrorException e) { - logger.error("Error in server getItemProperties: ", e); - } - } - return null; - } - - /** - * Returns a tree object - * @param workspaceFolderId - * @param ws - * @return ResourceElementBean a tree object - * @throws Exception - */ - public static ResourceElementBean getTreeFromFolder(String workspaceFolderId, Workspace ws) throws Exception{ - ResourceElementBean rootElem = new ResourceElementBean(); - String pathSeparator = ws.getPathSeparator(); - try{ - WorkspaceItem initialItem = ws.getItem(workspaceFolderId); - String fullPathBase = initialItem.getPath(); - fullPathBase = fullPathBase.endsWith(ws.getPathSeparator()) ? fullPathBase : fullPathBase + ws.getPathSeparator(); - rootElem.setFolder(initialItem.isFolder()); - rootElem.setFullPath(initialItem.getPath().replace(fullPathBase, "")); - rootElem.setParent(null); - rootElem.setName(initialItem.getName()); - rootElem.setOriginalIdInWorkspace(initialItem.getId()); - rootElem.setDescription(initialItem.getDescription()); - extractEditableNameFromPath(rootElem, pathSeparator); - - // recursive visiting - if(initialItem.isFolder()) - visit(rootElem, initialItem, fullPathBase, pathSeparator); - }catch(Exception e){ - logger.error("Failed to build the resource tree", e); - return null; - } - - logger.debug("Tree that is going to be returned is " + rootElem); - return rootElem; - } - - /** - * Recursive visit of a workspace item - * @param rootElem - * @param initialItemWS - * @throws InternalErrorException - */ - private static void visit(ResourceElementBean parent, WorkspaceItem initialItemWS, String fullPathBase, String pathSeparator) throws InternalErrorException { - List children = initialItemWS.getChildren(); - ArrayList childrenInTree = new ArrayList(children.size()); - for (WorkspaceItem item : children) { - ResourceElementBean elem = new ResourceElementBean(); - elem.setFolder(item.isFolder()); - elem.setOriginalIdInWorkspace(item.getId()); - elem.setFullPath(item.getPath().replace(fullPathBase, "")); - elem.setParent(parent); - elem.setName(item.getName()); - elem.setDescription(item.getDescription()); - extractEditableNameFromPath(elem, pathSeparator); - childrenInTree.add(elem); - logger.trace("Elem is " + elem); - if(item.isFolder()) - visit(elem, item, fullPathBase, pathSeparator); - } - // add these list as child of the rootElem - parent.setChildren(childrenInTree); - } - - /** - * Replaces the "/" char with a custom one and return an editable name for the user - * @param rootElem - * @param pathSeparatorInWs - */ - private static void extractEditableNameFromPath(ResourceElementBean rootElem, String pathSeparatorInWs) { - - if(rootElem == null) - return; - - String elemName = rootElem.getName(); - int lastIndex = rootElem.getFullPath().lastIndexOf(elemName); - String fullPath = rootElem.getFullPath().substring(0, lastIndex); - fullPath = fullPath.replaceAll(pathSeparatorInWs, RESOURCES_NAME_SEPARATOR) + elemName; - rootElem.setEditableName(fullPath); - } + // public static void main(String[] args) throws Exception { + // + // ScopeProvider.instance.set(); + // SecurityTokenProvider.instance.set(); + // String path = "/Workspace/DataMiner"; + // Workspace ws = HomeLibrary + // .getHomeManagerFactory() + // .getHomeManager() + // .getHome() + // .getWorkspace(); + // WorkspaceItem miner = ws.getItemByPath(path); + // handleWorkspaceResources(miner.getId(), "costantino.perciante", new DatasetBean()); + // + // } } \ No newline at end of file