package org.gcube.portlets.user.wswidget; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.portlet.ResourceRequest; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.storagehub.client.plugins.AbstractPlugin; import org.gcube.common.storagehub.client.proxies.ItemManagerClient; import org.gcube.common.storagehub.client.proxies.WorkspaceManagerClient; import org.gcube.common.storagehub.model.items.Item; import org.gcube.portlets.user.wswidget.shared.AuthorizedUser; import org.gcube.portlets.user.wswidget.shared.WSItem; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; public class StorageHubServiceUtil { private static Log _log = LogFactoryUtil.getLog(StorageHubServiceUtil.class); /** * * @param authUser * @param itemId * @param itemName * @param from * @param offset * @return */ public static List getItemChildren(AuthorizedUser authUser, String itemId, String itemName, int from, int offset) { WSItem toReturn = new WSItem(itemId, itemName, true); _log.debug("getItemChildren folder/item = " + itemId); ArrayList children = new ArrayList<>(); SecurityTokenProvider.instance.set(authUser.getSecurityToken()); ItemManagerClient client = AbstractPlugin.item().build(); List theChildren = null; if (offset >= 0) { int limit = offset; theChildren = client.getChildren(itemId, from, limit, "hl:accounting"); } else { //all the items theChildren = client.getChildren(itemId, "hl:accounting"); } if (theChildren == null || theChildren.isEmpty()) { toReturn.setChildren(children); _log.debug("*** Returning empty "); return new ArrayList<>(); } for (Item workspaceItem : theChildren) { WSItem toAdd = ItemBuilder.getItem(toReturn, workspaceItem, workspaceItem.getPath(), authUser.getUser().getUsername()); children.add(toAdd); } toReturn.setChildren(children); Collections.sort(toReturn.getChildren(), new ItemComparator()); _log.debug("*** Returning children size: "+toReturn.getChildren().size()); return children; } /** * */ public static int getItemChildrenCount(ResourceRequest request, String itemId) { String userName = Utils.getCurrentUser(request).getUsername(); String scope = Utils.getCurrentContext(request); String authorizationToken = Utils.getCurrentUserToken(scope, userName); SecurityTokenProvider.instance.set(authorizationToken); ItemManagerClient client = AbstractPlugin.item().build(); return client.childrenCount(itemId); } /** * * @param authUser * @param from * @param offset * @return */ public static List getRootChildren(AuthorizedUser authUser, int from, int offset) { try { SecurityTokenProvider.instance.set(authUser.getSecurityToken()); WorkspaceManagerClient client = AbstractPlugin.workspace().build(); Item itemRoot = client.getWorkspace("hl:accounting"); WSItem root = new WSItem(itemRoot.getId(), Utils.HOME_LABEL, true); root.setIsRoot(true); return getItemChildren(authUser, itemRoot.getId(), Utils.HOME_LABEL, from, offset); } catch (Exception e) { _log.error("Error during root retrieving", e); } return null; } }