1 |
58394
|
konstantin
|
package eu.dnetlib.uoaadmintools.controllers;
|
2 |
|
|
|
3 |
59377
|
konstantin
|
import eu.dnetlib.uoaadmintools.configuration.properties.SecurityConfig;
|
4 |
58394
|
konstantin
|
import eu.dnetlib.uoaadmintools.dao.PortalSubscribersDAO;
|
5 |
|
|
import eu.dnetlib.uoaadmintools.dao.SubscriberDAO;
|
6 |
|
|
import eu.dnetlib.uoaadmintools.entities.subscriber.PortalSubscribers;
|
7 |
|
|
import eu.dnetlib.uoaadmintools.entities.subscriber.Subscriber;
|
8 |
59377
|
konstantin
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
|
9 |
|
|
import eu.dnetlib.uoaadmintools.handlers.utils.AuthorizationUtils;
|
10 |
|
|
import eu.dnetlib.uoaadmintools.handlers.utils.UserInfo;
|
11 |
|
|
import eu.dnetlib.uoaadmintools.responses.SingleValueWrapperResponse;
|
12 |
58394
|
konstantin
|
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
|
13 |
|
|
import org.apache.log4j.Logger;
|
14 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
15 |
|
|
import org.springframework.web.bind.annotation.*;
|
16 |
|
|
|
17 |
|
|
import java.util.ArrayList;
|
18 |
59377
|
konstantin
|
import java.util.Iterator;
|
19 |
58394
|
konstantin
|
import java.util.List;
|
20 |
|
|
|
21 |
|
|
/**
|
22 |
|
|
* Created by argirok on 2/3/2018.
|
23 |
|
|
*/
|
24 |
|
|
@RestController
|
25 |
|
|
@CrossOrigin(origins = "*")
|
26 |
|
|
public class PortalSubscribersController {
|
27 |
|
|
@Autowired
|
28 |
|
|
PortalSubscribersDAO portalSubscribersDAO;
|
29 |
|
|
@Autowired
|
30 |
|
|
SubscriberDAO subscriberDAO;
|
31 |
|
|
@Autowired
|
32 |
|
|
PortalDAO portalDAO;
|
33 |
|
|
|
34 |
59377
|
konstantin
|
@Autowired
|
35 |
|
|
private SecurityConfig securityConfig;
|
36 |
|
|
|
37 |
58394
|
konstantin
|
private final Logger log = Logger.getLogger(this.getClass());
|
38 |
59377
|
konstantin
|
|
39 |
|
|
@RequestMapping(value = "/community/{pid}/subscribers/count", method = RequestMethod.GET)
|
40 |
|
|
public SingleValueWrapperResponse<Integer> getNumberOfSubscribersPerPortal(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException {
|
41 |
|
|
SingleValueWrapperResponse<Integer> singleValueWrapperResponse = new SingleValueWrapperResponse(0);
|
42 |
|
|
|
43 |
|
|
PortalSubscribers portalSubscribers = portalSubscribersDAO.findByPid(pid);
|
44 |
|
|
if(portalSubscribers != null){
|
45 |
|
|
if(portalSubscribers.getSubscribers() != null) {
|
46 |
|
|
singleValueWrapperResponse.setValue(portalSubscribers.getSubscribers().size());
|
47 |
|
|
}
|
48 |
|
|
}else{
|
49 |
|
|
throw new ContentNotFoundException("Portal Subscribers not found");
|
50 |
|
|
|
51 |
|
|
}
|
52 |
|
|
return singleValueWrapperResponse;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
58394
|
konstantin
|
@RequestMapping(value = "/community/subscribers", method = RequestMethod.GET)
|
56 |
|
|
public List<PortalSubscribers> getAllPortalSubscribers(){
|
57 |
|
|
return portalSubscribersDAO.findAll();
|
58 |
|
|
}
|
59 |
59377
|
konstantin
|
|
60 |
58394
|
konstantin
|
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.GET)
|
61 |
|
|
public PortalSubscribers getSubscribersPerPortal(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException {
|
62 |
|
|
PortalSubscribers portalSubscribers = portalSubscribersDAO.findByPid(pid);
|
63 |
|
|
if(portalSubscribers != null){
|
64 |
|
|
return portalSubscribers;
|
65 |
|
|
}else{
|
66 |
|
|
throw new ContentNotFoundException("Portal Subscribers not found");
|
67 |
|
|
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
}
|
71 |
59377
|
konstantin
|
// @RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.POST)
|
72 |
|
|
// public PortalSubscribers addSubscriberInPortal(@PathVariable(value="pid", required = true) String pid, @RequestBody Subscriber subscriber) throws ContentNotFoundException {
|
73 |
|
|
// PortalSubscribers portalSubscribers = portalSubscribersDAO.findByPid(pid);
|
74 |
|
|
// if(portalSubscribers == null){
|
75 |
|
|
// throw new ContentNotFoundException("Portal Subscribers not found");
|
76 |
|
|
// }
|
77 |
|
|
//
|
78 |
|
|
// Subscriber savedSubscriber = subscriberDAO.findByEmail(subscriber.getEmail());
|
79 |
|
|
// if(savedSubscriber==null){
|
80 |
|
|
// savedSubscriber = subscriberDAO.save(subscriber);
|
81 |
|
|
// }
|
82 |
|
|
// for(Subscriber sub: portalSubscribers.getSubscribers()){
|
83 |
|
|
// if(sub.getEmail().equals(subscriber.getEmail())){
|
84 |
|
|
// //already subscribed
|
85 |
|
|
// return portalSubscribers;
|
86 |
|
|
// }
|
87 |
|
|
// }
|
88 |
|
|
// //not subscribed yet
|
89 |
|
|
// portalSubscribers.getSubscribers().add(savedSubscriber);
|
90 |
|
|
// return portalSubscribersDAO.save(portalSubscribers);
|
91 |
|
|
//
|
92 |
|
|
// }
|
93 |
|
|
|
94 |
|
|
@RequestMapping(value = "/community/{pid}/is-subscriber", method = RequestMethod.GET)
|
95 |
|
|
public Boolean getIsSubscribedToPortal(@PathVariable(value="pid", required = true) String pid,
|
96 |
|
|
//@RequestBody String email,
|
97 |
|
|
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException {
|
98 |
|
|
AuthorizationUtils helper = new AuthorizationUtils();
|
99 |
|
|
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
100 |
|
|
UserInfo userInfo = helper.getUserInfo(token);
|
101 |
|
|
|
102 |
|
|
if(userInfo != null) {
|
103 |
|
|
String email = userInfo.getEmail();
|
104 |
|
|
PortalSubscribers communitySubscribers = portalSubscribersDAO.findByPid(pid);
|
105 |
|
|
if (communitySubscribers != null) {
|
106 |
|
|
if (communitySubscribers.getSubscribers() != null) {
|
107 |
|
|
for (Subscriber subscriber : communitySubscribers.getSubscribers()) {
|
108 |
|
|
if (subscriber.getEmail().equals(email)) {
|
109 |
|
|
return true;
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
} else {
|
114 |
|
|
throw new ContentNotFoundException("Portal Subscribers not found");
|
115 |
|
|
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
return false;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
@RequestMapping(value = "/community/{pid}/subscriber", method = RequestMethod.POST)
|
122 |
|
|
public Boolean addSubscriberInPortal(@PathVariable(value="pid", required = true) String pid,
|
123 |
|
|
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException {
|
124 |
|
|
AuthorizationUtils helper = new AuthorizationUtils();
|
125 |
|
|
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
126 |
|
|
UserInfo userInfo = helper.getUserInfo(token);
|
127 |
|
|
|
128 |
|
|
if(userInfo != null) {
|
129 |
|
|
String email = userInfo.getEmail();
|
130 |
|
|
Subscriber subscriber = new Subscriber(email);
|
131 |
|
|
|
132 |
|
|
PortalSubscribers communitySubscribers = portalSubscribersDAO.findByPid(pid);
|
133 |
|
|
if (communitySubscribers == null) {
|
134 |
|
|
throw new ContentNotFoundException("Community Subscribers not found");
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
Subscriber savedSubscriber = subscriberDAO.findByEmail(email);
|
138 |
|
|
if (savedSubscriber == null) {
|
139 |
|
|
savedSubscriber = subscriberDAO.save(subscriber);
|
140 |
|
|
}
|
141 |
|
|
for (Subscriber sub : communitySubscribers.getSubscribers()) {
|
142 |
|
|
if (sub.getEmail().equals(subscriber.getEmail())) {
|
143 |
|
|
//already subscribed
|
144 |
|
|
return false;
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
//not subscribed yet
|
148 |
|
|
communitySubscribers.getSubscribers().add(savedSubscriber);
|
149 |
|
|
portalSubscribersDAO.save(communitySubscribers);
|
150 |
|
|
return true;
|
151 |
|
|
}
|
152 |
|
|
return false;
|
153 |
|
|
|
154 |
|
|
}
|
155 |
|
|
@RequestMapping(value = "/community/{pid}/subscriber/delete", method = RequestMethod.POST)
|
156 |
|
|
public Boolean deleteSubscriberFromPortal(@PathVariable(value="pid", required = true) String pid,
|
157 |
|
|
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException {
|
158 |
|
|
AuthorizationUtils helper = new AuthorizationUtils();
|
159 |
|
|
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
160 |
|
|
UserInfo userInfo = helper.getUserInfo(token);
|
161 |
|
|
|
162 |
|
|
if(userInfo != null) {
|
163 |
|
|
String email = userInfo.getEmail();
|
164 |
|
|
|
165 |
|
|
PortalSubscribers communitySubscribers = portalSubscribersDAO.findByPid(pid);
|
166 |
|
|
if (communitySubscribers == null) {
|
167 |
|
|
throw new ContentNotFoundException("Community Subscribers not found");
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
Iterator<Subscriber> subscriberIterator = communitySubscribers.getSubscribers().iterator();
|
171 |
|
|
while(subscriberIterator.hasNext()) {
|
172 |
|
|
Subscriber subscriber = subscriberIterator.next();
|
173 |
|
|
if(subscriber.getEmail().equals(email)) {
|
174 |
|
|
subscriberIterator.remove();
|
175 |
|
|
portalSubscribersDAO.save(communitySubscribers);
|
176 |
|
|
return true;
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
return false;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
58394
|
konstantin
|
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.POST)
|
184 |
59377
|
konstantin
|
public PortalSubscribers addSubscriberInPortalByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody Subscriber subscriber) throws ContentNotFoundException {
|
185 |
|
|
PortalSubscribers communitySubscribers = portalSubscribersDAO.findByPid(pid);
|
186 |
|
|
if(communitySubscribers == null){
|
187 |
|
|
throw new ContentNotFoundException("Community Subscribers not found");
|
188 |
58394
|
konstantin
|
}
|
189 |
|
|
|
190 |
|
|
Subscriber savedSubscriber = subscriberDAO.findByEmail(subscriber.getEmail());
|
191 |
|
|
if(savedSubscriber==null){
|
192 |
|
|
savedSubscriber = subscriberDAO.save(subscriber);
|
193 |
|
|
}
|
194 |
59377
|
konstantin
|
for(Subscriber sub:communitySubscribers.getSubscribers()){
|
195 |
58394
|
konstantin
|
if(sub.getEmail().equals(subscriber.getEmail())){
|
196 |
|
|
//already subscribed
|
197 |
59377
|
konstantin
|
return communitySubscribers;
|
198 |
58394
|
konstantin
|
}
|
199 |
|
|
}
|
200 |
|
|
//not subscribed yet
|
201 |
59377
|
konstantin
|
communitySubscribers.getSubscribers().add(savedSubscriber);
|
202 |
|
|
return portalSubscribersDAO.save(communitySubscribers);
|
203 |
58394
|
konstantin
|
|
204 |
|
|
}
|
205 |
|
|
@RequestMapping(value = "/community/{pid}/subscribers/delete", method = RequestMethod.POST)
|
206 |
59377
|
konstantin
|
public PortalSubscribers deleteSubscriberFromPortalByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody List<String> emails) throws ContentNotFoundException {
|
207 |
|
|
PortalSubscribers communitySubscribers = portalSubscribersDAO.findByPid(pid);
|
208 |
|
|
if(communitySubscribers == null){
|
209 |
|
|
throw new ContentNotFoundException("Community Subscribers not found");
|
210 |
58394
|
konstantin
|
}
|
211 |
|
|
List<Subscriber> list = new ArrayList<>();
|
212 |
59377
|
konstantin
|
for(Subscriber s:communitySubscribers.getSubscribers()){
|
213 |
58394
|
konstantin
|
if(emails.indexOf(s.getEmail())==-1){
|
214 |
59377
|
konstantin
|
list.add(s);
|
215 |
58394
|
konstantin
|
}
|
216 |
|
|
}
|
217 |
59377
|
konstantin
|
communitySubscribers.setSubscribers(list);
|
218 |
|
|
return portalSubscribersDAO.save(communitySubscribers);
|
219 |
58394
|
konstantin
|
}
|
220 |
|
|
|
221 |
|
|
@RequestMapping(value = "/subscriber/communities", method = RequestMethod.GET)
|
222 |
59377
|
konstantin
|
public List<String> getPortalsPerSubcriber(//@RequestParam(value="email", required = true) String email,
|
223 |
|
|
@RequestHeader("X-XSRF-TOKEN") String token) {
|
224 |
|
|
AuthorizationUtils helper = new AuthorizationUtils();
|
225 |
|
|
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
226 |
|
|
UserInfo userInfo = helper.getUserInfo(token);
|
227 |
|
|
|
228 |
58394
|
konstantin
|
List<String> list = new ArrayList<>();
|
229 |
59377
|
konstantin
|
|
230 |
|
|
if (userInfo != null) {
|
231 |
|
|
String email = userInfo.getEmail();
|
232 |
|
|
List<PortalSubscribers> communitySubscribers = portalSubscribersDAO.findAll();
|
233 |
|
|
|
234 |
|
|
for (PortalSubscribers s : communitySubscribers) {
|
235 |
|
|
for (Subscriber sub : s.getSubscribers()) {
|
236 |
|
|
if (sub.getEmail().equals(email)) {
|
237 |
|
|
list.add(s.getPid());
|
238 |
|
|
break;
|
239 |
|
|
}
|
240 |
58394
|
konstantin
|
}
|
241 |
|
|
}
|
242 |
|
|
}
|
243 |
|
|
return list;
|
244 |
|
|
}
|
245 |
|
|
}
|