View Javadoc
1   /*
2    * Prometheus: Application Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.prometheus.threads;
18  
19  import io.github.tonywasher.joceanus.gordianknot.util.GordianUtilities;
20  import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceManager;
21  import io.github.tonywasher.joceanus.metis.toolkit.MetisToolkit;
22  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
23  import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
24  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataSet;
25  import io.github.tonywasher.joceanus.prometheus.exc.PrometheusDataException;
26  import io.github.tonywasher.joceanus.prometheus.preference.PrometheusBackup.PrometheusBackupPreferenceKey;
27  import io.github.tonywasher.joceanus.prometheus.preference.PrometheusBackup.PrometheusBackupPreferences;
28  import io.github.tonywasher.joceanus.prometheus.security.PrometheusSecurityPasswordManager;
29  import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheetWorkBookType;
30  import io.github.tonywasher.joceanus.prometheus.sheets.PrometheusSpreadSheet;
31  import io.github.tonywasher.joceanus.prometheus.toolkit.PrometheusToolkit;
32  import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataControl;
33  import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThread;
34  import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadManager;
35  
36  import java.io.File;
37  
38  /**
39   * Thread to create an encrypted backup of a data set.
40   *
41   * @author Tony Washer
42   */
43  public class PrometheusThreadCreateBackup
44          implements TethysUIThread<Void> {
45      /**
46       * Buffer length.
47       */
48      private static final int BUFFER_LEN = 100;
49  
50      /**
51       * Number 10.
52       */
53      private static final int TEN = 10;
54  
55      /**
56       * Data Control.
57       */
58      private final PrometheusDataControl theControl;
59  
60      /**
61       * Constructor (Event Thread).
62       *
63       * @param pControl data control
64       */
65      public PrometheusThreadCreateBackup(final PrometheusDataControl pControl) {
66          theControl = pControl;
67      }
68  
69      @Override
70      public String getTaskName() {
71          return PrometheusThreadId.CREATEBACKUP.toString();
72      }
73  
74      @Override
75      public Void performTask(final TethysUIThreadManager pManager) throws OceanusException {
76          /* Access the thread manager */
77          final PrometheusToolkit myPromToolkit = (PrometheusToolkit) pManager.getThreadData();
78          final PrometheusSecurityPasswordManager myPasswordMgr = myPromToolkit.getPasswordManager();
79          boolean doDelete = false;
80          File myFile = null;
81  
82          try {
83              /* Initialise the status window */
84              pManager.initTask(getTaskName());
85  
86              /* Access the Backup preferences */
87              final MetisPreferenceManager myMgr = theControl.getPreferenceManager();
88              final PrometheusBackupPreferences myProperties = myMgr.getPreferenceSet(PrometheusBackupPreferences.class);
89  
90              /* Determine the archive name */
91              final String myBackupDir = myProperties.getStringValue(PrometheusBackupPreferenceKey.BACKUPDIR);
92              final String myPrefix = myProperties.getStringValue(PrometheusBackupPreferenceKey.BACKUPPFIX);
93              final boolean doTimeStamp = myProperties.getBooleanValue(PrometheusBackupPreferenceKey.BACKUPTIME);
94              final PrometheusSheetWorkBookType myType = myProperties.getEnumValue(PrometheusBackupPreferenceKey.BACKUPTYPE, PrometheusSheetWorkBookType.class);
95  
96              /* Create the name of the file */
97              final StringBuilder myName = new StringBuilder(BUFFER_LEN);
98              myName.append(myBackupDir);
99              myName.append(File.separator);
100             myName.append(myPrefix);
101 
102             /* If we are doing time-stamps */
103             if (doTimeStamp) {
104                 /* Obtain the current date/time */
105                 final OceanusDate myNow = new OceanusDate();
106 
107                 myName.append(myNow.getYear());
108                 if (myNow.getMonth() < TEN) {
109                     myName.append('0');
110                 }
111                 myName.append(myNow.getMonth());
112                 if (myNow.getDay() < TEN) {
113                     myName.append('0');
114                 }
115                 myName.append(myNow.getDay());
116             }
117 
118             /* Set the standard backup name */
119             myFile = new File(myName.toString() + GordianUtilities.SECUREZIPFILE_EXT);
120 
121             /* Create backup */
122             final PrometheusSpreadSheet mySheet = theControl.getSpreadSheet();
123             final PrometheusDataSet myOldData = theControl.getData();
124             mySheet.createBackup(pManager, myOldData, myFile, myType);
125 
126             /* File created, so delete on error */
127             doDelete = true;
128 
129             /* Initialise the status window */
130             pManager.initTask("Verifying Backup");
131 
132             /* Load workbook */
133             final PrometheusDataSet myNewData = theControl.getNewData();
134             mySheet.loadBackup(pManager, myPasswordMgr, myNewData, myFile);
135 
136             /* Create a difference set between the two data copies */
137             final PrometheusDataSet myDiff = myNewData.getDifferenceSet(pManager, myOldData);
138 
139             /* If the difference set is non-empty */
140             if (!myDiff.isEmpty()) {
141                 /* Throw an exception */
142                 throw new PrometheusDataException(myDiff, "Backup is inconsistent");
143             }
144 
145             /* OK so switch off flag */
146             doDelete = false;
147 
148             /* State that we have completed */
149             pManager.setCompletion();
150 
151             /* Delete file on error */
152         } finally {
153             /* Try to delete the file if required */
154             if (doDelete) {
155                 MetisToolkit.cleanUpFile(myFile);
156             }
157         }
158 
159         /* Return nothing */
160         return null;
161     }
162 }