2013-01-31 20:23:36 +01:00
package org.gcube.portlets.user.notifications.client.view ;
import java.util.ArrayList ;
2013-02-01 00:29:10 +01:00
import java.util.HashMap ;
2013-06-05 18:45:10 +02:00
import java.util.LinkedHashMap ;
2013-01-31 20:23:36 +01:00
2013-06-07 17:42:36 +02:00
import org.gcube.portal.databook.shared.NotificationChannelType ;
import org.gcube.portal.databook.shared.NotificationType ;
2013-02-01 00:29:10 +01:00
import org.gcube.portlets.user.notifications.client.NotificationsServiceAsync ;
2013-06-05 18:45:10 +02:00
import org.gcube.portlets.user.notifications.client.view.templates.CategoryWrapper ;
import org.gcube.portlets.user.notifications.shared.NotificationPreference ;
2015-06-16 17:26:46 +02:00
import com.github.gwtbootstrap.client.ui.Button ;
2015-06-15 17:39:45 +02:00
import com.github.gwtbootstrap.client.ui.Modal ;
import com.github.gwtbootstrap.client.ui.ModalFooter ;
2015-06-16 17:26:46 +02:00
import com.github.gwtbootstrap.client.ui.constants.ButtonType ;
2013-02-01 00:29:10 +01:00
import com.google.gwt.core.client.GWT ;
import com.google.gwt.event.dom.client.ClickEvent ;
import com.google.gwt.event.dom.client.ClickHandler ;
2013-06-07 17:42:36 +02:00
import com.google.gwt.user.client.rpc.AsyncCallback ;
2013-01-31 20:23:36 +01:00
import com.google.gwt.user.client.ui.HTML ;
2013-02-01 00:29:10 +01:00
import com.google.gwt.user.client.ui.HasAlignment ;
import com.google.gwt.user.client.ui.Image ;
2013-01-31 20:23:36 +01:00
import com.google.gwt.user.client.ui.VerticalPanel ;
2013-06-05 18:45:10 +02:00
2013-01-31 20:23:36 +01:00
2013-02-01 00:49:12 +01:00
/ * *
* @author Massimiliano Assante ISTI - CNR
*
* /
2015-06-15 17:39:45 +02:00
public class NotificationSettingsDialog extends Modal {
2013-01-31 20:23:36 +01:00
2013-02-01 00:29:10 +01:00
public static final String loading = GWT . getModuleBaseURL ( ) + " ../images/feeds-loader.gif " ;
public static final String mailSentOK = GWT . getModuleBaseURL ( ) + " ../images/yes.png " ;
public static final String mailSentNOK = GWT . getModuleBaseURL ( ) + " ../images/warning_blue.png " ;
2013-06-07 17:42:36 +02:00
ArrayList < CategoryWrapper > myCategories = new ArrayList < CategoryWrapper > ( ) ;
2015-06-16 17:26:46 +02:00
private VerticalPanel placeholder = new VerticalPanel ( ) ;
private ModalFooter footer = new ModalFooter ( ) ;
2013-01-31 20:23:36 +01:00
private Button cancel = new Button ( " Cancel " ) ;
private Button save = new Button ( " Save " ) ;
2013-06-07 17:42:36 +02:00
public NotificationSettingsDialog ( LinkedHashMap < String , ArrayList < NotificationPreference > > preferences , final NotificationsServiceAsync notificationService ) {
2013-01-31 20:23:36 +01:00
super ( ) ;
2015-06-15 17:39:45 +02:00
setAnimation ( true ) ;
2015-06-16 17:26:46 +02:00
setTitle ( " Notification Settings " ) ;
placeholder . setHorizontalAlignment ( HasAlignment . ALIGN_CENTER ) ;
add ( placeholder ) ;
2013-06-05 18:45:10 +02:00
for ( String category : preferences . keySet ( ) ) {
2013-06-07 17:42:36 +02:00
CategoryWrapper cat = new CategoryWrapper ( category , preferences . get ( category ) ) ;
2015-06-15 17:39:45 +02:00
add ( cat ) ;
2013-06-07 17:42:36 +02:00
myCategories . add ( cat ) ;
2013-06-05 18:45:10 +02:00
}
2013-06-07 17:42:36 +02:00
2015-06-15 17:39:45 +02:00
save . setType ( ButtonType . PRIMARY ) ;
footer . add ( save ) ;
footer . add ( cancel ) ;
add ( footer ) ;
2013-06-07 17:42:36 +02:00
2013-06-06 18:53:28 +02:00
save . addClickHandler ( new ClickHandler ( ) {
2013-06-05 18:45:10 +02:00
@Override
2013-06-06 18:53:28 +02:00
public void onClick ( ClickEvent event ) {
2013-06-07 17:42:36 +02:00
HashMap < NotificationType , NotificationChannelType [ ] > toStore = new HashMap < NotificationType , NotificationChannelType [ ] > ( ) ;
for ( CategoryWrapper cat : myCategories ) {
for ( NotificationType notType : cat . getSelectedChannels ( ) . keySet ( ) ) {
toStore . put ( notType , cat . getSelectedChannels ( ) . get ( notType ) ) ;
//GWT.log(""+notType + " - " + cat.getSelectedChannels().get(notType));
}
}
notificationService . setUserNotificationPreferences ( toStore , new AsyncCallback < Boolean > ( ) {
@Override
public void onSuccess ( Boolean result ) {
showDeliveryResult ( result ) ;
}
@Override
public void onFailure ( Throwable caught ) {
showDeliveryResult ( false ) ;
}
} ) ;
2013-02-01 00:29:10 +01:00
}
2013-06-06 18:53:28 +02:00
} ) ;
2013-06-07 17:42:36 +02:00
cancel . addClickHandler ( new ClickHandler ( ) {
2013-02-01 00:29:10 +01:00
@Override
public void onClick ( ClickEvent event ) {
hide ( ) ;
}
} ) ;
2013-06-07 17:42:36 +02:00
2013-02-01 00:29:10 +01:00
}
2013-06-07 17:42:36 +02:00
2013-02-01 00:29:10 +01:00
private void showDeliveryResult ( boolean success ) {
2015-06-16 17:41:18 +02:00
for ( CategoryWrapper cat : myCategories ) {
cat . removeFromParent ( ) ;
}
2013-02-01 00:29:10 +01:00
if ( success ) {
2015-06-16 17:26:46 +02:00
placeholder . add ( new HTML ( " <span style= \" font-size: 20px; \" >Notifications Settings saved correctly</span> " ) ) ;
placeholder . add ( new Image ( mailSentOK ) ) ;
2013-02-01 00:29:10 +01:00
}
else {
2015-06-16 17:26:46 +02:00
placeholder . add ( new Image ( mailSentNOK ) ) ;
placeholder . add ( new HTML ( " <span style= \" font-size: 20px; \" >Sorry, there were problems contacting the server, please try again in a short while.</span> " ) ) ;
2013-02-01 00:29:10 +01:00
}
2015-06-16 17:26:46 +02:00
2013-02-01 00:29:10 +01:00
Button close = new Button ( " Close " ) ;
close . addClickHandler ( new ClickHandler ( ) {
@Override
public void onClick ( ClickEvent event ) {
hide ( ) ;
}
} ) ;
2015-06-16 17:26:46 +02:00
footer . clear ( ) ;
2015-06-15 18:00:48 +02:00
footer . add ( close ) ;
add ( footer ) ;
2013-01-31 20:23:36 +01:00
}
}