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.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import org.apache.logging.log4j.LogManager;
13 import org.apache.logging.log4j.Logger;
14
15 import de.tivsource.page.dao.reservation.ReservationDaoLocal;
16 import de.tivsource.page.entity.reservation.Reservation;
17
18 public class BackupReservation {
19
20
21
22
23 private static final Logger LOGGER = LogManager.getLogger(BackupReservation.class);
24
25 private static ReservationDaoLocal reservationDaoLocal;
26
27 private static final int max = 10000;
28
29 private List<File> backupFiles = new ArrayList<File>();
30
31 public static void setReservationDaoLocal(ReservationDaoLocal reservationDaoLocal) {
32 BackupReservation.reservationDaoLocal = reservationDaoLocal;
33 }
34
35 public List<File> getBackupFiles() throws IOException {
36 LOGGER.info("getBackupFiles() aufgerufen.");
37
38
39 File file = new File("/tmp/reservation.csv");
40 FileWriter fileWriter = new FileWriter(file);
41 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
42
43
44 bufferedWriter.write("[Format Definition] => uuid|gender|firstname|lastname|email|telephone|quantity|time|wishes|event|confirmed|created|createdAddress|confirmedAddress|confirmedDate|confirmedBy|modified|modifiedAddress|modifiedBy|origin|");
45
46 Iterator<Reservation> reservationIterator = reservationDaoLocal.findAll(0, max).iterator();
47 while(reservationIterator.hasNext()) {
48 Reservation next = reservationIterator.next();
49 bufferedWriter.write("\n");
50 bufferedWriter.write(convertToCsvLine(next));
51 generateContentFile(next);
52 }
53
54 bufferedWriter.close();
55 fileWriter.close();
56 backupFiles.add(file);
57
58 return backupFiles;
59 }
60
61 private String convertToCsvLine(Reservation next) {
62 LOGGER.info("convertToCsvLine(Page next) aufgerufen.");
63
64 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
65
66
67 StringBuffer nextString = new StringBuffer();
68
69 nextString.append(next.getUuid());
70 nextString.append("|");
71
72 nextString.append(next.getGender().toString());
73 nextString.append("|");
74
75 nextString.append(next.getFirstname());
76 nextString.append("|");
77
78 nextString.append(next.getLastname());
79 nextString.append("|");
80
81 nextString.append(next.getEmail());
82 nextString.append("|");
83
84 nextString.append(next.getTelephone());
85 nextString.append("|");
86
87 nextString.append(next.getQuantity().toString());
88 nextString.append("|");
89
90 nextString.append(simpleDateFormat.format(next.getTime()));
91 nextString.append("|");
92
93 nextString.append("reservation_" + next.getUuid() + ".txt");
94 nextString.append("|");
95
96 nextString.append(next.getEvent().getUuid());
97 nextString.append("|");
98
99 nextString.append(next.getConfirmed().toString());
100 nextString.append("|");
101
102 nextString.append(simpleDateFormat.format(next.getCreated()));
103 nextString.append("|");
104
105 nextString.append(next.getCreatedAddress());
106 nextString.append("|");
107
108 nextString.append(next.getConfirmedAddress() != null ? next.getConfirmedAddress() : "");
109 nextString.append("|");
110
111 nextString.append(next.getConfirmedDate() != null ? simpleDateFormat.format(next.getConfirmedDate()) : "");
112 nextString.append("|");
113
114 nextString.append(next.getConfirmedBy() != null ? next.getConfirmedBy() : "");
115 nextString.append("|");
116
117 nextString.append(simpleDateFormat.format(next.getModified()));
118 nextString.append("|");
119
120 nextString.append(next.getModifiedAddress());
121 nextString.append("|");
122
123 nextString.append(next.getModifiedBy());
124 nextString.append("|");
125
126 nextString.append(next.getOrigin().toString());
127 nextString.append("|");
128
129 return nextString.toString();
130 }
131
132 private void generateContentFile(Reservation message) throws IOException {
133 File file = new File("/tmp/reservation_" + message.getUuid() + ".txt");
134 FileWriter fileWriter = new FileWriter(file);
135 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
136 bufferedWriter.write(message.getWishes());
137 bufferedWriter.close();
138 fileWriter.close();
139 backupFiles.add(file);
140 }
141
142 }