Search in sources :

Example 26 with MagicCollection

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

the class PostgresqlDAO method getCollections.

@Override
public List<MagicCollection> getCollections() throws SQLException {
    try (PreparedStatement pst = con.prepareStatement("select * from collections");
        ResultSet rs = pst.executeQuery()) {
        List<MagicCollection> colls = new ArrayList<>();
        while (rs.next()) {
            MagicCollection mc = new MagicCollection();
            mc.setName(rs.getString(1));
            colls.add(mc);
        }
        return colls;
    }
}
Also used : ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) MagicCollection(org.magic.api.beans.MagicCollection)

Example 27 with MagicCollection

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

the class CollectionPanelGUI method initPopupCollection.

public void initPopupCollection() throws SQLException {
    popupMenuEdition = new JPopupMenu();
    popupMenuCards = new JPopupMenu();
    JMenu menuItemAdd = new JMenu(MTGControler.getInstance().getLangService().getCapitalize("ADD_MISSING_CARDS_IN"));
    JMenu menuItemMove = new JMenu(MTGControler.getInstance().getLangService().getCapitalize("MOVE_CARD_TO"));
    for (MagicCollection mc : dao.getCollections()) {
        JMenuItem adds = new JMenuItem(mc.getName());
        JMenuItem movs = new JMenuItem(mc.getName());
        movs.addActionListener(e -> {
            DefaultMutableTreeNode nodeCol = ((DefaultMutableTreeNode) path.getPathComponent(1));
            DefaultMutableTreeNode nodeCd = ((DefaultMutableTreeNode) path.getPathComponent(3));
            MagicCard card = (MagicCard) nodeCd.getUserObject();
            MagicCollection oldCol = (MagicCollection) nodeCol.getUserObject();
            final String collec = ((JMenuItem) e.getSource()).getText();
            MagicCollection nmagicCol = new MagicCollection();
            nmagicCol.setName(collec);
            try {
                dao.removeCard(card, oldCol);
                dao.saveCard(card, nmagicCol);
                nodeCd.removeFromParent();
                nodeCol.add(new DefaultMutableTreeNode(card));
                tree.refresh();
            } catch (SQLException e1) {
                logger.error(e1);
            }
        });
        adds.addActionListener(e -> {
            final String destinationCollection = ((JMenuItem) e.getSource()).getText();
            ThreadManager.getInstance().execute(() -> {
                try {
                    DefaultMutableTreeNode node = ((DefaultMutableTreeNode) path.getPathComponent(2));
                    MagicEdition me = (MagicEdition) node.getUserObject();
                    MagicCollection col = new MagicCollection();
                    col.setName(destinationCollection);
                    List<MagicCard> sets = provider.searchCardByCriteria("set", me.getId(), null, false);
                    MagicCollection sourceCol = new MagicCollection();
                    sourceCol.setName(node.getPath()[1].toString());
                    List<MagicCard> list = dao.listCardsFromCollection(sourceCol, me);
                    sets.removeAll(list);
                    for (MagicCard m : sets) dao.saveCard(m, col);
                    tree.refresh();
                } catch (Exception e1) {
                    MTGLogger.printStackTrace(e1);
                }
            }, "btnAdds addCardsCollection");
        });
        menuItemAdd.add(adds);
        menuItemMove.add(movs);
    }
    JMenuItem menuItemOpen = new JMenuItem(MTGControler.getInstance().getLangService().getCapitalize("OPEN"));
    menuItemOpen.addActionListener(e -> {
        MagicCollection col = (MagicCollection) ((DefaultMutableTreeNode) path.getPathComponent(1)).getUserObject();
        MagicEdition edition = (MagicEdition) ((DefaultMutableTreeNode) path.getPathComponent(2)).getUserObject();
        try {
            CardSearchPanel.getInstance().open(MTGControler.getInstance().getEnabledDAO().listCardsFromCollection(col, edition));
        } catch (SQLException e1) {
            logger.error(e1);
        }
    });
    popupMenuEdition.add(menuItemOpen);
    JMenuItem it = new JMenuItem(MTGControler.getInstance().getLangService().getCapitalize("MASS_MOVEMENTS"));
    it.addActionListener(e -> {
        MagicCollection col = (MagicCollection) ((DefaultMutableTreeNode) path.getPathComponent(1)).getUserObject();
        MagicEdition edition = (MagicEdition) ((DefaultMutableTreeNode) path.getPathComponent(2)).getUserObject();
        MassMoverDialog d = new MassMoverDialog(col, edition);
        d.setVisible(true);
        logger.debug("closing mass import with change =" + d.hasChange());
    });
    popupMenuEdition.add(it);
    popupMenuEdition.add(menuItemAdd);
    popupMenuCards.add(menuItemMove);
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) MagicCard(org.magic.api.beans.MagicCard) SQLException(java.sql.SQLException) MagicEdition(org.magic.api.beans.MagicEdition) JMenuItem(javax.swing.JMenuItem) JPopupMenu(javax.swing.JPopupMenu) JMenu(javax.swing.JMenu) MagicCollection(org.magic.api.beans.MagicCollection) SQLException(java.sql.SQLException) IOException(java.io.IOException) MassMoverDialog(org.magic.gui.components.dialog.MassMoverDialog)

Example 28 with MagicCollection

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

the class MagicWebSiteGenerator method generate.

public void generate(List<MagicCollection> cols, List<MTGPricesProvider> providers) throws TemplateException, IOException, SQLException {
    this.pricesProvider = providers;
    this.cols = cols;
    Template generatedTemplate = cfg.getTemplate("index.html");
    try (Writer out = new FileWriter(Paths.get(dest, "index.htm").toFile())) {
        Map<String, List<MagicCard>> root = new HashMap<>();
        for (MagicCollection col : cols) root.put(col.getName(), dao.listCardsFromCollection(col));
        generatedTemplate.process(root, out);
        generateCollectionsTemplate();
    }
}
Also used : HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) List(java.util.List) FileWriter(java.io.FileWriter) Writer(java.io.Writer) MagicCollection(org.magic.api.beans.MagicCollection) Template(freemarker.template.Template)

Example 29 with MagicCollection

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

the class JSONHttpServer method start.

@Override
public void start() throws IOException {
    port(getInt("SERVER-PORT"));
    exception(Exception.class, new ExceptionHandler<Exception>() {

        @Override
        public void handle(Exception exception, Request req, Response res) {
            res.status(500);
            res.body("{\"error\":\"" + exception + "\"}");
        }
    });
    notFound((req, res) -> {
        res.status(404);
        return "{\"error\":\"not found\"}";
    });
    before("/*", (request, response) -> {
        response.type(getString("MIME"));
        logger.info("Received api call from " + request.ip());
    });
    get("/cards/search/:att/:val", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().searchCardByCriteria(request.params(":att"), request.params(":val"), null, false);
    }, transformer);
    post("/cards/move/:from/:to/:id", getString("MIME"), (request, response) -> {
        MagicCollection from = new MagicCollection(request.params(":from"));
        MagicCollection to = new MagicCollection(request.params(":to"));
        MagicCard mc = MTGControler.getInstance().getEnabledProviders().getCardById(request.params(":id"));
        MTGControler.getInstance().getEnabledDAO().removeCard(mc, from);
        MTGControler.getInstance().getEnabledDAO().saveCard(mc, to);
        return "OK";
    }, transformer);
    get("/cards/list/:col/:idEd", getString("MIME"), (request, response) -> {
        MagicCollection col = new MagicCollection(request.params(":col"));
        MagicEdition ed = new MagicEdition();
        ed.setId(request.params(":idEd"));
        ed.setSet(request.params(":idEd"));
        return MTGControler.getInstance().getEnabledDAO().listCardsFromCollection(col, ed);
    }, transformer);
    get("/cards/:id", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().getCardById(request.params(":id"));
    }, transformer);
    get("/collections/list", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledDAO().getCollections();
    }, transformer);
    get("/collections/:name", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledDAO().getCollection(request.params(":name"));
    }, transformer);
    get("/editions/list", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().loadEditions();
    }, transformer);
    get("/editions/:idSet", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().getSetById(request.params(":idSet"));
    }, transformer);
    get("/editions/list/:colName", getString("MIME"), (request, response) -> {
        List<MagicEdition> eds = new ArrayList<>();
        List<String> list = MTGControler.getInstance().getEnabledDAO().getEditionsIDFromCollection(new MagicCollection(request.params(":colName")));
        for (String s : list) eds.add(MTGControler.getInstance().getEnabledProviders().getSetById(s));
        Collections.sort(eds);
        return eds;
    }, transformer);
    get("/prices/:idSet/:name", getString("MIME"), (request, response) -> {
        MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", request.params(":name"), null, false).get(0);
        MagicEdition ed = MTGControler.getInstance().getEnabledProviders().getSetById(request.params(":idSet"));
        List<MagicPrice> pricesret = new ArrayList<>();
        for (MTGPricesProvider prices : MTGControler.getInstance().getEnabledPricers()) pricesret.addAll(prices.getPrice(ed, mc));
        return pricesret;
    }, transformer);
    if (getBoolean("ENABLE_GZIP")) {
        after((request, response) -> {
            response.header("Content-Encoding", "gzip");
        });
    }
    Spark.init();
    logger.info("Server start on port " + getString("SERVER-PORT"));
    running = true;
}
Also used : MTGPricesProvider(org.magic.api.interfaces.MTGPricesProvider) MagicPrice(org.magic.api.beans.MagicPrice) Request(spark.Request) MagicEdition(org.magic.api.beans.MagicEdition) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Response(spark.Response) MagicCard(org.magic.api.beans.MagicCard) MagicCollection(org.magic.api.beans.MagicCollection)

Example 30 with MagicCollection

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

the class MagicEditionsTableModel method calculate.

public void calculate() throws SQLException {
    MagicCollection mc = new MagicCollection(MTGControler.getInstance().get("default-library"));
    Map<String, Integer> temp = MTGControler.getInstance().getEnabledDAO().getCardsCountGlobal(mc);
    countDefaultLibrary = 0;
    countTotal = 0;
    for (MagicEdition me : list) {
        mapCount.put(me, (temp.get(me.getId()) == null) ? 0 : temp.get(me.getId()));
        countDefaultLibrary += mapCount.get(me);
    }
    for (MagicEdition me : list) countTotal += me.getCardCount();
}
Also used : MagicEdition(org.magic.api.beans.MagicEdition) MagicCollection(org.magic.api.beans.MagicCollection)

Aggregations

MagicCollection (org.magic.api.beans.MagicCollection)32 ArrayList (java.util.ArrayList)20 MagicCard (org.magic.api.beans.MagicCard)15 SQLException (java.sql.SQLException)14 MagicEdition (org.magic.api.beans.MagicEdition)13 PreparedStatement (java.sql.PreparedStatement)12 ResultSet (java.sql.ResultSet)12 IOException (java.io.IOException)9 MagicCardStock (org.magic.api.beans.MagicCardStock)8 File (java.io.File)7 List (java.util.List)6 BorderLayout (java.awt.BorderLayout)4 Map (java.util.Map)4 JButton (javax.swing.JButton)4 JLabel (javax.swing.JLabel)4 JPanel (javax.swing.JPanel)4 JScrollPane (javax.swing.JScrollPane)4 BasicDBObject (com.mongodb.BasicDBObject)3 DeleteResult (com.mongodb.client.result.DeleteResult)3 Point (java.awt.Point)3