PrometheusSheetReader.java

/*
 * Prometheus: Application Framework
 * Copyright 2012-2026. Tony Washer
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.tonywasher.joceanus.prometheus.sheets;

import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipFactory;
import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipFileContents;
import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipFileEntry;
import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipLock;
import io.github.tonywasher.joceanus.gordianknot.api.zip.GordianZipReadFile;
import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
import io.github.tonywasher.joceanus.oceanus.profile.OceanusProfile;
import io.github.tonywasher.joceanus.prometheus.data.PrometheusCryptographyDataType;
import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataSet;
import io.github.tonywasher.joceanus.prometheus.exc.PrometheusDataException;
import io.github.tonywasher.joceanus.prometheus.exc.PrometheusIOException;
import io.github.tonywasher.joceanus.prometheus.exc.PrometheusSecurityException;
import io.github.tonywasher.joceanus.prometheus.security.PrometheusSecurityPasswordManager;
import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetProvider;
import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetWorkBook;
import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetWorkBookType;
import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadStatusReport;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Load control for spreadsheets.
 *
 * @author Tony Washer
 */
public abstract class PrometheusSheetReader
        implements PrometheusSheetControl {
    /**
     * Report.
     */
    private final TethysUIThreadStatusReport theReport;

    /**
     * The password manager.
     */
    private final PrometheusSecurityPasswordManager thePasswordMgr;

    /**
     * Spreadsheet.
     */
    private PrometheusSheetWorkBook theWorkBook;

    /**
     * DataSet.
     */
    private PrometheusDataSet theData;

    /**
     * The WorkSheets.
     */
    private List<PrometheusSheetDataItem<?>> theSheets;

    /**
     * Constructor.
     *
     * @param pReport      the report
     * @param pPasswordMgr the password manager
     */
    protected PrometheusSheetReader(final TethysUIThreadStatusReport pReport,
                                    final PrometheusSecurityPasswordManager pPasswordMgr) {
        theReport = pReport;
        thePasswordMgr = pPasswordMgr;
    }

    @Override
    public TethysUIThreadStatusReport getReport() {
        return theReport;
    }

    @Override
    public PrometheusDataSet getData() {
        return theData;
    }

    @Override
    public PrometheusSheetWorkBook getWorkBook() {
        return theWorkBook;
    }

    /**
     * Add Sheet to list.
     *
     * @param pSheet the sheet
     */
    protected void addSheet(final PrometheusSheetDataItem<?> pSheet) {
        theSheets.add(pSheet);
    }

    /**
     * Load a Backup Workbook.
     *
     * @param pData DataSet to load into
     * @param pFile the file to load
     * @throws OceanusException on error
     */
    public void loadBackup(final File pFile,
                           final PrometheusDataSet pData) throws OceanusException {
        try {
            loadBackup(new FileInputStream(pFile), pData, pFile.getName());
        } catch (IOException e) {
            throw new PrometheusIOException("Failed to access Backup", e);
        }
    }

    /**
     * Load a Backup Workbook.
     *
     * @param pInStream the zip input stream
     * @param pData     the data to load into
     * @param pName     the filename
     * @throws OceanusException on error
     */
    public void loadBackup(final InputStream pInStream,
                           final PrometheusDataSet pData,
                           final String pName) throws OceanusException {
        /* Protect against exceptions */
        try {
            /* Start the task */
            OceanusProfile myTask = theReport.getActiveTask();
            myTask = myTask.startTask("Loading");
            theData = pData;

            /* Access the zip file */
            final GordianZipFactory myZips = thePasswordMgr.getSecurityFactory().getZipFactory();
            final GordianZipReadFile myFile = myZips.openZipFile(pInStream);

            /* Obtain the lock from the file */
            final GordianZipLock myLock = myFile.getLock();

            /* Resolve the lock */
            thePasswordMgr.resolveZipLock(myLock, pName);

            /* Access ZipFile contents */
            final GordianZipFileContents myContents = myFile.getContents();

            /* Loop through the file entries */
            final Iterator<GordianZipFileEntry> myIterator = myContents.iterator();
            GordianZipFileEntry myEntry = null;
            while (myIterator.hasNext()) {
                /* Access the entry */
                myEntry = myIterator.next();

                /* Break loop if we have the right entry */
                if (myEntry.getFileName().startsWith(PrometheusSheetConstants.FILE_NAME)) {
                    break;
                }
            }

            /* If we did not find the entry */
            if (myEntry == null) {
                throw new PrometheusDataException("Invalid input file");
            }

            /* Load the workBook */
            loadEntry(myFile, myEntry);

            /* Complete the task */
            myTask.end();

        } catch (GordianException e) {
            throw new PrometheusSecurityException(e);
        }
    }

    /**
     * Load a Backup Workbook.
     *
     * @param pFile  the zip file
     * @param pEntry the zip file entry
     * @throws OceanusException on error
     */
    public void loadEntry(final GordianZipReadFile pFile,
                          final GordianZipFileEntry pEntry) throws OceanusException {
        /* Protect the workbook retrieval */
        try (InputStream myStream = pFile.createInputStream(pEntry)) {
            /* Obtain the active profile */
            final OceanusProfile myTask = theReport.getActiveTask();
            myTask.startTask("Parsing");

            /* Initialise the workbook */
            initialiseWorkBook(myStream, PrometheusSheetWorkBookType.determineType(pEntry.getFileName()));

            /* Load the workbook */
            myTask.startTask("Reading");
            loadWorkBook();

            /* Close the Stream to force out errors */
            myTask.startTask("Closing");

        } catch (IOException e) {
            /* Report the error */
            throw new PrometheusIOException("Failed to load Backup Workbook: " + pEntry.getFileName(), e);

        } catch (GordianException e) {
            throw new PrometheusSecurityException(e);
        }
    }

    /**
     * Register sheets.
     */
    protected abstract void registerSheets();

    /**
     * Create the list of sheets to load.
     *
     * @param pStream the input stream
     * @param pType   the workBookType
     * @throws OceanusException on error
     */
    private void initialiseWorkBook(final InputStream pStream,
                                    final PrometheusSheetWorkBookType pType) throws OceanusException {
        /* Initialise the list */
        theSheets = new ArrayList<>();

        /* Loop through the list types */
        for (PrometheusCryptographyDataType myType : PrometheusCryptographyDataType.values()) {
            /* Create the sheet */
            theSheets.add(newSheet(myType));
        }

        /* register additional sheets */
        registerSheets();

        /* Note the stage */
        theReport.setNewStage("Loading");

        /* Access the workbook from the stream */
        theWorkBook = PrometheusSheetProvider.loadFromStream(pType, pStream);
    }

    /**
     * Create new sheet of required type.
     *
     * @param pListType the list type
     * @return the new sheet
     */
    private PrometheusSheetDataItem<?> newSheet(final PrometheusCryptographyDataType pListType) {
        /* Switch on list Type */
        return switch (pListType) {
            case CONTROLDATA -> new PrometheusSheetControlData(this);
            case CONTROLKEY -> new PrometheusSheetControlKey(this);
            case CONTROLKEYSET -> new PrometheusSheetControlKeySet(this);
            case DATAKEYSET -> new PrometheusSheetDataKeySet(this);
            default -> throw new IllegalArgumentException(pListType.toString());
        };
    }

    /**
     * Load the WorkBook.
     *
     * @throws OceanusException on error
     */
    private void loadWorkBook() throws OceanusException {
        /* Obtain the active profile */
        final OceanusProfile myTask = theReport.getActiveTask();

        /* Declare the number of stages */
        theReport.setNumStages(theSheets.size());

        /* Loop through the sheets */
        for (PrometheusSheetDataItem<?> mySheet : theSheets) {
            /* Load data for the sheet */
            myTask.startTask(mySheet.toString());
            mySheet.loadSpreadSheet();
        }
    }
}