Coverage Report - org.jtheque.films.persistence.FilmsSchema
 
Classes in this File Line Coverage Branch Coverage Complexity
FilmsSchema
0 %
0/50
0 %
0/4
1.182
FilmsSchema$1
N/A
N/A
1.182
FilmsSchema$SimplePersonRowMapper
0 %
0/7
N/A
1.182
 
 1  
 package org.jtheque.films.persistence;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.beans.IBeansManager;
 21  
 import org.jtheque.core.managers.schema.AbstractSchema;
 22  
 import org.jtheque.core.managers.schema.HSQLImporter;
 23  
 import org.jtheque.core.managers.schema.Insert;
 24  
 import org.jtheque.films.persistence.dao.able.IDaoFilms;
 25  
 import org.jtheque.films.services.able.IActorService;
 26  
 import org.jtheque.films.services.able.IRealizersService;
 27  
 import org.jtheque.primary.dao.able.IDaoCollections;
 28  
 import org.jtheque.primary.dao.able.IDaoKinds;
 29  
 import org.jtheque.primary.dao.able.IDaoLanguages;
 30  
 import org.jtheque.primary.dao.able.IDaoLendings;
 31  
 import org.jtheque.primary.dao.able.IDaoPersons;
 32  
 import org.jtheque.primary.dao.able.IDaoSagas;
 33  
 import org.jtheque.primary.dao.able.IDaoTypes;
 34  
 import org.jtheque.utils.bean.Version;
 35  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 36  
 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
 37  
 
 38  
 import javax.annotation.Resource;
 39  
 import java.sql.ResultSet;
 40  
 import java.sql.SQLException;
 41  
 import java.util.List;
 42  
 
 43  
 /**
 44  
  * The database schema for the Movies Module.
 45  
  *
 46  
  * @author Baptiste Wicht
 47  
  */
 48  
 public final class FilmsSchema extends AbstractSchema {
 49  
     @Resource
 50  
     private SimpleJdbcTemplate jdbcTemplate;
 51  
 
 52  
     /**
 53  
      * Construct a new MoviesSchema.
 54  
      */
 55  
     public FilmsSchema() {
 56  0
         super();
 57  
 
 58  0
         Managers.getManager(IBeansManager.class).inject(this);
 59  0
     }
 60  
 
 61  
     @Override
 62  
     public Version getVersion() {
 63  0
         return new Version("1.1");
 64  
     }
 65  
 
 66  
     @Override
 67  
     public String getId() {
 68  0
         return "Films-Schema";
 69  
     }
 70  
 
 71  
     @Override
 72  
     public String[] getDependencies() {
 73  0
         return new String[]{"PrimaryUtils-Schema"};
 74  
     }
 75  
 
 76  
     @Override
 77  
     public void install() {
 78  0
         createDataTable();
 79  0
         createReferentialIntegrityConstraints();
 80  0
     }
 81  
 
 82  
     @Override
 83  
     public void update(Version from) {
 84  0
         if ("1.0".equals(from.getVersion())) {
 85  0
             createReferentialIntegrityConstraints();
 86  0
             convertToPersons("T_ACTORS", IActorService.PERSON_TYPE);
 87  0
             convertToPersons("T_REALIZERS", IRealizersService.PERSON_TYPE);
 88  
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Create the tables for the data.
 93  
      */
 94  
     private void createDataTable() {
 95  0
         jdbcTemplate.update("CREATE TABLE " + IDaoFilms.TABLE + " (ID INT IDENTITY PRIMARY KEY, TITLE VARCHAR(150) NOT NULL UNIQUE, YEAR INT, COMMENT VARCHAR(2000),DURATION INT,IMAGE VARCHAR(200),NOTE INT, FILEPATH VARCHAR(250), RESUME VARCHAR(2000),THE_REALIZER_FK INT,THE_COLLECTION_FK INT NOT NULL, THE_KIND_FK INT, THE_LANGUAGE_FK INT, THE_LENDING_FK INT, THE_SAGA_FK INT, THE_TYPE_FK INT)");
 96  0
         jdbcTemplate.update("CREATE TABLE " + IDaoFilms.ACTORS_FILMS_TABLE + " (THE_FILM_FK INT NOT NULL, THE_ACTOR_FK INT NOT NULL)");
 97  0
         jdbcTemplate.update("CREATE TABLE " + IDaoFilms.KINDS_FILMS_TABLE + " (THE_FILM_FK INT NOT NULL, THE_KIND_FK INT NOT NULL)");
 98  
 
 99  0
         jdbcTemplate.update("CREATE INDEX FILM_IDX ON " + IDaoFilms.TABLE + "(ID)");
 100  0
     }
 101  
 
 102  
     /**
 103  
      * Create the constraints of the referential integrity.
 104  
      */
 105  
     private void createReferentialIntegrityConstraints() {
 106  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.ACTORS_FILMS_TABLE + " ADD FOREIGN KEY (THE_FILM_FK) REFERENCES  " + IDaoFilms.TABLE + "  (ID) ON UPDATE SET NULL");
 107  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.ACTORS_FILMS_TABLE + " ADD FOREIGN KEY (THE_ACTOR_FK) REFERENCES  " + IDaoPersons.TABLE + "  (ID) ON UPDATE SET NULL");
 108  
 
 109  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.KINDS_FILMS_TABLE + " ADD FOREIGN KEY (THE_FILM_FK) REFERENCES  " + IDaoFilms.TABLE + "  (ID) ON UPDATE SET NULL");
 110  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.KINDS_FILMS_TABLE + " ADD FOREIGN KEY (THE_KIND_FK) REFERENCES  " + IDaoKinds.TABLE + "  (ID) ON UPDATE SET NULL");
 111  
 
 112  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.TABLE + " ADD FOREIGN KEY (THE_KIND_FK) REFERENCES  " + IDaoKinds.TABLE + "  (ID) ON UPDATE SET NULL");
 113  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.TABLE + " ADD FOREIGN KEY (THE_REALIZER_FK) REFERENCES  " + IDaoPersons.TABLE + "  (ID) ON UPDATE SET NULL");
 114  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.TABLE + " ADD FOREIGN KEY (THE_COLLECTION_FK) REFERENCES  " + IDaoCollections.TABLE + "  (ID) ON UPDATE SET NULL");
 115  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.TABLE + " ADD FOREIGN KEY (THE_LANGUAGE_FK) REFERENCES  " + IDaoLanguages.TABLE + "  (ID) ON UPDATE SET NULL");
 116  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.TABLE + " ADD FOREIGN KEY (THE_LENDING_FK) REFERENCES  " + IDaoLendings.TABLE + "  (ID) ON UPDATE SET NULL");
 117  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.TABLE + " ADD FOREIGN KEY (THE_SAGA_FK) REFERENCES  " + IDaoSagas.TABLE + "  (ID) ON UPDATE SET NULL");
 118  0
         jdbcTemplate.update("ALTER TABLE " + IDaoFilms.TABLE + " ADD FOREIGN KEY (THE_TYPE_FK) REFERENCES  " + IDaoTypes.TABLE + "  (ID) ON UPDATE SET NULL");
 119  0
     }
 120  
 
 121  
     /**
 122  
      * Convert an old table of persons with the new person table.
 123  
      *
 124  
      * @param table The table to replace. This table will be dropped.
 125  
      * @param type  The type of persons.
 126  
      */
 127  
     private void convertToPersons(String table, String type) {
 128  0
         List<Object[]> actors = jdbcTemplate.query("SELECT * FROM " + table, new SimplePersonRowMapper());
 129  
 
 130  0
         String query = "INSERT INTO " + IDaoPersons.TABLE + "(NAME, FIRST_NAME, NOTE, THE_COUNTRY_FK, TYPE) VALUES (?,?,?,?,?)";
 131  
 
 132  0
         for (Object[] author : actors) {
 133  0
             jdbcTemplate.update(query, author[0], author[1], author[2], author[3], type);
 134  
         }
 135  
 
 136  0
         jdbcTemplate.update("DROP TABLE IF EXISTS ?", table);
 137  0
     }
 138  
 
 139  
     /**
 140  
      * A simple mapper to get persons from the old tables of persons (actors & realizers).
 141  
      *
 142  
      * @author Baptiste Wicht
 143  
      */
 144  0
     private static final class SimplePersonRowMapper implements ParameterizedRowMapper<Object[]> {
 145  
         @Override
 146  
         public Object[] mapRow(ResultSet rs, int i) throws SQLException {
 147  0
             Object[] person = new Object[4];
 148  
 
 149  0
             person[0] = rs.getString("NAME");
 150  0
             person[1] = rs.getString("FIRSTNAME");
 151  0
             person[2] = rs.getInt("NOTE");
 152  0
             person[3] = rs.getInt("THE_COUNTRY_FK");
 153  
 
 154  0
             return person;
 155  
         }
 156  
     }
 157  
 
 158  
     @Override
 159  
     public void importDataFromHSQL(Iterable<Insert> inserts) {
 160  0
         HSQLImporter importer = new HSQLImporter();
 161  
 
 162  0
         importer.match("OD_SAGA_FILM", "INSERT INTO " + IDaoSagas.TABLE + " (ID, NAME, IMPL) VALUES (?,?,?)", "Films", 0, 2);
 163  0
         importer.match("OD_KIND", "INSERT INTO " + IDaoKinds.TABLE + " (ID, NAME, IMPL) VALUES (?,?,?)", "Films", 0, 2);
 164  0
         importer.match("OD_TYPE", "INSERT INTO " + IDaoTypes.TABLE + " (ID, NAME, IMPL) VALUES (?,?,?)", "Films", 0, 2);
 165  0
         importer.match("FILM_ACTOR", "INSERT INTO " + IDaoFilms.ACTORS_FILMS_TABLE + " (THE_FILM_FK, THE_ACTOR_FK) VALUES (?,?)", 0, 1);
 166  0
         importer.match("FILM_KIND", "INSERT INTO " + IDaoFilms.KINDS_FILMS_TABLE + " (THE_FILM_FK, THE_KIND_FK) VALUES (?,?)", 0, 1);
 167  0
         importer.match("OD_LENDING", "INSERT INTO " + IDaoLendings.TABLE + " (ID, DATE, THE_BORROWER_FK, IMPL) VALUES (?,?,?,?)", "Films", 0, 2, 3);
 168  0
         importer.match("OD_ACTOR", "INSERT INTO " + IDaoPersons.TABLE + " (ID, NAME, FIRST_NAME, NOTE, THE_COUNTRY_FK,TYPE) VALUES (?,?,?,?,?,?)", IActorService.PERSON_TYPE, 0, 3, 2, 4, 5);
 169  0
         importer.match("OD_REALIZER", "INSERT INTO " + IDaoPersons.TABLE + " (ID, NAME, FIRST_NAME, NOTE, THE_COUNTRY_FK,TYPE) VALUES (?,?,?,?,?,?)", IRealizersService.PERSON_TYPE, 0, 3, 2, 4, 5);
 170  0
         importer.match("OD_FILM_COLLECTION", "INSERT INTO " + IDaoCollections.TABLE + " (ID, TITLE, PROTECTED, PASSWORD, IMPL) VALUES(?,?,?,?,?)", "Films", 0, 4, 3, 2);
 171  0
         importer.match("OD_FILM", "INSERT INTO " + IDaoFilms.TABLE + " (ID, TITLE, YEAR, COMMENT,DURATION,IMAGE,NOTE,RESUME,THE_REALIZER_FK,THE_COLLECTION_FK, THE_KIND_FK, THE_LANGUAGE_FK, THE_LENDING_FK, THE_SAGA_FK, THE_TYPE_FK, FILEPATH) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
 172  
                 0, 7, 8, 2, 3, 4, 5, 6, 13, 9, 10, 11, 12, 15, 14, 16);
 173  
 
 174  0
         importer.importInserts(inserts);
 175  0
     }
 176  
 }