Commit 1404524c by Samir Sadykhov

fix updateMask api, добавление строк в таблице doc_number_input

parent 4cdefc0b
......@@ -8,36 +8,140 @@ import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class DocumentDAO {
private static final Logger LOGGER = LoggerFactory.getLogger(DocumentDAO.class);
public boolean updateUserMask(String documentID, String numberTemplate) throws SQLException, NamingException {
public boolean updateUserMask(String documentID, String numberTemplate) throws Exception {
Connection con = null;
try {
con = ConnectionPool.getConnection();
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement(
"UPDATE register_docs rd " +
"JOIN object_folders of ON rd.docID = of.objectID " +
"SET rd.user_mask = ? " +
"WHERE of.folderid = ? " +
"AND of.object_type = '1024'"
// 1. Получаем docID и formula
PreparedStatement select = 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'"
);
ps.setString(1, numberTemplate);
ps.setString(2, documentID);
select.setString(1, documentID);
int updated = ps.executeUpdate();
ResultSet rs = select.executeQuery();
return updated > 0;
if (!rs.next()) {
throw new Exception("document not found");
}
String docID = rs.getString("docID");
String formula = rs.getString("formula");
if (formula == null) {
throw new Exception("formula not found");
}
if (numberTemplate == null || numberTemplate.isEmpty()) {
throw new Exception("numberTemplate empty");
}
// 2. Обновляем user_mask
PreparedStatement update = con.prepareStatement(
"UPDATE register_docs SET user_mask = ? WHERE docID = ?"
);
update.setString(1, numberTemplate);
update.setString(2, docID);
update.executeUpdate();
// 3. Удаляем старые значения
PreparedStatement delete = 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");
}
// защита от слишком больших формул
int maxInsert = 100;
int starCount = 0;
StringBuilder sql = new StringBuilder(
"INSERT INTO doc_number_input(number, docID, value) VALUES "
);
for (int i = 0; i < formulaChars.length; i++) {
if (formulaChars[i] == '*') {
if (starCount >= maxInsert) {
throw new Exception("too many mask characters in formula");
}
if (starCount > 0) {
sql.append(",");
}
sql.append("(null, ?, ?)");
starCount++;
}
}
// если звездочек нет
if (starCount == 0) {
con.commit();
return true;
}
PreparedStatement insert = con.prepareStatement(sql.toString());
int paramIndex = 1;
for (int i = 0; i < formulaChars.length; i++) {
if (formulaChars[i] == '*') {
insert.setString(paramIndex++, docID);
insert.setString(paramIndex++, String.valueOf(templateChars[i]));
}
}
insert.executeUpdate();
con.commit();
return true;
} catch (Exception e) {
if (con != null) {
con.rollback();
}
throw e;
} finally {
if (con != null) {
con.setAutoCommit(true);
}
ConnectionPool.close(con);
}
}
}
\ No newline at end of file
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="1.1"
bean-discovery-mode="annotated">
</beans>
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