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.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.common.css.CSSFile;
16 import de.tivsource.page.dao.cssfile.CSSFileDaoLocal;
17
18
19
20
21
22
23
24
25 public class BackupCSSFile {
26
27
28
29
30 private static final Logger LOGGER = LogManager
31 .getLogger(BackupCSSFile.class);
32
33
34
35
36 private CSSFileDaoLocal cssFileDaoLocal;
37
38
39
40
41 private List<File> backupFiles = new ArrayList<File>();
42
43
44
45
46
47
48
49 public BackupCSSFile(final CSSFileDaoLocal cssFileDaoLocalInput) {
50 super();
51 this.cssFileDaoLocal = cssFileDaoLocalInput;
52 }
53
54
55
56
57
58
59
60 public List<File> getBackupFiles() throws IOException {
61 LOGGER.info("getBackupFile() aufgerufen.");
62
63
64 File backupFile = new File("/tmp/cssFile.csv");
65 FileWriter fileWriter = new FileWriter(backupFile);
66 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
67
68
69 bufferedWriter.write("[Format Definition] => uuid|name|description|priority|path|created|modified|modifiedBy|modifiedAddress|version|");
70
71 Iterator<CSSFile> cssFileIterator = cssFileDaoLocal.findAll(0, cssFileDaoLocal.countAll()).iterator();
72 while (cssFileIterator.hasNext()) {
73 CSSFile next = cssFileIterator.next();
74 bufferedWriter.write("\n");
75 bufferedWriter.write(convertToCsvLine(next));
76 storeCSSFileInList(next);
77 }
78
79 bufferedWriter.close();
80 fileWriter.close();
81
82 backupFiles.add(backupFile);
83
84 return backupFiles;
85 }
86
87
88
89
90
91
92
93 private String convertToCsvLine(CSSFile next) {
94 LOGGER.info("convertToCsvLine(CSSFile next) aufgerufen.");
95
96 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
97
98
99
100
101 StringBuffer nextString = new StringBuffer();
102
103 nextString.append(next.getUuid());
104 nextString.append("|");
105
106 nextString.append(next.getName());
107 nextString.append("|");
108
109 nextString.append(next.getDescription());
110 nextString.append("|");
111
112 nextString.append(next.getPriority().toString());
113 nextString.append("|");
114
115 nextString.append(next.getPath());
116 nextString.append("|");
117
118 nextString.append(simpleDateFormat.format(next.getCreated()));
119 nextString.append("|");
120
121 nextString.append(simpleDateFormat.format(next.getModified()));
122 nextString.append("|");
123
124 nextString.append(next.getModifiedBy());
125 nextString.append("|");
126
127 nextString.append(next.getModifiedAddress());
128 nextString.append("|");
129
130 nextString.append(next.getVersion().toString());
131 nextString.append("|");
132
133 return nextString.toString();
134 }
135
136
137
138
139
140
141
142
143 private void storeCSSFileInList(final CSSFile next) {
144 File file = new File(next.getPath());
145 backupFiles.add(file);
146 }
147
148 }