package org.gcube.applicationsupportlayer.social; import java.util.Date; import java.util.List; import java.util.UUID; import javax.servlet.http.HttpServlet; import org.gcube.application.framework.core.session.ASLSession; 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.portal.databook.shared.Application; import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.FeedType; import org.gcube.portal.databook.shared.PrivacyLevel; import org.gcube.portlets.user.homelibrary.home.data.application.ApplicationDataNotFoundException; /** * * @author Massimiliano Assante, ISTI-CNR * @version 0.1 Dec 2012 * * use to share updates from within your application, the update will be published in the Users News Feed belonging to the VRE your application runs into */ public class ApplicationNewsManager extends SocialPortalBridge implements NewsManager { private Application application; /** * * @param aslSession the ASLSession instance * @param applicationClass your servlet class name will be used ad unique identifier for your application */ public ApplicationNewsManager(ASLSession session, Class applicationClass) { super(session); this.application = getProfileFromInfrastrucure(applicationClass); } /** * this method looks up the application profile among the ones available in the infrastructure * @param applicationClass * @return the application profile */ private Application getProfileFromInfrastrucure(Class applicationClass) { try { Application toReturn = new Application(); 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 '" + applicationClass.getName() + "'" + "return $profile"); List appProfile = client.execute(query, aslSession.getScope().getInfrastructure()); if (appProfile == null || appProfile.size() == 0) throw new ApplicationDataNotFoundException("Your application 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 ApplicationDataNotFoundException("Your application 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 _log.warn("No Description exists for " + toReturn.getName()); currValue = node.evaluate("/Resource/Profile/Body/AppId/text()"); if (currValue != null && currValue.size() > 0) { toReturn.setKey(currValue.get(0)); } else throw new ApplicationDataNotFoundException("Your application ID n was not found in the profile, consider adding element in "); currValue = node.evaluate("/Resource/Profile/Body/ThumbnailURL/text()"); if (currValue != null && currValue.size() > 0) { toReturn.setImageUrl(currValue.get(0)); } else throw new ApplicationDataNotFoundException("Your application Image Url was not found in the profile, consider adding element in "); return toReturn; } } catch (Exception e) { _log.error("Error while trying to fetch application profile from the infrastructure"); e.printStackTrace(); return null; } } /** * {@inheritDoc} */ @Override public boolean shareApplicationUpdate(String description) { return getStoreInstance().saveAppFeed(buildFeed(description, "", "", "", "")); } /** * {@inheritDoc} */ @Override public boolean shareApplicationUpdate(String description, String uri) { return getStoreInstance().saveAppFeed(buildFeed(description, uri, "", "", "")); } /** * {@inheritDoc} */ @Override public boolean shareApplicationUpdate(String description, String uri, String previewTitle, String previewDescription, String previewThumbnailUrl) { return getStoreInstance().saveAppFeed(buildFeed(description, uri, previewTitle, previewDescription, previewThumbnailUrl)); } private Feed buildFeed(String description, String uri, String previewTitle, String previewDescription, String previewThumbnailUrl) { String descToAdd = escapeHtml(description); Feed toReturn = new Feed( UUID.randomUUID().toString(), FeedType.PUBLISH, application.getKey(), new Date(), getScopeByOrganizationId(""+aslSession.getGroupId()), uri, previewThumbnailUrl, descToAdd, PrivacyLevel.SINGLE_VRE, application.getName(), "no-email", application.getImageUrl(), previewTitle, previewDescription, ""); return toReturn; } }