185 lines
7.2 KiB
Java
185 lines
7.2 KiB
Java
package org.gcube.applicationsupportlayer.social;
|
|
|
|
import java.io.InputStream;
|
|
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.applicationsupportlayer.social.ex.ApplicationProfileNotFoundException;
|
|
import org.gcube.applicationsupportlayer.social.imageType.ImageType;
|
|
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.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;
|
|
|
|
|
|
/**
|
|
*
|
|
* @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, String portletClassName) {
|
|
super(session);
|
|
this.application = getProfileFromInfrastrucure(portletClassName);
|
|
}
|
|
/**
|
|
* this method looks up the application profile among the ones available in the infrastructure
|
|
* @param applicationClass
|
|
* @return the application profile
|
|
*/
|
|
private Application getProfileFromInfrastrucure(String portletClassName) {
|
|
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 '" + portletClassName + "'" +
|
|
"return $profile");
|
|
|
|
GCUBEScope scope = aslSession.getScope();
|
|
|
|
List<XMLResult> appProfile = client.execute(query, scope.getInfrastructure());
|
|
if (appProfile == null || appProfile.size() == 0)
|
|
throw new ApplicationProfileNotFoundException("Your application is not registered in the infrastructure");
|
|
else {
|
|
XMLResult node = appProfile.get(0);
|
|
List<String> 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 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 ApplicationProfileNotFoundException("Your application ID n was not found in the profile, consider adding <AppId> element in <Body>");
|
|
|
|
currValue = node.evaluate("/Resource/Profile/Body/ThumbnailURL/text()");
|
|
if (currValue != null && currValue.size() > 0) {
|
|
toReturn.setImageUrl(currValue.get(0));
|
|
}
|
|
else throw new ApplicationProfileNotFoundException("Your application Image Url was not found in the profile, consider adding <ThumbnailURL> element in <Body>");
|
|
currValue = node.evaluate("/Resource/Profile/Body/EndPoint/Scope/text()");
|
|
if (currValue != null && currValue.size() > 0) {
|
|
List<String> 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 application URL was not found in the profile for Scope: " + scope.toString());
|
|
}
|
|
else throw new ApplicationProfileNotFoundException("Your application EndPoint was not found in the profile, consider adding <EndPoint><Scope> element in <Body>");
|
|
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 feedText) {
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, "", "", "", ""));
|
|
}
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public boolean shareApplicationUpdate(String feedText, String uriParams) {
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, uriParams, "", "", ""));
|
|
}
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public boolean shareApplicationUpdate(String feedText, String uriParams, String previewTitle, String previewDescription, String previewThumbnailUrl) {
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, uriParams, previewTitle, previewDescription, previewThumbnailUrl));
|
|
}
|
|
/**
|
|
* buid a an Application Feed
|
|
*
|
|
* @param description add a description for the update you are sharing
|
|
* @param uriParams the additional parameteres your application needs to open the subject of this update e.g. id=12345&type=foo
|
|
* @param previewTitle the title to show in the preview
|
|
* @param previewDescription the description to show in the preview
|
|
* @param previewThumbnailUrl the image url to show in the preview
|
|
* @return a feed instance ready to be written
|
|
*/
|
|
private Feed buildFeed(String description, String uriParams, String previewTitle, String previewDescription, String previewThumbnailUrl) {
|
|
String descToAdd = escapeHtml(description);
|
|
|
|
String uri = application.getUrl();
|
|
//add the GET params if necessary
|
|
if (uriParams != null && uriParams.compareTo("") != 0)
|
|
uri += "?"+uriParams;
|
|
//String scope = getScopeByOrganizationId(""+aslSession.getGroupId());
|
|
String scope = aslSession.getScopeName();
|
|
System.out.println("scope: " + aslSession.getScopeName());
|
|
Feed toReturn = new Feed(
|
|
UUID.randomUUID().toString(),
|
|
FeedType.PUBLISH,
|
|
application.getKey(),
|
|
new Date(),
|
|
scope,
|
|
uri,
|
|
previewThumbnailUrl,
|
|
descToAdd,
|
|
PrivacyLevel.SINGLE_VRE,
|
|
application.getName(),
|
|
"no-email",
|
|
application.getImageUrl(),
|
|
previewTitle,
|
|
previewDescription,
|
|
"",
|
|
true);
|
|
return toReturn;
|
|
}
|
|
@Override
|
|
public boolean shareApplicationUpdate(String feedtext, String uriGETparams,
|
|
String previewTitle, String previewDescription,
|
|
InputStream previewThumbnailInputStream, ImageType imageExtension) {
|
|
// TODO Auto-generated method stub
|
|
return false;
|
|
}
|
|
}
|