package org.gcube.portlets.user.joinvre.server; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Properties; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.SessionManager; import org.gcube.portal.custom.communitymanager.OrganizationsUtil; import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.model.Organization; import com.liferay.portal.service.OrganizationLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil; /** * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ */ @SuppressWarnings("serial") public class JoinServiceImpl extends RemoteServiceServlet implements JoinService { private static final Logger _log = LoggerFactory.getLogger(JoinServiceImpl.class); /** * 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; } } /** * * @param session the Asl Session * @param withinPortal true when is on Liferay portal * @return the users belonging to the current organization (scope) */ @Override public ArrayList getVREs() { ArrayList vres = new ArrayList(); try { if (isWithinPortal()) { } else { List devsecCategories = new ArrayList(); devsecCategories.add("Development"); vres.add(new VRE(0, "devsec", "devsec VRE", "", "", "/group/devsec", devsecCategories, UserBelonging.NOT_BELONGING, false)); List devVRECategories = new ArrayList(devsecCategories); devVRECategories.add("Sailing"); vres.add(new VRE(1, "devVRE", "devVRE VRE", "", "", "/group/devVRE", devVRECategories, UserBelonging.NOT_BELONGING, false)); List devmodeategories = new ArrayList(devsecCategories); devmodeategories.add("Climbing"); vres.add(new VRE(2, "devmode", "devmode VRE", "", "", "/group/devmode", devmodeategories, UserBelonging.NOT_BELONGING, true)); } } catch (Exception e) { _log.error("Error in server get all contacts ", e); } // Ordering VREs by Name Collections.sort(vres); return vres; } public ArrayList getPortalOrganizationMappedToVRE() throws SystemException{ List organizations = OrganizationLocalServiceUtil.getOrganizations(0, OrganizationLocalServiceUtil.getOrganizationsCount()); Organization rootOrganization = null; for (Organization organization : organizations) { if (organization.getName().equals(getRootOrganizationName())) { rootOrganization = organization; break; } } try { _log.info("root: " + rootOrganization.getName() ); } 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 new ArrayList(); } //for each root sub organizations (VO) for (Organization vOrg : rootOrganization.getSuborganizations()) { for (Organization vre : vOrg.getSuborganizations()) { } } return null; } public static final String ROOT_ORG = "rootorganization"; /** * read the root VO name from a property file and retuns it */ protected static String getRootOrganizationName() { //get the portles to look for from the property file Properties props = new Properties(); String toReturn = ""; try { String propertyfile = OrganizationsUtil.getTomcatFolder()+"conf/gcube-data.properties"; File propsFile = new File(propertyfile); FileInputStream fis = new FileInputStream(propsFile); props.load( fis); toReturn = props.getProperty(ROOT_ORG); } //catch exception in case properties file does not exist catch(IOException e) { toReturn = "gcube"; _log.error("gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default VO Name " + toReturn); return toReturn; } _log.debug("Returning Root VO Name: " + toReturn ); return toReturn; } }