From 321512e876b5cf6b16bf9587e34bceeee22b70ca Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Fri, 18 Mar 2016 11:19:10 +0000 Subject: [PATCH] added virtual groups management git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vo-management/usermanagement-core@125711 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../usermanagement/GroupManager.java | 15 +++- .../exception/GroupRetrievalFault.java | 9 +- .../VirtualGroupNotExistingException.java | 14 +++ .../impl/LiferayGroupManager.java | 86 ++++++++++++++----- .../usermanagement/model/VirtualGroup.java | 61 +++++++++++++ 5 files changed, 154 insertions(+), 31 deletions(-) create mode 100644 src/main/java/org/gcube/vomanagement/usermanagement/exception/VirtualGroupNotExistingException.java create mode 100644 src/main/java/org/gcube/vomanagement/usermanagement/model/VirtualGroup.java diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java b/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java index d3bbeea..ce37b38 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java @@ -9,8 +9,10 @@ import org.gcube.vomanagement.usermanagement.exception.UserManagementNameExcepti import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException; 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.GCubeGroup; import org.gcube.vomanagement.usermanagement.model.GCubeRole; +import org.gcube.vomanagement.usermanagement.model.VirtualGroup; /** @@ -86,12 +88,19 @@ public interface GroupManager { GCubeGroup getGroup(long groupId) throws UserManagementSystemException, GroupRetrievalFault; /** * Virtual Groups are handled as Custom attributes in Liferay Sites table - * @param groupId the LR groupId + * @param actualGroupId the LR groupId + * @return the virtual group list available + * @throws GroupRetrievalFault + */ + List getVirtualGroups() throws VirtualGroupNotExistingException; + /** + * Virtual Groups are handled as Custom attributes in Liferay Sites table + * @param actualGroupId the LR groupId * @return the virtual group name associated to this group * @throws GroupRetrievalFault */ - String getVirtualGroupName(long groupId) throws GroupRetrievalFault; - /** + VirtualGroup getVirtualGroup(long actualGroupId) throws GroupRetrievalFault, VirtualGroupNotExistingException; + /* * * @param scope the infrastructure scope e.g. /gcube/devsec * @return the LR groupId, -1 otherwise diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/exception/GroupRetrievalFault.java b/src/main/java/org/gcube/vomanagement/usermanagement/exception/GroupRetrievalFault.java index ede38c2..7a4d192 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/exception/GroupRetrievalFault.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/exception/GroupRetrievalFault.java @@ -2,15 +2,8 @@ package org.gcube.vomanagement.usermanagement.exception; import com.liferay.portal.kernel.exception.PortalException; - - +@SuppressWarnings("serial") public class GroupRetrievalFault extends Exception{ - - - /** - * - */ - private static final long serialVersionUID = -5809619725100397844L; public GroupRetrievalFault(String errorMsg, String orgId ){ System.out.println(errorMsg + orgId); diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/exception/VirtualGroupNotExistingException.java b/src/main/java/org/gcube/vomanagement/usermanagement/exception/VirtualGroupNotExistingException.java new file mode 100644 index 0000000..6eedbdf --- /dev/null +++ b/src/main/java/org/gcube/vomanagement/usermanagement/exception/VirtualGroupNotExistingException.java @@ -0,0 +1,14 @@ +package org.gcube.vomanagement.usermanagement.exception; + +import com.liferay.portal.kernel.exception.PortalException; + +@SuppressWarnings("serial") +public class VirtualGroupNotExistingException extends Exception { + public VirtualGroupNotExistingException(String errorMsg, PortalException e){ + System.out.println(errorMsg); + e.printStackTrace(); + } + public VirtualGroupNotExistingException(String errorMsg){ + System.out.println(errorMsg); + } +} 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 7f18492..0857897 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java @@ -2,6 +2,7 @@ package org.gcube.vomanagement.usermanagement.impl; import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -14,8 +15,10 @@ import org.gcube.vomanagement.usermanagement.exception.UserManagementNameExcepti import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException; 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.GCubeGroup; import org.gcube.vomanagement.usermanagement.model.GCubeRole; +import org.gcube.vomanagement.usermanagement.model.VirtualGroup; import org.gcube.vomanagement.usermanagement.util.ManagementUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +42,8 @@ import com.liferay.portal.service.LayoutSetLocalServiceUtil; import com.liferay.portal.service.RoleServiceUtil; import com.liferay.portal.service.ServiceContext; import com.liferay.portal.service.UserLocalServiceUtil; +import com.liferay.portlet.expando.model.ExpandoBridge; +import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil; public class LiferayGroupManager implements GroupManager { /** @@ -109,29 +114,64 @@ public class LiferayGroupManager implements GroupManager { } return group; } - - public String getVirtualGroupName(long groupId) throws GroupRetrievalFault { - try { - _log.debug("Setting Thread Permission"); - long userId = LiferayUserManager.getAdmin().getUserId(); - PrincipalThreadLocal.setName(userId); - PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(UserLocalServiceUtil.getUser(userId)); - PermissionThreadLocal.setPermissionChecker(permissionChecker); - Group site = GroupLocalServiceUtil.getGroup(groupId); - if (site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()) == null || site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()).equals("")) { - _log.warn(String.format("Attribute %s not initialized.", CustomAttributeKeys.VIRTUAL_GROUP.getKeyName())); - return null; - } else { - String[] values = (String[]) site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()); - if (values != null) - return values[0]; - else return ""; + /** + * {@inheritDoc} + */ + @Override + public VirtualGroup getVirtualGroup(long actualGroupId) throws GroupRetrievalFault, VirtualGroupNotExistingException { + VirtualGroup toReturn = new VirtualGroup(); + try { + _log.debug("Setting Thread Permission"); + long userId = LiferayUserManager.getAdmin().getUserId(); + PrincipalThreadLocal.setName(userId); + PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(UserLocalServiceUtil.getUser(userId)); + PermissionThreadLocal.setPermissionChecker(permissionChecker); + Group site = GroupLocalServiceUtil.getGroup(actualGroupId); + if (site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()) == null || site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()).equals("")) { + String warningMessage = String.format("Attribute %s not initialized.", CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()); + _log.warn(warningMessage); + throw new VirtualGroupNotExistingException(warningMessage); + } else { + String[] values = (String[]) site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()); + if (values != null) { + String[] splits = values[0].split("\\|"); + toReturn.setName(splits[0]); + toReturn.setDescription(splits[1]); } - } catch (Exception e) { - e.printStackTrace(); - return "Exception "; } + } catch (Exception e) { + e.printStackTrace(); } + return null; + } + /** + * {@inheritDoc} + */ + @Override + public List getVirtualGroups() throws VirtualGroupNotExistingException { + ExpandoBridge expandoBridge = null; + List toReturn = new ArrayList(); + try { + expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(ManagementUtils.getCompany().getCompanyId(), Group.class.getName()); + String[] groups = (String[]) expandoBridge.getAttributeDefault(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()); + List virtualGroups = Arrays.asList(groups); + + for (String vg : virtualGroups) { + String[] splits = vg.split("\\|"); + String gName = splits[0]; + String gDescription = splits[1]; + toReturn.add(new VirtualGroup(gName, gDescription)); + } + } catch (PortalException e) { + throw new VirtualGroupNotExistingException("", e); + } catch (SystemException e) { + e.printStackTrace(); + } + return toReturn; + } + /** + * {@inheritDoc} + */ @Override public GCubeGroup createRootVO(String rootVOName, String description) throws UserManagementNameException, UserManagementSystemException, UserRetrievalFault, GroupRetrievalFault, UserManagementPortalException { Group group = null; @@ -147,6 +187,9 @@ public class LiferayGroupManager implements GroupManager { return null; } + /** + * {@inheritDoc} + */ @Override public GCubeGroup createVO(String virtualOrgName, long rootVOGroupId, String description) throws UserManagementNameException, UserManagementSystemException, UserRetrievalFault, @@ -163,6 +206,9 @@ public class LiferayGroupManager implements GroupManager { } return null; } + /** + * {@inheritDoc} + */ @Override public GCubeGroup createVRE(String virtualResearchEnvName, long virtualOrgGroupId, String description) throws UserManagementNameException, UserManagementSystemException, UserRetrievalFault, diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/model/VirtualGroup.java b/src/main/java/org/gcube/vomanagement/usermanagement/model/VirtualGroup.java new file mode 100644 index 0000000..2f1feb7 --- /dev/null +++ b/src/main/java/org/gcube/vomanagement/usermanagement/model/VirtualGroup.java @@ -0,0 +1,61 @@ +package org.gcube.vomanagement.usermanagement.model; + +import java.io.Serializable; +/** + * + * @author Massimiliano Assante, CNR-ISTI + * Virtual Groups are another way to group VREs indipendently from the scope, + * they are modeled as Custom attributes in Liferay Sites table wtih + */ +@SuppressWarnings("serial") +public class VirtualGroup implements Serializable{ + private String name; + private String description; + public VirtualGroup() {} + public VirtualGroup(String name, String description) { + super(); + this.name = name; + this.description = description; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + @Override + public String toString() { + return "VirtualGroup [name=" + name + ", description=" + description + + "]"; + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + VirtualGroup other = (VirtualGroup) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +}