View Javadoc
1   package de.dlr.shepard.data.file.services;
2   
3   import de.dlr.shepard.auth.permission.services.PermissionsService;
4   import de.dlr.shepard.auth.users.entities.User;
5   import de.dlr.shepard.auth.users.services.UserService;
6   import de.dlr.shepard.common.exceptions.InvalidAuthException;
7   import de.dlr.shepard.common.exceptions.InvalidPathException;
8   import de.dlr.shepard.common.mongoDB.NamedInputStream;
9   import de.dlr.shepard.common.util.DateHelper;
10  import de.dlr.shepard.common.util.PermissionType;
11  import de.dlr.shepard.common.util.QueryParamHelper;
12  import de.dlr.shepard.data.AbstractContainerService;
13  import de.dlr.shepard.data.file.daos.FileContainerDAO;
14  import de.dlr.shepard.data.file.entities.FileContainer;
15  import de.dlr.shepard.data.file.entities.ShepardFile;
16  import de.dlr.shepard.data.file.io.FileContainerIO;
17  import io.quarkus.logging.Log;
18  import jakarta.enterprise.context.RequestScoped;
19  import jakarta.inject.Inject;
20  import jakarta.ws.rs.InternalServerErrorException;
21  import java.io.InputStream;
22  import java.text.SimpleDateFormat;
23  import java.util.List;
24  
25  @RequestScoped
26  public class FileContainerService extends AbstractContainerService<FileContainer, FileContainerIO> {
27  
28    @Inject
29    FileContainerDAO fileContainerDAO;
30  
31    @Inject
32    UserService userService;
33  
34    @Inject
35    DateHelper dateHelper;
36  
37    @Inject
38    FileService fileService;
39  
40    @Inject
41    PermissionsService permissionsService;
42  
43    /**
44     * Creates a FileContainer and stores it in Neo4J
45     *
46     * @param fileContainerIO to be stored
47     * @param username        of the related user
48     * @return the created FileContainer
49     */
50    @Override
51    public FileContainer createContainer(FileContainerIO fileContainerIO) {
52      User user = userService.getCurrentUser();
53      FileContainer toCreate = new FileContainer();
54      toCreate.setCreatedAt(dateHelper.getDate());
55      toCreate.setCreatedBy(user);
56      toCreate.setMongoId(fileService.createFileContainer());
57      toCreate.setName(fileContainerIO.getName());
58  
59      var created = fileContainerDAO.createOrUpdate(toCreate);
60      permissionsService.createPermissions(created, user, PermissionType.Private);
61      return created;
62    }
63  
64    /**
65     * Gets the FileContainer
66     *
67     * @param id identifies the searched FileContainer
68     * @return the FileContainer with matching id or null
69     * @throws InvalidPathException if the file container cannot be found
70     * @throws InvalidAuthException if user has no read permission on container
71     */
72    @Override
73    public FileContainer getContainer(long id) {
74      FileContainer fileContainer = fileContainerDAO.findByNeo4jId(id);
75  
76      if (fileContainer == null || fileContainer.isDeleted()) {
77        String errorMsg = String.format("ID ERROR - File Container with id %s is null or deleted", id);
78        Log.errorf(errorMsg);
79        throw new InvalidPathException(errorMsg);
80      }
81      assertIsAllowedToReadContainer(id);
82      return fileContainer;
83    }
84  
85    /**
86     * Searches the database for all FileContainers
87     *
88     * @param params   QueryParamsHelper
89     * @param username the name of the user
90     * @return a list of FileContainers
91     */
92    @Override
93    public List<FileContainer> getAllContainers(QueryParamHelper params) {
94      User user = userService.getCurrentUser();
95      List<FileContainer> containers = fileContainerDAO.findAllFileContainers(params, user.getUsername());
96      return containers;
97    }
98  
99    /**
100    * Deletes a FileContainer in Neo4j
101    *
102    * @param fileContainerId identifies the FileContainer
103    * @param username        the deleting user
104    * @throws InvalidPathException if the file container cannot be found
105    * @throws InvalidAuthException if user has no write permission on container
106    */
107   @Override
108   public void deleteContainer(long fileContainerId) {
109     User user = userService.getCurrentUser();
110     FileContainer fileContainer = getContainer(fileContainerId);
111     assertIsAllowedToDeleteContainer(fileContainerId);
112 
113     String mongoId = fileContainer.getMongoId();
114     fileContainer.setDeleted(true);
115     fileContainer.setUpdatedAt(dateHelper.getDate());
116     fileContainer.setUpdatedBy(user);
117     fileContainerDAO.createOrUpdate(fileContainer);
118     fileService.deleteFileContainer(mongoId);
119   }
120 
121   /**
122    * Get file payload
123    *
124    * @param fileContainerId The container to get the payload from
125    * @param oid             The specific file
126    * @return a NamedInputStream
127    * @throws InvalidPathException if the file container cannot be found
128    * @throws InvalidAuthException if user has no read permission on container
129    */
130   public NamedInputStream getFile(long fileContainerId, String oid) {
131     FileContainer container = getContainer(fileContainerId);
132 
133     return fileService.getPayload(container.getMongoId(), oid);
134   }
135 
136   /**
137    * Create a new file
138    *
139    * @param fileContainerId identifies the file container
140    * @param fileName        the name of the new file
141    * @param inputStream     the file itself
142    * @return The newly created file
143    * @throws InternalServerErrorException if file creation fails
144    * @throws InvalidPathException if the file container cannot be found
145    * @throws InvalidAuthException if user has no read or write permission on container
146    */
147   public ShepardFile createFile(long fileContainerId, String fileName, InputStream inputStream) {
148     FileContainer fileContainer = getContainer(fileContainerId);
149     assertIsAllowedToEditContainer(fileContainerId);
150 
151     if (fileName == null || fileName.isBlank()) {
152       var sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
153       var dateStr = sdf.format(dateHelper.getDate());
154       fileName = "shepard-file-" + dateStr;
155     }
156 
157     ShepardFile result = fileService.createFile(fileContainer.getMongoId(), fileName, inputStream);
158 
159     fileContainer.addFile(result);
160     fileContainerDAO.createOrUpdate(fileContainer);
161     return result;
162   }
163 
164   /**
165    * Delete one file
166    *
167    * @param fileContainerId The container to get the payload from
168    * @param oid             The specific file
169    
170    */
171   public void deleteFile(long fileContainerId, String oid) {
172     FileContainer container = getContainer(fileContainerId);
173     assertIsAllowedToEditContainer(fileContainerId);
174 
175     fileService.deleteFile(container.getMongoId(), oid);
176 
177     List<ShepardFile> newFiles = container.getFiles().stream().filter(f -> !f.getOid().equals(oid)).toList();
178     container.setFiles(newFiles);
179     fileContainerDAO.createOrUpdate(container);
180   }
181 }