2012-12-08 19:49:21 +01:00
package org.gcube.applicationsupportlayer.social ;
2013-01-03 16:28:36 +01:00
import java.util.List ;
2012-12-09 17:11:26 +01:00
import org.gcube.application.framework.core.session.ASLSession ;
2013-01-03 16:28:36 +01:00
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 ;
2012-12-08 19:49:21 +01:00
import org.gcube.common.core.utils.logging.GCUBEClientLog ;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl ;
import org.gcube.portal.databook.server.DatabookStore ;
2013-01-03 16:28:36 +01:00
import org.gcube.portal.databook.shared.ApplicationProfile ;
2012-12-08 19:49:21 +01:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayGroupManager ;
/ * *
*
* @author Massimiliano Assante , ISTI - CNR
* @version 0 . 1 Dec 2012
*
2013-01-03 16:28:36 +01:00
* superclass for notifications , posting news and so on
2012-12-08 19:49:21 +01:00
* /
2013-01-03 16:28:36 +01:00
public class SocialPortalBridge {
2012-12-09 17:11:26 +01:00
protected static GCUBEClientLog _log = new GCUBEClientLog ( SocialPortalBridge . class ) ;
protected ASLSession aslSession ;
2013-01-03 16:28:36 +01:00
protected ApplicationProfile applicationProfile ;
2012-12-09 17:11:26 +01:00
//unique instance
private static DatabookStore store ;
2013-01-03 16:28:36 +01:00
2012-12-12 17:16:21 +01:00
/ * *
*
* @param session
* /
2013-01-03 16:28:36 +01:00
public SocialPortalBridge ( ASLSession session , String portletClassName ) {
2012-12-09 17:11:26 +01:00
this . aslSession = session ;
2013-01-03 16:28:36 +01:00
this . applicationProfile = getProfileFromInfrastrucure ( portletClassName ) ;
2012-12-09 17:11:26 +01:00
}
2013-01-09 17:47:22 +01:00
/ * *
*
* @param session
* /
public SocialPortalBridge ( ASLSession session ) {
this . aslSession = session ;
this . applicationProfile = null ;
}
2012-12-12 17:16:21 +01:00
/ * *
*
* @return the unique instance of the store
* /
2012-12-09 17:11:26 +01:00
public static synchronized DatabookStore getStoreInstance ( ) {
if ( store = = null ) {
store = new DBCassandraAstyanaxImpl ( ) ;
}
return store ;
2012-12-08 19:49:21 +01:00
}
2012-12-09 17:11:26 +01:00
2012-12-12 17:16:21 +01:00
protected String getScopeByOrganizationId ( String vreid ) {
2012-12-08 19:49:21 +01:00
GroupManager gm = new LiferayGroupManager ( ) ;
try {
return gm . getScope ( vreid ) ;
} catch ( Exception e ) {
_log . error ( " Could not find a scope for this VREid: " + vreid ) ;
return null ;
}
}
/ * *
* Escape an html string . Escaping data received from the client helps to
* prevent cross - site script vulnerabilities .
*
* @param html the html string to escape
* @return the escaped string
* /
2012-12-12 17:16:21 +01:00
protected String escapeHtml ( String html ) {
2012-12-08 19:49:21 +01:00
if ( html = = null ) {
return null ;
}
return html . replaceAll ( " & " , " & " ) . replaceAll ( " < " , " < " )
. replaceAll ( " > " , " > " ) ;
}
2013-01-03 16:28:36 +01:00
/ * *
* @return the applicationProfile profile among the ones available in the infrastructure
* /
public ApplicationProfile getApplicationProfile ( ) {
return applicationProfile ;
}
/ * *
* 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 getProfileFromInfrastrucure ( 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 < XMLResult > 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 < 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 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 _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 applicationProfile 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 applicationProfile 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 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 <EndPoint><Scope> element in <Body> " ) ;
return toReturn ;
}
} catch ( Exception e ) {
_log . error ( " Error while trying to fetch applicationProfile profile from the infrastructure " ) ;
e . printStackTrace ( ) ;
return null ;
}
}
2012-12-08 19:49:21 +01:00
}