Skip to content

Commit 46438d0

Browse files
committed
refactorings
1 parent ca0aafe commit 46438d0

File tree

12 files changed

+54
-81
lines changed

12 files changed

+54
-81
lines changed

logicaldoc-core/src/main/java/com/logicaldoc/core/HibernatePersistentObjectDAO.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ protected void logQuery(String query) {
165165
}
166166

167167
@Override
168-
public List<?> findByQuery(String query, Map<String, Object> parameters, Integer max) throws PersistenceException {
168+
public List<Object[]> findByQuery(String query, Map<String, Object> parameters, Integer max) throws PersistenceException {
169169
try {
170170
logQuery(query);
171171
return prepareQuery(query, parameters, max).list();
@@ -312,23 +312,6 @@ protected <R> Query<R> prepareQuery(String expression, Map<String, Object> value
312312
return queryObject;
313313
}
314314

315-
/**
316-
* Utility method useful for preparing an Hibernate query for updates
317-
*
318-
* @param expression The expression for the query
319-
* @param values The parameters values to be used (optional, if the query is
320-
* parametric)
321-
* @param max Optional maximum number of wanted results
322-
*
323-
* @return The Hibernate query (for updates/deletes this query cannot by
324-
* typed)
325-
*/
326-
protected Query<?> prepareQueryForUpdate(String expression, Map<String, Object> values, Integer max) {
327-
Query<?> queryObject = sessionFactory.getCurrentSession().createQuery(expression);
328-
applyParametersAndLimit(values, max, queryObject);
329-
return queryObject;
330-
}
331-
332315
private void applyParametersAndLimit(Map<String, Object> parameters, Integer max, Query<?> queryObject) {
333316
if (parameters != null)
334317
for (Map.Entry<String, Object> entry : parameters.entrySet())
@@ -558,8 +541,10 @@ public int bulkUpdate(String expression, Map<String, Object> parameters) throws
558541
return 0;
559542

560543
try {
561-
return prepareQueryForUpdate(UPDATE + entityClass.getCanonicalName() + " " + expression, parameters, null)
562-
.executeUpdate();
544+
Query<?> queryObject = sessionFactory.getCurrentSession()
545+
.createQuery(UPDATE + entityClass.getCanonicalName() + " " + expression);
546+
applyParametersAndLimit(parameters, null, queryObject);
547+
return queryObject.executeUpdate();
563548
} catch (Exception e) {
564549
throw new PersistenceException(e);
565550
}

logicaldoc-core/src/main/java/com/logicaldoc/core/PersistentObjectDAO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public List<T> findByObjectQuery(String query, Map<String, Object> parameters, I
151151
*
152152
* @throws PersistenceException raised in case of errors in the database
153153
*/
154-
public List<?> findByQuery(String query, Map<String, Object> parameters, Integer max) throws PersistenceException;
154+
public List<Object[]> findByQuery(String query, Map<String, Object> parameters, Integer max) throws PersistenceException;
155155

156156
/**
157157
* Find everything you want from the DB using the ORM query language
@@ -509,7 +509,7 @@ public <R> R queryForObject(String sql, Map<String, Object> parameters, Class<R>
509509
public void deleteAll(Collection<T> entities, int code) throws PersistenceException;
510510

511511
/**
512-
* Executes a bulk update as specified by the given expression
512+
* Executes a bulk update as specified by the given HQL expression
513513
*
514514
* @param expression The update expression.
515515
* @param parameters Optional map of parameters

logicaldoc-core/src/main/java/com/logicaldoc/core/folder/HibernateFolderDAO.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -847,18 +847,18 @@ public void restore(long folderId, long parentId, FolderHistory transaction) thr
847847
// The parent folder
848848
Folder parent = findFolder(parentId);
849849

850-
int count = bulkUpdate("set ld_deleted=0, ld_parentid=" + parent.getId()
850+
int count = jdbcUpdate("update ld_folder set ld_deleted=0, ld_parentid=" + parent.getId()
851851
+ ", ld_lastmodified=CURRENT_TIMESTAMP where not ld_type=" + Folder.TYPE_WORKSPACE + " and ld_id="
852-
+ folderId, (Map<String, Object>) null);
852+
+ folderId);
853853

854854
if (count == 0) {
855855
// The root of folders in the current tenant
856856
Folder root = findRoot(parent.getTenantId());
857857

858858
// Workspaces must always be restored under the root
859-
bulkUpdate("set ld_deleted=0, ld_parentid=" + root.getId()
859+
jdbcUpdate("update ld_folder set ld_deleted=0, ld_parentid=" + root.getId()
860860
+ ", ld_lastmodified=CURRENT_TIMESTAMP where ld_type=" + Folder.TYPE_WORKSPACE + " and ld_id="
861-
+ folderId, (Map<String, Object>) null);
861+
+ folderId);
862862
}
863863

864864
Folder fld = findFolder(folderId);
@@ -871,8 +871,9 @@ public void restore(long folderId, long parentId, FolderHistory transaction) thr
871871
Set<Long> treeIds = findFolderIdInTree(folderId, true);
872872
if (!treeIds.isEmpty()) {
873873
String idsStr = treeIds.toString().replace('[', '(').replace(']', ')');
874-
bulkUpdate("set ld_deleted=0, ld_lastmodified=CURRENT_TIMESTAMP where ld_deleted=1 and ld_id in " + idsStr,
875-
(Map<String, Object>) null);
874+
jdbcUpdate(
875+
"update ld_folder set ld_deleted=0, ld_lastmodified=CURRENT_TIMESTAMP where ld_deleted=1 and ld_id in "
876+
+ idsStr);
876877
jdbcUpdate(
877878
"update ld_document set ld_deleted=0, ld_lastmodified=CURRENT_TIMESTAMP where ld_deleted=1 and ld_folderid in "
878879
+ idsStr);
@@ -2000,7 +2001,7 @@ public void updateSecurityRef(long folderId, long rightsFolderId, FolderHistory
20002001
store(f, transaction);
20012002

20022003
// Now all the folders that are referencing this one must be updated
2003-
bulkUpdate("set securityRef=" + securityRef + " where securityRef=" + folderId, (Map<String, Object>) null);
2004+
bulkUpdate("set securityRef=" + securityRef + " where securityRef=" + folderId, null);
20042005
}
20052006

20062007
@Override

logicaldoc-core/src/main/java/com/logicaldoc/core/searchengine/IndexerTask.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected void runTask() throws TaskException {
207207
Map<String, Object> params = new HashMap<>();
208208
params.put("transactionId", transactionId);
209209

210-
documentDao.bulkUpdate("set ld_transactionid = null where ld_transactionId = :transactionId", params);
210+
documentDao.jdbcUpdate("update ld_document set ld_transactionid = null where ld_transactionId = :transactionId", params);
211211
} catch (PersistenceException e) {
212212
log.error(e.getMessage(), e);
213213
}
@@ -253,8 +253,8 @@ private void assignTransition(List<Long> docIds) throws PersistenceException {
253253
Map<String, Object> params = new HashMap<>();
254254
params.put("transactionId", transactionId);
255255

256-
documentDao.bulkUpdate(
257-
" set ld_transactionid = :transactionId where ld_transactionid is null and ld_id in " + idsStr,
256+
documentDao.jdbcUpdate(
257+
" update ld_document set ld_transactionid = :transactionId where ld_transactionid is null and ld_id in " + idsStr,
258258
params);
259259
}
260260
log.info("Documents marked for indexing in transaction {}", transactionId);

logicaldoc-core/src/main/java/com/logicaldoc/core/security/HibernateSessionDAO.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,24 @@
1313

1414
public class HibernateSessionDAO extends HibernatePersistentObjectDAO<Session> implements SessionDAO {
1515

16+
private static final String AND = " and ";
17+
1618
protected HibernateSessionDAO() {
1719
super(Session.class);
1820
}
1921

2022
@Override
2123
public void deleteCurrentNodeSessions() {
2224
try {
23-
Map<String, Object> params = new HashMap<>();
24-
params.put("node", SystemInfo.get().getInstallationId());
25-
26-
bulkUpdate(" set deleted=1 where node = :node and deleted=0", params);
25+
jdbcUpdate("update ld_session set ld_deleted=1 where ld_node = :node and ld_deleted=0",
26+
Map.of("node", SystemInfo.get().getInstallationId()));
2727
} catch (PersistenceException e) {
2828
log.warn(e.getMessage(), e);
2929
}
3030

3131
try {
32-
Map<String, Object> params = new HashMap<>();
33-
params.put("node", SystemInfo.get().getInstallationId());
34-
params.put("status", Session.STATUS_OPEN);
35-
36-
bulkUpdate(" set status=" + Session.STATUS_EXPIRED + " where node = :node and status = :status", params);
32+
jdbcUpdate("update ld_session set ld_status=" + Session.STATUS_EXPIRED + " where ld_node = :node and ld_status = :status",
33+
Map.of("node", SystemInfo.get().getInstallationId(), "status", Session.STATUS_OPEN));
3734
} catch (PersistenceException e) {
3835
log.error(e.getMessage(), e);
3936
}
@@ -43,13 +40,13 @@ public void deleteCurrentNodeSessions() {
4340
public int countSessions(Long tenantId, Integer status) {
4441
StringBuilder query = new StringBuilder(" 1=1 ");
4542
if (tenantId != null)
46-
query.append(" and " + ENTITY + ".tenantId = " + tenantId);
43+
query.append(AND + ENTITY + ".tenantId = " + tenantId);
4744
if (status != null) {
48-
query.append(" and " + ENTITY + ".status = " + status);
49-
if(status.intValue() == Session.STATUS_OPEN)
50-
query.append(" and " + ENTITY + ".finished is null ");
45+
query.append(AND + ENTITY + ".status = " + status);
46+
if (status.intValue() == Session.STATUS_OPEN)
47+
query.append(AND + ENTITY + ".finished is null ");
5148
}
52-
49+
5350
try {
5451
List<Session> sessions = findByWhere(query.toString(), null, null);
5552
return sessions.size();

logicaldoc-core/src/main/java/com/logicaldoc/core/security/menu/HibernateMenuDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public List<Menu> findParents(long menuId) {
449449

450450
@Override
451451
public void restore(long menuId, boolean parents) throws PersistenceException {
452-
bulkUpdate("set ld_deleted=0 where ld_id=" + menuId, (Map<String, Object>) null);
452+
jdbcUpdate("update ld_menu set ld_deleted=0 where ld_id=" + menuId);
453453

454454
// Restore parents
455455
if (parents) {

logicaldoc-core/src/main/java/com/logicaldoc/core/task/AbstractDocumentProcessor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.sql.SQLException;
66
import java.util.List;
77
import java.util.Locale;
8-
import java.util.Map;
98
import java.util.stream.Collectors;
109

1110
import javax.annotation.Resource;
@@ -84,8 +83,8 @@ private void processDocuments(List<Long> docIds, int max) throws PersistenceExce
8483

8584
// Mark all these documents as belonging to the current
8685
// transaction
87-
documentDao.bulkUpdate("set ld_transactionid='" + transactionId + "' where ld_id in " + idsStr,
88-
(Map<String, Object>) null);
86+
documentDao
87+
.jdbcUpdate("update ld_document set ld_transactionid='" + transactionId + "' where ld_id in " + idsStr);
8988

9089
// Now we can release the lock
9190
lockManager.release(getName(), transactionId);
@@ -130,8 +129,8 @@ public Object[] mapRow(ResultSet rs, int row) throws SQLException {
130129

131130
private void removeTransactionReference() {
132131
try {
133-
documentDao.bulkUpdate("set ld_transactionid=null where ld_transactionId='" + transactionId + "'",
134-
(Map<String, Object>) null);
132+
documentDao.jdbcUpdate(
133+
"update ld_document set ld_transactionid=null where ld_transactionId='" + transactionId + "'");
135134
} catch (PersistenceException e) {
136135
log.warn(e.getMessage(), e);
137136
}

logicaldoc-core/src/test/java/com/logicaldoc/core/folder/HibernateFolderDAOTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,12 +1303,14 @@ public void testUpdateSecurityRef() throws PersistenceException {
13031303

13041304
folder = testSubject.findById(1201L);
13051305
assertEquals(1200L, folder.getSecurityRef().longValue());
1306+
assertEquals(1200L, folder.getParentId());
13061307
folder = testSubject.findById(1202L);
13071308
assertEquals(1200L, folder.getSecurityRef().longValue());
1309+
assertEquals(1201L, folder.getParentId());
13081310

13091311
// The root has its own policies
13101312
testSubject.updateSecurityRef(1200L, 5L, transaction);
1311-
1313+
13121314
folder = testSubject.findById(1200L);
13131315
assertEquals(5L, folder.getSecurityRef().longValue());
13141316
folder = testSubject.findById(1201L);

logicaldoc-webapp/src/main/java/com/logicaldoc/web/data/DocumentsDataServlet.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ private List<Long> getForbiddenDocumentIds(User user, long folderId) throws Pers
438438
forbiddenDocsQuery.append(") and ld_folderid = ");
439439
forbiddenDocsQuery.append(Long.toString(folderId));
440440

441-
List<Long> forbiddenDocIds = docDao.queryForList(forbiddenDocsQuery.toString(), Long.class);
442-
return forbiddenDocIds;
441+
return docDao.queryForList(forbiddenDocsQuery.toString(), Long.class);
443442
}
444443

445444
private List<Document> enrichRecords(List<?> records, List<String> extendedAttributes,

logicaldoc-webapp/src/main/java/com/logicaldoc/web/service/DocumentServiceImpl.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ public List<GUIVersion> getVersionsById(long id1, long id2) throws ServerExcepti
956956
if (docVersion != null)
957957
versDao.initialize(docVersion);
958958
} catch (Exception e) {
959-
return super.<List<GUIVersion>>throwServerException(session, log, e);
959+
return super.<List<GUIVersion>> throwServerException(session, log, e);
960960
}
961961

962962
GUIVersion version2 = null;
@@ -1146,16 +1146,14 @@ public void markUnindexable(List<Long> docIds) throws ServerException {
11461146
public void restore(List<Long> docIds, long folderId) throws ServerException {
11471147
Session session = validateSession();
11481148

1149-
DocumentDAO docDao = Context.get().getBean(DocumentDAO.class);
1150-
11511149
for (Long docId : docIds) {
11521150
if (docId == null)
11531151
continue;
11541152
DocumentHistory transaction = new DocumentHistory();
11551153
transaction.setSession(session);
11561154

11571155
try {
1158-
docDao.restore(docId, folderId, transaction);
1156+
Context.get().getBean(DocumentDAO.class).restore(docId, folderId, transaction);
11591157
} catch (PersistenceException e) {
11601158
log.error(e.getMessage(), e);
11611159
}
@@ -2261,9 +2259,9 @@ public void deleteFromTrash(List<Long> ids) throws ServerException {
22612259
return;
22622260

22632261
String idsStr = Arrays.asList(ids).toString().replace('[', '(').replace(']', ')');
2264-
DocumentDAO dao = Context.get().getBean(DocumentDAO.class);
22652262
try {
2266-
dao.bulkUpdate("set ld_deleted=2 where ld_id in " + idsStr, (Map<String, Object>) null);
2263+
Context.get().getBean(DocumentDAO.class)
2264+
.jdbcUpdate("update ld_document set ld_deleted=2 where ld_id in " + idsStr);
22672265
} catch (PersistenceException e) {
22682266
throwServerException(session, log, e);
22692267
}
@@ -2275,13 +2273,12 @@ public void emptyTrash() throws ServerException {
22752273
Session session = validateSession();
22762274

22772275
try {
2278-
DocumentDAO dao = Context.get().getBean(DocumentDAO.class);
2279-
dao.bulkUpdate("set ld_deleted=2 where ld_deleted=1 and ld_deleteuserid=" + session.getUserId(),
2280-
(Map<String, Object>) null);
2276+
Context.get().getBean(DocumentDAO.class)
2277+
.jdbcUpdate("update ld_document set ld_deleted=2 where ld_deleted=1 and ld_deleteuserid="
2278+
+ session.getUserId());
22812279

2282-
FolderDAO fdao = Context.get().getBean(FolderDAO.class);
2283-
fdao.bulkUpdate("set ld_deleted=2 where ld_deleted=1 and ld_deleteuserid=" + session.getUserId(),
2284-
(Map<String, Object>) null);
2280+
Context.get().getBean(FolderDAO.class).jdbcUpdate(
2281+
"update ld_folder set ld_deleted=2 where ld_deleted=1 and ld_deleteuserid=" + session.getUserId());
22852282
} catch (PersistenceException e) {
22862283
throwServerException(session, log, e);
22872284
}

0 commit comments

Comments
 (0)