join-vre/src/main/java/org/gcube/portlets/user/joinvre/server/JoinServiceImpl.java

444 lines
20 KiB
Java

package org.gcube.portlets.user.joinvre.server;
import java.util.ArrayList;
import java.util.Collections;
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.CustomAttributeKeys;
import org.gcube.common.portal.GCubePortalConstants;
import org.gcube.common.portal.PortalContext;
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.Invite;
import org.gcube.portal.databook.shared.InviteStatus;
import org.gcube.portlets.user.joinvre.client.JoinService;
import org.gcube.portlets.user.joinvre.shared.UserBelonging;
import org.gcube.portlets.user.joinvre.shared.VRE;
import org.gcube.portlets.user.joinvre.shared.VRECategory;
import org.gcube.portlets.user.joinvre.shared.VRECustomAttributes;
import org.gcube.vomanagement.usermanagement.GroupManager;
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.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeMembershipRequest;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.gcube.vomanagement.usermanagement.model.MembershipRequestStatus;
import org.gcube.vomanagement.usermanagement.model.VirtualGroup;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
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.service.LayoutSetLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.VirtualHostLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
/**
* @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it
*/
@SuppressWarnings("serial")
public class JoinServiceImpl extends RemoteServiceServlet implements JoinService {
private static Log _log = LogFactoryUtil.getLog(JoinServiceImpl.class);
private static final String TEST_USER = "test.user";
private static DatabookStore store;
/**
* the current ASLSession
* @return the session
*/
private ASLSession getASLSession() {
String sessionID = this.getThreadLocalRequest().getSession().getId();
String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
if (user == null) {
_log.warn("USER IS NULL setting test.user and Running OUTSIDE PORTAL");
user = getDevelopmentUser();
SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube");
}
return SessionManager.getInstance().getASLSession(sessionID, user);
}
/**
* when packaging test will fail if the user is not set to test.user
* @return .
*/
public String getDevelopmentUser() {
String user = TEST_USER;
return user;
}
/**
*
* @return true if you're running into the portal, false if in development
*/
private boolean isWithinPortal() {
try {
UserLocalServiceUtil.getService();
return true;
} catch (com.liferay.portal.kernel.bean.BeanLocatorException ex) {
_log.trace("Development Mode ON");
return false;
}
}
@Override
public String joinVRE(Long vreID) {
try {
return getCurrGroupFromURL().getName();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
*
* @return the Liferay Group of the Site corresponding to the virtualhost being used to access the portal
* e.g. if i-marine.d4science.org it would return the Liferay Group i-marine, if services.d4science.org it would return the Liferay Group services
* it assums that you have a Site defined for each virtual host supported
* @throws Exception
*/
private Group getCurrGroupFromURL() throws Exception {
Group site = null;
String currentVirtualHost = this.getThreadLocalRequest().getServerName();
_log.debug("currentHost is " + currentVirtualHost);
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(currentVirtualHost) == 0) {
long layoutSetId = virtualHost.getLayoutSetId();
site = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId).getGroup();
return site;
}
}
return null;
}
/**
*
* @param session the Asl Session
* @param withinPortal true when is on Liferay portal
* @return the users belonging to the current organization (scope)
*/
@Override
public LinkedHashMap<VRECategory, ArrayList<VRE>> getVREs() {
LinkedHashMap<VRECategory, ArrayList<VRE>> toReturn = new LinkedHashMap<VRECategory, ArrayList<VRE>>();
try {
if (isWithinPortal()) {
toReturn = getPortalSitesMappedToVRE();
} else {
toReturn = getFakePortalVREs(); }
} catch (Exception e) {
_log.error("Error getting VREs", e);
}
return toReturn;
}
public VRECustomAttributes getVRECustomAttr(GCubeGroup group) throws PortalException, SystemException {
VRECustomAttributes toReturn = new VRECustomAttributes();
try {
GroupManager gm = new LiferayGroupManager();
toReturn.setUponRequest(group.isRequestBasedGroup());
System.out.println(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
String url = (String) gm.readCustomAttr(group.getGroupId(), CustomAttributeKeys.URL.getKeyName());
toReturn.setUrlIfAny(url);
}
} catch (Exception e) {
_log.error("Something went wrong when trying to read VRE Custom Attr, " + e);
return toReturn;
}
_log.trace("RETURNING VRECustomAttributes:\n" + toReturn.toString());
return toReturn;
}
/**
*
* @return the Virtual groups with their VREs in the order estabilished in the LR Control Panel
* @throws SystemException
* @throws PortalException
*/
public LinkedHashMap<VRECategory, ArrayList<VRE>> getPortalSitesMappedToVRE() throws Exception {
ASLSession session = getASLSession();
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) {
ArrayList<VRE> toCreate = new ArrayList<VRE>();
VRECategory cat = new VRECategory(1L, vg.getName(), vg.getDescription());
toReturn.put(cat, toCreate);
}
GCubeGroup rootGroupVO = gm.getRootVO();
try {
_log.debug("root: " + rootGroupVO.getGroupName() );
} catch (NullPointerException e) {
_log.error("Cannot find root organziation, please check gcube-data.properties file in $CATALINA_HOME/conf folder, unless your installing the Bundle");
return toReturn;
}
//for each root sub organizations (VO)
for (GCubeGroup vOrg : rootGroupVO.getChildren()) {
for (GCubeGroup vreOrganization : vOrg.getChildren()) {
long vreID = vreOrganization.getGroupId();
String vreName = vreOrganization.getGroupName();
String vreDescription = vreOrganization.getDescription();
long logoId = vreOrganization.getLogoId();
String vreLogoURL = "/image/layout_set_logo?img_id="+ logoId;
String groupName = gm.getInfrastructureScope(vreOrganization.getGroupId());
String friendlyURL = GCubePortalConstants.PREFIX_GROUP_URL+vreOrganization.getFriendlyURL();
VRECustomAttributes attrs = getVRECustomAttr(vreOrganization);
boolean requireAccessGrant = attrs.isUponRequest();
boolean isExternal = attrs.isExternal();
String urlIfAny = attrs.getUrlIfAny();
String catName = gm.getVirtualGroup(vreID).getName();
VRECategory toLookFor = null;
for (VRECategory vre : toReturn.keySet()) {
if (vre.getName().compareTo(catName)==0)
toLookFor = vre;
}
if (toLookFor != null) {
ArrayList<VRE> toUpdate = toReturn.get(toLookFor);
UserBelonging belongs = UserBelonging.NOT_BELONGING;
VRE toAdd = new VRE(vreID,vreName, vreDescription, vreLogoURL, groupName, friendlyURL, belongs, requireAccessGrant, isExternal, urlIfAny);
if (session.getUsername().compareTo(TEST_USER) != 0) {
GCubeUser currUser = new LiferayUserManager().getUserByUsername(session.getUsername());
//check if the user belongs to it
if (gm.listGroupsByUser(currUser.getUserId()).contains(vreOrganization)) {
toAdd.setUserBelonging(UserBelonging.BELONGING);
}
else if (checkPending(currUser.getUsername(), vreOrganization.getGroupId()))
toAdd.setUserBelonging(UserBelonging.PENDING);
}
toUpdate.add(toAdd);
}
}
}
//sort the vres in the groups
for (VRECategory cat : toReturn.keySet()) {
ArrayList<VRE> toSort = toReturn.get(cat);
Collections.sort(toSort);
}
return toReturn;
}
/**
* useful method for development purpose
* @return
*/
private LinkedHashMap<VRECategory, ArrayList<VRE>> getFakePortalVREs() {
LinkedHashMap<VRECategory, ArrayList<VRE>> toReturn = new LinkedHashMap<VRECategory, ArrayList<VRE>>();
VRECategory devsecCategory = new VRECategory(1, "Z_Development", "designed to apply Data Mining techniques to biological data. "
+ "The algorithms are executed in a distributed fashion on the e-Infrastructure nodes or on local multi-core machines.");
ArrayList<VRE> vres = new ArrayList<VRE>();
vres.add(new VRE(0, "BiodiversityLab", ""
+ "<h2>BiodiversityLab</h2>"
+ "The BiodiversityLab is a VRE designed to provide a collection of applications that allow scholars to perform complete experiments about "
+ "single individuals or groups of marine species. The VRE allows to: <ul> <li> inspect species maps;<li> produce a species distribution map by means of either an expert system (AquaMaps) or a machine learning model (e.g. Neural Networks);"
+ "<li> analyse species observation trends;"
+ "<li> inspect species occurrence data;"
+ "<li> inspect species descriptions and characteristics;"
+ "<li> perform analysis of climatic changes and of their effects on species distribution;"
+ "<li> produce GIS maps for geo-spatial datasets;"
+ "<li> discover Taxa names;"
+ "<li> cluster occurrence data;"
+ "<li> estimate similarities among habitats."
+ "</ul>"
+ "", "", "http://placehold.it/200x200", "/group/devsec", UserBelonging.NOT_BELONGING, false));
vres.add(new VRE(0, "Scalable Data", ""
+ "<h2>Scalable Data Mining</h2>"
+ "The Scalable Data Mining is a VRE designed to apply Data Mining techniques to biological data. The algorithms are executed in a distributed fashion on the e-Infrastructure nodes or on local multi-core machines. Scalability is thus meant as distributed data processing but even as services dynamically provided to the users. The system is scalable in the number of users and in the size of the data to process. Statistical data processing can be applied to perform Niche Modelling or Ecological Modelling experiments. Other applications can use general purpose techniques like Bayesian models. Time series of observations can be managed as well, in order to classify trends, catch anomaly patterns and perform simulations. The idea under the distributed computation for data mining techniques is to overcome common limitations that can happen when using statistical algorithms: "
+ "single individuals or groups of marine species. The VRE allows to: <ul> <li> inspect species maps;<li> produce a species distribution map by means of either an expert system (AquaMaps) or a machine learning model (e.g. Neural Networks);"
+ "<li> analyse species observation trends;"
+ "<li> inspect species occurrence data;"
+ "<li> inspect species descriptions and characteristics;"
+ "<li> perform analysis of climatic changes and of their effects on species distribution;"
+ "<li> produce GIS maps for geo-spatial datasets;"
+ "<li> discover Taxa names;"
+ "<li> cluster occurrence data;"
+ "<li> estimate similarities among habitats."
+ "</ul>"
+ "", "", "http://placehold.it/200x200", "/group/devsec", UserBelonging.NOT_BELONGING, true));
toReturn.put(devsecCategory, vres);
devsecCategory = new VRECategory(2, "Sailing", "Sailing prod desc");
vres = new ArrayList<VRE>();
vres.add(new VRE(1, "PerformanceEvaluationInAquaculture", "devVRE VRE description", "http://placehold.it/200x100", "http://placehold.it/200x100", "/group/devVRE", UserBelonging.NOT_BELONGING, false, true, "http://i-marine.d4science.org"));
vres.add(new VRE(2, "devmode", "devmode VRE description", "http://placehold.it/200x100", "https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97100&w=200&h=100", "/group/devmode", UserBelonging.NOT_BELONGING, true));
vres.add(new VRE(1, "StrategicInvestmentAnalysis", "devVRE VRE description", "", "https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97100&w=200&h=100", "/group/devVRE", UserBelonging.NOT_BELONGING, false));
vres.add(new VRE(2, "devmode2", "devmode VRE description", "http://placehold.it/200x100", "", "/group/devmode", UserBelonging.NOT_BELONGING, true));
vres.add(new VRE(1, "devVR3E", "devVRE VRE description", "http://placehold.it/200x200", "aaaa", "/group/devVRE", UserBelonging.NOT_BELONGING, false));
vres.add(new VRE(2, "devmode3", "devmode VRE description", "http://placehold.it/200x200", "", "/group/devmode", UserBelonging.NOT_BELONGING, true));
vres.add(new VRE(1, "devVRE4", "devVRE VRE description", "", "http://placehold.it/200x200", "/group/devVRE", UserBelonging.NOT_BELONGING, false));
vres.add(new VRE(2, "devmode4", "devmode VRE description", "", "http://placehold.it/200x200", "/group/devmode", UserBelonging.NOT_BELONGING, true));
vres.add(new VRE(1, "devVRE5", "devVRE VRE description", "", "http://placehold.it/200x200", "/group/devVRE", UserBelonging.NOT_BELONGING, false));
vres.add(new VRE(2, "devmode5", "devmode VRE description", "", "http://placehold.it/200x200", "/group/devmode", UserBelonging.NOT_BELONGING, true));
vres.add(new VRE(1, "devVRE6", "devVRE VRE description", "", "http://placehold.it/200x200", "/group/devVRE", UserBelonging.NOT_BELONGING, false));
vres.add(new VRE(2, "devmode6", "devmode VRE description", "", "http://placehold.it/200x200", "/group/devmode", UserBelonging.NOT_BELONGING, true));
vres.add(new VRE(1, "devVRE7", "devVRE VRE description", "", "http://placehold.it/200x200", "/group/devVRE", UserBelonging.NOT_BELONGING, false));
vres.add(new VRE(2, "devmod76", "devmode VRE description", "", "http://placehold.it/200x200", "/group/devmode", UserBelonging.NOT_BELONGING, true));
toReturn.put(devsecCategory, vres);
return toReturn;
}
@Override
public VRE getSelectedVRE(Long groupId) {
_log.debug("*getting Selected Research Environment from referral, site id = " + groupId);
ASLSession session = getASLSession();
VRE toReturn = null;
try {
GroupManager gm = new LiferayGroupManager();
UserManager um = new LiferayUserManager();
GCubeGroup selectedVRE = gm.getGroup(groupId);
String vreName = selectedVRE.getGroupName();
String vreDescription = selectedVRE.getDescription();
long logoId = selectedVRE.getLogoId();
String vreLogoURL = "/image/layout_set_logo?img_id="+ logoId;
String infraScope = gm.getInfrastructureScope(selectedVRE.getGroupId());
String friendlyURL = GCubePortalConstants.PREFIX_GROUP_URL+selectedVRE.getFriendlyURL();
VRECustomAttributes attrs = getVRECustomAttr(selectedVRE);
boolean requireAccessGrant = attrs.isUponRequest();
boolean isExternal = attrs.isExternal();
String urlIfAny = attrs.getUrlIfAny();
GCubeUser currUser = um.getUserByUsername(session.getUsername());
//check if the user belongs to it
UserBelonging belongEnum = UserBelonging.NOT_BELONGING;
if (gm.listGroupsByUser(currUser.getUserId()).contains(selectedVRE))
belongEnum = UserBelonging.BELONGING;
else if (checkPending(session.getUsername(), selectedVRE.getGroupId()))
belongEnum = UserBelonging.PENDING;
//return the selected VRE for this user
toReturn = new VRE(groupId, vreName, vreDescription, vreLogoURL, infraScope, friendlyURL, belongEnum, requireAccessGrant, isExternal, urlIfAny);
} catch (Exception e) {
_log.error("Something wrong happened while trying to getSite by id, probably the group id is wrong. " + e.getMessage());
}
return toReturn;
}
/**
*
* @param screenName
* @param groupId
* @return
* @throws UserRetrievalFault
* @throws GroupRetrievalFault
* @throws UserManagementSystemException
*/
private static boolean checkPending(String screenName, long groupId) throws UserManagementSystemException, GroupRetrievalFault, UserRetrievalFault {
UserManager um = new LiferayUserManager();
List<GCubeMembershipRequest> requests = um.listMembershipRequestsByGroup(groupId);
for (GCubeMembershipRequest r : requests) {
if ( r.getStatus() == MembershipRequestStatus.REQUEST && (r.getRequestingUser().getUsername().compareTo(screenName)==0))
return true;
}
return false;
}
@Override
public void addMembershipRequest(String scope, String optionalMessage) {
String username = getASLSession().getUsername();
try {
LoginServiceUtil.addMembershipRequest(username, scope, optionalMessage, getPortalUrl(), PortalContext.getConfiguration().getGatewayName());
} catch (Exception e) {
e.printStackTrace();
}
}
public String getPortalUrl() throws PortalException, SystemException {
return PortalUtil.getPortalURL(this.getThreadLocalRequest());
}
/**
* the user to the VRE, plus send notifications to the vre manages of the vre
*/
@Override
public boolean registerUser(String scope, long groupId, boolean isInvitation) {
UserManager userM = new LiferayUserManager();
try {
ASLSession session = getASLSession();
String username = session.getUsername();
//add the user to the VRE
userM.assignUserToGroup(groupId, userM.getUserId(username));
String gatewayName = PortalContext.getConfiguration().getGatewayName();
if (isInvitation) {
initStore();
String inviteId = store.isExistingInvite(scope, session.getUserEmailAddress());
if (inviteId != null) {
Invite invite = store.readInvite(inviteId);
store.setInviteStatus(scope, session.getUserEmailAddress(), InviteStatus.ACCEPTED);
LoginServiceUtil.notifyUserAcceptedInvite(username, scope, getPortalBasicUrl(), gatewayName, invite);
}
}
else {
//LoginServiceUtil.notifyUserSelfRegistration(username, scope, getPortalBasicUrl(), gatewayName);
_log.info("notifyUserSelfRegistration sent");
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
*
* @return the unique instance of the store
*/
public static synchronized DatabookStore initStore() {
if (store == null) {
store = new DBCassandraAstyanaxImpl();
}
return store;
}
/**
*
* @return the portal basic url, e.g. http://www.foo.com
*/
private String getPortalBasicUrl() {
HttpServletRequest request = this.getThreadLocalRequest();
String toReturn = "";
//protocol
String protocol = (request.isSecure()) ? "https://" : "http://" ;
toReturn += protocol;
//server name
toReturn += request.getServerName();
//port
toReturn += (request.getServerPort() == 80) ? "" : ":"+request.getServerPort() ;
//_log.trace("getPortalBasicUrl: " +toReturn + "request.getServerPort: " + request.getServerPort());
return toReturn;
}
}