1 package de.dlr.shepard.neo4Core.services;
2
3 import java.io.InputStream;
4 import java.text.SimpleDateFormat;
5 import java.util.List;
6
7 import de.dlr.shepard.mongoDB.FileService;
8 import de.dlr.shepard.mongoDB.NamedInputStream;
9 import de.dlr.shepard.mongoDB.ShepardFile;
10 import de.dlr.shepard.neo4Core.dao.FileContainerDAO;
11 import de.dlr.shepard.neo4Core.dao.PermissionsDAO;
12 import de.dlr.shepard.neo4Core.dao.UserDAO;
13 import de.dlr.shepard.neo4Core.entities.FileContainer;
14 import de.dlr.shepard.neo4Core.entities.Permissions;
15 import de.dlr.shepard.neo4Core.io.FileContainerIO;
16 import de.dlr.shepard.util.DateHelper;
17 import de.dlr.shepard.util.PermissionType;
18 import de.dlr.shepard.util.QueryParamHelper;
19 import lombok.extern.slf4j.Slf4j;
20
21 @Slf4j
22 public class FileContainerService implements IContainerService<FileContainer, FileContainerIO> {
23
24 private FileContainerDAO fileContainerDAO = new FileContainerDAO();
25 private PermissionsDAO permissionsDAO = new PermissionsDAO();
26 private UserDAO userDAO = new UserDAO();
27 private DateHelper dateHelper = new DateHelper();
28 private FileService fileService = new FileService();
29
30
31
32
33
34
35
36
37 @Override
38 public FileContainer createContainer(FileContainerIO fileContainerIO, String username) {
39 var user = userDAO.find(username);
40 var toCreate = new FileContainer();
41 toCreate.setCreatedAt(dateHelper.getDate());
42 toCreate.setCreatedBy(user);
43 toCreate.setMongoId(fileService.createFileContainer());
44 toCreate.setName(fileContainerIO.getName());
45
46 var created = fileContainerDAO.createOrUpdate(toCreate);
47 permissionsDAO.createOrUpdate(new Permissions(created, user, PermissionType.Private));
48 return created;
49 }
50
51
52
53
54
55
56
57 @Override
58 public FileContainer getContainer(long id) {
59 FileContainer fileContainer = fileContainerDAO.find(id);
60 if (fileContainer == null || fileContainer.isDeleted()) {
61 log.error("File Container with id {} is null or deleted", id);
62 return null;
63 }
64 return fileContainer;
65 }
66
67
68
69
70
71
72
73
74 @Override
75 public List<FileContainer> getAllContainers(QueryParamHelper params, String username) {
76 var containers = fileContainerDAO.findAllFileContainers(params, username);
77 return containers;
78 }
79
80
81
82
83
84
85
86
87 @Override
88 public boolean deleteContainer(long fileContainerId, String username) {
89 var user = userDAO.find(username);
90 FileContainer fileContainer = fileContainerDAO.find(fileContainerId);
91 if (fileContainer == null) {
92 return false;
93 }
94 String mongoid = fileContainer.getMongoId();
95 fileContainer.setDeleted(true);
96 fileContainer.setUpdatedAt(dateHelper.getDate());
97 fileContainer.setUpdatedBy(user);
98 fileContainerDAO.createOrUpdate(fileContainer);
99 return fileService.deleteFileContainer(mongoid);
100 }
101
102
103
104
105
106
107
108
109 public NamedInputStream getFile(long fileContainerId, String oid) {
110 var container = fileContainerDAO.findLight(fileContainerId);
111 if (container == null || container.isDeleted()) {
112 log.error("File Container with id {} is null or deleted", fileContainerId);
113 return null;
114 }
115 var result = fileService.getPayload(container.getMongoId(), oid);
116 return result;
117 }
118
119
120
121
122
123
124
125
126
127 public ShepardFile createFile(long fileContainerId, String fileName, InputStream inputStream) {
128 var fileContainer = fileContainerDAO.find(fileContainerId);
129 if (fileContainer == null || fileContainer.isDeleted()) {
130 log.error("File Container with id {} is null or deleted", fileContainerId);
131 return null;
132 }
133 if (fileName == null || fileName.isBlank()) {
134 var sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
135 var dateStr = sdf.format(dateHelper.getDate());
136 fileName = "shepard-file-" + dateStr;
137 }
138 var result = fileService.createFile(fileContainer.getMongoId(), fileName, inputStream);
139 if (result == null) {
140 log.error("Failed to create shepard file");
141 return null;
142 }
143 fileContainer.addFile(result);
144 fileContainerDAO.createOrUpdate(fileContainer);
145 return result;
146 }
147
148
149
150
151
152
153
154
155 public boolean deleteFile(long fileContainerId, String oid) {
156 var container = fileContainerDAO.find(fileContainerId);
157 if (container == null || container.isDeleted())
158 return false;
159 var result = fileService.deleteFile(container.getMongoId(), oid);
160 if (result) {
161 var newFiles = container.getFiles().stream().filter(f -> !f.getOid().equals(oid)).toList();
162 container.setFiles(newFiles);
163 fileContainerDAO.createOrUpdate(container);
164 }
165 return result;
166 }
167
168 }