1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.dao.PortalSubscribersDAO;
|
4
|
import eu.dnetlib.uoaadmintools.dao.SubscriberDAO;
|
5
|
import eu.dnetlib.uoaadmintools.entities.subscriber.PortalSubscribers;
|
6
|
import eu.dnetlib.uoaadmintools.entities.subscriber.Subscriber;
|
7
|
import eu.dnetlib.uoaadmintools.handlers.ContentNotFoundException;
|
8
|
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.web.bind.annotation.*;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
/**
|
17
|
* Created by argirok on 2/3/2018.
|
18
|
*/
|
19
|
@RestController
|
20
|
@CrossOrigin(origins = "*")
|
21
|
public class PortalSubscribersController {
|
22
|
@Autowired
|
23
|
PortalSubscribersDAO portalSubscribersDAO;
|
24
|
@Autowired
|
25
|
SubscriberDAO subscriberDAO;
|
26
|
@Autowired
|
27
|
PortalDAO portalDAO;
|
28
|
|
29
|
private final Logger log = Logger.getLogger(this.getClass());
|
30
|
@RequestMapping(value = "/community/subscribers", method = RequestMethod.GET)
|
31
|
public List<PortalSubscribers> getAllPortalSubscribers(){
|
32
|
return portalSubscribersDAO.findAll();
|
33
|
}
|
34
|
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.GET)
|
35
|
public PortalSubscribers getSubscribersPerPortal(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException {
|
36
|
PortalSubscribers portalSubscribers = portalSubscribersDAO.findByPid(pid);
|
37
|
if(portalSubscribers != null){
|
38
|
return portalSubscribers;
|
39
|
}else{
|
40
|
throw new ContentNotFoundException("Portal Subscribers not found");
|
41
|
|
42
|
}
|
43
|
|
44
|
}
|
45
|
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.POST)
|
46
|
public PortalSubscribers addSubscriberInPortal(@PathVariable(value="pid", required = true) String pid, @RequestBody Subscriber subscriber) throws ContentNotFoundException {
|
47
|
PortalSubscribers portalSubscribers = portalSubscribersDAO.findByPid(pid);
|
48
|
if(portalSubscribers == null){
|
49
|
throw new ContentNotFoundException("Portal Subscribers not found");
|
50
|
}
|
51
|
|
52
|
Subscriber savedSubscriber = subscriberDAO.findByEmail(subscriber.getEmail());
|
53
|
if(savedSubscriber==null){
|
54
|
savedSubscriber = subscriberDAO.save(subscriber);
|
55
|
}
|
56
|
for(Subscriber sub: portalSubscribers.getSubscribers()){
|
57
|
if(sub.getEmail().equals(subscriber.getEmail())){
|
58
|
//already subscribed
|
59
|
return portalSubscribers;
|
60
|
}
|
61
|
}
|
62
|
//not subscribed yet
|
63
|
portalSubscribers.getSubscribers().add(savedSubscriber);
|
64
|
return portalSubscribersDAO.save(portalSubscribers);
|
65
|
|
66
|
}
|
67
|
@RequestMapping(value = "/community/{pid}/subscribers/delete", method = RequestMethod.POST)
|
68
|
public PortalSubscribers deleteSubscriberFromPortal(@PathVariable(value="pid", required = true) String pid, @RequestBody List<String> emails) throws ContentNotFoundException {
|
69
|
PortalSubscribers portalSubscribers = portalSubscribersDAO.findByPid(pid);
|
70
|
if(portalSubscribers == null){
|
71
|
throw new ContentNotFoundException("Portal Subscribers not found");
|
72
|
}
|
73
|
List<Subscriber> list = new ArrayList<>();
|
74
|
for(Subscriber s: portalSubscribers.getSubscribers()){
|
75
|
if(emails.indexOf(s.getEmail())==-1){
|
76
|
list.add(s);
|
77
|
}
|
78
|
}
|
79
|
portalSubscribers.setSubscribers(list);
|
80
|
return portalSubscribersDAO.save(portalSubscribers);
|
81
|
}
|
82
|
|
83
|
@RequestMapping(value = "/subscriber/communities", method = RequestMethod.GET)
|
84
|
public List<String> getPortalsPerSubcriber(@RequestParam(value="email", required = true) String email) {
|
85
|
List<PortalSubscribers> portalSubscribers = portalSubscribersDAO.findAll();
|
86
|
List<String> list = new ArrayList<>();
|
87
|
for(PortalSubscribers s: portalSubscribers){
|
88
|
for(Subscriber sub:s.getSubscribers()) {
|
89
|
if (sub.getEmail().equals(email)) {
|
90
|
list.add(s.getPid());
|
91
|
break;
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
return list;
|
96
|
}
|
97
|
}
|