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.zip.GordianZipFactory;
21 import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipFileContents;
22 import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipFileEntry;
23 import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipLock;
24 import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipReadFile;
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.PrometheusDataException;
30 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusIOException;
31 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusSecurityException;
32 import io.github.tonywasher.joceanus.prometheus.security.PrometheusSecurityPasswordManager;
33 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetProvider;
34 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetWorkBook;
35 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetWorkBookType;
36 import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadStatusReport;
37
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.ArrayList;
43 import java.util.Iterator;
44 import java.util.List;
45
46
47
48
49
50
51 public abstract class PrometheusSheetReader
52 implements PrometheusSheetControl {
53
54
55
56 private final TethysUIThreadStatusReport theReport;
57
58
59
60
61 private final PrometheusSecurityPasswordManager thePasswordMgr;
62
63
64
65
66 private PrometheusSheetWorkBook theWorkBook;
67
68
69
70
71 private PrometheusDataSet theData;
72
73
74
75
76 private List<PrometheusSheetDataItem<?>> theSheets;
77
78
79
80
81
82
83
84 protected PrometheusSheetReader(final TethysUIThreadStatusReport pReport,
85 final PrometheusSecurityPasswordManager pPasswordMgr) {
86 theReport = pReport;
87 thePasswordMgr = pPasswordMgr;
88 }
89
90 @Override
91 public TethysUIThreadStatusReport getReport() {
92 return theReport;
93 }
94
95 @Override
96 public PrometheusDataSet getData() {
97 return theData;
98 }
99
100 @Override
101 public PrometheusSheetWorkBook getWorkBook() {
102 return theWorkBook;
103 }
104
105
106
107
108
109
110 protected void addSheet(final PrometheusSheetDataItem<?> pSheet) {
111 theSheets.add(pSheet);
112 }
113
114
115
116
117
118
119
120
121 public void loadBackup(final File pFile,
122 final PrometheusDataSet pData) throws OceanusException {
123 try {
124 loadBackup(new FileInputStream(pFile), pData, pFile.getName());
125 } catch (IOException e) {
126 throw new PrometheusIOException("Failed to access Backup", e);
127 }
128 }
129
130
131
132
133
134
135
136
137
138 public void loadBackup(final InputStream pInStream,
139 final PrometheusDataSet pData,
140 final String pName) throws OceanusException {
141
142 try {
143
144 OceanusProfile myTask = theReport.getActiveTask();
145 myTask = myTask.startTask("Loading");
146 theData = pData;
147
148
149 final GordianZipFactory myZips = thePasswordMgr.getSecurityFactory().getZipFactory();
150 final GordianZipReadFile myFile = myZips.openZipFile(pInStream);
151
152
153 final GordianZipLock myLock = myFile.getLock();
154
155
156 thePasswordMgr.resolveZipLock(myLock, pName);
157
158
159 final GordianZipFileContents myContents = myFile.getContents();
160
161
162 final Iterator<GordianZipFileEntry> myIterator = myContents.iterator();
163 GordianZipFileEntry myEntry = null;
164 while (myIterator.hasNext()) {
165
166 myEntry = myIterator.next();
167
168
169 if (myEntry.getFileName().startsWith(PrometheusSheetConstants.FILE_NAME)) {
170 break;
171 }
172 }
173
174
175 if (myEntry == null) {
176 throw new PrometheusDataException("Invalid input file");
177 }
178
179
180 loadEntry(myFile, myEntry);
181
182
183 myTask.end();
184
185 } catch (GordianException e) {
186 throw new PrometheusSecurityException(e);
187 }
188 }
189
190
191
192
193
194
195
196
197 public void loadEntry(final GordianZipReadFile pFile,
198 final GordianZipFileEntry pEntry) throws OceanusException {
199
200 try (InputStream myStream = pFile.createInputStream(pEntry)) {
201
202 final OceanusProfile myTask = theReport.getActiveTask();
203 myTask.startTask("Parsing");
204
205
206 initialiseWorkBook(myStream, PrometheusSheetWorkBookType.determineType(pEntry.getFileName()));
207
208
209 myTask.startTask("Reading");
210 loadWorkBook();
211
212
213 myTask.startTask("Closing");
214
215 } catch (IOException e) {
216
217 throw new PrometheusIOException("Failed to load Backup Workbook: " + pEntry.getFileName(), e);
218
219 } catch (GordianException e) {
220 throw new PrometheusSecurityException(e);
221 }
222 }
223
224
225
226
227 protected abstract void registerSheets();
228
229
230
231
232
233
234
235
236 private void initialiseWorkBook(final InputStream pStream,
237 final PrometheusSheetWorkBookType pType) throws OceanusException {
238
239 theSheets = new ArrayList<>();
240
241
242 for (PrometheusCryptographyDataType myType : PrometheusCryptographyDataType.values()) {
243
244 theSheets.add(newSheet(myType));
245 }
246
247
248 registerSheets();
249
250
251 theReport.setNewStage("Loading");
252
253
254 theWorkBook = PrometheusSheetProvider.loadFromStream(pType, pStream);
255 }
256
257
258
259
260
261
262
263 private PrometheusSheetDataItem<?> newSheet(final PrometheusCryptographyDataType pListType) {
264
265 return switch (pListType) {
266 case CONTROLDATA -> new PrometheusSheetControlData(this);
267 case CONTROLKEY -> new PrometheusSheetControlKey(this);
268 case CONTROLKEYSET -> new PrometheusSheetControlKeySet(this);
269 case DATAKEYSET -> new PrometheusSheetDataKeySet(this);
270 default -> throw new IllegalArgumentException(pListType.toString());
271 };
272 }
273
274
275
276
277
278
279 private void loadWorkBook() throws OceanusException {
280
281 final OceanusProfile myTask = theReport.getActiveTask();
282
283
284 theReport.setNumStages(theSheets.size());
285
286
287 for (PrometheusSheetDataItem<?> mySheet : theSheets) {
288
289 myTask.startTask(mySheet.toString());
290 mySheet.loadSpreadSheet();
291 }
292 }
293 }