[Trunk | Admin Tools]: Delete "CommunitySubscribersController.java" (replaced by PortalSubscribersController.java).
This commit is contained in:
parent
f2ca4c4275
commit
80bf0ca415
|
@ -1,221 +0,0 @@
|
||||||
package eu.dnetlib.uoaadmintools.controllers;
|
|
||||||
|
|
||||||
import eu.dnetlib.uoaadmintools.configuration.properties.SecurityConfig;
|
|
||||||
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
|
|
||||||
import eu.dnetlib.uoaadmintools.dao.CommunitySubscribersDAO;
|
|
||||||
import eu.dnetlib.uoaadmintools.dao.SubscriberDAO;
|
|
||||||
import eu.dnetlib.uoaadmintools.entities.CommunitySubscribers;
|
|
||||||
import eu.dnetlib.uoaadmintools.entities.Subscriber;
|
|
||||||
import eu.dnetlib.uoaadmintools.handlers.ContentNotFoundException;
|
|
||||||
import eu.dnetlib.uoaadmintools.handlers.utils.AuthorizationUtils;
|
|
||||||
import eu.dnetlib.uoaadmintools.handlers.utils.UserInfo;
|
|
||||||
import eu.dnetlib.uoaadmintools.responses.SingleValueWrapperResponse;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by argirok on 2/3/2018.
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@CrossOrigin(origins = "*")
|
|
||||||
public class CommunitySubscribersController {
|
|
||||||
@Autowired
|
|
||||||
CommunitySubscribersDAO communitySubscriberDAO;
|
|
||||||
@Autowired
|
|
||||||
SubscriberDAO subscriberDAO;
|
|
||||||
@Autowired
|
|
||||||
CommunityDAO communityDAO;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private SecurityConfig securityConfig;
|
|
||||||
|
|
||||||
private final Logger log = Logger.getLogger(this.getClass());
|
|
||||||
@RequestMapping(value = "/community/subscribers", method = RequestMethod.GET)
|
|
||||||
public List<CommunitySubscribers> getAllCommunitySubscribers(){
|
|
||||||
return communitySubscriberDAO.findAll();
|
|
||||||
}
|
|
||||||
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.GET)
|
|
||||||
public CommunitySubscribers getSubscribersPerCommunity(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException {
|
|
||||||
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid);
|
|
||||||
if(communitySubscribers != null){
|
|
||||||
return communitySubscribers;
|
|
||||||
}else{
|
|
||||||
throw new ContentNotFoundException("Community Subscribers not found");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/community/{pid}/subscribers/count", method = RequestMethod.GET)
|
|
||||||
public SingleValueWrapperResponse<Integer> getNumberOfSubscribersPerCommunity(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException {
|
|
||||||
SingleValueWrapperResponse<Integer> singleValueWrapperResponse = new SingleValueWrapperResponse(0);
|
|
||||||
|
|
||||||
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid);
|
|
||||||
if(communitySubscribers != null){
|
|
||||||
if(communitySubscribers.getSubscribers() != null) {
|
|
||||||
singleValueWrapperResponse.setValue(communitySubscribers.getSubscribers().size());
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
throw new ContentNotFoundException("Community Subscribers not found");
|
|
||||||
|
|
||||||
}
|
|
||||||
return singleValueWrapperResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/community/{pid}/is-subscriber", method = RequestMethod.GET)
|
|
||||||
public Boolean getIsSubscribedToCommunity(@PathVariable(value="pid", required = true) String pid,
|
|
||||||
//@RequestBody String email,
|
|
||||||
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException {
|
|
||||||
AuthorizationUtils helper = new AuthorizationUtils();
|
|
||||||
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
|
||||||
UserInfo userInfo = helper.getUserInfo(token);
|
|
||||||
|
|
||||||
if(userInfo != null) {
|
|
||||||
String email = userInfo.getEmail();
|
|
||||||
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid);
|
|
||||||
if (communitySubscribers != null) {
|
|
||||||
if (communitySubscribers.getSubscribers() != null) {
|
|
||||||
for (Subscriber subscriber : communitySubscribers.getSubscribers()) {
|
|
||||||
if (subscriber.getEmail().equals(email)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new ContentNotFoundException("Community Subscribers not found");
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/community/{pid}/subscriber", method = RequestMethod.POST)
|
|
||||||
public Boolean addSubscriberInCommunity(@PathVariable(value="pid", required = true) String pid,
|
|
||||||
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException {
|
|
||||||
AuthorizationUtils helper = new AuthorizationUtils();
|
|
||||||
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
|
||||||
UserInfo userInfo = helper.getUserInfo(token);
|
|
||||||
|
|
||||||
if(userInfo != null) {
|
|
||||||
String email = userInfo.getEmail();
|
|
||||||
Subscriber subscriber = new Subscriber(email);
|
|
||||||
|
|
||||||
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid);
|
|
||||||
if (communitySubscribers == null) {
|
|
||||||
throw new ContentNotFoundException("Community Subscribers not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
Subscriber savedSubscriber = subscriberDAO.findByEmail(email);
|
|
||||||
if (savedSubscriber == null) {
|
|
||||||
savedSubscriber = subscriberDAO.save(subscriber);
|
|
||||||
}
|
|
||||||
for (Subscriber sub : communitySubscribers.getSubscribers()) {
|
|
||||||
if (sub.getEmail().equals(subscriber.getEmail())) {
|
|
||||||
//already subscribed
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//not subscribed yet
|
|
||||||
communitySubscribers.getSubscribers().add(savedSubscriber);
|
|
||||||
communitySubscriberDAO.save(communitySubscribers);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
@RequestMapping(value = "/community/{pid}/subscriber/delete", method = RequestMethod.POST)
|
|
||||||
public Boolean deleteSubscriberFromCommunity(@PathVariable(value="pid", required = true) String pid,
|
|
||||||
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException {
|
|
||||||
AuthorizationUtils helper = new AuthorizationUtils();
|
|
||||||
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
|
||||||
UserInfo userInfo = helper.getUserInfo(token);
|
|
||||||
|
|
||||||
if(userInfo != null) {
|
|
||||||
String email = userInfo.getEmail();
|
|
||||||
|
|
||||||
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid);
|
|
||||||
if (communitySubscribers == null) {
|
|
||||||
throw new ContentNotFoundException("Community Subscribers not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<Subscriber> subscriberIterator = communitySubscribers.getSubscribers().iterator();
|
|
||||||
while(subscriberIterator.hasNext()) {
|
|
||||||
Subscriber subscriber = subscriberIterator.next();
|
|
||||||
if(subscriber.getEmail().equals(email)) {
|
|
||||||
subscriberIterator.remove();
|
|
||||||
communitySubscriberDAO.save(communitySubscribers);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.POST)
|
|
||||||
public CommunitySubscribers addSubscriberInCommunityByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody Subscriber subscriber) throws ContentNotFoundException {
|
|
||||||
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid);
|
|
||||||
if(communitySubscribers == null){
|
|
||||||
throw new ContentNotFoundException("Community Subscribers not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
Subscriber savedSubscriber = subscriberDAO.findByEmail(subscriber.getEmail());
|
|
||||||
if(savedSubscriber==null){
|
|
||||||
savedSubscriber = subscriberDAO.save(subscriber);
|
|
||||||
}
|
|
||||||
for(Subscriber sub:communitySubscribers.getSubscribers()){
|
|
||||||
if(sub.getEmail().equals(subscriber.getEmail())){
|
|
||||||
//already subscribed
|
|
||||||
return communitySubscribers;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//not subscribed yet
|
|
||||||
communitySubscribers.getSubscribers().add(savedSubscriber);
|
|
||||||
return communitySubscriberDAO.save(communitySubscribers);
|
|
||||||
|
|
||||||
}
|
|
||||||
@RequestMapping(value = "/community/{pid}/subscribers/delete", method = RequestMethod.POST)
|
|
||||||
public CommunitySubscribers deleteSubscriberFromCommunityByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody List<String> emails) throws ContentNotFoundException {
|
|
||||||
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid);
|
|
||||||
if(communitySubscribers == null){
|
|
||||||
throw new ContentNotFoundException("Community Subscribers not found");
|
|
||||||
}
|
|
||||||
List<Subscriber> list = new ArrayList<>();
|
|
||||||
for(Subscriber s:communitySubscribers.getSubscribers()){
|
|
||||||
if(emails.indexOf(s.getEmail())==-1){
|
|
||||||
list.add(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
communitySubscribers.setSubscribers(list);
|
|
||||||
return communitySubscriberDAO.save(communitySubscribers);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/subscriber/communities", method = RequestMethod.GET)
|
|
||||||
public List<String> getCommunitiesPerSubcriber(//@RequestParam(value="email", required = true) String email,
|
|
||||||
@RequestHeader("X-XSRF-TOKEN") String token) {
|
|
||||||
AuthorizationUtils helper = new AuthorizationUtils();
|
|
||||||
helper.setUserInfoUrl(securityConfig.getUserInfoUrl());
|
|
||||||
UserInfo userInfo = helper.getUserInfo(token);
|
|
||||||
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
|
|
||||||
if (userInfo != null) {
|
|
||||||
String email = userInfo.getEmail();
|
|
||||||
List<CommunitySubscribers> communitySubscribers = communitySubscriberDAO.findAll();
|
|
||||||
|
|
||||||
for (CommunitySubscribers s : communitySubscribers) {
|
|
||||||
for (Subscriber sub : s.getSubscribers()) {
|
|
||||||
if (sub.getEmail().equals(email)) {
|
|
||||||
list.add(s.getPid());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue