Revision 61826
Added by Konstantinos Triantafyllou almost 3 years ago
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
3 |
<modelVersion>4.0.0</modelVersion> |
|
4 |
<parent> |
|
5 |
<groupId>eu.dnetlib</groupId> |
|
6 |
<artifactId>dnet45-parent</artifactId> |
|
7 |
<version>1.0.0</version> |
|
8 |
</parent> |
|
9 |
<artifactId>uoa-notification-service</artifactId> |
|
10 |
<version>1.0.4</version> |
|
11 |
<packaging>jar</packaging> |
|
12 |
<scm> |
|
13 |
<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/uoa-notification-service/tags/uoa-notification-service-1.0.4</developerConnection> |
|
14 |
</scm> |
|
15 |
<name>uoa-notification-service</name> |
|
16 |
<properties> |
|
17 |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|
18 |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
|
19 |
<java.version>1.8</java.version> |
|
20 |
<timestampNotificationLibrary>${maven.build.timestamp}</timestampNotificationLibrary> |
|
21 |
<maven.build.timestamp.format>E MMM dd HH:mm:ss z yyyy</maven.build.timestamp.format> |
|
22 |
</properties> |
|
23 |
<dependencyManagement> |
|
24 |
<dependencies> |
|
25 |
<dependency> |
|
26 |
<groupId>org.springframework.boot</groupId> |
|
27 |
<artifactId>spring-boot-dependencies</artifactId> |
|
28 |
<version>1.5.8.RELEASE</version> |
|
29 |
<type>pom</type> |
|
30 |
<scope>import</scope> |
|
31 |
</dependency> |
|
32 |
</dependencies> |
|
33 |
</dependencyManagement> |
|
34 |
<dependencies> |
|
35 |
<dependency> |
|
36 |
<groupId>org.springframework.boot</groupId> |
|
37 |
<artifactId>spring-boot-starter-web</artifactId> |
|
38 |
<exclusions> |
|
39 |
<exclusion> |
|
40 |
<groupId> org.springframework.boot</groupId> |
|
41 |
<artifactId>spring-boot-starter-logging</artifactId> |
|
42 |
</exclusion> |
|
43 |
</exclusions> |
|
44 |
</dependency> |
|
45 |
<dependency> |
|
46 |
<groupId>org.springframework.boot</groupId> |
|
47 |
<artifactId>spring-boot-starter-data-mongodb</artifactId> |
|
48 |
</dependency> |
|
49 |
<!-- Starter for using Spring Security --> |
|
50 |
<dependency> |
|
51 |
<groupId>org.springframework.boot</groupId> |
|
52 |
<artifactId>spring-boot-starter-security</artifactId> |
|
53 |
</dependency> |
|
54 |
<dependency> |
|
55 |
<groupId>eu.dnetlib</groupId> |
|
56 |
<artifactId>uoa-authorization-library</artifactId> |
|
57 |
<version>2.1.0</version> |
|
58 |
</dependency> |
|
59 |
<dependency> |
|
60 |
<groupId>com.google.code.gson</groupId> |
|
61 |
<artifactId>gson</artifactId> |
|
62 |
<version>2.8.2</version> |
|
63 |
</dependency> |
|
64 |
<dependency> |
|
65 |
<groupId>log4j</groupId> |
|
66 |
<artifactId>log4j</artifactId> |
|
67 |
<version>1.2.17</version> |
|
68 |
</dependency> |
|
69 |
</dependencies> |
|
70 |
<build> |
|
71 |
<plugins> |
|
72 |
</plugins> |
|
73 |
<finalName>uoa-notification-service</finalName> |
|
74 |
<resources> |
|
75 |
<resource> |
|
76 |
<directory>src/main/resources</directory> |
|
77 |
<filtering>true</filtering> |
|
78 |
</resource> |
|
79 |
</resources> |
|
80 |
</build> |
|
81 |
</project> |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/configuration/GlobalVars.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.configuration; |
|
2 |
|
|
3 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
4 |
|
|
5 |
import java.util.Date; |
|
6 |
|
|
7 |
@ConfigurationProperties("notification.globalVars") |
|
8 |
public class GlobalVars { |
|
9 |
public static Date date = new Date(); |
|
10 |
private Date buildDate; |
|
11 |
private String version; |
|
12 |
|
|
13 |
public String getBuildDate() { |
|
14 |
if(buildDate == null) { |
|
15 |
return null; |
|
16 |
} |
|
17 |
return buildDate.toString(); |
|
18 |
} |
|
19 |
|
|
20 |
public void setBuildDate(Date buildDate) { |
|
21 |
this.buildDate = buildDate; |
|
22 |
} |
|
23 |
|
|
24 |
public static Date getDate() { |
|
25 |
return date; |
|
26 |
} |
|
27 |
|
|
28 |
public static void setDate(Date date) { |
|
29 |
GlobalVars.date = date; |
|
30 |
} |
|
31 |
|
|
32 |
public String getVersion() { |
|
33 |
return version; |
|
34 |
} |
|
35 |
|
|
36 |
public void setVersion(String version) { |
|
37 |
this.version = version; |
|
38 |
} |
|
39 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/configuration/NotificationConfiguration.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.configuration; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.configuration.AuthorizationConfiguration; |
|
4 |
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
5 |
import org.springframework.context.annotation.*; |
|
6 |
import org.springframework.scheduling.annotation.EnableScheduling; |
|
7 |
|
|
8 |
@Configuration |
|
9 |
@EnableConfigurationProperties({MongoConfig.class, GlobalVars.class}) |
|
10 |
@EnableScheduling |
|
11 |
@ComponentScan(basePackages = { "eu.dnetlib.uoanotificationservice"}) |
|
12 |
@Import({AuthorizationConfiguration.class}) |
|
13 |
public class NotificationConfiguration { } |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/configuration/MongoConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.configuration; |
|
2 |
|
|
3 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
4 |
|
|
5 |
@ConfigurationProperties("notification.mongodb") |
|
6 |
public class MongoConfig { |
|
7 |
|
|
8 |
private String host; |
|
9 |
private String database; |
|
10 |
private String username; |
|
11 |
private String password; |
|
12 |
private int port; |
|
13 |
|
|
14 |
|
|
15 |
public String getHost() { |
|
16 |
return host; |
|
17 |
} |
|
18 |
|
|
19 |
public void setHost(String host) { |
|
20 |
this.host = host; |
|
21 |
} |
|
22 |
|
|
23 |
public String getDatabase() { |
|
24 |
return database; |
|
25 |
} |
|
26 |
|
|
27 |
public void setDatabase(String database) { |
|
28 |
this.database = database; |
|
29 |
} |
|
30 |
|
|
31 |
public String getUsername() { |
|
32 |
return username; |
|
33 |
} |
|
34 |
|
|
35 |
public void setUsername(String username) { |
|
36 |
this.username = username; |
|
37 |
} |
|
38 |
|
|
39 |
public String getPassword() { |
|
40 |
return password; |
|
41 |
} |
|
42 |
|
|
43 |
public void setPassword(String password) { |
|
44 |
this.password = password; |
|
45 |
} |
|
46 |
|
|
47 |
public int getPort() { |
|
48 |
return port; |
|
49 |
} |
|
50 |
|
|
51 |
public void setPort(int port) { |
|
52 |
this.port = port; |
|
53 |
} |
|
54 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/configuration/NotificationMongoConnection.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.configuration; |
|
2 |
|
|
3 |
import com.mongodb.MongoClient; |
|
4 |
import com.mongodb.MongoCredential; |
|
5 |
import com.mongodb.ServerAddress; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
import org.springframework.context.annotation.Bean; |
|
8 |
import org.springframework.context.annotation.Configuration; |
|
9 |
import org.springframework.data.mongodb.MongoDbFactory; |
|
10 |
import org.springframework.data.mongodb.core.MongoTemplate; |
|
11 |
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; |
|
12 |
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; |
|
13 |
|
|
14 |
import java.util.Collections; |
|
15 |
|
|
16 |
@Configuration |
|
17 |
@EnableMongoRepositories(basePackages = {"eu.dnetlib.uoanotificationservice.dao"}, mongoTemplateRef = "notificationTemplate") |
|
18 |
public class NotificationMongoConnection { |
|
19 |
|
|
20 |
@Autowired |
|
21 |
private MongoConfig mongoConfig; |
|
22 |
|
|
23 |
@Bean |
|
24 |
public MongoDbFactory NotificationFactory() { |
|
25 |
return new SimpleMongoDbFactory(getMongoClient(), mongoConfig.getDatabase()); |
|
26 |
} |
|
27 |
|
|
28 |
@Bean(name = "notificationTemplate") |
|
29 |
public MongoTemplate getNotificationTemplate() { |
|
30 |
return new MongoTemplate(NotificationFactory()); |
|
31 |
} |
|
32 |
|
|
33 |
private MongoClient getMongoClient() { |
|
34 |
if(mongoConfig.getUsername() != null && mongoConfig.getPassword() != null){ |
|
35 |
return new MongoClient(Collections.singletonList( |
|
36 |
new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort())), |
|
37 |
Collections.singletonList(MongoCredential.createCredential(mongoConfig.getUsername(), mongoConfig.getDatabase(), mongoConfig.getPassword().toCharArray()))); |
|
38 |
} else { |
|
39 |
return new MongoClient(Collections.singletonList(new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort()))); |
|
40 |
} |
|
41 |
} |
|
42 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/controllers/NotificationServiceCheckDeployController.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.controllers; |
|
2 |
|
|
3 |
import com.mongodb.BasicDBObject; |
|
4 |
import com.mongodb.CommandResult; |
|
5 |
import com.mongodb.DBObject; |
|
6 |
import eu.dnetlib.uoanotificationservice.configuration.GlobalVars; |
|
7 |
import eu.dnetlib.uoanotificationservice.configuration.MongoConfig; |
|
8 |
import eu.dnetlib.uoanotificationservice.configuration.NotificationMongoConnection; |
|
9 |
import org.apache.log4j.Logger; |
|
10 |
import org.springframework.beans.factory.annotation.Autowired; |
|
11 |
import org.springframework.data.mongodb.core.MongoTemplate; |
|
12 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
13 |
import org.springframework.web.bind.annotation.CrossOrigin; |
|
14 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
15 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
16 |
import org.springframework.web.bind.annotation.RestController; |
|
17 |
|
|
18 |
import java.util.HashMap; |
|
19 |
import java.util.Map; |
|
20 |
|
|
21 |
@RestController |
|
22 |
@CrossOrigin(origins = "*") |
|
23 |
@RequestMapping("/notification-service") |
|
24 |
public class NotificationServiceCheckDeployController { |
|
25 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
26 |
|
|
27 |
@Autowired |
|
28 |
private NotificationMongoConnection mongoConnection; |
|
29 |
|
|
30 |
@Autowired |
|
31 |
private MongoConfig mongoConfig; |
|
32 |
|
|
33 |
@Autowired |
|
34 |
private GlobalVars globalVars; |
|
35 |
|
|
36 |
@RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET) |
|
37 |
public String hello() { |
|
38 |
log.debug("Hello from uoa-notification-service!"); |
|
39 |
return "Hello from uoa-notification-service!"; |
|
40 |
} |
|
41 |
|
|
42 |
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") |
|
43 |
@RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) |
|
44 |
public Map<String, String> checkEverything() { |
|
45 |
Map<String, String> response = new HashMap<>(); |
|
46 |
|
|
47 |
MongoTemplate mt = mongoConnection.getNotificationTemplate(); |
|
48 |
DBObject ping = new BasicDBObject("ping", "1"); |
|
49 |
try { |
|
50 |
CommandResult answer = mt.getDb().command(ping); |
|
51 |
response.put("Mongo try: error", answer.getErrorMessage()); |
|
52 |
} catch (Exception e) { |
|
53 |
response.put("Mongo catch: error", e.getMessage()); |
|
54 |
} |
|
55 |
|
|
56 |
response.put("notification.mongodb.database", mongoConfig.getDatabase()); |
|
57 |
response.put("notification.mongodb.host", mongoConfig.getHost()); |
|
58 |
response.put("notification.mongodb.port", String.valueOf(mongoConfig.getPort())); |
|
59 |
response.put("notification.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); |
|
60 |
response.put("notification.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); |
|
61 |
|
|
62 |
if(GlobalVars.date != null) { |
|
63 |
response.put("Date of deploy", GlobalVars.date.toString()); |
|
64 |
} |
|
65 |
if(globalVars.getBuildDate() != null) { |
|
66 |
response.put("Date of build", globalVars.getBuildDate()); |
|
67 |
} |
|
68 |
if (globalVars.getVersion() != null) { |
|
69 |
response.put("Version", globalVars.getVersion()); |
|
70 |
} |
|
71 |
return response; |
|
72 |
} |
|
73 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/controllers/NotificationController.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; |
|
4 |
import eu.dnetlib.uoanotificationservice.entities.Notification; |
|
5 |
import eu.dnetlib.uoanotificationservice.entities.User; |
|
6 |
import eu.dnetlib.uoanotificationservice.services.NotificationService; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
9 |
import org.springframework.web.bind.annotation.*; |
|
10 |
|
|
11 |
import java.util.Date; |
|
12 |
import java.util.List; |
|
13 |
|
|
14 |
@RestController |
|
15 |
@RequestMapping("/notification") |
|
16 |
@CrossOrigin(origins = "*") |
|
17 |
public class NotificationController { |
|
18 |
|
|
19 |
@Autowired |
|
20 |
private NotificationService notificationService; |
|
21 |
|
|
22 |
@Autowired |
|
23 |
private AuthorizationService authorizationService; |
|
24 |
|
|
25 |
@PreAuthorize("hasAuthority(@AuthorizationService.PORTAL_ADMIN)") |
|
26 |
@RequestMapping(value = {"/all"}, method = RequestMethod.GET) |
|
27 |
public List<Notification> getAllNotifications() { |
|
28 |
return notificationService.getAllNotifications(); |
|
29 |
} |
|
30 |
|
|
31 |
@PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER)") |
|
32 |
@RequestMapping(value = {"/{service}"}, method = RequestMethod.GET) |
|
33 |
public List<Notification> getMyNotifications(@PathVariable String service) { |
|
34 |
return notificationService.getMyNotifications(authorizationService.getAaiId(), authorizationService.getEmail(), |
|
35 |
service, authorizationService.getRoles()); |
|
36 |
} |
|
37 |
|
|
38 |
@PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER)") |
|
39 |
@RequestMapping(value = {"/all/{service}"}, method = RequestMethod.PUT) |
|
40 |
public User markAllAsRead(@PathVariable String service) { |
|
41 |
List<Notification> notifications = notificationService.getMyNotifications(authorizationService.getAaiId(), authorizationService.getEmail(), |
|
42 |
service, authorizationService.getRoles()); |
|
43 |
return notificationService.readAllNotifications(notifications, authorizationService.getAaiId()); |
|
44 |
} |
|
45 |
|
|
46 |
@PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER) && @Utils.hasValidGroups(#notification.groups)") |
|
47 |
@RequestMapping(value = {"/save"}, method = RequestMethod.POST) |
|
48 |
public Notification save(@RequestBody Notification notification) { |
|
49 |
notification.setUser(authorizationService.getAaiId()); |
|
50 |
notification.setDate(new Date()); |
|
51 |
notification.setRead(false); |
|
52 |
return notificationService.save(notification); |
|
53 |
} |
|
54 |
|
|
55 |
@PreAuthorize("hasAuthority(@AuthorizationService.REGISTERED_USER) && @Utils.canRead(#id)") |
|
56 |
@RequestMapping(value = {"/{id}"}, method = RequestMethod.PUT) |
|
57 |
public User readNotification(@PathVariable String id) { |
|
58 |
return notificationService.readNotification(authorizationService.getAaiId(), id); |
|
59 |
} |
|
60 |
} |
|
61 |
|
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/utils/Utils.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.utils; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; |
|
4 |
import eu.dnetlib.uoanotificationservice.dao.NotificationDAO; |
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
|
|
9 |
import java.util.HashSet; |
|
10 |
import java.util.Set; |
|
11 |
import java.util.stream.Collectors; |
|
12 |
|
|
13 |
@Component("Utils") |
|
14 |
public class Utils { |
|
15 |
|
|
16 |
Logger logger = Logger.getLogger(Utils.class); |
|
17 |
|
|
18 |
@Autowired |
|
19 |
private AuthorizationService authorizationService; |
|
20 |
|
|
21 |
@Autowired |
|
22 |
private NotificationDAO notificationDAO; |
|
23 |
|
|
24 |
public boolean canRead(String id) { |
|
25 |
Set<String> groups = new HashSet<>(authorizationService.getRoles()); |
|
26 |
groups.add("all"); |
|
27 |
groups.add(authorizationService.getEmail().toLowerCase()); |
|
28 |
return notificationDAO.findByIdAndUserNotAndGroupsIn(id, authorizationService.getAaiId(), groups).isPresent(); |
|
29 |
} |
|
30 |
|
|
31 |
public boolean hasValidGroups(Set<String> groups) { |
|
32 |
Set<String> roles = new HashSet<>(authorizationService.getRoles()); |
|
33 |
Set<String> check = new HashSet<>(groups); |
|
34 |
Set<String> types = getCuratorTypes(roles); |
|
35 |
if(roles.contains(authorizationService.PORTAL_ADMIN)) { |
|
36 |
return true; |
|
37 |
} else if(groups.contains("all")) { |
|
38 |
return false; |
|
39 |
} |
|
40 |
return check.stream().filter(group -> { |
|
41 |
for (String type : types) { |
|
42 |
if (group.contains(type)) { |
|
43 |
return false; |
|
44 |
} |
|
45 |
} |
|
46 |
return true; |
|
47 |
}).allMatch(roles::contains); |
|
48 |
} |
|
49 |
|
|
50 |
private Set<String> getCuratorTypes(Set<String> roles) { |
|
51 |
return roles.stream().filter(role -> role.contains("CURATOR_")). |
|
52 |
map(curator -> curator.replace("CURATOR_", "")).collect(Collectors.toSet()); |
|
53 |
} |
|
54 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/dao/UserDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.uoanotificationservice.entities.User; |
|
4 |
import org.springframework.data.mongodb.repository.MongoRepository; |
|
5 |
import org.springframework.stereotype.Repository; |
|
6 |
|
|
7 |
import java.util.List; |
|
8 |
import java.util.Optional; |
|
9 |
|
|
10 |
@Repository |
|
11 |
public interface UserDAO extends MongoRepository<User, String> { |
|
12 |
|
|
13 |
List<User> findAll(); |
|
14 |
|
|
15 |
Optional<User> findById(String Id); |
|
16 |
|
|
17 |
List<User> findByReadContains(String id); |
|
18 |
|
|
19 |
User save(User user); |
|
20 |
|
|
21 |
void deleteAll(); |
|
22 |
|
|
23 |
void delete(String id); |
|
24 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/dao/NotificationDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.uoanotificationservice.entities.Notification; |
|
4 |
import org.springframework.data.mongodb.repository.MongoRepository; |
|
5 |
import org.springframework.stereotype.Repository; |
|
6 |
|
|
7 |
import java.util.Collection; |
|
8 |
import java.util.Date; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Optional; |
|
11 |
|
|
12 |
@Repository |
|
13 |
public interface NotificationDAO extends MongoRepository<Notification, String> { |
|
14 |
|
|
15 |
Optional<Notification> findById(String Id); |
|
16 |
|
|
17 |
Optional<Notification> findByIdAndUserNotAndGroupsIn(String id, String user, Collection<String> groups); |
|
18 |
|
|
19 |
List<Notification> findByOrderByDateDesc(); |
|
20 |
|
|
21 |
List<Notification> findByUserNotAndServicesInAndGroupsInOrderByDateDesc(String user, Collection<String> services, Collection<String> groups); |
|
22 |
|
|
23 |
List<Notification> findByDateBefore(Date date); |
|
24 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/services/NotificationService.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.services; |
|
2 |
|
|
3 |
import eu.dnetlib.uoanotificationservice.dao.NotificationDAO; |
|
4 |
import eu.dnetlib.uoanotificationservice.dao.UserDAO; |
|
5 |
import eu.dnetlib.uoanotificationservice.entities.Notification; |
|
6 |
import eu.dnetlib.uoanotificationservice.entities.User; |
|
7 |
import eu.dnetlib.uoanotificationservice.exception.ResourceNotFoundException; |
|
8 |
import org.apache.log4j.Logger; |
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
import org.springframework.stereotype.Service; |
|
11 |
|
|
12 |
import java.util.HashSet; |
|
13 |
import java.util.List; |
|
14 |
import java.util.Set; |
|
15 |
|
|
16 |
@Service |
|
17 |
public class NotificationService { |
|
18 |
|
|
19 |
Logger logger = Logger.getLogger(NotificationService.class); |
|
20 |
|
|
21 |
@Autowired |
|
22 |
private NotificationDAO notificationDAO; |
|
23 |
|
|
24 |
@Autowired |
|
25 |
private UserDAO userDAO; |
|
26 |
|
|
27 |
public List<Notification> getAllNotifications() { |
|
28 |
return notificationDAO.findByOrderByDateDesc(); |
|
29 |
} |
|
30 |
|
|
31 |
public List<Notification> getMyNotifications(String userId, String email, String service, List<String> roles) { |
|
32 |
Set<String> services = new HashSet<>(); |
|
33 |
services.add(service); |
|
34 |
services.add("all"); |
|
35 |
Set<String> groups = new HashSet<>(roles); |
|
36 |
groups.add(email.toLowerCase()); |
|
37 |
groups.add("all"); |
|
38 |
List<Notification> notifications = notificationDAO.findByUserNotAndServicesInAndGroupsInOrderByDateDesc(userId, services, groups); |
|
39 |
userDAO.findById(userId).ifPresent(user -> notifications.forEach(notification -> { |
|
40 |
notification.setRead(user.getRead().contains(notification.getId())); |
|
41 |
})); |
|
42 |
return notifications; |
|
43 |
} |
|
44 |
|
|
45 |
public Notification save(Notification notification) { |
|
46 |
return notificationDAO.save(notification); |
|
47 |
} |
|
48 |
|
|
49 |
public User readNotification(String userId, String id) { |
|
50 |
Notification notification = notificationDAO.findById(id).orElseThrow(() -> new ResourceNotFoundException("Notification has not been found")); |
|
51 |
User user = userDAO.findById(userId).orElse(new User(userId, new HashSet<>())); |
|
52 |
user.getRead().add(notification.getId()); |
|
53 |
return userDAO.save(user); |
|
54 |
} |
|
55 |
|
|
56 |
public User readAllNotifications(List<Notification> notifications, String userId) { |
|
57 |
User user = userDAO.findById(userId).orElse(new User(userId, new HashSet<>())); |
|
58 |
notifications.forEach(notification -> { |
|
59 |
user.getRead().add(notification.getId()); |
|
60 |
}); |
|
61 |
return userDAO.save(user); |
|
62 |
} |
|
63 |
|
|
64 |
public void delete(String id) { |
|
65 |
List<User> users = userDAO.findByReadContains(id); |
|
66 |
users.forEach(user -> { |
|
67 |
user.getRead().remove(id); |
|
68 |
userDAO.save(user); |
|
69 |
}); |
|
70 |
notificationDAO.delete(id); |
|
71 |
} |
|
72 |
|
|
73 |
// // every min for testing |
|
74 |
// //@Scheduled(cron = "0 * * * * *" |
|
75 |
// // every day at midnight |
|
76 |
// @Scheduled(cron = "0 0 0 1/1 * ?") |
|
77 |
// protected void deleteNotifications() { |
|
78 |
// logger.info("Deleting notifications that have been created more than a week ago"); |
|
79 |
// Calendar calendar = Calendar.getInstance(); |
|
80 |
// calendar.add(Calendar.DAY_OF_MONTH, -7); |
|
81 |
// List<Notification> notifications = notificationDAO.findByDateBefore(calendar.getTime()); |
|
82 |
// notifications.forEach(notification -> { |
|
83 |
// delete(notification.getId()); |
|
84 |
// }); |
|
85 |
// } |
|
86 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/entities/User.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.entities; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.annotation.JsonProperty; |
|
4 |
import org.springframework.data.annotation.Id; |
|
5 |
|
|
6 |
import java.util.Set; |
|
7 |
|
|
8 |
public class User { |
|
9 |
|
|
10 |
@Id |
|
11 |
@JsonProperty("_id") |
|
12 |
private String id; |
|
13 |
private Set<String> read; |
|
14 |
|
|
15 |
public User() { |
|
16 |
} |
|
17 |
|
|
18 |
public User(String id, Set<String> read) { |
|
19 |
this.id = id; |
|
20 |
this.read = read; |
|
21 |
} |
|
22 |
|
|
23 |
public String getId() { |
|
24 |
return id; |
|
25 |
} |
|
26 |
|
|
27 |
public void setId(String id) { |
|
28 |
this.id = id; |
|
29 |
} |
|
30 |
|
|
31 |
public Set<String> getRead() { |
|
32 |
return read; |
|
33 |
} |
|
34 |
|
|
35 |
public void setRead(Set<String> read) { |
|
36 |
this.read = read; |
|
37 |
} |
|
38 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/entities/Notification.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.entities; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.annotation.JsonProperty; |
|
4 |
import org.springframework.data.annotation.Id; |
|
5 |
|
|
6 |
import java.util.Date; |
|
7 |
import java.util.Set; |
|
8 |
|
|
9 |
public class Notification { |
|
10 |
|
|
11 |
@Id |
|
12 |
@JsonProperty("_id") |
|
13 |
private String id; |
|
14 |
private String actionType; |
|
15 |
private Set<String> services; |
|
16 |
private String user; |
|
17 |
private String name; |
|
18 |
private String surname; |
|
19 |
private String title; |
|
20 |
private String message; |
|
21 |
private String stakeholder; |
|
22 |
private String stakeholderType; |
|
23 |
private String entity; |
|
24 |
private String entityType; |
|
25 |
private Date date; |
|
26 |
private Set<String> groups; |
|
27 |
private Boolean isRead; |
|
28 |
|
|
29 |
public Notification() { |
|
30 |
} |
|
31 |
|
|
32 |
public String getId() { |
|
33 |
return id; |
|
34 |
} |
|
35 |
|
|
36 |
public void setId(String id) { |
|
37 |
this.id = id; |
|
38 |
} |
|
39 |
|
|
40 |
public String getActionType() { |
|
41 |
return actionType; |
|
42 |
} |
|
43 |
|
|
44 |
public void setActionType(String actionType) { |
|
45 |
this.actionType = actionType; |
|
46 |
} |
|
47 |
|
|
48 |
public Set<String> getServices() { |
|
49 |
return services; |
|
50 |
} |
|
51 |
|
|
52 |
public void setServices(Set<String> service) { |
|
53 |
this.services = service; |
|
54 |
} |
|
55 |
|
|
56 |
public String getUser() { |
|
57 |
return user; |
|
58 |
} |
|
59 |
|
|
60 |
public void setUser(String user) { |
|
61 |
this.user = user; |
|
62 |
} |
|
63 |
|
|
64 |
public String getName() { |
|
65 |
return name; |
|
66 |
} |
|
67 |
|
|
68 |
public void setName(String name) { |
|
69 |
this.name = name; |
|
70 |
} |
|
71 |
|
|
72 |
public String getSurname() { |
|
73 |
return surname; |
|
74 |
} |
|
75 |
|
|
76 |
public void setSurname(String surname) { |
|
77 |
this.surname = surname; |
|
78 |
} |
|
79 |
|
|
80 |
public String getTitle() { |
|
81 |
return title; |
|
82 |
} |
|
83 |
|
|
84 |
public void setTitle(String title) { |
|
85 |
this.title = title; |
|
86 |
} |
|
87 |
|
|
88 |
public String getMessage() { |
|
89 |
return message; |
|
90 |
} |
|
91 |
|
|
92 |
public void setMessage(String message) { |
|
93 |
this.message = message; |
|
94 |
} |
|
95 |
|
|
96 |
public String getStakeholder() { |
|
97 |
return stakeholder; |
|
98 |
} |
|
99 |
|
|
100 |
public void setStakeholder(String stakeholder) { |
|
101 |
this.stakeholder = stakeholder; |
|
102 |
} |
|
103 |
|
|
104 |
public String getStakeholderType() { |
|
105 |
return stakeholderType; |
|
106 |
} |
|
107 |
|
|
108 |
public void setStakeholderType(String stakeholderType) { |
|
109 |
this.stakeholderType = stakeholderType; |
|
110 |
} |
|
111 |
|
|
112 |
public String getEntity() { |
|
113 |
return entity; |
|
114 |
} |
|
115 |
|
|
116 |
public void setEntity(String entity) { |
|
117 |
this.entity = entity; |
|
118 |
} |
|
119 |
|
|
120 |
public String getEntityType() { |
|
121 |
return entityType; |
|
122 |
} |
|
123 |
|
|
124 |
public void setEntityType(String entityType) { |
|
125 |
this.entityType = entityType; |
|
126 |
} |
|
127 |
|
|
128 |
public Date getDate() { |
|
129 |
return date; |
|
130 |
} |
|
131 |
|
|
132 |
public void setDate(Date date) { |
|
133 |
this.date = date; |
|
134 |
} |
|
135 |
|
|
136 |
public Set<String> getGroups() { |
|
137 |
return groups; |
|
138 |
} |
|
139 |
|
|
140 |
public void setGroups(Set<String> groups) { |
|
141 |
this.groups = groups; |
|
142 |
} |
|
143 |
|
|
144 |
public Boolean getRead() { |
|
145 |
return isRead; |
|
146 |
} |
|
147 |
|
|
148 |
public void setRead(Boolean read) { |
|
149 |
isRead = read; |
|
150 |
} |
|
151 |
} |
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/java/eu/dnetlib/uoanotificationservice/exception/ResourceNotFoundException.java | ||
---|---|---|
1 |
package eu.dnetlib.uoanotificationservice.exception; |
|
2 |
import org.springframework.http.HttpStatus; |
|
3 |
import org.springframework.web.bind.annotation.ResponseStatus; |
|
4 |
|
|
5 |
@ResponseStatus(value = HttpStatus.NOT_FOUND) // 404 |
|
6 |
public class ResourceNotFoundException extends RuntimeException { |
|
7 |
|
|
8 |
public ResourceNotFoundException(String message) { |
|
9 |
super(message); |
|
10 |
} |
|
11 |
|
|
12 |
public ResourceNotFoundException(String message, Throwable err) { |
|
13 |
super(message, err); |
|
14 |
} |
|
15 |
|
|
16 |
public HttpStatus getStatus() { |
|
17 |
return HttpStatus.NOT_FOUND; |
|
18 |
} |
|
19 |
} |
|
20 |
|
modules/uoa-notification-service/tags/uoa-notification-service-1.0.4/src/main/resources/notification.properties | ||
---|---|---|
1 |
#notification.mongodb.host = localhost |
|
2 |
#notification.mongodb.port = 27017 |
|
3 |
#notification.mongodb.database = openaire_notification |
|
4 |
notification.globalVars.buildDate=@timestampNotificationLibrary@ |
|
5 |
notification.globalVars.version=${project.version} |
Also available in: Unified diff
[maven-release-plugin] copy for tag uoa-notification-service-1.0.4