Skip to content

Commit

Permalink
Always use JdbcTemplate to run JDBC statements
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed Nov 14, 2024
1 parent 76a1ee5 commit 3cd1a22
Show file tree
Hide file tree
Showing 63 changed files with 228 additions and 782 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.slf4j.LoggerFactory;

import com.logicaldoc.core.communication.EventCollector;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;
import com.logicaldoc.core.history.History;
import com.logicaldoc.core.security.TenantDAO;
import com.logicaldoc.util.Context;
import com.logicaldoc.util.config.ContextProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* @author Marco Meschieri - LogicalDOC
* @since 4.0
*
* @param <T> Class of the implementation of a {@link PersistentObject} this DAO handles
* @param <T> Class of the implementation of a {@link PersistentObject} this DAO
* handles
*/
public interface PersistentObjectDAO<T extends PersistentObject> {

Expand Down Expand Up @@ -204,8 +205,7 @@ public List<Long> findIdsByWhere(String where, Map<String, Object> parameters, S
*
* @throws PersistenceException raised in case of errors in the database
*/
@SuppressWarnings("rawtypes")
public List query(String sql, RowMapper rowMapper, Integer maxRows) throws PersistenceException;
public <P> List<P> query(String sql, RowMapper<P> rowMapper, Integer maxRows) throws PersistenceException;

/**
* Query given SQL to create a prepared statement from SQL and a list of
Expand All @@ -223,8 +223,7 @@ public List<Long> findIdsByWhere(String where, Map<String, Object> parameters, S
*
* @throws PersistenceException raised in case of errors in the database
*/
@SuppressWarnings("rawtypes")
public List query(String sql, Map<String, Object> parameters, RowMapper rowMapper, Integer maxRows)
public <P> List<P> query(String sql, Map<String, Object> parameters, RowMapper<P> rowMapper, Integer maxRows)
throws PersistenceException;

/**
Expand Down Expand Up @@ -331,7 +330,7 @@ public List queryForList(String sql, Map<String, Object> parameters, Class eleme
* @throws PersistenceException raised in case of errors in the database
*/
public int queryForInt(String sql) throws PersistenceException;

/**
* Execute a query that results in an int value, given static SQL. Uses a
* JDBC Statement, not a PreparedStatement. If you want to execute a static
Expand Down Expand Up @@ -384,7 +383,7 @@ public List queryForList(String sql, Map<String, Object> parameters, Class eleme
* @throws PersistenceException raised in case of errors in the database
*/
public long queryForLong(String sql, Map<String, Object> parameters) throws PersistenceException;

/**
* Execute a query that results in a double value, given static SQL. Uses a
* JDBC Statement, not a PreparedStatement. If you want to execute a static
Expand All @@ -400,7 +399,7 @@ public List queryForList(String sql, Map<String, Object> parameters, Class eleme
* @throws PersistenceException raised in case of errors in the database
*/
public double queryForDouble(String sql) throws PersistenceException;

/**
* Execute a query that results in a double value, given static SQL. Uses a
* JDBC Statement, not a PreparedStatement. If you want to execute a static
Expand Down Expand Up @@ -540,6 +539,8 @@ public Object queryForObject(String sql, @SuppressWarnings("rawtypes")

public boolean isOracle();

public boolean isMySQL();

/**
* Retrieves the metadata from the database
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.logicaldoc.core.History;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.RunLevel;
import com.logicaldoc.core.document.Document;
import com.logicaldoc.core.document.DocumentDAO;
import com.logicaldoc.core.history.History;
import com.logicaldoc.core.threading.ThreadPools;
import com.logicaldoc.util.Context;
import com.logicaldoc.util.config.ContextProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.logicaldoc.core.communication;

import com.logicaldoc.core.History;
import com.logicaldoc.core.history.History;

/**
* A listener for the event emitted by the collector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;
import com.logicaldoc.util.sql.SqlUtil;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.RunLevel;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;
import com.logicaldoc.core.security.user.User;
import com.logicaldoc.core.security.user.UserDAO;
import com.logicaldoc.core.security.user.UserEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import org.slf4j.LoggerFactory;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.generic.HibernateGenericDAO;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;

/**
* Hibernate implementation of <code>ContactDAO</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.LoggerFactory;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;

/**
* Hibernate implementation of <code>DashletDAO</code>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.logicaldoc.core.document;

import com.logicaldoc.core.History;
import javax.persistence.Table;

import com.logicaldoc.core.history.History;

/**
* Registers an event on folder or document
Expand All @@ -9,6 +11,7 @@
* @author Alessandro Gasparini - LogicalDOC
* @author Marco Meschieri - LogicalDOC
*/
@Table(name = "ld_history")
public class DocumentHistory extends History {
private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.logicaldoc.core.History;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.conversion.FormatConverterManager;
import com.logicaldoc.core.folder.Folder;
import com.logicaldoc.core.folder.FolderDAO;
import com.logicaldoc.core.folder.FolderEvent;
import com.logicaldoc.core.folder.FolderHistory;
import com.logicaldoc.core.history.History;
import com.logicaldoc.core.metadata.Attribute;
import com.logicaldoc.core.metadata.Template;
import com.logicaldoc.core.metadata.TemplateDAO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import org.slf4j.LoggerFactory;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;

/**
* Hibernate implementation of <code>BookmarkDAO</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -34,14 +32,14 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.PersistentObject;
import com.logicaldoc.core.RunLevel;
import com.logicaldoc.core.communication.EventCollector;
import com.logicaldoc.core.folder.Folder;
import com.logicaldoc.core.folder.FolderDAO;
import com.logicaldoc.core.generic.GenericDAO;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;
import com.logicaldoc.core.metadata.Attribute;
import com.logicaldoc.core.security.AccessControlUtil;
import com.logicaldoc.core.security.Permission;
Expand Down Expand Up @@ -215,7 +213,6 @@ public List<Long> findByUserId(long userId) throws PersistenceException {
return findIdsByWhere(query.toString(), null, null);
}

@SuppressWarnings("unchecked")
@Override
public List<Document> findByLockUserAndStatus(Long userId, Integer status) {
StringBuilder sb = new StringBuilder(
Expand Down Expand Up @@ -1197,7 +1194,6 @@ public void updateCountUniqueTags() throws PersistenceException {
}
}

@SuppressWarnings("unchecked")
@Override
public List<TagCloud> getTagCloud(long tenantId, int maxTags) throws PersistenceException {
GenericDAO gendao = (GenericDAO) Context.get().getBean(GenericDAO.class);
Expand Down Expand Up @@ -1332,7 +1328,7 @@ public Document findByPath(String path, long tenantId) throws PersistenceExcepti
return null;
}

@SuppressWarnings("unchecked")

@Override
public List<String> findDuplicatedDigests(Long tenantId, Long folderId) throws PersistenceException {
// First of all, find all duplicates digests.
Expand Down Expand Up @@ -1419,8 +1415,8 @@ public Set<Permission> getAllowedPermissions(long docId, long userId) throws Per
ld_customid as LDCUSTOMID from ld_document_acl where ld_docid=
""");
query.append(Long.toString(docId));
query.append(" and ld_groupid in (");
query.append(user.getGroups().stream().map(ug -> Long.toString(ug.getId())).collect(Collectors.joining(",")));
query.append(" and ld_groupid in (select ld_groupid from ld_usergroup where ld_userid=");
query.append(Long.toString(userId));
query.append(")");

Map<String, Permission> permissionColumn = new HashMap<>();
Expand All @@ -1445,23 +1441,14 @@ public Set<Permission> getAllowedPermissions(long docId, long userId) throws Per
permissionColumn.put("LDPREVIEW", Permission.PREVIEW);
permissionColumn.put("LDCUSTOMID", Permission.CUSTOMID);

/**
* IMPORTANT: the connection MUST be explicitly closed, otherwise it is
* probable that the connection pool will leave open it indefinitely.
*/
try (Connection con = getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query.toString())) {
while (rs.next()) {
for (Entry<String, Permission> entry : permissionColumn.entrySet()) {
String column = entry.getKey();
Permission permission = entry.getValue();
if (rs.getInt(column) == 1)
permissions.add(permission);
}
SqlRowSet rows = queryForRowSet(query.toString(), null);
while (rows.next()) {
for (Entry<String, Permission> entry : permissionColumn.entrySet()) {
String column = entry.getKey();
Permission permission = entry.getValue();
if (rows.getInt(column) == 1)
permissions.add(permission);
}
} catch (SQLException se) {
throw new PersistenceException(se.getMessage(), se);
}

if (permissions.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;

/**
* Hibernate implementation of <code>DocumentLinkDAO</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;
import com.logicaldoc.core.security.Session;
import com.logicaldoc.core.security.SessionManager;
import com.logicaldoc.util.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;
import com.logicaldoc.core.security.Session;
import com.logicaldoc.core.security.SessionManager;
import com.logicaldoc.util.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import com.logicaldoc.core.HibernatePersistentObjectDAO;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.PersistentObject;
import com.logicaldoc.core.folder.Folder;
import com.logicaldoc.core.folder.FolderDAO;
import com.logicaldoc.core.history.HibernatePersistentObjectDAO;
import com.logicaldoc.core.store.Store;
import com.logicaldoc.util.config.ContextProperties;
import com.logicaldoc.util.io.FileUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.logicaldoc.core.folder;

import com.logicaldoc.core.History;
import javax.persistence.Table;

import com.logicaldoc.core.history.History;

/**
* History entry due to an event on a folder.
*
* @author Marco Meschieri - LogicalDOC
* @since 6.4
*/
@Table(name = "ld_folder_history")
public class FolderHistory extends History {

private static final long serialVersionUID = 1L;
Expand Down
Loading

0 comments on commit 3cd1a22

Please sign in to comment.