2012-12-07 17:43:49 +01:00
package org.gcube.applicationsupportlayer.social ;
2012-12-07 17:47:02 +01:00
2012-12-08 19:49:21 +01:00
import java.util.Date ;
import java.util.List ;
import java.util.UUID ;
2012-12-12 17:16:21 +01:00
import javax.servlet.http.HttpServlet ;
2012-12-07 17:47:02 +01:00
import org.gcube.application.framework.core.session.ASLSession ;
2012-12-08 19:49:21 +01:00
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 ;
2012-12-07 17:47:02 +01:00
2012-12-07 17:43:49 +01:00
/ * *
*
* @author Massimiliano Assante , ISTI - CNR
* @version 0 . 1 Dec 2012
*
2012-12-07 18:05:51 +01:00
* 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
2012-12-07 17:43:49 +01:00
* /
2012-12-12 17:16:21 +01:00
public class ApplicationNewsManager extends SocialPortalBridge implements NewsManager {
2012-12-08 19:49:21 +01:00
private Application application ;
2012-12-07 17:47:02 +01:00
/ * *
*
2012-12-09 17:11:26 +01:00
* @param aslSession the ASLSession instance
2012-12-08 19:49:21 +01:00
* @param applicationClass your servlet class name will be used ad unique identifier for your application
2012-12-07 17:47:02 +01:00
* /
2012-12-12 17:16:21 +01:00
public ApplicationNewsManager ( ASLSession session , Class < ? extends HttpServlet > applicationClass ) {
2012-12-09 17:11:26 +01:00
super ( session ) ;
2012-12-08 19:49:21 +01:00
this . application = getProfileFromInfrastrucure ( applicationClass ) ;
2012-12-07 17:47:02 +01:00
}
2012-12-08 19:49:21 +01:00
/ * *
* this method looks up the application profile among the ones available in the infrastructure
* @param applicationClass
* @return the application profile
* /
2012-12-12 17:16:21 +01:00
private Application getProfileFromInfrastrucure ( Class < ? extends HttpServlet > applicationClass ) {
2012-12-08 19:49:21 +01:00
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 " ) ;
2012-12-09 17:11:26 +01:00
List < XMLResult > appProfile = client . execute ( query , aslSession . getScope ( ) . getInfrastructure ( ) ) ;
2012-12-08 19:49:21 +01:00
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 ) ) ;
}
2012-12-09 17:11:26 +01:00
else _log . warn ( " No Description exists for " + toReturn . getName ( ) ) ;
2012-12-08 19:49:21 +01:00
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 ) {
2012-12-09 17:11:26 +01:00
_log . error ( " Error while trying to fetch application profile from the infrastructure " ) ;
2012-12-08 19:49:21 +01:00
e . printStackTrace ( ) ;
return null ;
}
}
2012-12-07 18:05:51 +01:00
/ * *
* { @inheritDoc }
* /
@Override
2012-12-08 19:49:21 +01:00
public boolean shareApplicationUpdate ( String description ) {
2012-12-12 17:16:21 +01:00
return getStoreInstance ( ) . saveAppFeed ( buildFeed ( description , " " , " " , " " , " " ) ) ;
2012-12-07 18:05:51 +01:00
}
2012-12-07 17:43:49 +01:00
/ * *
2012-12-07 18:02:20 +01:00
* { @inheritDoc }
2012-12-07 17:43:49 +01:00
* /
@Override
2012-12-08 19:49:21 +01:00
public boolean shareApplicationUpdate ( String description , String uri ) {
2012-12-12 17:16:21 +01:00
return getStoreInstance ( ) . saveAppFeed ( buildFeed ( description , uri , " " , " " , " " ) ) ;
2012-12-07 17:43:49 +01:00
}
/ * *
2012-12-07 18:02:20 +01:00
* { @inheritDoc }
2012-12-07 17:43:49 +01:00
* /
@Override
2012-12-08 19:49:21 +01:00
public boolean shareApplicationUpdate ( String description , String uri , String previewTitle , String previewDescription , String previewThumbnailUrl ) {
2012-12-12 17:16:21 +01:00
return getStoreInstance ( ) . saveAppFeed ( buildFeed ( description , uri , previewTitle , previewDescription , previewThumbnailUrl ) ) ;
2012-12-07 17:43:49 +01:00
}
2012-12-12 17:16:21 +01:00
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 ;
2012-12-08 19:49:21 +01:00
}
2012-12-07 17:43:49 +01:00
}