modified to add possibility to display virtual groups depending on the custom field VirtualGroup checkbox selected

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/join-vre@130251 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2016-07-11 15:10:22 +00:00
parent 92c634518f
commit 10da8598b0
1 changed files with 86 additions and 4 deletions

View File

@ -6,6 +6,8 @@ import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.common.portal.GCubePortalConstants;
@ -26,6 +28,7 @@ import org.gcube.vomanagement.usermanagement.UserManager;
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.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys;
@ -42,6 +45,11 @@ import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.VirtualHost;
import com.liferay.portal.security.auth.PrincipalThreadLocal;
import com.liferay.portal.security.permission.PermissionChecker;
import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
import com.liferay.portal.security.permission.PermissionThreadLocal;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.LayoutSetLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.VirtualHostLocalServiceUtil;
@ -133,7 +141,7 @@ public class JoinServiceImpl extends RemoteServiceServlet implements JoinService
try {
GroupManager gm = new LiferayGroupManager();
toReturn.setUponRequest(group.isRequestBasedGroup());
System.out.println(group.getGroupName() + " getVRECustomAttr group.isRequestBasedGroup?" + group.isRequestBasedGroup());
_log.debug(group.getGroupName() + " getVRECustomAttr group.isRequestBasedGroup?" + group.isRequestBasedGroup());
Boolean attributeValue = (Boolean) gm.readCustomAttr(group.getGroupId(), CustomAttributeKeys.IS_EXTERNAL.getKeyName());
toReturn.setExternal(attributeValue);
if (attributeValue) { //we read the custom attr URL if and only if the VRE is External, in the other case is useless
@ -160,8 +168,16 @@ public class JoinServiceImpl extends RemoteServiceServlet implements JoinService
GroupManager gm = new LiferayGroupManager();
LinkedHashMap<VRECategory, ArrayList<VRE>> toReturn = new LinkedHashMap<VRECategory, ArrayList<VRE>>();
//before iterating the actual groups create the virtualGroups in the correct order
List<VirtualGroup> virtualGroups = gm.getVirtualGroups();
for (VirtualGroup vg : virtualGroups) {
// List<VirtualGroup> virtualGroups = gm.getVirtualGroups();
// for (VirtualGroup vg : virtualGroups) {
// ArrayList<VRE> toCreate = new ArrayList<VRE>();
// VRECategory cat = new VRECategory(1L, vg.getName(), vg.getDescription());
// toReturn.put(cat, toCreate);
// }
long currentSiteGroupId = getSiteFromServletRequest(getThreadLocalRequest()).getGroupId();
List<VirtualGroup> currentSiteVGroups = getVirtualGroups(currentSiteGroupId);
for (VirtualGroup vg : currentSiteVGroups) {
ArrayList<VRE> toCreate = new ArrayList<VRE>();
VRECategory cat = new VRECategory(1L, vg.getName(), vg.getDescription());
toReturn.put(cat, toCreate);
@ -169,7 +185,6 @@ public class JoinServiceImpl extends RemoteServiceServlet implements JoinService
GCubeGroup rootGroupVO = gm.getRootVO();
try {
_log.debug("root: " + rootGroupVO.getGroupName() );
} catch (NullPointerException e) {
@ -456,4 +471,71 @@ public class JoinServiceImpl extends RemoteServiceServlet implements JoinService
return null;
}
/**
* read the list of virtual groups the current site (i-marine, services etc. ) should show up
* @param actualGroupId
* @return he list of virtual groups the current site (i-marine, services etc. ) should show up
* @throws GroupRetrievalFault
* @throws VirtualGroupNotExistingException
*/
private List<VirtualGroup> getVirtualGroups(long actualGroupId) throws GroupRetrievalFault, VirtualGroupNotExistingException {
List<VirtualGroup> toReturn = new ArrayList<VirtualGroup>();
try {
long userId = LiferayUserManager.getAdmin().getUserId();
PrincipalThreadLocal.setName(userId);
PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(UserLocalServiceUtil.getUser(userId));
PermissionThreadLocal.setPermissionChecker(permissionChecker);
Group site = GroupLocalServiceUtil.getGroup(actualGroupId);
_log.debug("Set Thread Permission done, getVirtual Group of " + site.getName());
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());
VirtualGroup toAdd = new VirtualGroup();
if (values != null && values.length > 0) {
for (int i = 0; i < values.length; i++) {
toAdd = new VirtualGroup();
String[] splits = values[i].split("\\|");
toAdd.setName(splits[0]);
toAdd.setDescription(splits[1]);
toReturn.add(toAdd);
_log.debug("VirtualGroup selected found for " + site.getName() + " -> " + toAdd.getName());
}
} else {
toAdd.setName("NoVirtualGroupAssigned");
toAdd.setDescription("NoVirtualGroupDescription");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return toReturn;
}
/**
*
* @param request
* @return the current Group instance based on the request
* @throws PortalException
* @throws SystemException
*/
private Group getSiteFromServletRequest(final HttpServletRequest request) throws PortalException, SystemException {
String serverName = request.getServerName();
_log.debug("currentHost is " + serverName);
Group site = null;
List<VirtualHost> vHosts = VirtualHostLocalServiceUtil.getVirtualHosts(0, VirtualHostLocalServiceUtil.getVirtualHostsCount());
for (VirtualHost virtualHost : vHosts) {
_log.debug("Found " + virtualHost.getHostname());
if (virtualHost.getHostname().compareTo("localhost") != 0 &&
virtualHost.getLayoutSetId() != 0 &&
virtualHost.getHostname().compareTo(serverName) == 0) {
long layoutSetId = virtualHost.getLayoutSetId();
site = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId).getGroup();
_log.debug("Found match! Your site is " + site.getName());
return site;
}
}
return null;
}
}