package org.gcube.portlets.widgets.wsexplorer.server; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpSession; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.SessionManager; import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException; import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; import org.gcube.common.homelibrary.home.workspace.Workspace; import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder; import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException; import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerConstants; import org.gcube.portlets.widgets.wsexplorer.client.rpc.WorkspaceExplorerService; import org.gcube.portlets.widgets.wsexplorer.shared.FilterCriteria; import org.gcube.portlets.widgets.wsexplorer.shared.Item; import org.gcube.portlets.widgets.wsexplorer.shared.ItemCategory; import org.gcube.portlets.widgets.wsexplorer.shared.ItemType; import org.gcube.portlets.widgets.wsexplorer.shared.WorkspaceNavigatorServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gwt.user.server.rpc.RemoteServiceServlet; /** * The server side implementation of the RPC service. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * Jun 25, 2015 */ @SuppressWarnings("serial") public class WorkspaceExplorerServiceImpl extends RemoteServiceServlet implements WorkspaceExplorerService { /** * */ public static final Logger _log = LoggerFactory.getLogger(WorkspaceExplorerServiceImpl.class); public static final String USERNAME_ATTRIBUTE = "username"; public static final String TEST_USER = "francesco.mangiacrapa"; /** * Gets the ASL session. * * @param httpSession the http session * @return the ASL session */ private ASLSession getASLSession(HttpSession httpSession) { String sessionID = httpSession.getId(); String user = (String) httpSession.getAttribute(USERNAME_ATTRIBUTE); _log.error("WORKSPACE PORTLET SessionID= " + sessionID); //TODO we check for the older attribute name if (user == null) user = (String) httpSession.getAttribute("user"); if (user == null) { _log.error("WORKSPACE PORTLET STARTING IN TEST MODE - NO USER FOUND"); //for test only // user = "test.user"; user = TEST_USER; httpSession.setAttribute(USERNAME_ATTRIBUTE, user); ASLSession session = SessionManager.getInstance().getASLSession(sessionID, user); session.setScope("/gcube/devsec/devVRE"); return session; } else _log.trace("user found in session "+user); return SessionManager.getInstance().getASLSession(sessionID, user); } /** * Gets the workspace. * * @return the workspace * @throws InternalErrorException the internal error exception * @throws HomeNotFoundException the home not found exception * @throws WorkspaceFolderNotFoundException the workspace folder not found exception */ protected Workspace getWorkspace() throws InternalErrorException, HomeNotFoundException, WorkspaceFolderNotFoundException { ASLSession session = getASLSession(this.getThreadLocalRequest().getSession()); Workspace workspace = HomeLibrary.getUserWorkspace(session.getUsername()); return workspace; } /** * {@inheritDoc} */ @Override public Item getRoot(List showableTypes, boolean purgeEmpyFolders, FilterCriteria filterCriteria) throws WorkspaceNavigatorServiceException { _log.trace("getRoot showableTypes: "+showableTypes+" purgeEmpyFolders: "+purgeEmpyFolders+" filterCriteria: "+ filterCriteria); try { Workspace workspace = getWorkspace(); _log.trace("Start getRoot..."); WorkspaceItem root = workspace.getRoot(); _log.trace("GetRoot - Replyiing root"); long startTime = System.currentTimeMillis(); _log.trace("start time - " + startTime); Item rootItem = ItemBuilder.getItem(null, root, showableTypes, filterCriteria, true); rootItem.setName(WorkspaceExplorerConstants.HOME_LABEL); /* SPECIAL FOLDERS Item specialFolders = ItemBuilder.getItem(null, specials, showableTypes, filterCriteria, 2); specialFolders.setShared(true); rootItem.addChild(specialFolders); */ if (purgeEmpyFolders) rootItem = ItemBuilder.purgeEmptyFolders(rootItem); _log.trace("Returning:"); Long endTime = System.currentTimeMillis() - startTime; String time = String.format("%d msc %d sec", endTime, TimeUnit.MILLISECONDS.toSeconds(endTime)); _log.trace("end time - " + time); Collections.sort(rootItem.getChildren(), new ItemComparator()); _log.info("Returning children size: "+rootItem.getChildren().size()); return rootItem; } catch (Exception e) { _log.error("Error during root retrieving", e); throw new WorkspaceNavigatorServiceException("Sorry, an error occurred when performing get root"); } } /** * {@inheritDoc} */ @Override public Item getFolder(String folderId, List showableTypes, boolean purgeEmpyFolders, FilterCriteria filterCriteria) throws WorkspaceNavigatorServiceException { _log.trace("getFolder folderId: "+folderId+" showableTypes: "+showableTypes+" purgeEmpyFolders: "+purgeEmpyFolders+" filterCriteria: "+filterCriteria); try { Workspace workspace = getWorkspace(); WorkspaceItem folder = workspace.getItem(folderId); _log.trace("GetFolder - Replyiing folder"); long startTime = System.currentTimeMillis(); _log.trace("start time - " + startTime); Item itemFolder = ItemBuilder.getItem(null, folder, showableTypes, filterCriteria, true); _log.trace("Only showable types:"); if (purgeEmpyFolders) itemFolder = ItemBuilder.purgeEmptyFolders(itemFolder); _log.trace("Returning:"); Long endTime = System.currentTimeMillis() - startTime; String time = String.format("%d msc %d sec", endTime, TimeUnit.MILLISECONDS.toSeconds(endTime)); _log.trace("end time - " + time); Collections.sort(itemFolder.getChildren(), new ItemComparator()); return itemFolder; } catch (Exception e) { _log.error("Error during folder retrieving", e); throw new WorkspaceNavigatorServiceException("Sorry, an error occurred when performing get folder"); } } @Override public Item getItemByCategory(ItemCategory category) throws WorkspaceNavigatorServiceException{ _log.trace("GetItemByCategory category: "+category); try { Workspace workspace = getWorkspace(); Item item = null; switch(category){ case HOME:{ WorkspaceItem root = workspace.getRoot(); ASLSession session = getASLSession(this.getThreadLocalRequest().getSession()); String fullName = UserUtil.getUserFullName(session.getUsername()); if(fullName.indexOf(" ")>0){ fullName = fullName.substring(0, fullName.indexOf(" ")); }else if(fullName.indexOf(".")>0){ fullName = fullName.substring(0, fullName.indexOf(".")); } item = new Item(null, root.getId(), fullName+"'s", ItemType.ROOT, root.getPath(), root.getOwner().getPortalLogin(), true, true); break; } case VRE_FOLDER:{ WorkspaceItem folder = workspace.getMySpecialFolders(); item = new Item(null, folder.getId(), WorkspaceExplorerConstants.VRE_FOLDERS_LABEL, ItemType.FOLDER, folder.getPath(), folder.getOwner().getPortalLogin(), true, false); break; } } return item; } catch (Exception e) { _log.error("Error during get item by category", e); throw new WorkspaceNavigatorServiceException("Sorry, an error occurred when performing get item by category"); } } /** * {@inheritDoc} */ @Override public Item getMySpecialFolder(List showableTypes, boolean purgeEmpyFolders, FilterCriteria filterCriteria) throws WorkspaceNavigatorServiceException { _log.trace("GetMySpecialFolder showableTypes: "+showableTypes+" purgeEmpyFolders: "+purgeEmpyFolders+" filterCriteria: "+filterCriteria); try { Workspace workspace = getWorkspace(); WorkspaceItem folder = workspace.getMySpecialFolders(); long startTime = System.currentTimeMillis(); _log.trace("start time - " + startTime); Item itemFolder = ItemBuilder.getItem(null, folder, showableTypes, filterCriteria, true); itemFolder.setName(WorkspaceExplorerConstants.VRE_FOLDERS_LABEL); itemFolder.setSpecialFolder(true); _log.trace("Builded MySpecialFolder: "+itemFolder); _log.trace("Only showable types:"); //printName("", folderItem); if (purgeEmpyFolders) itemFolder = ItemBuilder.purgeEmptyFolders(itemFolder); _log.trace("Returning:"); Long endTime = System.currentTimeMillis() - startTime; String time = String.format("%d msc %d sec", endTime, TimeUnit.MILLISECONDS.toSeconds(endTime)); _log.trace("end time - " + time); //printName("", folderItem); Collections.sort(itemFolder.getChildren(), new ItemComparator()); return itemFolder; } catch (Exception e) { _log.error("Error during special folders retrieving", e); throw new WorkspaceNavigatorServiceException("Sorry, an error occurred when performing get My Special Folder"); } } /** * {@inheritDoc} */ @Override public boolean checkName(String name) throws WorkspaceNavigatorServiceException { _log.trace("checkName name: "+name); try { ASLSession session = getASLSession(this.getThreadLocalRequest().getSession()); Workspace workspace = HomeLibrary.getUserWorkspace(session.getUsername()); return workspace.isValidName(name); } catch (Exception e) { _log.error("Error during folder retrieving", e); throw new WorkspaceNavigatorServiceException(e.getMessage()); } } /*protected void printName(String indentation, Item item) { if(item!=null){ _log.trace(indentation+item.getName()); for (Item child:item.getChildren()) printName(indentation+"\t", child); } }*/ /** * Gets Breadcrumbs (the list of parents) by item identifier. * * @param itemIdentifier the item identifier * @param includeItemAsParent - if parameter is true and item passed in input is a folder, the folder is included in path returned as last parent * @return the list parents by item identifier * @throws Exception the exception */ @Override public List getBreadcrumbsByItemIdentifier(String itemIdentifier, boolean includeItemAsParent) throws Exception { List listParents = new ArrayList(); _log.trace("ListParents By Item Identifier "+ itemIdentifier); try { Workspace workspace = getWorkspace(); WorkspaceItem wsItem = workspace.getItem(itemIdentifier); _log.trace("workspace retrieve item name: "+wsItem.getName()); if(includeItemAsParent==true && wsItem.isFolder()){ listParents.add(ItemBuilder.buildFolderForBreadcrumbs((WorkspaceFolder) wsItem, null)); } //HANDLE MY_SPECIAL_FOLDER if(wsItem.isFolder()){ if(ItemBuilder.isSpecialFolder((WorkspaceFolder) wsItem)) return listParents; } //CREATING LIST PARENTS while(wsItem!=null && wsItem.getParent()!=null){ WorkspaceFolder wsParentFolder = wsItem.getParent(); Item parent = ItemBuilder.buildFolderForBreadcrumbs((WorkspaceFolder) wsParentFolder, null); listParents.add(parent); if(parent.isSpecialFolder()) //SKIP HOME PARENT FOR MY_SPECIAL_FOLDER break; wsItem = wsParentFolder; } Collections.reverse(listParents); /* System.out.println("Parent's:"); for (Item item : listParents) { System.out.println(item); }*/ //SET PARENTS for(int i=0; i