Revision 59471
Added by Konstantina Galouni over 3 years ago
modules/uoa-admin-tools/trunk/src/main/java/eu/dnetlib/uoaadmintools/controllers/CommunitySubscribersController.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaadmintools.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaadmintools.configuration.properties.SecurityConfig; |
|
4 |
import eu.dnetlib.uoaadmintools.dao.CommunityDAO; |
|
5 |
import eu.dnetlib.uoaadmintools.dao.CommunitySubscribersDAO; |
|
6 |
import eu.dnetlib.uoaadmintools.dao.SubscriberDAO; |
|
7 |
import eu.dnetlib.uoaadmintools.entities.CommunitySubscribers; |
|
8 |
import eu.dnetlib.uoaadmintools.entities.Subscriber; |
|
9 |
import eu.dnetlib.uoaadmintools.handlers.ContentNotFoundException; |
|
10 |
import eu.dnetlib.uoaadmintools.handlers.utils.AuthorizationUtils; |
|
11 |
import eu.dnetlib.uoaadmintools.handlers.utils.UserInfo; |
|
12 |
import eu.dnetlib.uoaadmintools.responses.SingleValueWrapperResponse; |
|
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 |
import java.util.Iterator; |
|
19 |
import java.util.List; |
|
20 |
|
|
21 |
/** |
|
22 |
* Created by argirok on 2/3/2018. |
|
23 |
*/ |
|
24 |
@RestController |
|
25 |
@CrossOrigin(origins = "*") |
|
26 |
public class CommunitySubscribersController { |
|
27 |
@Autowired |
|
28 |
CommunitySubscribersDAO communitySubscriberDAO; |
|
29 |
@Autowired |
|
30 |
SubscriberDAO subscriberDAO; |
|
31 |
@Autowired |
|
32 |
CommunityDAO communityDAO; |
|
33 |
|
|
34 |
@Autowired |
|
35 |
private SecurityConfig securityConfig; |
|
36 |
|
|
37 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
38 |
@RequestMapping(value = "/community/subscribers", method = RequestMethod.GET) |
|
39 |
public List<CommunitySubscribers> getAllCommunitySubscribers(){ |
|
40 |
return communitySubscriberDAO.findAll(); |
|
41 |
} |
|
42 |
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.GET) |
|
43 |
public CommunitySubscribers getSubscribersPerCommunity(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException { |
|
44 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
45 |
if(communitySubscribers != null){ |
|
46 |
return communitySubscribers; |
|
47 |
}else{ |
|
48 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
49 |
|
|
50 |
} |
|
51 |
|
|
52 |
} |
|
53 |
|
|
54 |
@RequestMapping(value = "/community/{pid}/subscribers/count", method = RequestMethod.GET) |
|
55 |
public SingleValueWrapperResponse<Integer> getNumberOfSubscribersPerCommunity(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException { |
|
56 |
SingleValueWrapperResponse<Integer> singleValueWrapperResponse = new SingleValueWrapperResponse(0); |
|
57 |
|
|
58 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
59 |
if(communitySubscribers != null){ |
|
60 |
if(communitySubscribers.getSubscribers() != null) { |
|
61 |
singleValueWrapperResponse.setValue(communitySubscribers.getSubscribers().size()); |
|
62 |
} |
|
63 |
}else{ |
|
64 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
65 |
|
|
66 |
} |
|
67 |
return singleValueWrapperResponse; |
|
68 |
} |
|
69 |
|
|
70 |
@RequestMapping(value = "/community/{pid}/is-subscriber", method = RequestMethod.GET) |
|
71 |
public Boolean getIsSubscribedToCommunity(@PathVariable(value="pid", required = true) String pid, |
|
72 |
//@RequestBody String email, |
|
73 |
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException { |
|
74 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
75 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
76 |
UserInfo userInfo = helper.getUserInfo(token); |
|
77 |
|
|
78 |
if(userInfo != null) { |
|
79 |
String email = userInfo.getEmail(); |
|
80 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
81 |
if (communitySubscribers != null) { |
|
82 |
if (communitySubscribers.getSubscribers() != null) { |
|
83 |
for (Subscriber subscriber : communitySubscribers.getSubscribers()) { |
|
84 |
if (subscriber.getEmail().equals(email)) { |
|
85 |
return true; |
|
86 |
} |
|
87 |
} |
|
88 |
} |
|
89 |
} else { |
|
90 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
91 |
|
|
92 |
} |
|
93 |
} |
|
94 |
return false; |
|
95 |
} |
|
96 |
|
|
97 |
@RequestMapping(value = "/community/{pid}/subscriber", method = RequestMethod.POST) |
|
98 |
public Boolean addSubscriberInCommunity(@PathVariable(value="pid", required = true) String pid, |
|
99 |
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException { |
|
100 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
101 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
102 |
UserInfo userInfo = helper.getUserInfo(token); |
|
103 |
|
|
104 |
if(userInfo != null) { |
|
105 |
String email = userInfo.getEmail(); |
|
106 |
Subscriber subscriber = new Subscriber(email); |
|
107 |
|
|
108 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
109 |
if (communitySubscribers == null) { |
|
110 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
111 |
} |
|
112 |
|
|
113 |
Subscriber savedSubscriber = subscriberDAO.findByEmail(email); |
|
114 |
if (savedSubscriber == null) { |
|
115 |
savedSubscriber = subscriberDAO.save(subscriber); |
|
116 |
} |
|
117 |
for (Subscriber sub : communitySubscribers.getSubscribers()) { |
|
118 |
if (sub.getEmail().equals(subscriber.getEmail())) { |
|
119 |
//already subscribed |
|
120 |
return false; |
|
121 |
} |
|
122 |
} |
|
123 |
//not subscribed yet |
|
124 |
communitySubscribers.getSubscribers().add(savedSubscriber); |
|
125 |
communitySubscriberDAO.save(communitySubscribers); |
|
126 |
return true; |
|
127 |
} |
|
128 |
return false; |
|
129 |
|
|
130 |
} |
|
131 |
@RequestMapping(value = "/community/{pid}/subscriber/delete", method = RequestMethod.POST) |
|
132 |
public Boolean deleteSubscriberFromCommunity(@PathVariable(value="pid", required = true) String pid, |
|
133 |
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException { |
|
134 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
135 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
136 |
UserInfo userInfo = helper.getUserInfo(token); |
|
137 |
|
|
138 |
if(userInfo != null) { |
|
139 |
String email = userInfo.getEmail(); |
|
140 |
|
|
141 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
142 |
if (communitySubscribers == null) { |
|
143 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
144 |
} |
|
145 |
|
|
146 |
Iterator<Subscriber> subscriberIterator = communitySubscribers.getSubscribers().iterator(); |
|
147 |
while(subscriberIterator.hasNext()) { |
|
148 |
Subscriber subscriber = subscriberIterator.next(); |
|
149 |
if(subscriber.getEmail().equals(email)) { |
|
150 |
subscriberIterator.remove(); |
|
151 |
communitySubscriberDAO.save(communitySubscribers); |
|
152 |
return true; |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
return false; |
|
157 |
} |
|
158 |
|
|
159 |
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.POST) |
|
160 |
public CommunitySubscribers addSubscriberInCommunityByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody Subscriber subscriber) throws ContentNotFoundException { |
|
161 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
162 |
if(communitySubscribers == null){ |
|
163 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
164 |
} |
|
165 |
|
|
166 |
Subscriber savedSubscriber = subscriberDAO.findByEmail(subscriber.getEmail()); |
|
167 |
if(savedSubscriber==null){ |
|
168 |
savedSubscriber = subscriberDAO.save(subscriber); |
|
169 |
} |
|
170 |
for(Subscriber sub:communitySubscribers.getSubscribers()){ |
|
171 |
if(sub.getEmail().equals(subscriber.getEmail())){ |
|
172 |
//already subscribed |
|
173 |
return communitySubscribers; |
|
174 |
} |
|
175 |
} |
|
176 |
//not subscribed yet |
|
177 |
communitySubscribers.getSubscribers().add(savedSubscriber); |
|
178 |
return communitySubscriberDAO.save(communitySubscribers); |
|
179 |
|
|
180 |
} |
|
181 |
@RequestMapping(value = "/community/{pid}/subscribers/delete", method = RequestMethod.POST) |
|
182 |
public CommunitySubscribers deleteSubscriberFromCommunityByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody List<String> emails) throws ContentNotFoundException { |
|
183 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
184 |
if(communitySubscribers == null){ |
|
185 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
186 |
} |
|
187 |
List<Subscriber> list = new ArrayList<>(); |
|
188 |
for(Subscriber s:communitySubscribers.getSubscribers()){ |
|
189 |
if(emails.indexOf(s.getEmail())==-1){ |
|
190 |
list.add(s); |
|
191 |
} |
|
192 |
} |
|
193 |
communitySubscribers.setSubscribers(list); |
|
194 |
return communitySubscriberDAO.save(communitySubscribers); |
|
195 |
} |
|
196 |
|
|
197 |
@RequestMapping(value = "/subscriber/communities", method = RequestMethod.GET) |
|
198 |
public List<String> getCommunitiesPerSubcriber(//@RequestParam(value="email", required = true) String email, |
|
199 |
@RequestHeader("X-XSRF-TOKEN") String token) { |
|
200 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
201 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
202 |
UserInfo userInfo = helper.getUserInfo(token); |
|
203 |
|
|
204 |
List<String> list = new ArrayList<>(); |
|
205 |
|
|
206 |
if (userInfo != null) { |
|
207 |
String email = userInfo.getEmail(); |
|
208 |
List<CommunitySubscribers> communitySubscribers = communitySubscriberDAO.findAll(); |
|
209 |
|
|
210 |
for (CommunitySubscribers s : communitySubscribers) { |
|
211 |
for (Subscriber sub : s.getSubscribers()) { |
|
212 |
if (sub.getEmail().equals(email)) { |
|
213 |
list.add(s.getPid()); |
|
214 |
break; |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
return list; |
|
220 |
} |
|
221 |
} |
Also available in: Unified diff
[Trunk | Admin Tools]: Delete "CommunitySubscribersController.java" (replaced by PortalSubscribersController.java).