package org.gcube.portlets.user.workspace.server.util; import java.util.List; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.applicationsupportlayer.social.ex.ApplicationProfileNotFoundException; import org.gcube.common.core.contexts.GHNContext; import org.gcube.common.core.informationsystem.client.ISClient; import org.gcube.common.core.informationsystem.client.XMLResult; import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.utils.logging.GCUBEClientLog; import org.gcube.portlets.user.workspace.shared.ApplicationProfile; /** * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * */ public class GcubeApplicationProfileReader { protected static final String RESOURCE_PROFILE_BODY_ENDPOINT_URL_TEXT = "/Resource/Profile/Body/EndPoint/URL/text()"; protected static final String RESOURCE_PROFILE_BODY_ENDPOINT_SCOPE_TEXT = "/Resource/Profile/Body/EndPoint/Scope/text()"; protected static final String RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT = "/Resource/Profile/Body/ThumbnailURL/text()"; protected static final String RESOURCE_PROFILE_BODY_APP_ID_TEXT = "/Resource/Profile/Body/AppId/text()"; protected static final String RESOURCE_PROFILE_DESCRIPTION_TEXT = "/Resource/Profile/Description/text()"; protected static final String RESOURCE_PROFILE_NAME_TEXT = "/Resource/Profile/Name/text()"; private ASLSession aslSession; private ApplicationProfile applicationProfile; private GCUBEClientLog logger = new GCUBEClientLog(GcubeApplicationProfileReader.class); public ApplicationProfile getApplicationProfile() { return applicationProfile; } /** * * @param session */ public GcubeApplicationProfileReader(ASLSession session, String portletClassName) { this.aslSession = session; this.applicationProfile = readProfileFromInfrastrucure(portletClassName); } public GcubeApplicationProfileReader(ASLSession session) { this.aslSession = session; this.applicationProfile = null; } /** * this method looks up the applicationProfile profile among the ones available in the infrastructure * @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile * @return the applicationProfile profile */ private ApplicationProfile readProfileFromInfrastrucure(String portletClassName) { try { ApplicationProfile toReturn = new ApplicationProfile(); ISClient client = GHNContext.getImplementation(ISClient.class); GCUBEGenericQuery query = client.getQuery(GCUBEGenericQuery.class); query.setExpression("for $profile in collection('/db/Profiles/GenericResource')//Resource " + "where $profile/Profile/SecondaryType/string() eq 'ApplicationProfile' and $profile/Profile/Body/AppId/string() " + " eq '" + portletClassName + "'" + "return $profile"); GCUBEScope scope = aslSession.getScope(); List appProfile = client.execute(query, scope.getInfrastructure()); if (appProfile == null || appProfile.size() == 0) throw new ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure"); else { XMLResult node = appProfile.get(0); List currValue = null; currValue = node.evaluate(RESOURCE_PROFILE_NAME_TEXT); if (currValue != null && currValue.size() > 0) { toReturn.setName(currValue.get(0)); } else throw new ApplicationProfileNotFoundException("Your applicationProfile NAME was not found in the profile"); currValue = node.evaluate(RESOURCE_PROFILE_DESCRIPTION_TEXT); if (currValue != null && currValue.size() > 0) { toReturn.setDescription(currValue.get(0)); } else logger.warn("No Description exists for " + toReturn.getName()); currValue = node.evaluate(RESOURCE_PROFILE_BODY_APP_ID_TEXT); if (currValue != null && currValue.size() > 0) { toReturn.setKey(currValue.get(0)); } else throw new ApplicationProfileNotFoundException("Your applicationProfile ID n was not found in the profile, consider adding element in "); currValue = node.evaluate(RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT); if (currValue != null && currValue.size() > 0) { toReturn.setImageUrl(currValue.get(0)); } else throw new ApplicationProfileNotFoundException("Your applicationProfile Image Url was not found in the profile, consider adding element in "); currValue = node.evaluate(RESOURCE_PROFILE_BODY_ENDPOINT_SCOPE_TEXT); if (currValue != null && currValue.size() > 0) { List scopes = currValue; boolean foundUrl = false; for (int i = 0; i < scopes.size(); i++) { if (currValue.get(i).trim().compareTo(scope.toString()) == 0) { toReturn.setUrl(node.evaluate(RESOURCE_PROFILE_BODY_ENDPOINT_URL_TEXT).get(i)); toReturn.setScope(scope.toString()); foundUrl = true; break; } } if (! foundUrl) throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString()); } else throw new ApplicationProfileNotFoundException("Your applicationProfile EndPoint was not found in the profile, consider adding element in "); return toReturn; } } catch (Exception e) { logger.error("Error while trying to fetch applicationProfile profile from the infrastructure"); e.printStackTrace(); return null; } } }