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