notifications/src/main/java/org/gcube/portlets/user/notifications/client/view/NotificationsPanel.java

130 lines
4.0 KiB
Java

package org.gcube.portlets.user.notifications.client.view;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;
import org.gcube.portal.databook.shared.Notification;
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;
import com.google.gwt.user.client.Timer;
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;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
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";
private static final String warning = GWT.getModuleBaseURL() + "../images/warning_blue.png";
private UserInfo myUserInfo;
private Image loadingImage;
private VerticalPanel mainPanel;
public NotificationsPanel() {
mainPanel = new VerticalPanel();
mainPanel.setWidth("100%");
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();
}
}
});
initWidget(mainPanel);
}
private void showUserNotifications() {
showLoader();
notificationService.getUserNotifications(new AsyncCallback<HashMap<Date, ArrayList<Notification>>>() {
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
public void onSuccess(HashMap<Date, ArrayList<Notification>> notificationsPerDay) {
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);
ArrayList<Date> sortedKeys=new ArrayList<Date>(notificationsPerDay.keySet());
Collections.sort(sortedKeys, Collections.reverseOrder());
for (Date day : sortedKeys) {
mainPanel.add(new DayWrapper(day));
for (Notification notif : notificationsPerDay.get(day))
mainPanel.add(new SingleNotificationView(notif));
}
setNotificationsRead();
}
}
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);
}
Timer t;
private void setNotificationsRead() {
t = new Timer() {
@Override
public void run() {
notificationService.setAllUserNotificationsRead(new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {
}
public void onSuccess(Boolean result) {
}
});
}
};
t.schedule(500);
}
}