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.property.PropertyDaoLocal;
14 import de.tivsource.page.entity.property.Property;
15
16 public class BackupProperty {
17
18
19
20
21 private static final Logger LOGGER = LogManager.getLogger(BackupProperty.class);
22
23 private static final int MAX = 1500;
24
25 private static PropertyDaoLocal propertyDaoLocal;
26
27 public static void setPropertyDaoLocal(PropertyDaoLocal propertyDaoLocal) {
28 BackupProperty.propertyDaoLocal = propertyDaoLocal;
29 }
30
31 public static File getBackupFile() throws IOException {
32 LOGGER.info("getBackupFile() aufgerufen.");
33
34
35 File backupFile = new File("/tmp/property.csv");
36 FileWriter backupFileWriter = new FileWriter(backupFile);
37 BufferedWriter backupFileWriterOut = new BufferedWriter(backupFileWriter);
38
39
40 backupFileWriterOut.write("[Format Definition] => key|value|modified|modifiedBy|modifiedAddress|");
41
42 Iterator<Property> typeIterator = propertyDaoLocal.findAll(0, MAX).iterator();
43 while(typeIterator.hasNext()) {
44 Property next = typeIterator.next();
45 backupFileWriterOut.write("\n");
46 backupFileWriterOut.write(convertToCsvLine(next));
47 }
48 backupFileWriterOut.close();
49 backupFileWriter.close();
50
51 return backupFile;
52 }
53
54 private static String convertToCsvLine(Property next) {
55
56 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
57
58
59
60 StringBuffer nextString = new StringBuffer();
61
62 nextString.append(next.getKey());
63 nextString.append("|");
64
65 nextString.append(next.getValue());
66 nextString.append("|");
67
68 nextString.append(simpleDateFormat.format(next.getModified()));
69 nextString.append("|");
70
71 nextString.append(next.getModifiedBy());
72 nextString.append("|");
73
74 nextString.append(next.getModifiedAddress());
75 nextString.append("|");
76
77 return nextString.toString();
78 }
79
80 }