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.companion.CompanionGroupDaoLocal;
14 import de.tivsource.page.entity.companion.CompanionGroup;
15 import de.tivsource.page.entity.enumeration.Language;
16
17 public class BackupCompanionGroup {
18
19
20
21
22 private static final Logger LOGGER = LogManager.getLogger(BackupCompanionGroup.class);
23
24 private static CompanionGroupDaoLocal companionGroupDaoLocal;
25
26 public static void setCompanionGroupDaoLocal(CompanionGroupDaoLocal companionGroupDaoLocal) {
27 BackupCompanionGroup.companionGroupDaoLocal = companionGroupDaoLocal;
28 }
29
30 public static File getBackupFile() throws IOException {
31 LOGGER.info("getBackupFile() aufgerufen.");
32
33 File backupFile = new File("/tmp/companionGroup.csv");
34 FileWriter backupFileWriter = new FileWriter(backupFile);
35 BufferedWriter backupFileWriterOut = new BufferedWriter(backupFileWriter);
36
37
38 backupFileWriterOut.write("[Format Definition] => uuid|uuid(de)|name(de)|description(de)|keywords(de)|uuid(en)|name(en)|description(en)|keywords(en)|visible|created|modified|modifiedBy|modifiedAddress|picture|pictureOnPage|technical|orderNumber|");
39
40 Iterator<CompanionGroup> typeIterator = companionGroupDaoLocal.findAll(0, companionGroupDaoLocal.countAll()).iterator();
41 while(typeIterator.hasNext()) {
42 CompanionGroup next = typeIterator.next();
43 backupFileWriterOut.write("\n");
44 backupFileWriterOut.write(convertToCsvLine(next));
45 }
46 backupFileWriterOut.close();
47 backupFileWriter.close();
48
49 return backupFile;
50 }
51
52 private static String convertToCsvLine(CompanionGroup next) {
53
54 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
55
56
57
58
59
60
61
62 StringBuffer nextString = new StringBuffer();
63
64 nextString.append(next.getUuid());
65 nextString.append("|");
66
67
68 nextString.append(next.getDescriptionObject(Language.DE).getUuid());
69 nextString.append("|");
70
71 nextString.append(next.getDescriptionObject(Language.DE).getName());
72 nextString.append("|");
73
74 nextString.append(next.getDescriptionObject(Language.DE).getDescription());
75 nextString.append("|");
76
77 nextString.append(next.getDescriptionObject(Language.DE).getKeywords());
78 nextString.append("|");
79
80
81 nextString.append(next.getDescriptionObject(Language.EN).getUuid());
82 nextString.append("|");
83
84 nextString.append(next.getDescriptionObject(Language.EN).getName());
85 nextString.append("|");
86
87 nextString.append(next.getDescriptionObject(Language.EN).getDescription());
88 nextString.append("|");
89
90 nextString.append(next.getDescriptionObject(Language.EN).getKeywords());
91 nextString.append("|");
92
93
94 nextString.append(next.getVisible().toString());
95 nextString.append("|");
96
97 nextString.append(simpleDateFormat.format(next.getCreated()));
98 nextString.append("|");
99
100 nextString.append(simpleDateFormat.format(next.getModified()));
101 nextString.append("|");
102
103 nextString.append(next.getModifiedBy());
104 nextString.append("|");
105
106 nextString.append(next.getModifiedAddress());
107 nextString.append("|");
108
109 nextString.append(next.getPicture().getUuid());
110 nextString.append("|");
111
112 nextString.append(next.getPictureOnPage().toString());
113 nextString.append("|");
114
115 nextString.append(next.getTechnical());
116 nextString.append("|");
117
118 nextString.append(next.getOrderNumber().toString());
119 nextString.append("|");
120
121 return nextString.toString();
122 }
123
124 }