aslsocial/src/main/java/org/gcube/applicationsupportlayer/social/AslNewsManager.java

143 lines
4.9 KiB
Java

package org.gcube.applicationsupportlayer.social;
import java.util.Date;
import java.util.List;
import java.util.UUID;
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.server.DBCassandraAstyanaxImpl;
import org.gcube.portal.databook.server.DatabookStore;
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 AslNewsManager 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 AslNewsManager(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<XMLResult> 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<String> 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 <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 ApplicationDataNotFoundException("Your application Image Url was not found in the profile, consider adding <ThumbnailURL> 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 description) {
String descToAdd = escapeHtml(description);
Feed toShare = new Feed(
UUID.randomUUID().toString(),
FeedType.PUBLISH,
application.getName(),
new Date(),
getScopeByOrganizationId(""+aslSession.getGroupId()),
"",
"",
descToAdd,
PrivacyLevel.SINGLE_VRE,
"fullName",
"email",
"thumbnailURL",
"linkTitle",
"linkDescription",
"linkHost");
return getStoreInstance().saveAppFeed(toShare);
}
/**
* {@inheritDoc}
*/
@Override
public boolean shareApplicationUpdate(String description, String uri) {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public boolean shareApplicationUpdate(String description, String uri, String previewTitle, String previewDescription, String previewThumbnailUrl) {
return true;
}
@Override
public DatabookStore connect() {
return new DBCassandraAstyanaxImpl();
}
}