1 package de.tivsource.page.admin.backup;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.text.SimpleDateFormat;
8 import java.util.Iterator;
9
10 import org.apache.logging.log4j.LogManager;
11 import org.apache.logging.log4j.Logger;
12
13 import de.tivsource.page.dao.picture.PictureDaoLocal;
14 import de.tivsource.page.entity.enumeration.Language;
15 import de.tivsource.page.entity.picture.Picture;
16 import de.tivsource.page.enumeration.UrlType;
17
18 public class BackupPicture {
19
20
21
22
23 private static final Logger LOGGER = LogManager.getLogger(BackupPicture.class);
24
25 private static PictureDaoLocal pictureDaoLocal;
26
27 public static void setPictureDaoLocal(PictureDaoLocal pictureDaoLocal) {
28 BackupPicture.pictureDaoLocal = pictureDaoLocal;
29 }
30
31 public static File getBackupFile() throws IOException {
32 LOGGER.info("getBackupFile() aufgerufen.");
33
34 File backupFile = new File("/tmp/picture.csv");
35 FileWriter backupFileWriter = new FileWriter(backupFile);
36 BufferedWriter backupFileWriterOut = new BufferedWriter(backupFileWriter);
37
38
39 backupFileWriterOut.write("[Format Definition] => uuid|uuid(de)|name(de)|description(de)|keywords(de)|uuid(en)|name(en)|description(en)|keywords(en)|pictureUrls|orderNumber|visible|gallery|created|modified|modifiedBy|modifiedAddress|");
40
41 Iterator<Picture> pictureIterator = pictureDaoLocal.findAll(0, pictureDaoLocal.countAll()).iterator();
42 while(pictureIterator.hasNext()) {
43 Picture next = pictureIterator.next();
44 backupFileWriterOut.write("\n");
45 backupFileWriterOut.write(convertToCsvLine(next));
46 }
47 backupFileWriterOut.close();
48 backupFileWriter.close();
49
50 return backupFile;
51 }
52
53 private static String convertToCsvLine(Picture next) {
54
55 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
56
57
58
59
60
61
62 StringBuffer nextString = new StringBuffer();
63
64
65 nextString.append(next.getUuid());
66 nextString.append("|");
67
68
69 nextString.append(next.getDescriptionMap().get(Language.DE).getUuid());
70 nextString.append("|");
71
72 nextString.append(next.getDescriptionMap().get(Language.DE).getName());
73 nextString.append("|");
74
75 nextString.append(next.getDescriptionMap().get(Language.DE).getDescription());
76 nextString.append("|");
77
78 nextString.append(next.getDescriptionMap().get(Language.DE).getKeywords());
79 nextString.append("|");
80
81 nextString.append(next.getDescriptionMap().get(Language.EN).getUuid());
82 nextString.append("|");
83
84 nextString.append(next.getDescriptionMap().get(Language.EN).getName());
85 nextString.append("|");
86
87 nextString.append(next.getDescriptionMap().get(Language.EN).getDescription());
88 nextString.append("|");
89
90 nextString.append(next.getDescriptionMap().get(Language.EN).getKeywords());
91 nextString.append("|");
92
93
94 nextString.append(next.getPictureUrls().get(UrlType.THUMBNAIL).getUuid());
95 nextString.append(",");
96 nextString.append(next.getPictureUrls().get(UrlType.THUMBNAIL).getUrl());
97 nextString.append(",");
98 nextString.append("THUMBNAIL");
99 nextString.append(";");
100
101 nextString.append(next.getPictureUrls().get(UrlType.NORMAL).getUuid());
102 nextString.append(",");
103 nextString.append(next.getPictureUrls().get(UrlType.NORMAL).getUrl());
104 nextString.append(",");
105 nextString.append("NORMAL");
106 nextString.append(";");
107
108 nextString.append(next.getPictureUrls().get(UrlType.LARGE).getUuid());
109 nextString.append(",");
110 nextString.append(next.getPictureUrls().get(UrlType.LARGE).getUrl());
111 nextString.append(",");
112 nextString.append("LARGE");
113 nextString.append(";");
114
115 nextString.append(next.getPictureUrls().get(UrlType.FULL).getUuid());
116 nextString.append(",");
117 nextString.append(next.getPictureUrls().get(UrlType.FULL).getUrl());
118 nextString.append(",");
119 nextString.append("FULL");
120 nextString.append(";");
121 nextString.append("|");
122
123 nextString.append(next.getOrderNumber().toString());
124 nextString.append("|");
125
126 nextString.append(next.getVisible().toString());
127 nextString.append("|");
128
129 nextString.append(next.getGallery().getUuid());
130 nextString.append("|");
131
132 nextString.append(simpleDateFormat.format(next.getCreated()));
133 nextString.append("|");
134
135 nextString.append(simpleDateFormat.format(next.getModified()));
136 nextString.append("|");
137
138 nextString.append(next.getModifiedBy());
139 nextString.append("|");
140
141 nextString.append(next.getModifiedAddress());
142 nextString.append("|");
143
144 return nextString.toString();
145 }
146
147 }