1 package de.tivsource.page.admin.actions.others.picture;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.Map;
8
9 import org.apache.logging.log4j.LogManager;
10 import org.apache.logging.log4j.Logger;
11 import org.apache.struts2.convention.annotation.Action;
12 import org.apache.struts2.convention.annotation.Actions;
13 import org.apache.struts2.convention.annotation.Result;
14 import org.apache.struts2.tiles.annotation.TilesDefinition;
15 import org.apache.struts2.tiles.annotation.TilesDefinitions;
16 import org.apache.struts2.tiles.annotation.TilesPutAttribute;
17
18 import de.tivsource.ejb3plugin.InjectEJB;
19 import de.tivsource.page.admin.actions.EmptyAction;
20 import de.tivsource.page.dao.picture.PictureDaoLocal;
21 import de.tivsource.page.entity.picture.Picture;
22 import de.tivsource.page.entity.picture.PictureUrl;
23 import de.tivsource.page.enumeration.UrlType;
24
25
26
27
28
29
30 @TilesDefinitions({
31 @TilesDefinition(name="pictureDeleteError", extend = "adminTemplate", putAttributes = {
32 @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/others.jsp"),
33 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/picture/error.jsp")
34 }),
35 @TilesDefinition(name="pictureReferences", extend = "adminTemplate", putAttributes = {
36 @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/others.jsp"),
37 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/picture/references.jsp")
38 })
39 })
40 public class DeleteAction extends EmptyAction {
41
42
43
44
45 private static final long serialVersionUID = -2086102480376636224L;
46
47
48
49
50 private static final Logger LOGGER = LogManager.getLogger(DeleteAction.class);
51
52 @InjectEJB(name="PictureDao")
53 private PictureDaoLocal pictureDaoLocal;
54
55 private Picture picture;
56
57 public Picture getPicture() {
58 return picture;
59 }
60
61 public void setPicture(Picture picture) {
62 this.picture = picture;
63 }
64
65 @Override
66 @Actions({
67 @Action(
68 value = "delete",
69 results = {
70 @Result(name = "success", type = "redirectAction", location = "index.html"),
71 @Result(name = "input", type="tiles", location = "pictureDeleteForm"),
72 @Result(name = "error", type="tiles", location = "pictureDeleteError"),
73 @Result(name = "references", type="tiles", location = "pictureReferences")
74 }
75 )
76 })
77 public String execute() throws Exception {
78 LOGGER.info("execute() aufgerufen.");
79
80 if(picture != null) {
81 if(!pictureDaoLocal.hasReferences(picture.getUuid())) {
82 Picture dbPicture = pictureDaoLocal.findByUuid(picture.getUuid());
83 if(dbPicture.getPictureUrls() != null && dbPicture.getPictureUrls().size() > 0) {
84 deletePictures(dbPicture.getPictureUrls());
85 }
86 pictureDaoLocal.delete(dbPicture);
87 return SUCCESS;
88 } else {
89 return "references";
90 }
91 }
92 else {
93 return ERROR;
94 }
95
96
97 }
98
99 private static void deletePictures(Map<UrlType, PictureUrl> pictureUrls) throws IOException {
100 String picturePath = "/var/www/html/pictures/";
101 String pathFULL = picturePath + "FULL/" + pictureUrls.get(UrlType.FULL).getUrl();
102 deleteFile(pathFULL);
103 String pathLARGE = picturePath + "LARGE/" + pictureUrls.get(UrlType.LARGE).getUrl();
104 deleteFile(pathLARGE);
105 String pathNORMAL = picturePath + "NORMAL/" + pictureUrls.get(UrlType.NORMAL).getUrl();
106 deleteFile(pathNORMAL);
107 String pathTHUMBNAIL = picturePath + "THUMBNAIL/" + pictureUrls.get(UrlType.THUMBNAIL).getUrl();
108 deleteFile(pathTHUMBNAIL);
109 }
110
111 private static void deleteFile(String source) throws IOException {
112 Path filePath = Paths.get(source);
113 if (Files.exists(filePath) && !Files.isDirectory(filePath)
114 && Files.isRegularFile(filePath)) {
115
116 Files.delete(filePath);
117 LOGGER.info("Datei: "+ source +" erfolgreich gelöscht");
118 } else {
119 LOGGER.info("Konnte die Datei: "+ source +" nicht löschen.");
120 }
121 }
122
123 }