View Javadoc
1   package de.dlr.shepard.common.subscription.services;
2   
3   import de.dlr.shepard.auth.users.services.UserService;
4   import de.dlr.shepard.common.exceptions.InvalidAuthException;
5   import de.dlr.shepard.common.exceptions.InvalidPathException;
6   import de.dlr.shepard.common.subscription.daos.SubscriptionDAO;
7   import de.dlr.shepard.common.subscription.entities.Subscription;
8   import de.dlr.shepard.common.subscription.io.SubscriptionIO;
9   import de.dlr.shepard.common.util.DateHelper;
10  import de.dlr.shepard.common.util.RequestMethod;
11  import jakarta.enterprise.context.RequestScoped;
12  import jakarta.inject.Inject;
13  import java.util.ArrayList;
14  import java.util.List;
15  import org.neo4j.ogm.cypher.ComparisonOperator;
16  import org.neo4j.ogm.cypher.Filter;
17  
18  @RequestScoped
19  public class SubscriptionService {
20  
21    @Inject
22    SubscriptionDAO subscriptionDAO;
23  
24    @Inject
25    UserService userService;
26  
27    @Inject
28    DateHelper dateHelper;
29  
30    /**
31     * Creates an Subscription and stores it in Neo4J
32     *
33     * @param subscription to be stored
34     * @param username of the related user
35     * @return the stored Subscription with the auto generated id
36     * @throws InvalidPathException if the user of this name does not exist
37     * @throws InvalidAuthException if the username does not match the user making the request
38     */
39    public Subscription createSubscription(SubscriptionIO subscription, String username) {
40      var user = userService.getUser(username);
41      userService.assertCurrentUserEquals(username);
42  
43      var toCreate = new Subscription();
44      toCreate.setCallbackURL(subscription.getCallbackURL());
45      toCreate.setCreatedAt(dateHelper.getDate());
46      toCreate.setCreatedBy(user);
47      toCreate.setName(subscription.getName());
48      toCreate.setRequestMethod(subscription.getRequestMethod());
49      toCreate.setSubscribedURL(subscription.getSubscribedURL());
50      return subscriptionDAO.createOrUpdate(toCreate);
51    }
52  
53    /**
54     * Searches the neo4j database for an Subscription
55     *
56     * @param id identifies the searched Subscription
57     * @param username of the related user
58     * @return the Subscription with the given id
59     * @throws InvalidPathException if the subscription or the user of this name does not exist
60     * @throws InvalidAuthException if the username does not match the user making the request or the subscription does not belong to the user
61     */
62    public Subscription getSubscription(long id, String username) {
63      userService.getUser(username);
64      userService.assertCurrentUserEquals(username);
65  
66      Subscription subscription = subscriptionDAO.findByNeo4jId(id);
67      if (subscription == null) {
68        throw new InvalidPathException("ID ERROR - Subscription does not exist");
69      }
70      if (!subscription.getCreatedBy().getUsername().equals(username)) {
71        throw new InvalidAuthException("You do not have permissions for this Subscription.");
72      }
73      return subscription;
74    }
75  
76    /**
77     * Delete the given subscription
78     *
79     * @param subscriptionId identifies the Subscription to be deleted
80     * @param username of the related user
81     * @return a boolean to identify if the Subscription was successfully removed
82     * @throws InvalidPathException if the subscription or the user of this name does not exist
83     * @throws InvalidAuthException if the username does not match the user making the request or the subscription does not belong to the user
84     */
85    public boolean deleteSubscription(long subscriptionId, String username) {
86      getSubscription(subscriptionId, username);
87  
88      return subscriptionDAO.deleteByNeo4jId(subscriptionId);
89    }
90  
91    /**
92     * Searches the database for subscriptions.
93     *
94     * @param username The name of the user
95     * @return a List of Subscriptions
96     * @throws InvalidPathException if the user of this name does not exist
97     * @throws InvalidAuthException if the username does not match the user making the request
98     */
99    public List<Subscription> getAllSubscriptions(String username) {
100     var user = userService.getUser(username);
101     userService.assertCurrentUserEquals(username);
102 
103     return user.getSubscriptions();
104   }
105 
106   /**
107    * Return all subscriptions matching a given request.
108    *
109    * @param method The request method to match against
110    * @return A list of matching subscriptions
111    */
112   public List<Subscription> getMatchingSubscriptions(RequestMethod method) {
113     Filter methodFilter = new Filter("requestMethod", ComparisonOperator.EQUALS, method);
114     var subscriptions = subscriptionDAO.findMatching(methodFilter);
115     var result = new ArrayList<Subscription>(subscriptions.size());
116     subscriptions.forEach(result::add);
117     return result;
118   }
119 }