Commit 022fe56a authored by Piotr Gawron's avatar Piotr Gawron
Browse files

removing background should remove images

parent ca567797
Pipeline #46598 passed with stage
in 19 minutes and 30 seconds
......@@ -4,6 +4,7 @@ minerva (16.0.0~beta.2) stable; urgency=medium
* Bug fix: problem with export map with submaps fixed (#1540)
* Bug fix: removing user removed backgrounds in projects created by user
(#1527)
* Bug fix: removing background did not remove images from server (#1526)
-- Piotr Gawron <piotr.gawron@uni.lu> Fri, 3 Sep 2021 11:00:00 +0200
......
package lcsb.mapviewer.api.projects;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Hibernate;
......@@ -22,14 +36,25 @@ import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.converter.Converter;
import lcsb.mapviewer.converter.zip.*;
import lcsb.mapviewer.converter.zip.GlyphZipEntryFile;
import lcsb.mapviewer.converter.zip.ImageZipEntryFile;
import lcsb.mapviewer.converter.zip.LayoutZipEntryFile;
import lcsb.mapviewer.converter.zip.ModelZipEntryFile;
import lcsb.mapviewer.converter.zip.ZipEntryFile;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.ProjectLogEntry;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.graphics.MapCanvasType;
import lcsb.mapviewer.model.map.*;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.OverviewImage;
import lcsb.mapviewer.model.map.OverviewImageLink;
import lcsb.mapviewer.model.map.OverviewLink;
import lcsb.mapviewer.model.map.OverviewModelLink;
import lcsb.mapviewer.model.map.OverviewSearchLink;
import lcsb.mapviewer.model.map.layout.ProjectBackground;
import lcsb.mapviewer.model.map.layout.ProjectBackgroundImageLayer;
import lcsb.mapviewer.model.map.model.SubmodelType;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.security.PrivilegeType;
......@@ -38,8 +63,13 @@ import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
import lcsb.mapviewer.persist.dao.map.species.ElementProperty;
import lcsb.mapviewer.services.*;
import lcsb.mapviewer.services.interfaces.*;
import lcsb.mapviewer.services.ObjectExistsException;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IElementService;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.interfaces.IReactionService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.utils.CreateProjectParams;
@Transactional
......@@ -79,13 +109,16 @@ public class ProjectRestImpl extends BaseRestImpl {
private UploadedFileEntryDao uploadedFileEntryDao;
private ServletContext context;
public ProjectRestImpl(IProjectService projectService,
ProjectDao projectDao,
UploadedFileEntryDao uploadedFileEntryDao,
IUserService userService,
MeSHParser meshParser,
IElementService elementService,
IReactionService reactionService) {
IReactionService reactionService,
ServletContext context) {
this.projectService = projectService;
this.projectDao = projectDao;
this.meshParser = meshParser;
......@@ -93,6 +126,7 @@ public class ProjectRestImpl extends BaseRestImpl {
this.uploadedFileEntryDao = uploadedFileEntryDao;
this.elementService = elementService;
this.reactionService = reactionService;
this.context = context;
}
public Map<String, Object> getProject(String projectId) throws ObjectNotFoundException {
......@@ -780,6 +814,14 @@ public class ProjectRestImpl extends BaseRestImpl {
Project project = getProjectByProjectId(projectId);
for (ProjectBackground projectBackground : project.getProjectBackgrounds()) {
if (projectBackground.getId() == backgroundId) {
for (ProjectBackgroundImageLayer layer : projectBackground.getProjectBackgroundImageLayer()) {
String path = context.getRealPath("/") + "/../map_images/" + project.getDirectory() + "/" + layer.getDirectory();
try {
FileUtils.deleteDirectory(new File(path));
} catch (IOException e) {
logger.error("Problem with removing background directory", e);
}
}
project.removeProjectBackground(projectBackground);
projectDao.update(project);
return okStatus();
......
......@@ -8,6 +8,7 @@ import javax.mail.MessagingException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -117,6 +118,9 @@ public class ProjectBackgroundService implements IProjectBackgroundService {
public List<ProjectBackground> getProjectBackgroundsByProject(Project project) {
List<ProjectBackground> result = new ArrayList<>();
List<ProjectBackground> backgrounds = projectBackgroundDao.getProjectBackgroundsByProject(project);
for (ProjectBackground projectBackground : backgrounds) {
Hibernate.initialize(projectBackground.getProjectBackgroundImageLayer());
}
result.addAll(backgrounds);
result.sort(ProjectBackground.ID_COMPARATOR);
return result;
......
package lcsb.mapviewer.web;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.restdocs.request.*;
import org.springframework.restdocs.request.ParameterDescriptor;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.restdocs.request.RequestParametersSnippet;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
......@@ -43,6 +60,7 @@ import lcsb.mapviewer.model.security.Privilege;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.services.interfaces.IProjectBackgroundService;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.web.serialization.ProjectBackgroundUpdateMixIn;
......@@ -61,6 +79,9 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
@Autowired
private IProjectService projectService;
@Autowired
private IProjectBackgroundService projectBackgroundService;
@Autowired
private ProjectDao projectDao;
......@@ -1051,20 +1072,46 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
@Test
public void testRemoveBackgroundById() throws Exception {
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
Project project = createProjectInSeparateThread(TEST_PROJECT);
RequestBuilder request = delete("/projects/{projectId}/backgrounds/{backgroundId}", TEST_PROJECT,
project.getProjectBackgrounds().get(0).getId())
User admin = userService.getUserByLogin(BUILT_IN_TEST_ADMIN_LOGIN);
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
admin);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
waitForProjectToFinishLoading(TEST_PROJECT);
Project project = projectService.getProjectByProjectId(TEST_PROJECT);
List<ProjectBackground> backgrounds = projectBackgroundService.getProjectBackgroundsByProject(project);
ProjectBackground background = backgrounds.get(0);
String path = "src/main/webapp/../map_images/" + project.getDirectory() + "/" + background.getProjectBackgroundImageLayer().iterator().next().getDirectory();
assertTrue(new File(path).exists());
request = delete("/projects/{projectId}/backgrounds/{backgroundId}", TEST_PROJECT,
backgrounds.get(0).getId())
.session(session);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andDo(document("projects/project_backgrounds/delete_background",
backgroundPathParameters(),
responseFields()));
assertFalse(new File(path).exists());
int backgrounds = callInSeparateThread(() -> {
int backgroundCount = callInSeparateThread(() -> {
return projectDao.getById(project.getId()).getProjectBackgrounds().size();
});
assertEquals(project.getProjectBackgrounds().size() - 1, backgrounds);
assertEquals(backgrounds.size() - 1, backgroundCount);
}
private PathParametersSnippet projectPathParameters() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment