From d3005a099cdfcba6ca8c1ad035739bee72fa28f7 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Mon, 16 Jan 2017 17:16:36 +0000 Subject: [PATCH] added getGateways method, implemented other two methods in the WS version of the GroupManager git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vo-management/usermanagement-core@141592 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../usermanagement/GroupManager.java | 6 ++ .../impl/LiferayGroupManager.java | 27 +++++- .../impl/ws/LiferayWSGroupManager.java | 82 ++++++++++++++++++- .../test/LiferayWSGroupTest.java | 35 ++++++++ 4 files changed, 144 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java b/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java index cda1813..df0d6fc 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java @@ -232,4 +232,10 @@ public interface GroupManager { * @throws GroupRetrievalFault */ String getGroupLogoURL(long logoId); + + /** + * Retrieve the list of GCubeGroups that are Gateways (i.e. groups with no father and children). + * @return a list of gateways + */ + List getGateways(); } diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java b/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java index 78d1207..49e8547 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java @@ -51,7 +51,7 @@ import com.liferay.portlet.expando.model.ExpandoBridge; import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil; public class LiferayGroupManager implements GroupManager { - + private static final String DEFAULT_INFRA_NAME = "gcube"; public static final String INFRASTRUCTURE_NAME = "infrastructure"; /** @@ -434,7 +434,7 @@ public class LiferayGroupManager implements GroupManager { Set toReturn = new HashSet<>(); try { List currSiteVirtualGroups = getVirtualGroups(siteGroupId); - + for (GCubeGroup userGroup : listGroupsByUser(userId)) { if (isVRE(userGroup.getGroupId())) { for (VirtualGroup currSiteVGroup : currSiteVirtualGroups) { @@ -446,7 +446,7 @@ public class LiferayGroupManager implements GroupManager { } } } - + } catch (Exception e) { e.printStackTrace(); } @@ -710,5 +710,26 @@ public class LiferayGroupManager implements GroupManager { return layoutSetLogo; } + @Override + public List getGateways() { + List gateways = new ArrayList(); + try{ + List candidateGateways = GroupLocalServiceUtil.getGroups(ManagementUtils.getCompany().getCompanyId(), 0, true); + + // real gateways have no children as well + for (Group group : candidateGateways) { + List children = group.getChildren(true); + if(children == null || children.isEmpty()) + if(!group.getFriendlyURL().equals("/guest") && !group.getFriendlyURL().equals("/global")) // created by Liferay + gateways.add(mapLRGroup(group)); + } + + }catch(Exception e){ + _log.error("Failed to retrieve the list of gateways", e); + return null; + } + return gateways; + } + } diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/impl/ws/LiferayWSGroupManager.java b/src/main/java/org/gcube/vomanagement/usermanagement/impl/ws/LiferayWSGroupManager.java index f57497f..4b5c14a 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/impl/ws/LiferayWSGroupManager.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/impl/ws/LiferayWSGroupManager.java @@ -27,6 +27,7 @@ import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalExcep import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.VirtualGroupNotExistingException; +import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys; import org.gcube.vomanagement.usermanagement.model.GCubeGroup; import org.gcube.vomanagement.usermanagement.model.GCubeRole; import org.gcube.vomanagement.usermanagement.model.GroupMembershipType; @@ -63,6 +64,7 @@ public class LiferayWSGroupManager implements GroupManager { private static final String GET_GROUP_BY_ID = "/group/get-group/group-id/$GROUP_ID"; private static final String GET_GROUPS_BY_PARENT_ID = "/group/get-groups/company-id/$COMPANY_ID/parent-group-id/$GROUP_PARENT_ID/site/$SITE"; private static final String GET_GROUPS_BY_USERID = "/group/get-user-sites-groups/user-id/$USER_ID/class-names/%5B%22com.liferay.portal.model.Group%22%5D/max/$MAX_GROUP"; + private static final String GET_GROUP_CUSTOM_FIELDS = "/expandovalue/get-data/company-id/$COMPANY_ID/class-name/com.liferay.portal.model.Group/table-name/CUSTOM_FIELDS/column-name/$CUSTOM_KEY/class-pk/$GROUP_ID"; // logger private static final org.slf4j.Logger logger = LoggerFactory.getLogger(LiferayWSGroupManager.class); @@ -358,8 +360,36 @@ public class LiferayWSGroupManager implements GroupManager { @Override public List getVirtualGroups(long actualGroupId) throws GroupRetrievalFault, VirtualGroupNotExistingException { - // TODO Auto-generated method stub + List toReturn = new ArrayList(); + + try { + + String jsonCustomFields = + executeHTTPGETRequest(API_BASE_URL + GET_GROUP_CUSTOM_FIELDS.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$CUSTOM_KEY", CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()).replace("$GROUP_ID", String.valueOf(actualGroupId)), + credsProvider, localContext, target); + + if(jsonCustomFields != null){ + + //parse this array + JSONParser parser = new JSONParser(); + JSONArray array = (JSONArray)parser.parse(jsonCustomFields); + + for (int i = 0; i < array.size(); i++) { + String obj = (String)array.get(i); + String[] splits = obj.split("\\|"); + String gName = splits[0]; + String gDescription = splits[1]; + toReturn.add(new VirtualGroup(gName, gDescription)); + } + } + + return toReturn; + } catch (Exception e) { + logger.error("Failed to read Virtualgroups for this group", e); + } + return null; + } @Override @@ -572,8 +602,15 @@ public class LiferayWSGroupManager implements GroupManager { @Override public Serializable readCustomAttr(long groupId, String attributeKey) throws GroupRetrievalFault { - // TODO Auto-generated method stub - return null; + + String result = + executeHTTPGETRequest(API_BASE_URL + GET_GROUP_CUSTOM_FIELDS.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_ID", String.valueOf(groupId)).replace("$CUSTOM_KEY", attributeKey), + credsProvider, localContext, target); + + logger.debug("Data is " + result); + + + return result; } @Override @@ -596,4 +633,43 @@ public class LiferayWSGroupManager implements GroupManager { return null; } + @Override + public List getGateways() { + List gateways = new ArrayList(); + try{ + + String jsonGroups = + executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_PARENT_ID.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_PARENT_ID", String.valueOf(0)) + .replace("$SITE", Boolean.toString(true)), + credsProvider, localContext, target); + + JSONArray candidateGateways = (JSONArray)(new JSONParser().parse(jsonGroups)); + List idsCandidateGateways = new ArrayList(); + + for (int i = 0; i < candidateGateways.size(); i++) { + idsCandidateGateways.add((Long) ((JSONObject)(candidateGateways.get(i))).get("groupId")); + } + + // real gateways have no children as well + for (int i = 0; i < idsCandidateGateways.size(); i++) { + List children = getChildren(idsCandidateGateways.get(i)); + String friendlyUrl = (String) ((JSONObject)candidateGateways.get(i)).get("friendlyURL"); + + // check if it is not a group created by Liferay + boolean defaultGroup = friendlyUrl.equals("/guest") || friendlyUrl.equals("/global"); + + if(defaultGroup) + continue; + + if(children == null || children.isEmpty()) + gateways.add(mapLRGroup(((JSONObject)candidateGateways.get(i)).toJSONString())); + } + + }catch(Exception e){ + logger.error("Failed to retrieve the list of gateways", e); + return null; + } + return gateways; + } + } diff --git a/src/test/java/org/gcube/vomanagement/usermanagement/test/LiferayWSGroupTest.java b/src/test/java/org/gcube/vomanagement/usermanagement/test/LiferayWSGroupTest.java index 35ba795..344f625 100644 --- a/src/test/java/org/gcube/vomanagement/usermanagement/test/LiferayWSGroupTest.java +++ b/src/test/java/org/gcube/vomanagement/usermanagement/test/LiferayWSGroupTest.java @@ -5,8 +5,10 @@ import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; +import org.gcube.vomanagement.usermanagement.exception.VirtualGroupNotExistingException; import org.gcube.vomanagement.usermanagement.impl.ws.LiferayWSGroupManager; import org.gcube.vomanagement.usermanagement.model.GCubeGroup; +import org.gcube.vomanagement.usermanagement.model.VirtualGroup; import org.slf4j.LoggerFactory; /** @@ -76,4 +78,37 @@ public class LiferayWSGroupTest { logger.debug("Retrieved groups " + groups); } + //@Test + public void getGateways(){ + List gateways = groupManager.getGateways(); + logger.debug("Retrieved gateways are " + gateways.size()); + + for (GCubeGroup gCubeGroup : gateways) { + logger.debug("Name is " + gCubeGroup.getGroupName()); + } + + } + + //@Test + public void getVirtualGroups() throws GroupRetrievalFault, VirtualGroupNotExistingException{ + // virtual groups of nextnext vre + List virtualGroups = groupManager.getVirtualGroups(21660); + logger.debug("Retrieved vg " + virtualGroups); + + // virtual groups of the gateway + List virtualGroupsGateway = groupManager.getVirtualGroups(1142377); + logger.debug("Retrieved vg " + virtualGroupsGateway); + + } + + //@Test + public void getGatewayInfo() throws UserManagementSystemException, GroupRetrievalFault{ + + GCubeGroup getGroup = groupManager.getGroup(1142377); + logger.debug("Retrieved vg " + getGroup); + + String result = (String)groupManager.readCustomAttr(getGroup.getGroupId(), "Emailsender"); + logger.debug("Value is " + result); + } + } \ No newline at end of file