2013-01-04 19:22:23 +01:00
|
|
|
package org.gcube.portlets.user.notifications.server;
|
|
|
|
|
2013-06-05 18:45:10 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
2013-01-04 19:22:23 +01:00
|
|
|
import java.util.ArrayList;
|
2013-01-05 18:56:12 +01:00
|
|
|
import java.util.Calendar;
|
2014-03-04 16:01:04 +01:00
|
|
|
import java.util.Collections;
|
2013-01-04 19:22:23 +01:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
2013-06-05 18:45:10 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2013-06-04 18:21:54 +02:00
|
|
|
import java.util.Map;
|
2013-06-05 18:45:10 +02:00
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext;
|
2013-01-04 19:22:23 +01:00
|
|
|
|
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
|
|
|
import org.gcube.application.framework.core.session.SessionManager;
|
|
|
|
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
|
|
|
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
|
|
|
|
import org.gcube.portal.databook.server.DatabookStore;
|
|
|
|
import org.gcube.portal.databook.shared.Notification;
|
2013-01-31 20:23:36 +01:00
|
|
|
import org.gcube.portal.databook.shared.NotificationChannelType;
|
2013-06-04 18:21:54 +02:00
|
|
|
import org.gcube.portal.databook.shared.NotificationType;
|
2013-01-04 19:22:23 +01:00
|
|
|
import org.gcube.portal.databook.shared.UserInfo;
|
|
|
|
import org.gcube.portlets.user.notifications.client.NotificationsService;
|
2014-04-02 18:02:48 +02:00
|
|
|
import org.gcube.portlets.user.notifications.shared.NotificationConstants;
|
2013-06-05 18:45:10 +02:00
|
|
|
import org.gcube.portlets.user.notifications.shared.NotificationPreference;
|
2016-04-01 14:29:34 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
2013-09-27 14:52:15 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2013-01-04 19:22:23 +01:00
|
|
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The server side implementation of the RPC service.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
public class NotificationsServiceImpl extends RemoteServiceServlet implements NotificationsService {
|
2013-09-27 14:52:15 +02:00
|
|
|
|
|
|
|
private static final Logger _log = LoggerFactory.getLogger(NotificationsServiceImpl.class);
|
2013-06-05 18:45:10 +02:00
|
|
|
private final static String TYPE_CATEGORIES_FILENAME = "categorybytype.properties";
|
|
|
|
private final static String TYPE_LABELS_FILENAME = "labelbytype.properties";
|
|
|
|
private final static String TYPE_DESCRIPTIONS_FILENAME = "descbytype.properties";
|
2013-01-04 19:22:23 +01:00
|
|
|
/**
|
|
|
|
* The store interface
|
|
|
|
*/
|
|
|
|
private DatabookStore store;
|
|
|
|
/**
|
|
|
|
* connect to cassandra at startup
|
|
|
|
*/
|
|
|
|
public void init() {
|
|
|
|
store = new DBCassandraAstyanaxImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void destroy() {
|
|
|
|
store.closeConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the current ASLSession
|
|
|
|
* @return the session
|
|
|
|
*/
|
|
|
|
private ASLSession getASLSession() {
|
|
|
|
String sessionID = this.getThreadLocalRequest().getSession().getId();
|
|
|
|
String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
|
|
|
|
if (user == null) {
|
2014-04-03 15:07:08 +02:00
|
|
|
user = getDevelopmentUser();
|
2013-01-29 01:32:04 +01:00
|
|
|
_log.warn("USER IS NULL setting "+user+" and Running OUTSIDE PORTAL");
|
2013-01-04 19:22:23 +01:00
|
|
|
}
|
2016-04-01 14:29:34 +02:00
|
|
|
_log.debug("SessionID = " + sessionID);
|
2013-01-04 19:22:23 +01:00
|
|
|
return SessionManager.getInstance().getASLSession(sessionID, user);
|
|
|
|
}
|
2016-04-01 14:29:34 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return true if you're running into the portal, false if in development
|
|
|
|
*/
|
|
|
|
private boolean isWithinPortal() {
|
|
|
|
try {
|
|
|
|
UserLocalServiceUtil.getService();
|
|
|
|
return true;
|
|
|
|
} catch (com.liferay.portal.kernel.bean.BeanLocatorException ex) {
|
|
|
|
_log.trace("Development Mode ON");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-04-04 15:18:26 +02:00
|
|
|
/**
|
|
|
|
* set the user in development mode
|
|
|
|
* @return
|
|
|
|
*/
|
2014-04-03 15:07:08 +02:00
|
|
|
public String getDevelopmentUser() {
|
|
|
|
String user = "test.user";
|
2016-04-08 14:55:54 +02:00
|
|
|
// user = "andrea.rossi";
|
2014-04-03 15:07:08 +02:00
|
|
|
return user;
|
|
|
|
}
|
2013-01-04 19:22:23 +01:00
|
|
|
public UserInfo getUserInfo() {
|
|
|
|
try {
|
|
|
|
String username = getASLSession().getUsername();
|
|
|
|
String email = username+"@isti.cnr.it";
|
|
|
|
String fullName = username+" FULL";
|
|
|
|
String thumbnailURL = "images/Avatar_default.png";
|
|
|
|
|
2016-04-01 14:29:34 +02:00
|
|
|
if (isWithinPortal()) {
|
|
|
|
GCubeUser user = new LiferayUserManager().getUserByUsername(username);
|
|
|
|
|
|
|
|
thumbnailURL = user.getUserAvatarURL();
|
|
|
|
fullName = user.getFullname();
|
|
|
|
email = user.getEmail();
|
2016-04-07 19:21:30 +02:00
|
|
|
UserInfo toReturn = new UserInfo(username, fullName, thumbnailURL, email, null, true, false, null);
|
2013-01-04 19:22:23 +01:00
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_log.info("Returning test USER");
|
|
|
|
return new UserInfo(getASLSession().getUsername(), fullName, thumbnailURL, email, "fakeAccountUrl", true, false, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new UserInfo();
|
|
|
|
}
|
2013-06-05 18:45:10 +02:00
|
|
|
|
2013-01-04 19:22:23 +01:00
|
|
|
/**
|
2014-04-02 18:02:48 +02:00
|
|
|
* returns the notifications separated per day
|
2013-01-04 19:22:23 +01:00
|
|
|
*/
|
|
|
|
public HashMap<Date, ArrayList<Notification>> getUserNotifications() {
|
|
|
|
HashMap<Date, ArrayList<Notification>> toReturn = new HashMap<Date, ArrayList<Notification>>();
|
|
|
|
try {
|
2014-04-03 15:07:08 +02:00
|
|
|
for (Notification notification : store.getAllNotificationByUser(getASLSession().getUsername(), NotificationConstants.NOTIFICATION_NUMBER_PRE)) {
|
2013-01-05 18:56:12 +01:00
|
|
|
Date dateWithoutTime = removeTimePart(notification.getTime());
|
2014-04-04 15:18:26 +02:00
|
|
|
notification.setDescription(replaceAmpersand(notification.getDescription()));
|
2013-01-05 18:56:12 +01:00
|
|
|
if (! toReturn.containsKey(dateWithoutTime)) {
|
2013-01-04 19:22:23 +01:00
|
|
|
ArrayList<Notification> nots = new ArrayList<Notification>();
|
|
|
|
nots.add(notification);
|
2013-01-05 18:56:12 +01:00
|
|
|
toReturn.put(dateWithoutTime, nots);
|
2013-01-04 19:22:23 +01:00
|
|
|
} else {
|
2013-01-05 18:56:12 +01:00
|
|
|
toReturn.get(dateWithoutTime).add(notification);
|
2013-07-12 15:34:05 +02:00
|
|
|
}
|
2013-10-29 15:11:47 +01:00
|
|
|
//System.out.println(notification.getDescription());
|
2013-01-04 19:22:23 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error("While trying to get User notifications");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
2014-04-02 18:02:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the notifications in the range separated per day
|
|
|
|
*/
|
|
|
|
public HashMap<Date, ArrayList<Notification>> getUserNotificationsByRange(int from, int quantity) {
|
|
|
|
HashMap<Date, ArrayList<Notification>> toReturn = new HashMap<Date, ArrayList<Notification>>();
|
|
|
|
try {
|
|
|
|
for (Notification notification : store.getRangeNotificationsByUser(getASLSession().getUsername(), from, quantity)) {
|
|
|
|
Date dateWithoutTime = removeTimePart(notification.getTime());
|
|
|
|
if (! toReturn.containsKey(dateWithoutTime)) {
|
|
|
|
ArrayList<Notification> nots = new ArrayList<Notification>();
|
|
|
|
nots.add(notification);
|
|
|
|
toReturn.put(dateWithoutTime, nots);
|
|
|
|
} else {
|
|
|
|
toReturn.get(dateWithoutTime).add(notification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error("While trying to get User notifications");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2013-01-05 18:56:12 +01:00
|
|
|
/**
|
|
|
|
* we want notification split per day
|
|
|
|
* @param date
|
|
|
|
* @return the date passad as param with time part set to 00:00:00.0
|
|
|
|
*/
|
|
|
|
private Date removeTimePart(Date date) {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.setTime(date);
|
2013-01-08 18:24:40 +01:00
|
|
|
|
2013-01-05 18:56:12 +01:00
|
|
|
// Set time fields to zero
|
|
|
|
cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
cal.set(Calendar.MINUTE, 0);
|
|
|
|
cal.set(Calendar.SECOND, 0);
|
|
|
|
cal.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
return cal.getTime();
|
|
|
|
}
|
2013-01-08 18:24:40 +01:00
|
|
|
/**
|
2013-01-30 00:55:20 +01:00
|
|
|
* this sets all the notifications for this user read
|
2013-01-08 18:24:40 +01:00
|
|
|
*/
|
|
|
|
public boolean setAllUserNotificationsRead() {
|
|
|
|
try {
|
2013-01-30 00:55:20 +01:00
|
|
|
store.setAllNotificationReadByUser(getASLSession().getUsername());
|
2013-01-08 18:24:40 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error("While trying to set User notifications Read");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-05 18:45:10 +02:00
|
|
|
|
2013-01-31 20:23:36 +01:00
|
|
|
@Override
|
2013-06-05 18:45:10 +02:00
|
|
|
public LinkedHashMap<String, ArrayList<NotificationPreference>> getUserNotificationPreferences() {
|
2013-06-04 18:21:54 +02:00
|
|
|
String userid = getASLSession().getUsername();
|
2013-06-05 18:45:10 +02:00
|
|
|
//load the 3 prop files
|
|
|
|
Properties categories = getCategoriesByType();
|
|
|
|
Properties labels = getLabelsByType();
|
|
|
|
Properties descriptions = getDescriptionsByType();
|
|
|
|
|
|
|
|
TreeMap<String, ArrayList<NotificationPreference>> treeMap = new TreeMap<String, ArrayList<NotificationPreference>>();
|
2014-04-02 18:02:48 +02:00
|
|
|
|
2013-06-05 18:45:10 +02:00
|
|
|
try {
|
|
|
|
Map<NotificationType, NotificationChannelType[]> storePreferences = store.getUserNotificationPreferences(userid);
|
2014-04-02 18:02:48 +02:00
|
|
|
|
2013-06-05 18:45:10 +02:00
|
|
|
for (NotificationType type : storePreferences.keySet()) {
|
|
|
|
String category = categories.getProperty(type.toString());
|
|
|
|
String typeLabel = labels.getProperty(type.toString());
|
|
|
|
String typeDesc = descriptions.getProperty(type.toString());
|
|
|
|
if (category != null) {
|
|
|
|
if (treeMap.containsKey(category)) {
|
2013-06-07 17:42:36 +02:00
|
|
|
treeMap.get(category).add(new NotificationPreference(type, typeLabel, typeDesc, storePreferences.get(type)));
|
2013-06-05 18:45:10 +02:00
|
|
|
} else {
|
|
|
|
ArrayList<NotificationPreference> toAdd = new ArrayList<NotificationPreference>();
|
2013-06-07 17:42:36 +02:00
|
|
|
toAdd.add(new NotificationPreference(type, typeLabel, typeDesc, storePreferences.get(type)));
|
2013-06-05 18:45:10 +02:00
|
|
|
treeMap.put(category, toAdd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 18:21:54 +02:00
|
|
|
} catch (Exception e) {
|
2013-06-07 17:42:36 +02:00
|
|
|
_log.error("While trying to get getUser Notification Preferences for " + userid);
|
2013-01-31 20:23:36 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-07 17:42:36 +02:00
|
|
|
_log.debug("Got Notification Preferences, returning to the client for user: " + userid);
|
2013-06-05 18:45:10 +02:00
|
|
|
//need the key in revers order so that workspace appears first
|
|
|
|
LinkedHashMap<String, ArrayList<NotificationPreference>> toReturn = new LinkedHashMap<String, ArrayList<NotificationPreference>>();
|
|
|
|
for (String category : treeMap.descendingKeySet()) {
|
|
|
|
ArrayList<NotificationPreference> toAdd = new ArrayList<NotificationPreference>();
|
|
|
|
for (NotificationPreference pref : treeMap.get(category)) {
|
|
|
|
toAdd.add(pref);
|
|
|
|
}
|
2013-06-07 17:42:36 +02:00
|
|
|
Collections.sort(toAdd); //sort the labels from the less length to the more length
|
2013-06-05 18:45:10 +02:00
|
|
|
toReturn.put(category, toAdd);
|
|
|
|
}
|
2013-01-31 20:23:36 +01:00
|
|
|
return toReturn;
|
|
|
|
}
|
2014-04-04 15:18:26 +02:00
|
|
|
private String replaceAmpersand(String toReplace) {
|
|
|
|
String toReturn = toReplace.replaceAll("&", "&");
|
|
|
|
return toReturn;
|
|
|
|
}
|
2016-04-07 19:21:30 +02:00
|
|
|
|
2013-02-01 00:29:10 +01:00
|
|
|
@Override
|
2013-06-04 18:21:54 +02:00
|
|
|
public boolean setUserNotificationPreferences(Map<NotificationType, NotificationChannelType[]> enabledChannels) {
|
|
|
|
return store.setUserNotificationPreferences(getASLSession().getUsername(), enabledChannels);
|
2013-02-01 00:29:10 +01:00
|
|
|
}
|
2013-06-05 18:45:10 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type the type to look for
|
2015-06-15 17:39:45 +02:00
|
|
|
* @return the Category if there is correspondance, null otherwise
|
2013-06-05 18:45:10 +02:00
|
|
|
*/
|
|
|
|
private Properties getCategoriesByType() {
|
|
|
|
Properties props = new Properties();
|
|
|
|
try {
|
|
|
|
ServletContext servletContext = getServletContext();
|
|
|
|
String contextPath = servletContext.getRealPath(File.separator);
|
2015-06-15 17:39:45 +02:00
|
|
|
String propertyfile = contextPath + File.separator + "conf" + File.separator + TYPE_CATEGORIES_FILENAME;
|
2013-06-05 18:45:10 +02:00
|
|
|
File propsFile = new File(propertyfile);
|
|
|
|
FileInputStream fis = new FileInputStream(propsFile);
|
2015-06-15 17:39:45 +02:00
|
|
|
props.load(fis);
|
2013-06-05 18:45:10 +02:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
//catch exception in case properties file does not exist
|
|
|
|
catch(IOException e) {
|
|
|
|
_log.error(TYPE_CATEGORIES_FILENAME + "file not found under conf dir, returning null");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return the properties for labels
|
|
|
|
*/
|
|
|
|
private Properties getLabelsByType() {
|
|
|
|
Properties props = new Properties();
|
2015-06-15 17:39:45 +02:00
|
|
|
String propertyfile = "";
|
2013-06-05 18:45:10 +02:00
|
|
|
try {
|
|
|
|
ServletContext servletContext = getServletContext();
|
|
|
|
String contextPath = servletContext.getRealPath(File.separator);
|
2015-06-15 17:39:45 +02:00
|
|
|
propertyfile = contextPath + File.separator + "conf" + File.separator + TYPE_LABELS_FILENAME;
|
2013-06-05 18:45:10 +02:00
|
|
|
File propsFile = new File(propertyfile);
|
|
|
|
FileInputStream fis = new FileInputStream(propsFile);
|
|
|
|
props.load( fis);
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
//catch exception in case properties file does not exist
|
|
|
|
catch(IOException e) {
|
2015-06-15 17:39:45 +02:00
|
|
|
_log.error(TYPE_LABELS_FILENAME + "file not found under conf dir, returning null propertyfile -> " + propertyfile);
|
2013-06-05 18:45:10 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return the properties for descriptions
|
|
|
|
*/
|
|
|
|
private Properties getDescriptionsByType() {
|
|
|
|
Properties props = new Properties();
|
|
|
|
try {
|
|
|
|
ServletContext servletContext = getServletContext();
|
|
|
|
String contextPath = servletContext.getRealPath(File.separator);
|
2015-06-15 17:39:45 +02:00
|
|
|
String propertyfile = contextPath + File.separator +"conf" + File.separator + TYPE_DESCRIPTIONS_FILENAME;
|
2013-06-05 18:45:10 +02:00
|
|
|
File propsFile = new File(propertyfile);
|
|
|
|
FileInputStream fis = new FileInputStream(propsFile);
|
|
|
|
props.load( fis);
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
//catch exception in case properties file does not exist
|
|
|
|
catch(IOException e) {
|
|
|
|
_log.error(TYPE_DESCRIPTIONS_FILENAME + "file not found under conf dir, returning null");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 18:21:54 +02:00
|
|
|
|
2013-01-04 19:22:23 +01:00
|
|
|
}
|