diff options
author | Alexander Alexeev | 2016-11-13 23:45:32 +0700 |
---|---|---|
committer | Alexander Alexeev | 2016-11-13 23:45:32 +0700 |
commit | 590c650f8ac3478b31fd3fd3382c9b35284844b7 (patch) | |
tree | 950de6ee245ba45aba1b1f147ec475ac921ab291 | |
parent | e20ab1d1f26d18211004ee8d1b64eea51d790253 (diff) |
jdbc service (repository) layer is added
23 files changed, 357 insertions, 0 deletions
diff --git a/server-core/src/main/java/com/juick/configuration/DataConfiguration.java b/server-core/src/main/java/com/juick/configuration/DataConfiguration.java index f31dbcdb..88fc00b3 100644 --- a/server-core/src/main/java/com/juick/configuration/DataConfiguration.java +++ b/server-core/src/main/java/com/juick/configuration/DataConfiguration.java @@ -2,6 +2,7 @@ package com.juick.configuration; import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; @@ -20,6 +21,7 @@ import javax.sql.DataSource; @Configuration @EnableTransactionManagement @PropertySource(value = {"classpath:juick.conf"}) +@ComponentScan(basePackages = {"com.juick.service"}) public class DataConfiguration implements TransactionManagementConfigurer { @Resource private Environment env; diff --git a/server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java b/server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java index 986c8275..61109c47 100644 --- a/server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java +++ b/server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java @@ -1,5 +1,7 @@ package com.juick.server.helpers; +import org.apache.commons.lang3.builder.ToStringBuilder; + /** * Created by vt on 03/09/16. */ @@ -7,6 +9,14 @@ public class ApplicationStatus { private boolean connected; private boolean crosspostEnabled; + @Override + public String toString() { + return new ToStringBuilder(this) + .append("connected", connected) + .append("crosspostEnabled", crosspostEnabled) + .toString(); + } + public boolean isConnected() { return connected; } diff --git a/server-core/src/main/java/com/juick/service/AdsService.java b/server-core/src/main/java/com/juick/service/AdsService.java new file mode 100644 index 00000000..f8a0f5cf --- /dev/null +++ b/server-core/src/main/java/com/juick/service/AdsService.java @@ -0,0 +1,11 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface AdsService { + + int getAdMid(int uid); + + int logAdMid(int uid, int mid); +} diff --git a/server-core/src/main/java/com/juick/service/AdsServiceImpl.java b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java new file mode 100644 index 00000000..32effd21 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java @@ -0,0 +1,43 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class AdsServiceImpl extends BaseJdbcDao implements AdsService { + + @Transactional(readOnly = true) + @Override + public int getAdMid(final int uid) { + List<Integer> list; + + if (uid > 0) + list = getJdbcTemplate().queryForList( + "SELECT message_id FROM ads_messages " + + "WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id = ? " + + "AND ts > UNIX_TIMESTAMP() - 60*60*24 GROUP BY message_id HAVING COUNT(*) > 2) ORDER BY RAND() LIMIT 1", + Integer.class, + uid); + + else + list = getJdbcTemplate().queryForList( + "SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", + Integer.class); + + return list.isEmpty() ? + 0 : list.get(0); + } + + @Transactional + @Override + public int logAdMid(final int uid, final int mid) { + return getJdbcTemplate().update( + "INSERT INTO ads_messages_log(user_id, message_id, ts) VALUES (?, ?, UNIX_TIMESTAMP())", + uid, mid); + } +} diff --git a/server-core/src/main/java/com/juick/service/BaseJdbcDao.java b/server-core/src/main/java/com/juick/service/BaseJdbcDao.java new file mode 100644 index 00000000..948e6e6b --- /dev/null +++ b/server-core/src/main/java/com/juick/service/BaseJdbcDao.java @@ -0,0 +1,42 @@ +package com.juick.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.util.Assert; + +import javax.inject.Inject; + +/** + * Created by aalexeev on 11/13/16. + */ +public class BaseJdbcDao implements InitializingBean { + protected final Logger logger = LoggerFactory.getLogger(getClass()); + + private JdbcTemplate jdbcTemplate; + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + protected JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + protected NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { + return namedParameterJdbcTemplate; + } + + @Override + public void afterPropertiesSet() throws Exception { + logger.debug("Checking required beans"); + + Assert.notNull(jdbcTemplate); + Assert.notNull(namedParameterJdbcTemplate); + } + + @Inject + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); + } +} diff --git a/server-core/src/main/java/com/juick/service/CrosspostService.java b/server-core/src/main/java/com/juick/service/CrosspostService.java new file mode 100644 index 00000000..88821b60 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/CrosspostService.java @@ -0,0 +1,24 @@ +package com.juick.service; + +import com.juick.server.helpers.ApplicationStatus; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.Optional; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface CrosspostService { + + Optional<Pair<String, String>> getTwitterTokens(int uid); + + Optional<String> getFacebookToken(int uid); + + ApplicationStatus getFbCrossPostStatus(int uid); + + String getTwitterName(int uid); + + String getTelegramName(int uid); + + Optional<Pair<String, String>> getVkTokens(int uid); +} diff --git a/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java new file mode 100644 index 00000000..6c6f7442 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java @@ -0,0 +1,89 @@ +package com.juick.service; + +import com.juick.server.helpers.ApplicationStatus; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class CrosspostServiceImpl extends BaseJdbcDao implements CrosspostService { + + @Transactional(readOnly = true) + @Override + public Optional<Pair<String, String>> getTwitterTokens(final int uid) { + List<Optional<Pair<String, String>>> list = getJdbcTemplate().query( + "SELECT access_token,access_token_secret FROM twitter WHERE user_id = ? AND crosspost = 1", + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), + uid); + + return list.isEmpty() ? + Optional.empty() : list.get(0); + } + + @Override + public Optional<String> getFacebookToken(final int uid) { + List<String> list = getJdbcTemplate().queryForList( + "SELECT access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1", + String.class, + uid); + return list.isEmpty() ? + Optional.empty() : Optional.of(list.get(0)); + } + + @Override + public ApplicationStatus getFbCrossPostStatus(final int uid) { + List<ApplicationStatus> list = getJdbcTemplate().query( + "SELECT 1, crosspost FROM facebook WHERE user_id = ? LIMIT 1", + (rs, num) -> { + ApplicationStatus status = new ApplicationStatus(); + + status.setConnected(rs.getInt(1) > 0); + status.setCrosspostEnabled(rs.getBoolean(2)); + + return status; + }, + uid); + + return list.isEmpty() ? + new ApplicationStatus() : list.get(0); + } + + @Override + public String getTwitterName(final int uid) { + List<String> list = getJdbcTemplate().queryForList( + "SELECT uname FROM twitter WHERE user_id = ?", + String.class, + uid); + + return list.isEmpty() ? + "" : list.get(0); + } + + @Override + public String getTelegramName(final int uid) { + List<String> list = getJdbcTemplate().queryForList( + "SELECT tg_name FROM telegram WHERE user_id = ?", + String.class, + uid); + + return list.isEmpty() ? + "" : list.get(0); + } + + @Override + public Optional<Pair<String, String>> getVkTokens(final int uid) { + List<Optional<Pair<String, String>>> list = getJdbcTemplate().query( + "SELECT vk_id, access_token FROM vk WHERE user_id = ? AND crosspost = 1", + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), + uid); + + return list.isEmpty() ? + Optional.empty() : list.get(0); + } +} diff --git a/server-core/src/main/java/com/juick/service/MessagesService.java b/server-core/src/main/java/com/juick/service/MessagesService.java new file mode 100644 index 00000000..23ccf8e9 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/MessagesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface MessagesService { +} diff --git a/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java new file mode 100644 index 00000000..1955d9f9 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class MessagesServiceImpl extends BaseJdbcDao implements MessagesService { +} diff --git a/server-core/src/main/java/com/juick/service/PMQueriesService.java b/server-core/src/main/java/com/juick/service/PMQueriesService.java new file mode 100644 index 00000000..5e2e2b75 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PMQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PMQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java new file mode 100644 index 00000000..4c8f4ae9 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class PMQueriesServiceImpl extends BaseJdbcDao implements PushQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java b/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java new file mode 100644 index 00000000..29116081 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PrivacyQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java new file mode 100644 index 00000000..d4e43728 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class PrivacyQueriesServiceImpl extends BaseJdbcDao implements PrivacyQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PushQueriesService.java b/server-core/src/main/java/com/juick/service/PushQueriesService.java new file mode 100644 index 00000000..dbb5ac06 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PushQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PushQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java new file mode 100644 index 00000000..0381f913 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class PushQueriesServiceImpl extends BaseJdbcDao implements PushQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/ShowQueriesService.java b/server-core/src/main/java/com/juick/service/ShowQueriesService.java new file mode 100644 index 00000000..2d42988d --- /dev/null +++ b/server-core/src/main/java/com/juick/service/ShowQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface ShowQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java new file mode 100644 index 00000000..d3470c75 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class ShowQueriesServiceImpl extends BaseJdbcDao implements SubscriptionService { +} diff --git a/server-core/src/main/java/com/juick/service/SubscriptionService.java b/server-core/src/main/java/com/juick/service/SubscriptionService.java new file mode 100644 index 00000000..0ed3f55a --- /dev/null +++ b/server-core/src/main/java/com/juick/service/SubscriptionService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface SubscriptionService { +} diff --git a/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java new file mode 100644 index 00000000..811f5af0 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class SubscriptionServiceImpl extends BaseJdbcDao implements SubscriptionService { +} diff --git a/server-core/src/main/java/com/juick/service/TagService.java b/server-core/src/main/java/com/juick/service/TagService.java new file mode 100644 index 00000000..1083d663 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/TagService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface TagService { +} diff --git a/server-core/src/main/java/com/juick/service/TagServiceImpl.java b/server-core/src/main/java/com/juick/service/TagServiceImpl.java new file mode 100644 index 00000000..9649ce74 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/TagServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class TagServiceImpl extends BaseJdbcDao implements TagService { +} diff --git a/server-core/src/main/java/com/juick/service/UserService.java b/server-core/src/main/java/com/juick/service/UserService.java new file mode 100644 index 00000000..ecf8ad07 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/UserService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface UserService { +} diff --git a/server-core/src/main/java/com/juick/service/UserServiceImpl.java b/server-core/src/main/java/com/juick/service/UserServiceImpl.java new file mode 100644 index 00000000..93dc90dc --- /dev/null +++ b/server-core/src/main/java/com/juick/service/UserServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class UserServiceImpl extends BaseJdbcDao implements UserService { +} |