SubscriptionService.java

  1. package de.dlr.shepard.neo4Core.services;

  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;

  5. import org.neo4j.ogm.cypher.ComparisonOperator;
  6. import org.neo4j.ogm.cypher.Filter;

  7. import de.dlr.shepard.neo4Core.dao.SubscriptionDAO;
  8. import de.dlr.shepard.neo4Core.dao.UserDAO;
  9. import de.dlr.shepard.neo4Core.entities.Subscription;
  10. import de.dlr.shepard.neo4Core.io.SubscriptionIO;
  11. import de.dlr.shepard.util.DateHelper;
  12. import de.dlr.shepard.util.RequestMethod;
  13. import lombok.extern.slf4j.Slf4j;

  14. @Slf4j
  15. public class SubscriptionService {
  16.     private SubscriptionDAO subscriptionDAO = new SubscriptionDAO();
  17.     private UserDAO userDAO = new UserDAO();
  18.     private DateHelper dateHelper = new DateHelper();

  19.     /**
  20.      * Creates an Subscription and stores it in Neo4J
  21.      *
  22.      * @param subscription to be stored
  23.      * @param username     of the related user
  24.      * @return the stored Subscription with the auto generated id
  25.      */
  26.     public Subscription createSubscription(SubscriptionIO subscription, String username) {
  27.         var user = userDAO.find(username);

  28.         var toCreate = new Subscription();
  29.         toCreate.setCallbackURL(subscription.getCallbackURL());
  30.         toCreate.setCreatedAt(dateHelper.getDate());
  31.         toCreate.setCreatedBy(user);
  32.         toCreate.setName(subscription.getName());
  33.         toCreate.setRequestMethod(subscription.getRequestMethod());
  34.         toCreate.setSubscribedURL(subscription.getSubscribedURL());
  35.         return subscriptionDAO.createOrUpdate(toCreate);
  36.     }

  37.     /**
  38.      * Searches the neo4j database for an Subscription
  39.      *
  40.      * @param id identifies the searched Subscription
  41.      * @return the Subscription with the given id
  42.      */
  43.     public Subscription getSubscription(long id) {
  44.         Subscription subscription = subscriptionDAO.find(id);
  45.         if (subscription == null) {
  46.             log.error("Subscription with id {} is null", id);
  47.             return null;
  48.         }
  49.         return subscription;
  50.     }

  51.     /**
  52.      * Updates an Subscription with new attributes
  53.      *
  54.      * @param id           identifies the subscription
  55.      * @param subscription contains the new attributes.
  56.      * @return the old Subscription with updated attributes.
  57.      */
  58.     public Subscription updateSubscription(long id, SubscriptionIO subscription) {
  59.         var old = getSubscription(id);

  60.         old.setCallbackURL(subscription.getCallbackURL());
  61.         old.setName(subscription.getName());
  62.         old.setRequestMethod(subscription.getRequestMethod());
  63.         old.setSubscribedURL(subscription.getSubscribedURL());
  64.         return subscriptionDAO.createOrUpdate(old);
  65.     }

  66.     /**
  67.      * Delete the given subscription
  68.      *
  69.      * @param subscriptionId identifies the Subscription to be deleted
  70.      * @return a boolean to identify if the Subscription was successfully removed
  71.      */
  72.     public boolean deleteSubscription(long subscriptionId) {
  73.         return subscriptionDAO.delete(subscriptionId);
  74.     }

  75.     /**
  76.      * Searches the database for subscriptions.
  77.      *
  78.      * @param username The name of the user
  79.      * @return a List of Subscriptions
  80.      */
  81.     public List<Subscription> getAllSubscriptions(String username) {
  82.         var user = userDAO.find(username);
  83.         if (user != null) {
  84.             return user.getSubscriptions();
  85.         }
  86.         return Collections.emptyList();
  87.     }

  88.     /**
  89.      * Return all subscriptions matching a given request.
  90.      *
  91.      * @param method The request method to match against
  92.      * @return A list of matching subscriptions
  93.      */
  94.     public List<Subscription> getMatchingSubscriptions(RequestMethod method) {
  95.         Filter methodFilter = new Filter("requestMethod", ComparisonOperator.EQUALS, method);
  96.         var subscriptions = subscriptionDAO.findMatching(methodFilter);
  97.         var result = new ArrayList<Subscription>(subscriptions.size());
  98.         subscriptions.forEach(result::add);
  99.         return result;
  100.     }
  101. }