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