diff --git a/src/main/java/org/beta/RedisDAO.java b/src/main/java/org/beta/RedisDAO.java index 42687ca58..bb4e56c42 100644 --- a/src/main/java/org/beta/RedisDAO.java +++ b/src/main/java/org/beta/RedisDAO.java @@ -97,10 +97,7 @@ public void commandSucceeded(CommandSucceededEvent event) { connection = redisClient.connect(); syncCommands = connection.sync(); - - - - saveCollection(new MagicCollection("Library")); + initDefaultData(); } @@ -357,6 +354,7 @@ public int saveOrUpdateContact(Contact c) throws SQLException { if(c.getId()<0) c.setId(incr(Contact.class).intValue()); + syncCommands.set(key(c), serialiser.toJson(c)); return c.getId(); diff --git a/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractKeyValueDao.java b/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractKeyValueDao.java index 2a4bc62a2..042cce88d 100644 --- a/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractKeyValueDao.java +++ b/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractKeyValueDao.java @@ -1,6 +1,8 @@ package org.magic.api.interfaces.abstracts.extra; +import java.sql.SQLException; + import org.magic.api.beans.Announce; import org.magic.api.beans.MagicCard; import org.magic.api.beans.MagicCardAlert; @@ -15,6 +17,7 @@ import org.magic.api.beans.technical.GedEntry; import org.magic.api.interfaces.MTGSerializable; import org.magic.api.interfaces.abstracts.AbstractMagicDAO; +import org.magic.services.MTGConstants; public abstract class AbstractKeyValueDao extends AbstractMagicDAO { @@ -36,7 +39,28 @@ public boolean isSQL() { protected static final String KEY_NEWS ="news"; protected static final String KEY_GED ="ged"; + + @Override + public void init() throws SQLException { + initDefaultData(); + + } + + protected void initDefaultData() throws SQLException + { + + if(listCollections().isEmpty()) { + for(String s : MTGConstants.getDefaultCollectionsNames()) + saveCollection(s); + } + + if(getContactById(1)==null) + saveOrUpdateContact(MTGConstants.DEFAULT_CONTACT); + + } + + protected String key(GedEntry gedItem) { return KEY_GED+SEPARATOR+gedItem.getClasse().getSimpleName()+SEPARATOR+gedItem.getId(); } diff --git a/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractMagicSQLDAO.java b/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractMagicSQLDAO.java index 61d4d3bab..0c590a2ed 100644 --- a/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractMagicSQLDAO.java +++ b/src/main/java/org/magic/api/interfaces/abstracts/extra/AbstractMagicSQLDAO.java @@ -57,12 +57,12 @@ import org.magic.api.pool.impl.NoPool; import org.magic.services.MTGControler; import org.magic.services.PluginRegistry; -import org.magic.services.SQLTools; import org.magic.services.TechnicalServiceManager; import org.magic.services.tools.CryptoUtils; import org.magic.services.tools.IDGenerator; import org.magic.services.tools.ImageTools; import org.magic.services.tools.MTG; +import org.magic.services.tools.SQLTools; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -232,11 +232,7 @@ private boolean createDB() throws SQLException { postCreation(stat); - logger.debug("populate default datas"); - stat.executeUpdate(hlper.insertDefaultCollections()); - stat.executeUpdate(hlper.insertMainContact()); - - + createIndex(stat); @@ -472,7 +468,9 @@ public void deleteAnnounce(Announce a) throws SQLException { protected void postCreation(Statement stat) throws SQLException { - //do nothing + logger.debug("populate default datas"); + stat.executeUpdate(hlper.insertDefaultCollections()); + stat.executeUpdate(hlper.insertMainContact()); } diff --git a/src/main/java/org/magic/main/ChangeDAO.java b/src/main/java/org/magic/main/ChangeDAO.java new file mode 100644 index 000000000..81c2f198f --- /dev/null +++ b/src/main/java/org/magic/main/ChangeDAO.java @@ -0,0 +1,53 @@ +package org.magic.main; + +import java.awt.BorderLayout; + +import javax.swing.JButton; +import javax.swing.JPanel; + +import org.magic.api.interfaces.MTGDao; +import org.magic.gui.abstracts.MTGUIComponent; +import org.magic.services.MTGConstants; +import org.magic.services.MTGControler; +import org.magic.services.tools.MTG; +import org.magic.services.tools.UITools; + +public class ChangeDAO { + public static void main(String[] args) + { + MTGControler.getInstance().loadAccountsConfiguration(); + + var cbo = UITools.createComboboxPlugins(MTGDao.class, true); + var btnValid = new JButton("Validate"); + + var pane = new JPanel(); + pane.setLayout(new BorderLayout()); + pane.add(cbo,BorderLayout.CENTER); + pane.add(btnValid,BorderLayout.SOUTH); + + + var diag = MTGUIComponent.createJDialog(MTGUIComponent.build(pane, "Change DAO", MTGConstants.ICON_TAB_DAO), false, false); + diag.setVisible(true); + + + btnValid.addActionListener(al->{ + + var selectedProvider = (MTGDao)cbo.getSelectedItem(); + selectedProvider.enable(true); + MTGControler.getInstance().setProperty(selectedProvider, selectedProvider.isEnable()); + + MTG.listPlugins(MTGDao.class).stream().filter(p->p!=selectedProvider).forEach(p->{ + p.enable(false); + MTGControler.getInstance().setProperty(p, p.isEnable()); + }); + + + System.exit(0); + + }); + + + + } + +} diff --git a/src/main/java/org/magic/services/MTGConstants.java b/src/main/java/org/magic/services/MTGConstants.java index 6e9bec0d9..081876bf3 100644 --- a/src/main/java/org/magic/services/MTGConstants.java +++ b/src/main/java/org/magic/services/MTGConstants.java @@ -20,6 +20,7 @@ import org.jdesktop.swingx.util.PaintUtils; import org.magic.api.beans.MagicCollection; import org.magic.api.beans.MagicEdition; +import org.magic.api.beans.shop.Contact; import org.magic.services.providers.IconSetProvider; import freemarker.template.Configuration; @@ -46,6 +47,22 @@ public static String[] getDefaultCollectionsNames() { return DEFAULT_COLLECTIONS_NAMES; } + public static final Contact DEFAULT_CONTACT; + + static { + DEFAULT_CONTACT = new Contact(); + DEFAULT_CONTACT.setId(1); + DEFAULT_CONTACT.setName("MTG"); + DEFAULT_CONTACT.setLastName("Companion"); + DEFAULT_CONTACT.setTelephone("123456789"); + DEFAULT_CONTACT.setAddress("Somewhere"); + DEFAULT_CONTACT.setCity("In the middle of nowhere"); + DEFAULT_CONTACT.setWebsite("https://www.mtgcompanion.org"); + DEFAULT_CONTACT.setEmail("mtgdesktopcompanion@gmail.com"); + DEFAULT_CONTACT.setActive(true); + DEFAULT_CONTACT.setEmailAccept(true); + } + //CONFIG FILES CONSTANTS public static final String MTG_DESKTOP_VERSION_FILE = "/version"; diff --git a/src/main/java/org/magic/services/SQLTools.java b/src/main/java/org/magic/services/tools/SQLTools.java similarity index 98% rename from src/main/java/org/magic/services/SQLTools.java rename to src/main/java/org/magic/services/tools/SQLTools.java index e2dbcbd26..191757b64 100644 --- a/src/main/java/org/magic/services/SQLTools.java +++ b/src/main/java/org/magic/services/tools/SQLTools.java @@ -1,4 +1,4 @@ -package org.magic.services; +package org.magic.services.tools; import org.apache.logging.log4j.Logger; import org.jooq.DSLContext; @@ -9,6 +9,8 @@ import org.jooq.conf.StatementType; import org.jooq.impl.DSL; import org.jooq.impl.SQLDataType; +import org.magic.services.MTGConstants; +import org.magic.services.TransactionService; import org.magic.services.logging.MTGLogger;