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