2013-01-21 12:30:24 +01:00
package org.gcube.portlets.user.socialprofile.client ;
2014-09-23 17:56:40 +02:00
import net.eliasbalasis.tibcopagebus4gwt.client.PageBusAdapter ;
import net.eliasbalasis.tibcopagebus4gwt.client.PageBusAdapterException ;
import net.eliasbalasis.tibcopagebus4gwt.testsubscriber.client.Person ;
import net.eliasbalasis.tibcopagebus4gwt.testsubscriber.client.PersonJsonizer ;
2024-02-01 17:26:38 +01:00
import org.gcube.social_networking.socialnetworking.model.client.GCubeSocialNetworking ;
import org.gcube.social_networking.socialnetworking.model.client.util.Encoder ;
2013-05-08 17:17:55 +02:00
import org.gcube.portlets.user.socialprofile.client.ui.DisplayProfile ;
2014-07-23 16:39:04 +02:00
import org.gcube.portlets.user.socialprofile.client.ui.DisplaySummary ;
2014-08-08 17:11:25 +02:00
import org.gcube.portlets.user.socialprofile.client.ui.ErrorAlert ;
import org.gcube.portlets.user.socialprofile.client.ui.OkAlert ;
2014-07-23 16:39:04 +02:00
import org.gcube.portlets.user.socialprofile.shared.UserContext ;
2014-09-23 17:56:40 +02:00
import org.jsonmaker.gwt.client.Jsonizer ;
2013-05-08 17:17:55 +02:00
2013-01-21 12:30:24 +01:00
import com.google.gwt.core.client.EntryPoint ;
2014-07-23 16:39:04 +02:00
import com.google.gwt.core.client.GWT ;
2014-09-02 19:08:38 +02:00
import com.google.gwt.user.client.Cookies ;
2014-07-23 16:39:04 +02:00
import com.google.gwt.user.client.Window ;
2016-07-19 15:09:13 +02:00
import com.google.gwt.user.client.Window.Location ;
2014-07-23 16:39:04 +02:00
import com.google.gwt.user.client.rpc.AsyncCallback ;
2013-01-21 12:30:24 +01:00
import com.google.gwt.user.client.ui.RootPanel ;
2014-07-23 16:39:04 +02:00
import com.google.gwt.user.client.ui.VerticalPanel ;
2013-01-21 12:30:24 +01:00
/ * *
* Entry point classes define < code > onModuleLoad ( ) < / code > .
* /
public class SocialProfile implements EntryPoint {
2014-09-02 19:08:38 +02:00
2014-07-23 16:39:04 +02:00
private final SocialServiceAsync socialService = GWT . create ( SocialService . class ) ;
2014-09-23 17:56:40 +02:00
private final PageBusAdapter pageBusAdapter = new PageBusAdapter ( ) ;
2014-09-02 19:08:38 +02:00
2014-07-23 16:39:04 +02:00
private VerticalPanel mainPanel = new VerticalPanel ( ) ;
private DisplayProfile dispProfile = new DisplayProfile ( ) ;
2016-05-05 18:19:00 +02:00
private DisplaySummary summary = new DisplaySummary ( ) ;
2014-09-02 19:08:38 +02:00
2014-07-23 16:39:04 +02:00
public void onModuleLoad ( ) {
2016-05-06 11:46:15 +02:00
2016-05-05 18:19:00 +02:00
// set main panel width
2016-05-06 12:35:29 +02:00
mainPanel . setWidth ( " 100% " ) ;
2016-05-06 11:46:15 +02:00
2014-08-08 17:11:25 +02:00
if ( isUserAuthZFromLinkedIn ( ) ) {
2014-09-02 19:08:38 +02:00
String authorizationCode = checkLinkedInAuthZ ( ) ;
if ( authorizationCode ! = null ) {
2014-09-03 18:34:35 +02:00
mainPanel . add ( new OkAlert ( " Authorization OK! Please wait while we import from LinkedIn ... " , false ) ) ;
2014-09-23 17:56:40 +02:00
socialService . fetchUserProfile ( authorizationCode , DisplayProfile . getRedirectURI ( ) , new AsyncCallback < String > ( ) {
2014-09-02 19:08:38 +02:00
@Override
2014-09-23 17:56:40 +02:00
public void onSuccess ( String result ) {
if ( result = = null ) {
2014-09-02 19:08:38 +02:00
mainPanel . clear ( ) ;
2014-09-04 18:08:31 +02:00
mainPanel . add ( new ErrorAlert ( " Something went wrong while parsing your professional summary from LinkedIn, please report the issue. " , true ) ) ;
displayProfile ( ) ;
2014-09-02 19:08:38 +02:00
}
2014-09-03 18:34:35 +02:00
else {
mainPanel . clear ( ) ;
2019-07-09 16:53:21 +02:00
mainPanel . add ( new OkAlert ( " Picture retrieved, please note that your professional summary could not due to recent changes in LinkedIn API. Please use Edit Profile Manually. " , true ) ) ;
2014-09-03 18:34:35 +02:00
displayProfile ( ) ;
2019-07-09 16:53:21 +02:00
// //result contain the publicProfileLinkedInUrl
// sendRefreshClientEvent(result);
// // force refresh to reload updated content
// setUrlBase();
2014-09-03 18:34:35 +02:00
}
2014-09-02 19:08:38 +02:00
}
@Override
public void onFailure ( Throwable caught ) {
mainPanel . clear ( ) ;
2014-09-04 18:08:31 +02:00
mainPanel . add ( new ErrorAlert ( " Something went wrong while communicating with LinkedIn service, please report us the issue. " , true ) ) ;
displayProfile ( ) ;
2014-09-02 19:08:38 +02:00
}
} ) ;
2014-09-03 18:34:35 +02:00
2014-09-02 19:08:38 +02:00
}
} else {
displayProfile ( ) ;
2014-08-08 17:11:25 +02:00
}
2014-09-02 19:08:38 +02:00
RootPanel . get ( " SocialProfileDiv " ) . add ( mainPanel ) ;
}
2014-09-23 17:56:40 +02:00
protected void sendRefreshClientEvent ( String inPublicProfileURL ) {
//create the Contact bean data
Person person = new Person ( ) ;
person . setName ( inPublicProfileURL ) ;
// publish a message with Contact bean data
try {
pageBusAdapter . PageBusPublish ( " net.eliasbalasis.tibcopagebus4gwt.testsubscriber.client.Person " , person , ( Jsonizer ) GWT . create ( PersonJsonizer . class ) ) ;
} catch ( PageBusAdapterException e ) {
e . printStackTrace ( ) ;
}
}
2014-09-02 19:08:38 +02:00
/ * *
* display the profile of the user
* /
private void displayProfile ( ) {
2018-06-15 12:45:54 +02:00
socialService . getUserContext ( getUserToShowId ( true ) , new AsyncCallback < UserContext > ( ) {
2014-07-23 16:39:04 +02:00
@Override
public void onSuccess ( UserContext result ) {
mainPanel . add ( dispProfile ) ;
dispProfile . show ( result ) ;
if ( result . getSummary ( ) ! = null & & result . getSummary ( ) . compareTo ( " " ) ! = 0 ) {
2016-05-05 18:19:00 +02:00
summary . setSummary ( result . getSummary ( ) ) ;
2014-07-23 16:39:04 +02:00
}
2016-05-05 18:19:00 +02:00
mainPanel . add ( summary ) ;
dispProfile . setDisplaySummarySibling ( summary ) ;
2014-07-23 16:39:04 +02:00
}
@Override
public void onFailure ( Throwable caught ) {
mainPanel . add ( dispProfile ) ;
2016-07-20 11:26:50 +02:00
// dispProfile.showError(caught.getMessage());
2014-07-23 16:39:04 +02:00
}
} ) ;
}
2016-07-19 15:09:13 +02:00
/ * *
* Removes any get parameter ( such as code , state )
* /
private void setUrlBase ( ) {
String baseUrl = Window . Location . getHref ( ) . split ( " \\ ? " ) [ 0 ] ;
GWT . log ( baseUrl ) ;
Location . assign ( baseUrl ) ;
}
2014-09-02 19:08:38 +02:00
/ * *
*
* @return the token if everything goers ok , null otherwise
* /
private String checkLinkedInAuthZ ( ) {
2014-08-08 17:11:25 +02:00
if ( Window . Location . getParameter ( " error " ) ! = null ) {
2014-09-04 18:08:31 +02:00
mainPanel . add ( new ErrorAlert ( " it seems you denied our request to import your professional summary from LinkedIn. " , true ) ) ;
2014-09-02 19:08:38 +02:00
return null ;
}
String code = Window . Location . getParameter ( " code " ) ;
String controlSequence = Window . Location . getParameter ( " state " ) ;
String cSeq2Compare = Cookies . getCookie ( DisplayProfile . CONTROL_SEQUENCE_COOKIE ) ;
if ( controlSequence . compareTo ( cSeq2Compare ) ! = 0 ) {
2014-09-04 18:08:31 +02:00
mainPanel . add ( new ErrorAlert ( " Something went wrong when importing your professional summary from LinkedIn, please try again. " , true ) ) ;
2014-09-02 19:08:38 +02:00
return null ;
2014-08-08 17:11:25 +02:00
}
2014-09-02 19:08:38 +02:00
GWT . log ( " key= " + code + " state= " + controlSequence ) ;
GWT . log ( " state= " + controlSequence ) ;
return code ;
2014-08-08 17:11:25 +02:00
}
2013-07-17 18:32:55 +02:00
/ * *
2014-07-23 16:39:04 +02:00
* decode the userid from the location param
* @return the decoded ( base64 ) userid
2013-07-17 18:32:55 +02:00
* /
2018-06-15 12:45:54 +02:00
public static String getUserToShowId ( boolean decode ) {
2014-07-23 16:39:04 +02:00
String encodedOid = Encoder . encode ( GCubeSocialNetworking . USER_PROFILE_OID ) ;
if ( Window . Location . getParameter ( encodedOid ) = = null )
return null ;
String encodedUserId = Window . Location . getParameter ( encodedOid ) ;
2018-06-15 12:45:54 +02:00
return decode ? Encoder . decode ( encodedUserId ) : encodedUserId ;
2013-07-17 18:32:55 +02:00
}
2014-08-08 17:11:25 +02:00
/ * *
*
2014-09-02 19:08:38 +02:00
* @return true if the user has clicked import from LinkedIn
2014-08-08 17:11:25 +02:00
* /
private boolean isUserAuthZFromLinkedIn ( ) {
if ( Window . Location . getParameter ( " error " ) ! = null | | Window . Location . getParameter ( " code " ) ! = null )
return true ;
return false ;
}
2013-01-21 12:30:24 +01:00
}