diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-07-27 14:33:25 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-07-27 14:33:38 -0700 |
commit | 4738428c2c205f42200386ae09b44b9ec07b9144 (patch) | |
tree | a6402978fe1b4655f90c3c8a181f4d246fbc5e89 /subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao | |
parent | 82ec8315f777c319f2372540098e21111019d629 (diff) | |
download | dsub-4738428c2c205f42200386ae09b44b9ec07b9144.tar.gz dsub-4738428c2c205f42200386ae09b44b9ec07b9144.tar.bz2 dsub-4738428c2c205f42200386ae09b44b9ec07b9144.zip |
Move subsonic-android to root
Diffstat (limited to 'subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao')
7 files changed, 0 insertions, 618 deletions
diff --git a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/AbstractDao.java b/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/AbstractDao.java deleted file mode 100644 index 86830b7c..00000000 --- a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/AbstractDao.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.sourceforge.subsonic.backend.dao; - -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; - -import java.util.List; - -/** - * Abstract superclass for all DAO's. - * - * @author Sindre Mehus - */ -public class AbstractDao { - private DaoHelper daoHelper; - - /** - * Returns a JDBC template for performing database operations. - * @return A JDBC template. - */ - public JdbcTemplate getJdbcTemplate() { - return daoHelper.getJdbcTemplate(); - } - - protected String questionMarks(String columns) { - int count = columns.split(", ").length; - StringBuffer buf = new StringBuffer(); - for (int i = 0; i < count; i++) { - buf.append('?'); - if (i < count - 1) { - buf.append(", "); - } - } - return buf.toString(); - } - - protected int update(String sql, Object... args) { - return getJdbcTemplate().update(sql, args); - } - - protected <T> List<T> query(String sql, RowMapper rowMapper, Object... args) { - return getJdbcTemplate().query(sql, args, rowMapper); - } - - protected <T> T queryOne(String sql, RowMapper rowMapper, Object... args) { - List<T> result = query(sql, rowMapper, args); - return result.isEmpty() ? null : result.get(0); - } - - protected Integer queryForInt(String sql, Integer defaultValue, Object... args) { - List<Integer> result = getJdbcTemplate().queryForList(sql, args, Integer.class); - return result.isEmpty() ? defaultValue : result.get(0) == null ? defaultValue : result.get(0); - } - - public void setDaoHelper(DaoHelper daoHelper) { - this.daoHelper = daoHelper; - } -} diff --git a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/DaoHelper.java b/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/DaoHelper.java deleted file mode 100644 index 2f5911f9..00000000 --- a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/DaoHelper.java +++ /dev/null @@ -1,84 +0,0 @@ -package net.sourceforge.subsonic.backend.dao; - -import net.sourceforge.subsonic.backend.dao.schema.Schema; -import net.sourceforge.subsonic.backend.dao.schema.Schema10; -import net.sourceforge.subsonic.backend.dao.schema.Schema20; -import net.sourceforge.subsonic.backend.Util; -import org.apache.log4j.Logger; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DriverManagerDataSource; - -import javax.sql.DataSource; -import java.io.File; - -/** - * DAO helper class which creates the data source, and updates the database schema. - * - * @author Sindre Mehus - */ -public class DaoHelper { - - private static final Logger LOG = Logger.getLogger(DaoHelper.class); - - private Schema[] schemas = {new Schema10(), new Schema20()}; - private DataSource dataSource; - private static boolean shutdownHookAdded; - - public DaoHelper() { - dataSource = createDataSource(); - checkDatabase(); - addShutdownHook(); - } - - private void addShutdownHook() { - if (shutdownHookAdded) { - return; - } - shutdownHookAdded = true; - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - System.err.println("Shutting down database."); - try { - getJdbcTemplate().execute("shutdown"); - System.err.println("Done."); - } catch (Throwable x) { - System.err.println("Failed to shut down database."); - x.printStackTrace(); - } - } - }); - } - - /** - * Returns a JDBC template for performing database operations. - * - * @return A JDBC template. - */ - public JdbcTemplate getJdbcTemplate() { - return new JdbcTemplate(dataSource); - } - - private DataSource createDataSource() { - File home = Util.getBackendHome(); - DriverManagerDataSource ds = new DriverManagerDataSource(); - ds.setDriverClassName("org.hsqldb.jdbcDriver"); - ds.setUrl("jdbc:hsqldb:file:" + home.getPath() + "/db/subsonic-backend"); - ds.setUsername("sa"); - ds.setPassword(""); - - return ds; - } - - private void checkDatabase() { - LOG.info("Checking database schema."); - try { - for (Schema schema : schemas) { - schema.execute(getJdbcTemplate()); - } - LOG.info("Done checking database schema."); - } catch (Exception x) { - LOG.error("Failed to initialize database.", x); - } - } -} diff --git a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/PaymentDao.java b/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/PaymentDao.java deleted file mode 100644 index e9f2eb21..00000000 --- a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/PaymentDao.java +++ /dev/null @@ -1,125 +0,0 @@ -package net.sourceforge.subsonic.backend.dao; - -import net.sourceforge.subsonic.backend.domain.Payment; -import org.apache.commons.lang.StringUtils; -import org.apache.log4j.Logger; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.core.simple.ParameterizedRowMapper; -import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Date; -import java.util.List; - -/** - * Provides database services for PayPal payments. - * - * @author Sindre Mehus - */ -public class PaymentDao extends AbstractDao { - - private static final Logger LOG = Logger.getLogger(PaymentDao.class); - private static final String COLUMNS = "id, transaction_id, transaction_type, item, " + - "payment_type, payment_status, payment_amount, payment_currency, " + - "payer_email, payer_email_lower, payer_first_name, payer_last_name, payer_country, " + - "processing_status, created, last_updated"; - - private RowMapper paymentRowMapper = new PaymentRowMapper(); - private RowMapper listRowMapper = new ParameterizedSingleColumnRowMapper<Integer>(); - - /** - * Returns the payment with the given transaction ID. - * - * @param transactionId The transaction ID. - * @return The payment or <code>null</code> if not found. - */ - public Payment getPaymentByTransactionId(String transactionId) { - String sql = "select " + COLUMNS + " from payment where transaction_id=?"; - return queryOne(sql, paymentRowMapper, transactionId); - } - - /** - * Returns the payment with the given payer email. - * - * @param email The payer email. - * @return The payment or <code>null</code> if not found. - */ - public Payment getPaymentByEmail(String email) { - if (email == null) { - return null; - } - String sql = "select " + COLUMNS + " from payment where payer_email_lower=?"; - return queryOne(sql, paymentRowMapper, email.toLowerCase()); - } - - /** - * Returns all payments with the given processing status. - * - * @param status The status. - * @return List of payments. - */ - public List<Payment> getPaymentsByProcessingStatus(Payment.ProcessingStatus status) { - return query("select " + COLUMNS + " from payment where processing_status=?", paymentRowMapper, status.name()); - } - - /** - * Creates a new payment. - * - * @param payment The payment to create. - */ - public void createPayment(Payment payment) { - String sql = "insert into payment (" + COLUMNS + ") values (" + questionMarks(COLUMNS) + ")"; - update(sql, null, payment.getTransactionId(), payment.getTransactionType(), payment.getItem(), - payment.getPaymentType(), payment.getPaymentStatus(), payment.getPaymentAmount(), - payment.getPaymentCurrency(), payment.getPayerEmail(), StringUtils.lowerCase(payment.getPayerEmail()), - payment.getPayerFirstName(), payment.getPayerLastName(), payment.getPayerCountry(), - payment.getProcessingStatus().name(), payment.getCreated(), payment.getLastUpdated()); - LOG.info("Created " + payment); - } - - /** - * Updates the given payment. - * - * @param payment The payment to update. - */ - public void updatePayment(Payment payment) { - String sql = "update payment set transaction_type=?, item=?, payment_type=?, payment_status=?, " + - "payment_amount=?, payment_currency=?, payer_email=?, payer_email_lower=?, payer_first_name=?, payer_last_name=?, " + - "payer_country=?, processing_status=?, created=?, last_updated=? where id=?"; - update(sql, payment.getTransactionType(), payment.getItem(), payment.getPaymentType(), payment.getPaymentStatus(), - payment.getPaymentAmount(), payment.getPaymentCurrency(), payment.getPayerEmail(), StringUtils.lowerCase(payment.getPayerEmail()), - payment.getPayerFirstName(), payment.getPayerLastName(), payment.getPayerCountry(), payment.getProcessingStatus().name(), - payment.getCreated(), payment.getLastUpdated(), payment.getId()); - LOG.info("Updated " + payment); - } - - public int getPaymentAmount(Date from, Date to) { - String sql = "select sum(payment_amount) from payment where created between ? and ?"; - return queryForInt(sql, 0, from, to); - } - - public boolean isBlacklisted(String email) { - String sql = "select 1 from blacklist where email=?"; - return queryOne(sql, listRowMapper, StringUtils.lowerCase(email)) != null; - } - - public boolean isWhitelisted(String email) { - String sql = "select 1 from whitelist where email=?"; - return queryOne(sql, listRowMapper, StringUtils.lowerCase(email)) != null; - } - - public void whitelist(String email) { - update("insert into whitelist(email) values (?)", StringUtils.lowerCase(email)); - } - - private static class PaymentRowMapper implements ParameterizedRowMapper<Payment> { - - public Payment mapRow(ResultSet rs, int rowNum) throws SQLException { - return new Payment(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), - rs.getString(6), rs.getInt(7), rs.getString(8), rs.getString(9), rs.getString(11), - rs.getString(12), rs.getString(13), Payment.ProcessingStatus.valueOf(rs.getString(14)), - rs.getTimestamp(15), rs.getTimestamp(16)); - } - } -}
\ No newline at end of file diff --git a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/RedirectionDao.java b/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/RedirectionDao.java deleted file mode 100644 index edd73222..00000000 --- a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/RedirectionDao.java +++ /dev/null @@ -1,96 +0,0 @@ -package net.sourceforge.subsonic.backend.dao; - -import net.sourceforge.subsonic.backend.domain.Redirection; -import org.apache.log4j.Logger; -import org.springframework.jdbc.core.simple.ParameterizedRowMapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides database services for xxx.subsonic.org redirections. - * - * @author Sindre Mehus - */ -public class RedirectionDao extends AbstractDao { - - private static final Logger LOG = Logger.getLogger(RedirectionDao.class); - private static final String COLUMNS = "id, license_holder, server_id, redirect_from, redirect_to, local_redirect_to, trial, trial_expires, last_updated, last_read, read_count"; - - private RedirectionRowMapper rowMapper = new RedirectionRowMapper(); - - /** - * Returns the redirection with the given "redirect from". - * - * @param redirectFrom The "redirect from" string. - * @return The redirection or <code>null</code> if not found. - */ - public Redirection getRedirection(String redirectFrom) { - String sql = "select " + COLUMNS + " from redirection where redirect_from=?"; - return queryOne(sql, rowMapper, redirectFrom); - } - - /** - * Returns all redirections with respect to the given row offset and count. - * - * @param offset Number of rows to skip. - * @param count Maximum number of rows to return. - * @return Redirections with respect to the given row offset and count. - */ - public List<Redirection> getAllRedirections(int offset, int count) { - if (count < 1) { - return new ArrayList<Redirection>(); - } - String sql = "select " + COLUMNS + " from redirection " + - "order by id " + - "limit " + count + " offset " + offset; - return query(sql, rowMapper); - } - - /** - * Creates a new redirection. - * - * @param redirection The redirection to create. - */ - public void createRedirection(Redirection redirection) { - String sql = "insert into redirection (" + COLUMNS + ") values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - update(sql, redirection.getLicenseHolder(), redirection.getServerId(), redirection.getRedirectFrom(), - redirection.getRedirectTo(), redirection.getLocalRedirectTo(), redirection.isTrial(), - redirection.getTrialExpires(), redirection.getLastUpdated(), - redirection.getLastRead(), redirection.getReadCount()); - LOG.info("Created redirection " + redirection.getRedirectFrom() + " -> " + redirection.getRedirectTo()); - } - - /** - * Updates the given redirection. - * - * @param redirection The redirection to update. - */ - public void updateRedirection(Redirection redirection) { - String sql = "update redirection set license_holder=?, server_id=?, redirect_from=?, redirect_to=?, " + - "local_redirect_to=?, trial=?, trial_expires=?, last_updated=?, last_read=?, read_count=? where id=?"; - update(sql, redirection.getLicenseHolder(), redirection.getServerId(), redirection.getRedirectFrom(), - redirection.getRedirectTo(), redirection.getLocalRedirectTo(), redirection.isTrial(), redirection.getTrialExpires(), - redirection.getLastUpdated(), redirection.getLastRead(), redirection.getReadCount(), redirection.getId()); - } - - /** - * Deletes all redirections with the given server ID. - * - * @param serverId The server ID. - */ - public void deleteRedirectionsByServerId(String serverId) { - update("delete from redirection where server_id=?", serverId); - LOG.info("Deleted redirections for server ID " + serverId); - } - - private static class RedirectionRowMapper implements ParameterizedRowMapper<Redirection> { - public Redirection mapRow(ResultSet rs, int rowNum) throws SQLException { - return new Redirection(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), - rs.getString(6), rs.getBoolean(7), rs.getTimestamp(8), rs.getTimestamp(9), rs.getTimestamp(10), - rs.getInt(11)); - } - } -} diff --git a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema.java b/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema.java deleted file mode 100644 index 850b82da..00000000 --- a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - This file is part of Subsonic. - - Subsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Subsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Subsonic. If not, see <http://www.gnu.org/licenses/>. - - Copyright 2009 (C) Sindre Mehus - */ -package net.sourceforge.subsonic.backend.dao.schema; - -import org.springframework.jdbc.core.JdbcTemplate; - -/** - * Used for creating and evolving the database schema. - * - * @author Sindre Mehus - */ -public abstract class Schema { - - /** - * Executes this schema. - * @param template The JDBC template to use. - */ - public abstract void execute(JdbcTemplate template); - - /** - * Returns whether the given table exists. - * @param template The JDBC template to use. - * @param table The table in question. - * @return Whether the table exists. - */ - protected boolean tableExists(JdbcTemplate template, String table) { - try { - template.execute("select 1 from " + table); - } catch (Exception x) { - return false; - } - return true; - } - - /** - * Returns whether the given column in the given table exists. - * @param template The JDBC template to use. - * @param column The column in question. - * @param table The table in question. - * @return Whether the column exists. - */ - protected boolean columnExists(JdbcTemplate template, String column, String table) { - try { - template.execute("select " + column + " from " + table + " where 1 = 0"); - } catch (Exception x) { - return false; - } - return true; - } -} diff --git a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema10.java b/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema10.java deleted file mode 100644 index 29c4c492..00000000 --- a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema10.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - This file is part of Subsonic. - - Subsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Subsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Subsonic. If not, see <http://www.gnu.org/licenses/>. - - Copyright 2009 (C) Sindre Mehus - */ -package net.sourceforge.subsonic.backend.dao.schema; - -import org.apache.log4j.Logger; -import org.springframework.jdbc.core.JdbcTemplate; - -/** - * Used for creating and evolving the database schema. - * This class implementes the database schema for Subsonic Backend version 1.0. - * - * @author Sindre Mehus - */ -public class Schema10 extends Schema { - private static final Logger LOG = Logger.getLogger(Schema10.class); - - public void execute(JdbcTemplate template) { - - /* - Example row 1: - - id: 123 - license_holder: sindre@activeobjects.no - server_id: 972342834928656 - redirect_from: sindre - redirect_to: http://23.45.123.56:8080/subsonic - local_redirect_to: http://192.168.0.7:80/subsonic - trial: false - trial_expires: null - - Example row 2: - - id: 124 - license_holder: null - server_id: 72121983567129 - redirect_from: joe - redirect_to: http://232.21.18.14/subsonic - local_redirect_to: http://192.168.0.7:80/subsonic - trial: true - trial_expires: 2010-01-13 05:34:17 - */ - - if (!tableExists(template, "redirection")) { - LOG.info("Database table 'redirection' not found. Creating it."); - template.execute("create cached table redirection (" + - "id identity," + - "license_holder varchar," + - "server_id varchar not null," + - "redirect_from varchar not null," + - "redirect_to varchar not null," + - "trial boolean not null," + - "trial_expires datetime," + - "last_updated datetime," + - "last_read datetime," + - "unique(redirect_from))"); - template.execute("create index idx_redirection_redirect_from on redirection(redirect_from)"); - template.execute("create index idx_redirection_server_id on redirection(server_id)"); - - createRedirection(template, "demo", "http://subsonic.org/demo"); - - LOG.info("Database table 'redirection' was created successfully."); - } - - if (!columnExists(template, "local_redirect_to", "redirection")) { - LOG.info("Database column 'redirection.local_redirect_to' not found. Creating it."); - template.execute("alter table redirection " + - "add local_redirect_to varchar"); - LOG.info("Database column 'redirection.local_redirect_to' was added successfully."); - } - - if (!columnExists(template, "read_count", "redirection")) { - LOG.info("Database column 'redirection.read_count' not found. Creating it."); - template.execute("alter table redirection " + - "add read_count int default 0 not null"); - LOG.info("Database column 'redirection.read_count' was added successfully."); - } - } - - private void createRedirection(JdbcTemplate template, String redirectFrom, String redirectTo) { - template.update("insert into redirection values (null, 'sindre@activeobjects.no', '-1', ?, ?, false, null, null, null)", - new Object[] {redirectFrom, redirectTo}); - LOG.info("Creating redirection from " + redirectFrom + " to " + redirectTo); - } -} diff --git a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema20.java b/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema20.java deleted file mode 100644 index 99ea679e..00000000 --- a/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema20.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - This file is part of Subsonic. - - Subsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Subsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Subsonic. If not, see <http://www.gnu.org/licenses/>. - - Copyright 2009 (C) Sindre Mehus - */ -package net.sourceforge.subsonic.backend.dao.schema; - -import org.apache.log4j.Logger; -import org.springframework.jdbc.core.JdbcTemplate; - -/** - * Used for creating and evolving the database schema. - * This class implementes the database schema for Subsonic Backend version 2.0. - * - * @author Sindre Mehus - */ -public class Schema20 extends Schema { - private static final Logger LOG = Logger.getLogger(Schema20.class); - - public void execute(JdbcTemplate template) { - - if (!tableExists(template, "payment")) { - LOG.info("Database table 'payment' not found. Creating it."); - template.execute("create cached table payment (" + - "id identity," + - "transaction_id varchar not null," + - "transaction_type varchar," + // cart, web_accept - "item varchar," + - "payment_type varchar," + // echeck, instant - "payment_status varchar," + // Completed, Pending, Denied, Failed, ... - "payment_amount int," + - "payment_currency varchar," + - "payer_email varchar," + - "payer_first_name varchar," + - "payer_last_name varchar," + - "payer_country varchar," + - "processing_status varchar not null," + - "created datetime," + - "last_updated datetime," + - "unique(transaction_id))"); - template.execute("create index idx_payment_transaction_id on payment(transaction_id)"); - template.execute("create index idx_payment_created on payment(created)"); - template.execute("create index idx_payment_payer_email on payment(payer_email)"); - - LOG.info("Database table 'payment' was created successfully."); - } - - if (!columnExists(template, "payer_email_lower", "payment")) { - LOG.info("Database column 'payment.payer_email_lower' not found. Creating it."); - template.execute("alter table payment " + - "add payer_email_lower varchar"); - template.execute("update payment set payer_email_lower=lcase(payer_email)"); - template.execute("create index idx_payment_payer_email_lower on payment(payer_email_lower)"); - LOG.info("Database column 'payment.payer_email_lower' was added successfully."); - } - - if (!tableExists(template, "whitelist")) { - LOG.info("Database table 'whitelist' not found. Creating it."); - template.execute("create cached table whitelist (" + - "id identity," + - "email varchar not null)"); - template.execute("create index idx_whitelist_email on whitelist(email)"); - - LOG.info("Database table 'whitelist' was created successfully."); - } - - if (!tableExists(template, "blacklist")) { - LOG.info("Database table 'blacklist' not found. Creating it."); - template.execute("create cached table blacklist (" + - "id identity," + - "email varchar not null)"); - template.execute("create index idx_blacklist_email on blacklist(email)"); - - LOG.info("Database table 'blacklist' was created successfully."); - } - } -}
\ No newline at end of file |