130 lines
4.1 KiB
Java
130 lines
4.1 KiB
Java
package eu.dnetlib.lbs.controllers;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.StreamSupport;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import eu.dnetlib.lbs.LiteratureBrokerServiceConfiguration;
|
|
import eu.dnetlib.lbs.cron.ScheduledActions;
|
|
import eu.dnetlib.lbs.elasticsearch.Event;
|
|
import eu.dnetlib.lbs.elasticsearch.EventRepository;
|
|
import eu.dnetlib.lbs.elasticsearch.EventStatsManager;
|
|
import eu.dnetlib.lbs.elasticsearch.EventStatsManager.BrowseEntry;
|
|
import eu.dnetlib.lbs.subscriptions.Subscription;
|
|
import eu.dnetlib.lbs.subscriptions.SubscriptionRepository;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/events")
|
|
@Api(tags = LiteratureBrokerServiceConfiguration.TAG_EVENTS)
|
|
public class EventsController extends AbstractLbsController {
|
|
|
|
@Autowired
|
|
private EventRepository eventRepository;
|
|
|
|
@Autowired
|
|
private SubscriptionRepository subscriptionRepo;
|
|
|
|
@Autowired
|
|
private EventStatsManager eventStatsManager;
|
|
|
|
@Autowired(required = false)
|
|
private ScheduledActions scheduledActions;
|
|
|
|
@ApiOperation("Return an event by ID")
|
|
@GetMapping("/{id}")
|
|
public Event getEvent(@PathVariable final String id) {
|
|
return eventRepository.findById(id).get();
|
|
}
|
|
|
|
@ApiOperation("Delete an event by ID")
|
|
@DeleteMapping("/{id}")
|
|
public void deleteEvent(@PathVariable final String id) {
|
|
eventRepository.deleteById(id);
|
|
}
|
|
|
|
@ApiOperation("Save an event by ID")
|
|
@PostMapping("/{id}")
|
|
public Event saveEvent(@RequestBody final Event event) {
|
|
return eventRepository.save(event);
|
|
}
|
|
|
|
@ApiOperation("Return a page of events")
|
|
@GetMapping("/list/{page}/{pageSize}")
|
|
public List<Event> events(
|
|
@PathVariable final int page,
|
|
@PathVariable final int pageSize) {
|
|
return Lists.newArrayList(eventRepository.findAll(PageRequest.of(page, pageSize)));
|
|
}
|
|
|
|
@ApiOperation("Return a page of events by topic")
|
|
@GetMapping("/byTopic/{page}/{pageSize}")
|
|
public List<Event> eventsByTopic(
|
|
@PathVariable final int page,
|
|
@PathVariable final int pageSize,
|
|
@RequestParam final String topic) {
|
|
return Lists.newArrayList(eventRepository.findByTopic(topic, PageRequest.of(page, pageSize)));
|
|
}
|
|
|
|
@ApiOperation("Delete all the events")
|
|
@DeleteMapping("/all")
|
|
public Map<String, Object> clearEvents() {
|
|
eventRepository.deleteAll();
|
|
final Map<String, Object> res = new HashMap<>();
|
|
res.put("deleted", true);
|
|
return res;
|
|
}
|
|
|
|
@ApiOperation("Delete the expired events")
|
|
@DeleteMapping("/expired")
|
|
public Map<String, Long> deleteExpiredEvents() {
|
|
final Map<String, Long> res = new HashMap<>();
|
|
if (scheduledActions != null) {
|
|
res.put("deleted", scheduledActions.deleteExpiredEvents());
|
|
} else {
|
|
res.put("error (deletion of expired events is disabled)", 0l);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
@ApiOperation("Return the topics of the indexed events (all)")
|
|
@GetMapping("/topics/all")
|
|
public List<BrowseEntry> browseTopics() {
|
|
return eventStatsManager.browseTopics();
|
|
}
|
|
|
|
@ApiOperation("Return the topics of the indexed events (only with subscriptions)")
|
|
@GetMapping("/topics/withSubscriptions")
|
|
public List<BrowseEntry> browseTopicsWithSubscriptions() {
|
|
|
|
final Iterable<Subscription> iter = subscriptionRepo.findAll();
|
|
|
|
final Set<String> validTopics = StreamSupport.stream(iter.spliterator(), false)
|
|
.map(Subscription::getTopic)
|
|
.collect(Collectors.toSet());
|
|
|
|
return this.browseTopics()
|
|
.stream()
|
|
.filter(e -> validTopics.contains(e.getValue()))
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
}
|