2013-01-04 19:22:23 +01:00
|
|
|
package org.gcube.portlets.user.notifications.client.view;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2013-01-08 18:24:40 +01:00
|
|
|
import java.util.Collections;
|
2013-01-04 19:22:23 +01:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
2013-01-08 18:24:40 +01:00
|
|
|
import java.util.SortedSet;
|
|
|
|
import java.util.TreeSet;
|
2013-01-04 19:22:23 +01:00
|
|
|
|
|
|
|
import org.gcube.portal.databook.shared.Notification;
|
2013-01-31 20:23:36 +01:00
|
|
|
import org.gcube.portal.databook.shared.NotificationChannelType;
|
2013-01-04 19:22:23 +01:00
|
|
|
import org.gcube.portal.databook.shared.UserInfo;
|
|
|
|
import org.gcube.portlets.user.notifications.client.NotificationsService;
|
|
|
|
import org.gcube.portlets.user.notifications.client.NotificationsServiceAsync;
|
|
|
|
import org.gcube.portlets.user.notifications.client.view.templates.DayWrapper;
|
|
|
|
import org.gcube.portlets.user.notifications.client.view.templates.SingleNotificationView;
|
|
|
|
|
|
|
|
import com.google.gwt.core.client.GWT;
|
2013-01-31 19:39:25 +01:00
|
|
|
import com.google.gwt.dom.client.Style.Unit;
|
2013-01-31 20:23:36 +01:00
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
2013-01-08 18:24:40 +01:00
|
|
|
import com.google.gwt.user.client.Timer;
|
2013-01-04 19:22:23 +01:00
|
|
|
import com.google.gwt.user.client.Window;
|
|
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
|
|
|
import com.google.gwt.user.client.ui.HTML;
|
|
|
|
import com.google.gwt.user.client.ui.HasAlignment;
|
2013-01-31 20:23:36 +01:00
|
|
|
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
|
2013-01-04 19:22:23 +01:00
|
|
|
import com.google.gwt.user.client.ui.HasVerticalAlignment;
|
2013-01-31 20:23:36 +01:00
|
|
|
import com.google.gwt.user.client.ui.HorizontalPanel;
|
2013-01-04 19:22:23 +01:00
|
|
|
import com.google.gwt.user.client.ui.Image;
|
|
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
|
|
|
|
|
|
|
public class NotificationsPanel extends Composite {
|
|
|
|
/**
|
|
|
|
* Create a remote service proxy to talk to the server-side Greeting service.
|
|
|
|
*/
|
|
|
|
private final NotificationsServiceAsync notificationService = GWT.create(NotificationsService.class);
|
|
|
|
|
|
|
|
public static final String loading = GWT.getModuleBaseURL() + "../images/feeds-loader.gif";
|
2013-01-31 19:39:25 +01:00
|
|
|
private static final String spacer = GWT.getModuleBaseURL() + "../images/feeds-spacer.gif";
|
2013-01-04 19:22:23 +01:00
|
|
|
private static final String warning = GWT.getModuleBaseURL() + "../images/warning_blue.png";
|
|
|
|
private UserInfo myUserInfo;
|
|
|
|
private Image loadingImage;
|
2013-01-31 20:23:36 +01:00
|
|
|
|
|
|
|
private VerticalPanel container = new VerticalPanel();
|
|
|
|
private HorizontalPanel settingsPanel = new HorizontalPanel();
|
|
|
|
|
2013-01-04 19:22:23 +01:00
|
|
|
private VerticalPanel mainPanel;
|
2013-01-31 20:23:36 +01:00
|
|
|
|
|
|
|
private HTML notificationSettings = new HTML("<a style=\"padding-right: 15px;\" class=\"link\">Notification Settings</a>");
|
2013-01-04 19:22:23 +01:00
|
|
|
|
|
|
|
public NotificationsPanel() {
|
2013-01-31 20:23:36 +01:00
|
|
|
notificationSettings.setVisible(false);
|
2013-01-04 19:22:23 +01:00
|
|
|
mainPanel = new VerticalPanel();
|
|
|
|
mainPanel.setWidth("100%");
|
2013-01-31 20:23:36 +01:00
|
|
|
container.setWidth("100%");
|
|
|
|
settingsPanel.setWidth("100%");
|
|
|
|
settingsPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
|
|
|
|
settingsPanel.add(notificationSettings);
|
|
|
|
container.add(settingsPanel);
|
|
|
|
container.add(mainPanel);
|
2013-01-04 19:22:23 +01:00
|
|
|
loadingImage = new Image(loading);
|
|
|
|
showLoader();
|
|
|
|
|
|
|
|
notificationService.getUserInfo(new AsyncCallback<UserInfo>() {
|
|
|
|
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
}
|
|
|
|
public void onSuccess(UserInfo result) {
|
|
|
|
myUserInfo = result;
|
|
|
|
if (result.getUsername().equals("test.user")) {
|
|
|
|
Window.alert("Your session has expired, please log out and login again");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showUserNotifications();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-01-31 20:23:36 +01:00
|
|
|
initWidget(container);
|
|
|
|
|
|
|
|
notificationSettings.addClickHandler(new ClickHandler() {
|
|
|
|
@Override
|
|
|
|
public void onClick(ClickEvent event) {
|
|
|
|
notificationService.getNotificationChannels(new AsyncCallback<ArrayList<NotificationChannelType>>() {
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) { }
|
2013-01-04 19:22:23 +01:00
|
|
|
|
2013-01-31 20:23:36 +01:00
|
|
|
@Override
|
|
|
|
public void onSuccess(ArrayList<NotificationChannelType> result) {
|
|
|
|
NotificationSettings dlg = new NotificationSettings(result);
|
|
|
|
dlg.center();
|
|
|
|
dlg.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-01-04 19:22:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 20:23:36 +01:00
|
|
|
|
|
|
|
|
2013-01-04 19:22:23 +01:00
|
|
|
private void showUserNotifications() {
|
|
|
|
showLoader();
|
|
|
|
notificationService.getUserNotifications(new AsyncCallback<HashMap<Date, ArrayList<Notification>>>() {
|
|
|
|
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onSuccess(HashMap<Date, ArrayList<Notification>> notificationsPerDay) {
|
2013-01-31 20:23:36 +01:00
|
|
|
notificationSettings.setVisible(true);
|
2013-01-04 19:22:23 +01:00
|
|
|
if (notificationsPerDay != null) {
|
|
|
|
mainPanel.clear();
|
|
|
|
if (notificationsPerDay.size() == 0) {
|
|
|
|
mainPanel.add(new HTML("<div class=\"no-notification-message\">" +
|
|
|
|
"Looks like we've got nothing for you at the moment. <br> " +
|
|
|
|
"You may begin by <strong>sharing</strong> an update!</div>"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mainPanel.setHeight("");
|
|
|
|
mainPanel.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
|
|
|
|
mainPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
|
2013-01-31 19:39:25 +01:00
|
|
|
|
2013-01-08 18:24:40 +01:00
|
|
|
ArrayList<Date> sortedKeys=new ArrayList<Date>(notificationsPerDay.keySet());
|
|
|
|
Collections.sort(sortedKeys, Collections.reverseOrder());
|
2013-01-31 19:39:25 +01:00
|
|
|
|
|
|
|
int notCounter = 0;
|
2013-01-08 18:24:40 +01:00
|
|
|
for (Date day : sortedKeys) {
|
2013-01-04 19:22:23 +01:00
|
|
|
mainPanel.add(new DayWrapper(day));
|
2013-01-31 19:39:25 +01:00
|
|
|
for (Notification notif : notificationsPerDay.get(day)) {
|
2013-01-04 19:22:23 +01:00
|
|
|
mainPanel.add(new SingleNotificationView(notif));
|
2013-01-31 19:39:25 +01:00
|
|
|
notCounter++;
|
|
|
|
}
|
2013-01-04 19:22:23 +01:00
|
|
|
}
|
2013-01-08 18:24:40 +01:00
|
|
|
setNotificationsRead();
|
2013-01-31 19:39:25 +01:00
|
|
|
if (notCounter < 5) {
|
|
|
|
mainPanel.add(new Image(spacer));
|
|
|
|
mainPanel.add(new Image(spacer));
|
|
|
|
}
|
|
|
|
if (notCounter > 5 && notCounter < 10)
|
|
|
|
mainPanel.add(new Image(spacer));
|
2013-01-04 19:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
loadingImage.setUrl(warning);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showLoader() {
|
|
|
|
mainPanel.clear();
|
|
|
|
mainPanel.setWidth("100%");
|
|
|
|
mainPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
|
|
|
|
mainPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
|
|
|
|
mainPanel.add(loadingImage);
|
|
|
|
}
|
2013-01-31 19:39:25 +01:00
|
|
|
|
2013-01-08 18:24:40 +01:00
|
|
|
Timer t;
|
|
|
|
private void setNotificationsRead() {
|
|
|
|
t = new Timer() {
|
2013-01-31 19:39:25 +01:00
|
|
|
|
2013-01-08 18:24:40 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
notificationService.setAllUserNotificationsRead(new AsyncCallback<Boolean>() {
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
}
|
|
|
|
public void onSuccess(Boolean result) {
|
|
|
|
}
|
|
|
|
});
|
2013-01-31 19:39:25 +01:00
|
|
|
|
2013-01-08 18:24:40 +01:00
|
|
|
}
|
|
|
|
};
|
2013-01-31 19:39:25 +01:00
|
|
|
|
2013-01-08 19:02:38 +01:00
|
|
|
t.schedule(500);
|
2013-01-08 18:24:40 +01:00
|
|
|
}
|
2013-01-04 19:22:23 +01:00
|
|
|
}
|