Commit 3660d6d3 by Samir Sadykhov

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

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