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.event.EventDaoLocal;
14 import de.tivsource.page.entity.enumeration.Language;
15 import de.tivsource.page.entity.event.Event;
16
17 public class BackupEvent {
18
19
20
21
22 private static final Logger LOGGER = LogManager.getLogger(BackupEvent.class);
23
24 private static final int MAX = 1500;
25
26 private static EventDaoLocal eventDaoLocal;
27
28 public static void setEventDaoLocal(EventDaoLocal eventDaoLocal) {
29 BackupEvent.eventDaoLocal = eventDaoLocal;
30 }
31
32 public static File getBackupFile() throws IOException {
33 LOGGER.info("getBackupFile() aufgerufen.");
34
35 File backupFile = new File("/tmp/event.csv");
36 FileWriter backupFileWriter = new FileWriter(backupFile);
37 BufferedWriter backupFileWriterOut = new BufferedWriter(backupFileWriter);
38
39
40 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|price|beginning|ending|deadline|location|reservation|picture|piwikGoal|maxReservations|maxPersons|pictureOnPage|");
41
42 Iterator<Event> typeIterator = eventDaoLocal.findAll(0, MAX).iterator();
43 while(typeIterator.hasNext()) {
44 Event 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(Event next) {
55
56 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
57
58
59
60
61
62
63
64
65 StringBuffer nextString = new StringBuffer();
66
67
68 nextString.append(next.getUuid());
69 nextString.append("|");
70
71
72 nextString.append(next.getDescriptionObject(Language.DE).getUuid());
73 nextString.append("|");
74
75 nextString.append(next.getDescriptionObject(Language.DE).getName());
76 nextString.append("|");
77
78 nextString.append(next.getDescriptionObject(Language.DE).getDescription());
79 nextString.append("|");
80
81 nextString.append(next.getDescriptionObject(Language.DE).getKeywords());
82 nextString.append("|");
83
84 nextString.append(next.getDescriptionObject(Language.EN).getUuid());
85 nextString.append("|");
86
87 nextString.append(next.getDescriptionObject(Language.EN).getName());
88 nextString.append("|");
89
90 nextString.append(next.getDescriptionObject(Language.EN).getDescription());
91 nextString.append("|");
92
93 nextString.append(next.getDescriptionObject(Language.EN).getKeywords());
94 nextString.append("|");
95
96
97 nextString.append(next.getVisible().toString());
98 nextString.append("|");
99
100 nextString.append(simpleDateFormat.format(next.getCreated()));
101 nextString.append("|");
102
103 nextString.append(simpleDateFormat.format(next.getModified()));
104 nextString.append("|");
105
106 nextString.append(next.getModifiedBy());
107 nextString.append("|");
108
109 nextString.append(next.getModifiedAddress());
110 nextString.append("|");
111
112
113 nextString.append(next.getPrice().toString());
114 nextString.append("|");
115
116 nextString.append(simpleDateFormat.format(next.getBeginning()));
117 nextString.append("|");
118
119 nextString.append(simpleDateFormat.format(next.getEnding()));
120 nextString.append("|");
121
122 nextString.append(simpleDateFormat.format(next.getDeadline()));
123 nextString.append("|");
124
125 nextString.append(next.getLocation().getUuid());
126 nextString.append("|");
127
128 nextString.append(next.getReservation().toString());
129 nextString.append("|");
130
131 nextString.append(next.getPicture().getUuid());
132 nextString.append("|");
133
134 nextString.append(next.getPiwikGoal().toString());
135 nextString.append("|");
136
137 nextString.append(next.getMaxReservations().toString());
138 nextString.append("|");
139
140 nextString.append(next.getMaxPersons().toString());
141 nextString.append("|");
142
143 nextString.append(next.getPictureOnPage().toString());
144 nextString.append("|");
145
146 nextString.append(next.getTimeSelection().toString());
147 nextString.append("|");
148
149 return nextString.toString();
150 }
151
152 }