mirror of
https://github.com/correl/mage.git
synced 2024-11-15 11:09:30 +00:00
added ExpansionRepository
This commit is contained in:
parent
d91813b08a
commit
e2152c59a2
5 changed files with 181 additions and 0 deletions
|
@ -98,6 +98,10 @@ public abstract class ExpansionSet implements Serializable {
|
|||
return packageName;
|
||||
}
|
||||
|
||||
public String getBlockName() {
|
||||
return blockName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
59
Mage/src/mage/cards/repository/ExpansionInfo.java
Normal file
59
Mage/src/mage/cards/repository/ExpansionInfo.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
73
Mage/src/mage/cards/repository/ExpansionRepository.java
Normal file
73
Mage/src/mage/cards/repository/ExpansionRepository.java
Normal file
|
@ -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<ExpansionInfo, Object> 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<String> getSetCodes() {
|
||||
List<String> setCodes = new ArrayList<String>();
|
||||
try {
|
||||
List<ExpansionInfo> expansions = expansionDao.queryForAll();
|
||||
for (ExpansionInfo expansion : expansions) {
|
||||
setCodes.add(expansion.getCode());
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
}
|
||||
return setCodes;
|
||||
}
|
||||
|
||||
public List<ExpansionInfo> getAll() {
|
||||
try {
|
||||
return expansionDao.queryForAll();
|
||||
} catch (SQLException ex) {
|
||||
}
|
||||
return new ArrayList<ExpansionInfo>();
|
||||
}
|
||||
}
|
34
Mage/src/mage/cards/repository/RepositoryUtil.java
Normal file
34
Mage/src/mage/cards/repository/RepositoryUtil.java
Normal file
|
@ -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<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
|
||||
|
||||
QueryBuilder<DatabaseVersion, Object> queryBuilder = dbVersionDao.queryBuilder();
|
||||
queryBuilder.where().eq("entity", new SelectArg(entityName)).and().eq("version", version);
|
||||
List<DatabaseVersion> dbVersions = dbVersionDao.query(queryBuilder.prepare());
|
||||
|
||||
if (dbVersions.isEmpty()) {
|
||||
DatabaseVersion dbVersion = new DatabaseVersion();
|
||||
dbVersion.setEntity(entityName);
|
||||
dbVersion.setVersion(version);
|
||||
dbVersionDao.create(dbVersion);
|
||||
}
|
||||
return dbVersions.isEmpty();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue