Coverage Report - org.jtheque.core.managers.schema.SchemaManager
 
Classes in this File Line Coverage Branch Coverage Complexity
SchemaManager
0 %
0/46
0 %
0/16
2.125
 
 1  
 package org.jtheque.core.managers.schema;
 2  
 
 3  
 import org.jtheque.core.managers.AbstractActivableManager;
 4  
 import org.jtheque.core.managers.ManagerException;
 5  
 import org.jtheque.core.managers.Managers;
 6  
 import org.jtheque.core.managers.error.IErrorManager;
 7  
 import org.jtheque.core.managers.error.InternationalizedError;
 8  
 import org.jtheque.core.managers.log.ILoggingManager;
 9  
 import org.jtheque.core.managers.state.IStateManager;
 10  
 import org.jtheque.core.managers.state.StateException;
 11  
 import org.jtheque.utils.bean.Version;
 12  
 
 13  
 import java.io.File;
 14  
 import java.util.ArrayList;
 15  
 import java.util.Collection;
 16  
 import java.util.Collections;
 17  
 import java.util.List;
 18  
 
 19  
 /*
 20  
  * This file is part of JTheque.
 21  
  *
 22  
  * JTheque is free software: you can redistribute it and/or modify
 23  
  * it under the terms of the GNU General Public License as published by
 24  
  * the Free Software Foundation, either version 3 of the License.
 25  
  *
 26  
  * JTheque is distributed in the hope that it will be useful,
 27  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 28  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 29  
  * GNU General Public License for more details.
 30  
  *
 31  
  * You should have received a copy of the GNU General Public License
 32  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 33  
  */
 34  
 
 35  
 /**
 36  
  * A Schema manager implementation.
 37  
  *
 38  
  * @author Baptiste Wicht
 39  
  * @see ISchemaManager
 40  
  */
 41  0
 public final class SchemaManager extends AbstractActivableManager implements ISchemaManager {
 42  0
     private final List<Schema> schemas = new ArrayList<Schema>(10);
 43  
 
 44  
     private SchemaConfiguration configuration;
 45  
 
 46  
     @Override
 47  
     public void init() throws ManagerException {
 48  0
         loadConfiguration();
 49  
 
 50  0
         Collections.sort(schemas);
 51  
 
 52  0
         boolean recoveryNecessary = checkForRecovery();
 53  
 
 54  0
         if (recoveryNecessary) {
 55  0
             recoverData();
 56  
         }
 57  0
     }
 58  
 
 59  
     /**
 60  
      * Recover the data from HSQLDB.
 61  
      */
 62  
     private void recoverData() {
 63  0
         File logFile = new File(Managers.getCore().getFolders().getApplicationFolder(), "localhost.log");
 64  0
         File scriptFile = new File(Managers.getCore().getFolders().getApplicationFolder(), "localhost.script");
 65  
 
 66  0
         Collection<Insert> inserts = new ArrayList<Insert>(50);
 67  
 
 68  0
         read(logFile, inserts);
 69  0
         read(scriptFile, inserts);
 70  
 
 71  0
         for (Schema schema : schemas) {
 72  0
             recoverIfNecessary(inserts, schema);
 73  
         }
 74  0
     }
 75  
 
 76  
     /**
 77  
      * Recover the schema if necessary.
 78  
      *
 79  
      * @param inserts The inserts of HSQLDB.
 80  
      * @param schema  The schema to recover.
 81  
      */
 82  
     private void recoverIfNecessary(Iterable<Insert> inserts, Schema schema) {
 83  0
         if (configuration.isNotRecovered(schema.getId())) {
 84  0
             schema.importDataFromHSQL(inserts);
 85  
 
 86  0
             configuration.setRecovered(schema.getId());
 87  
         }
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Read a file of HSQLDB.
 92  
      *
 93  
      * @param file    The file to read.
 94  
      * @param inserts The inserts to add to.
 95  
      */
 96  
     private static void read(File file, Collection<Insert> inserts) {
 97  0
         if (file.exists()) {
 98  0
             inserts.addAll(HSQLFileReader.readFile(file));
 99  
         }
 100  0
     }
 101  
 
 102  
     /**
 103  
      * Check if the recover is necessary.
 104  
      *
 105  
      * @return true if the recover is necessary else false.
 106  
      */
 107  
     private boolean checkForRecovery() {
 108  0
         boolean dataToRecover = false;
 109  
 
 110  0
         for (Schema schema : schemas) {
 111  0
             Version installedVersion = configuration.getVersion(schema.getId());
 112  
 
 113  0
             if (installedVersion == null) {
 114  0
                 schema.install();
 115  
 
 116  0
                 configuration.setVersion(schema.getId(), schema.getVersion());
 117  0
             } else if (schema.getVersion().isGreaterThan(installedVersion)) {
 118  0
                 schema.update(installedVersion);
 119  
             }
 120  
 
 121  0
             if (configuration.isNotRecovered(schema.getId())) {
 122  0
                 dataToRecover = true;
 123  
             }
 124  0
         }
 125  
 
 126  0
         return dataToRecover;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Load the configuration.
 131  
      */
 132  
     private void loadConfiguration() {
 133  
         try {
 134  0
             configuration = Managers.getManager(IStateManager.class).getOrCreateState(SchemaConfiguration.class);
 135  0
         } catch (StateException e) {
 136  0
             configuration = new SchemaConfiguration();
 137  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 138  0
             Managers.getManager(IErrorManager.class).addStartupError(new InternationalizedError("error.loading.configuration"));
 139  0
         }
 140  0
     }
 141  
 
 142  
     @Override
 143  
     public void registerSchema(Schema schema) {
 144  0
         schemas.add(schema);
 145  0
     }
 146  
 
 147  
     @Override
 148  
     public void unregisterSchema(Schema schema) {
 149  0
         schemas.remove(schema);
 150  0
     }
 151  
 }