1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.sheets;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
20 import io.github.tonywasher.joceanus.gordianknot.api.factory.GordianFactory.GordianFactoryLock;
21 import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipFactory;
22 import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipLock;
23 import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipWriteFile;
24 import io.github.tonywasher.joceanus.metis.toolkit.MetisToolkit;
25 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
26 import io.github.tonywasher.joceanus.oceanus.profile.OceanusProfile;
27 import io.github.tonywasher.joceanus.prometheus.data.PrometheusCryptographyDataType;
28 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataSet;
29 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusIOException;
30 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusSecurityException;
31 import io.github.tonywasher.joceanus.prometheus.security.PrometheusSecurityPasswordManager;
32 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetProvider;
33 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetWorkBook;
34 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetWorkBookType;
35 import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadStatusReport;
36
37 import java.io.File;
38 import java.io.FileOutputStream;
39 import java.io.IOException;
40 import java.io.OutputStream;
41 import java.util.ArrayList;
42 import java.util.List;
43
44
45
46
47
48
49 public abstract class PrometheusSheetWriter
50 implements PrometheusSheetControl {
51
52
53
54 private final TethysUIThreadStatusReport theReport;
55
56
57
58
59 private PrometheusSheetWorkBook theWorkBook;
60
61
62
63
64 private PrometheusDataSet theData;
65
66
67
68
69 private PrometheusSheetWorkBookType theType;
70
71
72
73
74 private List<PrometheusSheetDataItem<?>> theSheets;
75
76
77
78
79
80
81 protected PrometheusSheetWriter(final TethysUIThreadStatusReport pReport) {
82 theReport = pReport;
83 }
84
85 @Override
86 public TethysUIThreadStatusReport getReport() {
87 return theReport;
88 }
89
90 @Override
91 public PrometheusSheetWorkBook getWorkBook() {
92 return theWorkBook;
93 }
94
95 @Override
96 public PrometheusDataSet getData() {
97 return theData;
98 }
99
100
101
102
103
104
105 protected void addSheet(final PrometheusSheetDataItem<?> pSheet) {
106 theSheets.add(pSheet);
107 }
108
109
110
111
112
113
114
115
116
117 public void createBackup(final PrometheusDataSet pData,
118 final File pFile,
119 final PrometheusSheetWorkBookType pType) throws OceanusException {
120
121 boolean writeFailed = false;
122 try (FileOutputStream myOutputStream = new FileOutputStream(pFile)) {
123
124 createBackup(pData, myOutputStream, pType);
125
126
127 } catch (IOException
128 | OceanusException e) {
129 writeFailed = true;
130 throw new PrometheusIOException("Failed to create backup Workbook", e);
131
132
133 } finally {
134
135 if (writeFailed) {
136 MetisToolkit.cleanUpFile(pFile);
137 }
138 }
139 }
140
141
142
143
144
145
146
147
148
149 public void createBackup(final PrometheusDataSet pData,
150 final OutputStream pZipStream,
151 final PrometheusSheetWorkBookType pType) throws OceanusException {
152
153 theData = pData;
154 theType = pType;
155
156
157 createBackup(pZipStream);
158 }
159
160
161
162
163
164
165
166 public void createBackup(final OutputStream pZipStream) throws OceanusException {
167
168 OceanusProfile myTask = theReport.getActiveTask();
169 myTask = myTask.startTask("Writing");
170
171
172 try {
173
174 final PrometheusSecurityPasswordManager myPasswordMgr = theData.getPasswordMgr();
175 final GordianFactoryLock myBase = theData.getFactoryLock();
176 final GordianFactoryLock myLock = myPasswordMgr.similarFactoryLock(myBase);
177 final GordianZipFactory myZips = myPasswordMgr.getSecurityFactory().getZipFactory();
178 final GordianZipLock myZipLock = myZips.zipLock(myLock);
179
180
181 createBackup(myZipLock, pZipStream);
182
183
184 } catch (GordianException e) {
185 throw new PrometheusSecurityException(e);
186 }
187
188
189 myTask.end();
190 }
191
192
193
194
195
196
197
198
199 public void createBackup(final GordianZipLock pZipLock,
200 final OutputStream pZipStream) throws OceanusException {
201
202 final PrometheusSecurityPasswordManager myPasswordMgr = theData.getPasswordMgr();
203 final GordianZipFactory myZips = myPasswordMgr.getSecurityFactory().getZipFactory();
204
205
206 final String myName = PrometheusSheetConstants.FILE_NAME + theType.getExtension();
207
208
209 try (GordianZipWriteFile myZipFile = myZips.createZipFile(pZipLock, pZipStream);
210 OutputStream myStream = myZipFile.createOutputStream(new File(myName), false)) {
211
212 initialiseWorkBook(theType);
213
214
215 writeWorkBook(myStream);
216
217 } catch (IOException
218 | OceanusException e) {
219
220 throw new PrometheusIOException("Failed to create Backup Workbook", e);
221 } catch (GordianException e) {
222 throw new PrometheusSecurityException(e);
223 }
224 }
225
226
227
228
229 protected abstract void registerSheets();
230
231
232
233
234
235
236
237 private void initialiseWorkBook(final PrometheusSheetWorkBookType pType) throws OceanusException {
238
239 theWorkBook = PrometheusSheetProvider.newWorkBook(pType);
240
241
242 theSheets = new ArrayList<>();
243
244
245 for (PrometheusCryptographyDataType myType : PrometheusCryptographyDataType.values()) {
246
247 theSheets.add(newSheet(myType));
248 }
249
250
251 registerSheets();
252 }
253
254
255
256
257
258
259
260 private PrometheusSheetDataItem<?> newSheet(final PrometheusCryptographyDataType pListType) {
261
262 return switch (pListType) {
263 case CONTROLDATA -> new PrometheusSheetControlData(this);
264 case CONTROLKEY -> new PrometheusSheetControlKey(this);
265 case CONTROLKEYSET -> new PrometheusSheetControlKeySet(this);
266 case DATAKEYSET -> new PrometheusSheetDataKeySet(this);
267 default -> throw new IllegalArgumentException(pListType.toString());
268 };
269 }
270
271
272
273
274
275
276
277 private void writeWorkBook(final OutputStream pStream) throws OceanusException {
278
279 final OceanusProfile myTask = theReport.getActiveTask();
280
281
282 theReport.setNumStages(theSheets.size() + 1);
283
284
285 for (PrometheusSheetDataItem<?> mySheet : theSheets) {
286
287
288 myTask.startTask(mySheet.toString());
289 mySheet.writeSpreadSheet();
290 }
291
292
293 theReport.setNewStage("Writing");
294
295
296
297 myTask.startTask("Saving");
298 theWorkBook.saveToStream(pStream);
299 }
300 }