View Javadoc
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  	 * Creates a FileContainer and stores it in Neo4J
32  	 *
33  	 * @param fileContainerIO to be stored
34  	 * @param username        of the related user
35  	 * @return the created FileContainer
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  	 * Searches the FileContainer in Neo4j
53  	 *
54  	 * @param id identifies the searched FileContainer
55  	 * @return the FileContainer with matching id or null
56  	 */
57  	@Override
58  	public FileContainer getContainer(long id) {
59  		FileContainer fileContainer = fileContainerDAO.findByNeo4jId(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  	 * Searches the database for all FileContainers
69  	 *
70  	 * @param params   QueryParamsHelper
71  	 * @param username the name of the user
72  	 * @return a list of FileContainers
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  	 * Deletes a FileContainer in Neo4j
82  	 *
83  	 * @param fileContainerId identifies the FileContainer
84  	 * @param username        the deleting user
85  	 * @return a boolean to determine if FileContainer was successfully deleted
86  	 */
87  	@Override
88  	public boolean deleteContainer(long fileContainerId, String username) {
89  		var user = userDAO.find(username);
90  		FileContainer fileContainer = fileContainerDAO.findByNeo4jId(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 	 * Get file payload
104 	 *
105 	 * @param fileContainerId The container to get the payload from
106 	 * @param oid             The specific file
107 	 * @return a NamedInputStream
108 	 */
109 	public NamedInputStream getFile(long fileContainerId, String oid) {
110 		var container = fileContainerDAO.findLightByNeo4jId(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 	 * Create a new file
121 	 *
122 	 * @param fileContainerId identifies the file container
123 	 * @param fileName        the name of the new file
124 	 * @param inputStream     the file itself
125 	 * @return The newly created file
126 	 */
127 	public ShepardFile createFile(long fileContainerId, String fileName, InputStream inputStream) {
128 		var fileContainer = fileContainerDAO.findByNeo4jId(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 	 * Delete one file
150 	 *
151 	 * @param fileContainerId The container to get the payload from
152 	 * @param oid             The specific file
153 	 * @return Whether the deletion was successful or not
154 	 */
155 	public boolean deleteFile(long fileContainerId, String oid) {
156 		var container = fileContainerDAO.findByNeo4jId(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 }