From e2152c59a273d6df427dffa1a2a8fe971112904e Mon Sep 17 00:00:00 2001 From: North Date: Tue, 18 Jun 2013 22:44:34 +0300 Subject: [PATCH] added ExpansionRepository --- Mage/src/mage/cards/ExpansionSet.java | 4 + .../cards/repository/DatabaseVersion.java | 11 +++ .../mage/cards/repository/ExpansionInfo.java | 59 +++++++++++++++ .../cards/repository/ExpansionRepository.java | 73 +++++++++++++++++++ .../mage/cards/repository/RepositoryUtil.java | 34 +++++++++ 5 files changed, 181 insertions(+) create mode 100644 Mage/src/mage/cards/repository/ExpansionInfo.java create mode 100644 Mage/src/mage/cards/repository/ExpansionRepository.java create mode 100644 Mage/src/mage/cards/repository/RepositoryUtil.java diff --git a/Mage/src/mage/cards/ExpansionSet.java b/Mage/src/mage/cards/ExpansionSet.java index 1b1e05309d..1400944a8c 100644 --- a/Mage/src/mage/cards/ExpansionSet.java +++ b/Mage/src/mage/cards/ExpansionSet.java @@ -98,6 +98,10 @@ public abstract class ExpansionSet implements Serializable { return packageName; } + public String getBlockName() { + return blockName; + } + @Override public String toString() { return name; diff --git a/Mage/src/mage/cards/repository/DatabaseVersion.java b/Mage/src/mage/cards/repository/DatabaseVersion.java index 7f1b3aaab0..55f87e066e 100644 --- a/Mage/src/mage/cards/repository/DatabaseVersion.java +++ b/Mage/src/mage/cards/repository/DatabaseVersion.java @@ -10,9 +10,20 @@ import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName = "version") public class DatabaseVersion { + @DatabaseField + protected String entity; + @DatabaseField protected Long version; + public String getEntity() { + return entity; + } + + public void setEntity(String entity) { + this.entity = entity; + } + public Long getVersion() { return version; } diff --git a/Mage/src/mage/cards/repository/ExpansionInfo.java b/Mage/src/mage/cards/repository/ExpansionInfo.java new file mode 100644 index 0000000000..db1f0e8e67 --- /dev/null +++ b/Mage/src/mage/cards/repository/ExpansionInfo.java @@ -0,0 +1,59 @@ +package mage.cards.repository; + +import com.j256.ormlite.field.DataType; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; +import mage.cards.ExpansionSet; +import mage.constants.SetType; + +import java.util.Date; + +/** + * + * @author North + */ +@DatabaseTable(tableName = "expansion") +public class ExpansionInfo { + + @DatabaseField(unique = true) + protected String name; + @DatabaseField(unique = true) + protected String code; + @DatabaseField + protected String blockName; + @DatabaseField + protected Date releaseDate; + @DatabaseField(dataType = DataType.ENUM_STRING) + protected SetType type; + + public ExpansionInfo() { + } + + public ExpansionInfo(ExpansionSet expansionSet) { + this.name = expansionSet.getName(); + this.code = expansionSet.getCode(); + this.blockName = expansionSet.getBlockName(); + this.releaseDate = expansionSet.getReleaseDate(); + this.type = expansionSet.getSetType(); + } + + public String getName() { + return name; + } + + public String getCode() { + return code; + } + + public String getBlockName() { + return blockName; + } + + public Date getReleaseDate() { + return releaseDate; + } + + public SetType getType() { + return type; + } +} diff --git a/Mage/src/mage/cards/repository/ExpansionRepository.java b/Mage/src/mage/cards/repository/ExpansionRepository.java new file mode 100644 index 0000000000..bb66b60ecb --- /dev/null +++ b/Mage/src/mage/cards/repository/ExpansionRepository.java @@ -0,0 +1,73 @@ +package mage.cards.repository; + +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.dao.DaoManager; +import com.j256.ormlite.jdbc.JdbcConnectionSource; +import com.j256.ormlite.support.ConnectionSource; +import com.j256.ormlite.table.TableUtils; + +import java.io.File; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author North + */ +public enum ExpansionRepository { + + instance; + + private static final String JDBC_URL = "jdbc:sqlite:db/cards.db"; + private static final String VERSION_ENTITY_NAME = "expansion"; + private static final long EXPANSION_DB_VERSION = 1; + + private Dao expansionDao; + + private ExpansionRepository() { + File file = new File("db"); + if (!file.exists()) { + file.mkdirs(); + } + try { + ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL); + boolean obsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, EXPANSION_DB_VERSION); + + if (obsolete) { + TableUtils.dropTable(connectionSource, ExpansionInfo.class, true); + } + + TableUtils.createTableIfNotExists(connectionSource, ExpansionInfo.class); + expansionDao = DaoManager.createDao(connectionSource, ExpansionInfo.class); + } catch (SQLException ex) { + } + } + + public void add(ExpansionInfo expansion) { + try { + expansionDao.create(expansion); + } catch (SQLException e) { + } + } + + public List getSetCodes() { + List setCodes = new ArrayList(); + try { + List expansions = expansionDao.queryForAll(); + for (ExpansionInfo expansion : expansions) { + setCodes.add(expansion.getCode()); + } + } catch (SQLException ex) { + } + return setCodes; + } + + public List getAll() { + try { + return expansionDao.queryForAll(); + } catch (SQLException ex) { + } + return new ArrayList(); + } +} diff --git a/Mage/src/mage/cards/repository/RepositoryUtil.java b/Mage/src/mage/cards/repository/RepositoryUtil.java new file mode 100644 index 0000000000..43f9df24e1 --- /dev/null +++ b/Mage/src/mage/cards/repository/RepositoryUtil.java @@ -0,0 +1,34 @@ +package mage.cards.repository; + +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.dao.DaoManager; +import com.j256.ormlite.stmt.QueryBuilder; +import com.j256.ormlite.stmt.SelectArg; +import com.j256.ormlite.support.ConnectionSource; +import com.j256.ormlite.table.TableUtils; +import java.sql.SQLException; +import java.util.List; + +/** + * + * @author North + */ +public class RepositoryUtil { + + public static boolean isDatabaseObsolete(ConnectionSource connectionSource, String entityName, long version) throws SQLException { + TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class); + Dao dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class); + + QueryBuilder queryBuilder = dbVersionDao.queryBuilder(); + queryBuilder.where().eq("entity", new SelectArg(entityName)).and().eq("version", version); + List dbVersions = dbVersionDao.query(queryBuilder.prepare()); + + if (dbVersions.isEmpty()) { + DatabaseVersion dbVersion = new DatabaseVersion(); + dbVersion.setEntity(entityName); + dbVersion.setVersion(version); + dbVersionDao.create(dbVersion); + } + return dbVersions.isEmpty(); + } +}