Skip to content

Commit

Permalink
Merge pull request #6377 from BartChris/speed_up_template_list
Browse files Browse the repository at this point in the history
Speed up process template page
  • Loading branch information
solth authored Feb 7, 2025
2 parents 6f65f3d + bbb8a75 commit de9e235
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,22 @@ public Long count(String query) throws DAOException {
}
}

/**
* Executes an HQL query that returns scalar projections (e.g., specific fields or aggregate results)
* instead of full entity objects.
*
* @param hql the HQL query string
* @param parameters query parameters
* @return list of scalar projection results
*/
public List<Object[]> getProjectionByQuery(String hql, Map<String, Object> parameters) {
try (Session session = HibernateUtil.getSession()) {
Query<Object[]> query = session.createQuery(hql, Object[].class);
addParameters(query, parameters);
return query.getResultList();
}
}

/**
* Removes the object from the database with with specified class type and
* {@code id}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -46,7 +47,7 @@
import org.kitodo.production.workflow.model.Converter;

@Named("TemplateForm")
@SessionScoped
@ViewScoped
public class TemplateForm extends TemplateBaseForm {

private static final Logger logger = LogManager.getLogger(TemplateForm.class);
Expand All @@ -60,6 +61,7 @@ public class TemplateForm extends TemplateBaseForm {
private List<String> templateFilters;
private List<String> selectedTemplateFilters;
private static final String DEACTIVATED_TEMPLATES_FILTER = "deactivatedTemplates";
private Map<Integer,Boolean> templateUsageMap;

/**
* Constructor.
Expand Down Expand Up @@ -457,12 +459,11 @@ private void prepareTasks() throws DAOException, IOException, WorkflowException,
* @return whether template is used by any processes or not
*/
public boolean isTemplateUsed(int templateId) {
try {
return !ServiceManager.getProcessService().findByTemplate(templateId).isEmpty();
} catch (DataException e) {
Helper.setErrorMessage(e);
return false;
if (Objects.isNull(templateUsageMap)) {
templateUsageMap = ServiceManager.getTemplateService().getTemplateUsageMap();
}
Boolean isUsed = templateUsageMap.get(templateId);
return Boolean.TRUE.equals(isUsed);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ public static TemplateService getInstance() {
return localReference;
}

/**
* Retrieves a map indicating the usage status of templates.
* The method executes an HQL query to determine whether each template is used
* (i.e., has associated processes).
*
* @return a map where the key is the template ID and the value is a boolean
* indicating whether the template is used
*/
public Map<Integer, Boolean> getTemplateUsageMap() {
String hql = "SELECT t.id AS templateId, "
+ " CASE WHEN EXISTS (SELECT 1 FROM Process p WHERE p.template.id = t.id) "
+ " THEN true ELSE false END AS isUsed "
+ " FROM Template t";
List<Object[]> results = getProjectionByQuery(hql);
return results.stream()
.collect(Collectors.toMap(
row -> (Integer) row[0], // templateId
row -> (Boolean) row[1] // isUsed
));
}

@Override
public Long countDatabaseRows() throws DAOException {
return countDatabaseRows("SELECT COUNT(*) FROM Template");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ public List<T> getAll(int offset, int size) throws DAOException {
return dao.getAll(offset, size);
}

/**
* Executes an HQL query that returns scalar projections (e.g., specific fields or aggregate results)
* instead of full entity objects.
*
* @param hql the HQL query string
* @return list of scalar projection results
*/
protected List<Object[]> getProjectionByQuery(String hql) {
return dao.getProjectionByQuery(hql, null);
}

/**
* Evict given bean object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -102,4 +103,17 @@ public void shouldHasCompleteTasks() throws Exception {
condition = templateService.hasCompleteTasks(templateDTO.getTasks());
assertFalse(condition, "Process DTO has complete tasks!");
}

@Test
public void shouldCorrectlyDetermineTemplateUsage() throws Exception {
Map<Integer, Boolean> templateUsageMap = templateService.getTemplateUsageMap();
Map<Integer, Boolean> expectedMap = Map.of(
1, true,
2, false,
3, false,
4, false
);
// Assert that the generated map matches the expected map
assertEquals(expectedMap, templateUsageMap, "The template usage map does not match expected results.");
}
}

0 comments on commit de9e235

Please sign in to comment.