From 19709fa2ade17433b9a76ad4459e1e09d27b88bf Mon Sep 17 00:00:00 2001
From: Alexander Alexeev
Date: Thu, 17 Nov 2016 23:45:13 +0700
Subject: repository improvenments: using Collection in params, forse using
exists in some query, force using Lists as query result
---
.../com/juick/service/MessagesServiceImpl.java | 926 ++++++++++-----------
1 file changed, 435 insertions(+), 491 deletions(-)
(limited to 'juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java')
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
index bb030ed4..6fc71ac3 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -5,24 +5,24 @@ import com.juick.Tag;
import com.juick.User;
import com.juick.server.helpers.PrivacyOpts;
import com.juick.util.MessageUtils;
+import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringEscapeUtils;
-import org.springframework.dao.EmptyResultDataAccessException;
+import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
+import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
-import org.springframework.util.StringUtils;
import javax.inject.Inject;
import java.sql.*;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
/**
* Created by aalexeev on 11/13/16.
@@ -77,85 +77,92 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
}
}
+ /**
+ * @param uid
+ * @param txt
+ * @param attachment
+ * @param tags
+ * @return
+ * @see Java, JDBC and MySQL Types
+ */
@Transactional
@Override
- public int createMessage(final int uid, final String txt, final String attachment, final List tags) {
+ public int createMessage(final int uid, final String txt, final String attachment, final Collection tags) {
KeyHolder holder = new GeneratedKeyHolder();
- getJdbcTemplate().update(con -> {
- PreparedStatement stmt = con.prepareStatement(
- "INSERT INTO messages(user_id,attach) VALUES (?,?)",
- Statement.RETURN_GENERATED_KEYS);
- stmt.setInt(1, uid);
- if (attachment != null) {
- stmt.setString(2, attachment);
- } else {
- stmt.setNull(2, Types.VARCHAR);
- }
- return stmt;
- }, holder);
+ getJdbcTemplate().update(
+ con -> {
+ PreparedStatement stmt = con.prepareStatement(
+ "INSERT INTO messages(user_id, attach) VALUES (?, ?)",
+ Statement.RETURN_GENERATED_KEYS);
+ stmt.setInt(1, uid);
+ if (attachment != null)
+ stmt.setString(2, attachment);
+ else
+ stmt.setNull(2, Types.CHAR);
+
+ return stmt;
+ },
+ holder);
int mid = holder.getKey().intValue();
if (mid > 0) {
String tagsNames = "";
- String tagsIDs = "";
- for (int i = 0; i < tags.size(); i++) {
- if (i > 0) {
- tagsNames += " ";
- tagsIDs += ",";
+ if (CollectionUtils.isNotEmpty(tags)) {
+ StringBuilder tasNamesBuilder = new StringBuilder();
+ List