Search in sources :

Example 16 with MagicCard

use of org.magic.api.beans.MagicCard in project MtgDesktopCompanion by nicho92.

the class HsqlDAO method listCards.

@Override
public List<MagicCard> listCards() throws SQLException {
    logger.trace("list all cards");
    String sql = "select mcard from cards";
    try (PreparedStatement pst = con.prepareStatement(sql)) {
        try (ResultSet rs = pst.executeQuery()) {
            List<MagicCard> ret = new ArrayList<>();
            while (rs.next()) {
                try {
                    ret.add((MagicCard) rs.getObject(cardField));
                } catch (Exception e) {
                    throw new SQLException("ERROR", e);
                }
            }
            return ret;
        }
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 17 with MagicCard

use of org.magic.api.beans.MagicCard in project MtgDesktopCompanion by nicho92.

the class MongoDbDAO method listStocks.

@Override
public List<MagicCardStock> listStocks(MagicCard mc, MagicCollection col) throws SQLException {
    ArrayList<MagicCardStock> ret = new ArrayList<>();
    BasicDBObject filter = new BasicDBObject(dbCardIDField, IDGenerator.generate(mc));
    filter.put("stockItem.magicCollection.name", col.getName());
    logger.debug(filter);
    db.getCollection(colStocks, BasicDBObject.class).find(filter).forEach((Consumer<BasicDBObject>) result -> ret.add(deserialize(result.get(dbStockField).toString(), MagicCardStock.class)));
    return ret;
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) Document(org.bson.Document) Arrays(java.util.Arrays) MagicCollection(org.magic.api.beans.MagicCollection) MongoCollection(com.mongodb.client.MongoCollection) MagicCardAlert(org.magic.api.beans.MagicCardAlert) Aggregates(com.mongodb.client.model.Aggregates) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) MongoDatabase(com.mongodb.client.MongoDatabase) MagicCardStock(org.magic.api.beans.MagicCardStock) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) STATUT(org.magic.api.interfaces.MTGCardsProvider.STATUT) Filters(com.mongodb.client.model.Filters) SQLException(java.sql.SQLException) MongoClientOptions(com.mongodb.MongoClientOptions) UpdateResult(com.mongodb.client.result.UpdateResult) Gson(com.google.gson.Gson) Map(java.util.Map) AbstractMagicDAO(org.magic.api.interfaces.abstracts.AbstractMagicDAO) CodecRegistries.fromRegistries(org.bson.codecs.configuration.CodecRegistries.fromRegistries) ServerAddress(com.mongodb.ServerAddress) PojoCodecProvider(org.bson.codecs.pojo.PojoCodecProvider) Accumulators(com.mongodb.client.model.Accumulators) MagicNews(org.magic.api.beans.MagicNews) CodecRegistries.fromProviders(org.bson.codecs.configuration.CodecRegistries.fromProviders) BasicDBObject(com.mongodb.BasicDBObject) IOException(java.io.IOException) File(java.io.File) Consumer(java.util.function.Consumer) List(java.util.List) MagicEdition(org.magic.api.beans.MagicEdition) TreeMap(java.util.TreeMap) IDGenerator(org.magic.tools.IDGenerator) MongoClient(com.mongodb.MongoClient) MagicCard(org.magic.api.beans.MagicCard) DeleteResult(com.mongodb.client.result.DeleteResult) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) MagicCardStock(org.magic.api.beans.MagicCardStock)

Example 18 with MagicCard

use of org.magic.api.beans.MagicCard in project MtgDesktopCompanion by nicho92.

the class MysqlDAO method listCardsFromCollection.

@Override
public List<MagicCard> listCardsFromCollection(MagicCollection collection, MagicEdition me) throws SQLException {
    logger.debug("getCardsFromCollection " + collection + " " + me);
    String sql = "select * from cards where collection= ?";
    if (me != null)
        sql = "select * from cards where collection= ? and edition = ?";
    try (PreparedStatement pst = con.prepareStatement(sql)) {
        pst.setString(1, collection.getName());
        if (me != null)
            pst.setString(2, me.getId());
        try (ResultSet rs = pst.executeQuery()) {
            List<MagicCard> ret = new ArrayList<>();
            while (rs.next()) {
                ret.add((MagicCard) rs.getObject(cardFieldName));
            }
            return ret;
        }
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 19 with MagicCard

use of org.magic.api.beans.MagicCard in project MtgDesktopCompanion by nicho92.

the class PostgresqlDAO method listCardsFromCollection.

@Override
public List<MagicCard> listCardsFromCollection(MagicCollection collection, MagicEdition me) throws SQLException {
    logger.debug("getCardsFromCollection " + collection + " " + me);
    String sql = "select * from cards where collection= ?";
    if (me != null)
        sql = "select * from cards where collection= ? and edition = ?";
    try (PreparedStatement pst = con.prepareStatement(sql)) {
        pst.setString(1, collection.getName());
        if (me != null)
            pst.setString(2, me.getId());
        try (ResultSet rs = pst.executeQuery()) {
            List<MagicCard> ret = new ArrayList<>();
            while (rs.next()) {
                ret.add(readObject(MagicCard.class, rs.getBinaryStream(mcardField)));
            }
            return ret;
        }
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 20 with MagicCard

use of org.magic.api.beans.MagicCard in project MtgDesktopCompanion by nicho92.

the class MTGSalvationDeckSniffer method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    String url = info.getUrl() + "#Details:deck-export";
    logger.debug("sniff url : " + url);
    MagicDeck deck = new MagicDeck();
    deck.setName(info.getName());
    deck.setDateCreation(new Date());
    Document d = Jsoup.connect(url).userAgent(getString("USER_AGENT")).get();
    deck.setDescription(info.getUrl().toString() + "\n" + d.select("section.guide div").text());
    for (Element a : d.select("span.deck-type")) deck.getTags().add(a.text());
    String plainDeck = d.select("section.deck-export-section pre").get(1).text();
    boolean sideboard = false;
    List<String> elements = new ArrayList<>(Arrays.asList(plainDeck.split("\n")));
    elements.remove(0);
    for (String s : elements) {
        if (s.toLowerCase().startsWith("sideboard")) {
            sideboard = true;
        } else if (s.length() > 1) {
            try {
                int qte = Integer.parseInt(s.substring(0, s.indexOf(' ')));
                String cardName = s.substring(s.indexOf(' '), s.length()).trim();
                MagicEdition ed = null;
                if (cardName.trim().equalsIgnoreCase("Plains") || cardName.trim().equalsIgnoreCase("Island") || cardName.trim().equalsIgnoreCase("Swamp") || cardName.trim().equalsIgnoreCase("Mountain") || cardName.trim().equalsIgnoreCase("Forest")) {
                    ed = new MagicEdition();
                    ed.setId(MTGControler.getInstance().get("default-land-deck"));
                }
                MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true).get(0);
                if (!sideboard) {
                    deck.getMap().put(mc, qte);
                } else {
                    deck.getMapSideBoard().put(mc, qte);
                }
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
    return deck;
}
Also used : MagicCard(org.magic.api.beans.MagicCard) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) MagicEdition(org.magic.api.beans.MagicEdition) Document(org.jsoup.nodes.Document) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MagicDeck(org.magic.api.beans.MagicDeck)

Aggregations

MagicCard (org.magic.api.beans.MagicCard)106 MagicEdition (org.magic.api.beans.MagicEdition)40 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)28 MagicDeck (org.magic.api.beans.MagicDeck)21 MagicCollection (org.magic.api.beans.MagicCollection)14 SQLException (java.sql.SQLException)12 Date (java.util.Date)11 MagicCardStock (org.magic.api.beans.MagicCardStock)10 DisplayableCard (org.magic.game.gui.components.DisplayableCard)10 JsonArray (com.google.gson.JsonArray)8 List (java.util.List)8 FileReader (java.io.FileReader)7 Before (org.junit.Before)7 BufferedReader (java.io.BufferedReader)6 File (java.io.File)6 FileWriter (java.io.FileWriter)6 MalformedURLException (java.net.MalformedURLException)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6