Commit 3660d6d3 by Samir Sadykhov

fix добавление строк в таблицу doc_number_input, теперь группами

parent 1404524c
...@@ -9,13 +9,14 @@ import java.sql.Connection; ...@@ -9,13 +9,14 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class DocumentDAO { public class DocumentDAO {
private static final Logger LOGGER = LoggerFactory.getLogger(DocumentDAO.class); private static final Logger LOGGER = LoggerFactory.getLogger(DocumentDAO.class);
public boolean updateUserMask(String documentID, String numberTemplate) throws Exception { public boolean updateUserMask(String documentID, String numberTemplate) throws Exception {
Connection con = null; Connection con = null;
try { try {
...@@ -23,125 +24,137 @@ public class DocumentDAO { ...@@ -23,125 +24,137 @@ public class DocumentDAO {
con = ConnectionPool.getConnection(); con = ConnectionPool.getConnection();
con.setAutoCommit(false); con.setAutoCommit(false);
// 1. Получаем docID и formula String docID;
PreparedStatement select = con.prepareStatement( String formula;
// Получаем docID и formula
try (PreparedStatement ps = con.prepareStatement(
"SELECT rg.docID, nt.formula " + "SELECT rg.docID, nt.formula " +
"FROM register_docs rg " + "FROM register_docs rg " +
"LEFT JOIN object_folders of ON rg.docID = of.objectID " + "LEFT JOIN object_folders of ON rg.docID = of.objectID " +
"LEFT JOIN registers r ON r.registerID = rg.registerID " + "LEFT JOIN registers r ON r.registerID = rg.registerID " +
"LEFT JOIN number_templates nt ON nt.templateID = r.templateID " + "LEFT JOIN number_templates nt ON nt.templateID = r.templateID " +
"WHERE of.folderid = ? " + "WHERE of.folderid = ? AND of.object_type = '1024'"
"AND of.object_type = '1024'" )) {
);
select.setString(1, documentID); ps.setString(1, documentID);
ResultSet rs = select.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) { if (!rs.next()) {
throw new Exception("document not found"); throw new Exception("Document not found");
} }
String docID = rs.getString("docID"); docID = rs.getString("docID");
String formula = rs.getString("formula"); formula = rs.getString("formula");
}
}
if (formula == null) { if (formula == null || numberTemplate == null) {
throw new Exception("formula not found"); throw new Exception("Invalid template data");
} }
if (numberTemplate == null || numberTemplate.isEmpty()) { if (numberTemplate.length() < formula.length()) {
throw new Exception("numberTemplate empty"); throw new Exception("numberTemplate shorter than formula");
} }
// 2. Обновляем user_mask // update user_mask
PreparedStatement update = con.prepareStatement( try (PreparedStatement ps = con.prepareStatement(
"UPDATE register_docs SET user_mask = ? WHERE docID = ?" "UPDATE register_docs SET user_mask = ? WHERE docID = ?"
); )) {
update.setString(1, numberTemplate); ps.setString(1, numberTemplate);
update.setString(2, docID); ps.setString(2, docID);
update.executeUpdate(); ps.executeUpdate();
}
// 3. Удаляем старые значения // delete old inputs
PreparedStatement delete = con.prepareStatement( try (PreparedStatement ps = con.prepareStatement(
"DELETE FROM doc_number_input WHERE docID = ?" "DELETE FROM doc_number_input WHERE docID = ?"
); )) {
delete.setString(1, docID); ps.setString(1, docID);
delete.executeUpdate(); ps.executeUpdate();
char[] formulaChars = formula.toCharArray();
char[] templateChars = numberTemplate.toCharArray();
if (templateChars.length < formulaChars.length) {
throw new Exception("numberTemplate shorter than formula");
} }
// защита от слишком больших формул // парсим formula
int maxInsert = 100;
int starCount = 0;
StringBuilder sql = new StringBuilder( StringBuilder sql = new StringBuilder(
"INSERT INTO doc_number_input(number, docID, value) VALUES " "INSERT INTO doc_number_input(number, docID, value) VALUES "
); );
for (int i = 0; i < formulaChars.length; i++) { List<String> values = new ArrayList<>();
int templateIndex = 0;
for (int i = 0; i < formula.length(); i++) {
char c = formula.charAt(i);
if (formulaChars[i] == '*') { if (c == '*') {
if (starCount >= maxInsert) { int start = templateIndex;
throw new Exception("too many mask characters in formula"); int starLen = 0;
while (i < formula.length() && formula.charAt(i) == '*') {
starLen++;
templateIndex++;
i++;
} }
if (starCount > 0) { values.add(numberTemplate.substring(start, start + starLen));
if (values.size() > 1) {
sql.append(","); sql.append(",");
} }
sql.append("(null, ?, ?)"); sql.append("(null, ?, ?)");
starCount++; i--;
} else {
templateIndex++;
} }
} }
// если звездочек нет if (!values.isEmpty()) {
if (starCount == 0) {
con.commit();
return true;
}
PreparedStatement insert = con.prepareStatement(sql.toString()); try (PreparedStatement ps = con.prepareStatement(sql.toString())) {
int paramIndex = 1; int index = 1;
for (int i = 0; i < formulaChars.length; i++) { for (String v : values) {
if (formulaChars[i] == '*') { ps.setString(index++, docID);
ps.setString(index++, v);
}
insert.setString(paramIndex++, docID); ps.executeUpdate();
insert.setString(paramIndex++, String.valueOf(templateChars[i]));
} }
} }
insert.executeUpdate();
con.commit(); con.commit();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
if (con != null) { if (con != null) {
try {
con.rollback(); con.rollback();
} catch (Exception ignored) {}
} }
throw e; throw e;
} finally { } finally {
if (con != null) { if (con != null) {
try {
con.setAutoCommit(true); con.setAutoCommit(true);
} } catch (Exception ignored) {}
ConnectionPool.close(con); ConnectionPool.close(con);
} }
} }
} }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment