View Javadoc
1   package de.dlr.shepard.neo4Core.services;
2   
3   import de.dlr.shepard.neo4Core.dao.UserDAO;
4   import de.dlr.shepard.neo4Core.entities.User;
5   import io.quarkus.logging.Log;
6   import jakarta.enterprise.context.RequestScoped;
7   import jakarta.inject.Inject;
8   
9   @RequestScoped
10  public class UserService {
11  
12    private UserDAO userDAO;
13  
14    UserService() {}
15  
16    @Inject
17    public UserService(UserDAO userDAO) {
18      this.userDAO = userDAO;
19    }
20  
21    /**
22     * Stores a new user in Neo4J.
23     *
24     * @param user The user to be stored.
25     * @return The created user
26     */
27    public User createUser(User user) {
28      Log.infof("Create user %s", user);
29      return userDAO.createOrUpdate(user);
30    }
31  
32    /**
33     * Update a user in Neo4J. The user is created if it does not exist.
34     *
35     * @param user The user to be updated
36     * @return The updated user
37     */
38    public User updateUser(User user) {
39      User old = getUser(user.getUsername());
40      if (old == null) {
41        Log.infof("The user %s does not exist, creating...", user.getUsername());
42        return userDAO.createOrUpdate(user);
43      }
44  
45      String firstName = user.getFirstName() != null ? user.getFirstName() : old.getFirstName();
46      String lastName = user.getLastName() != null ? user.getLastName() : old.getLastName();
47      String email = user.getEmail() != null ? user.getEmail() : old.getEmail();
48  
49      if (!firstName.equals(old.getFirstName()) || !lastName.equals(old.getLastName()) || !email.equals(old.getEmail())) {
50        old.setFirstName(firstName);
51        old.setLastName(lastName);
52        old.setEmail(email);
53        Log.infof("Update user %s", old);
54        return userDAO.createOrUpdate(old);
55      }
56  
57      return old;
58    }
59  
60    /**
61     * Returns the user with the given name.
62     *
63     * @param username of the user to be returned.
64     * @return The requested user.
65     */
66    public User getUser(String username) {
67      return userDAO.find(username);
68    }
69  }