Skip to content

Commit

Permalink
Implement BeanQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Apr 2, 2024
1 parent 0ee49c3 commit 80d1f8d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.production.services.data.search;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.Pair;
import org.kitodo.data.database.beans.BaseBean;

public class BeanQuery<T extends BaseBean> {

private static final String KEYWORD_FROM = "FROM";
private static final String KEYWORD_SELECT = "SELECT";
private static final String KEYWORD_SORT = "ORDER BY";
private static final String KEYWORD_SORT_ASCENDING = "ASC";

private static final String BASEBEAN_FIELD_ID = "id";

private static final List<Pair<String, String>> DEFAULT_SORTING = Collections
.singletonList(Pair.of(BASEBEAN_FIELD_ID, KEYWORD_SORT_ASCENDING));

private final Class<T> beanClass;
private List<Pair<String, String>> sorting;

/**
* Creates a new object for creating search queries.
*
* @param beanClass
*/
public BeanQuery(Class<T> beanClass) {
this.beanClass = beanClass;
}

/**
* Returns the class of bean for which this object forms queries.
*
* @return the class of bean
*/
public Class<T> getBeanClass() {
return beanClass;
}

/**
* Returns the query parameters used for the last query formed. Can be
* {@code null} if no parameters were used.
*
* @return the query parameters used, or {@code null}
*/
public Map<String, Object> getQueryParameters() {
return null;
}

/**
* Forms the compiled query as a string.
*
* @return the query
*/
public String formIdQuery() {
List<String> query = Arrays.asList(KEYWORD_SELECT, BASEBEAN_FIELD_ID, KEYWORD_FROM, beanClass.getName(),
formSortString());
return String.join(" ", query);
}

private String formSortString() {
List<Pair<String, String>> useSorting = Objects.nonNull(sorting) ? sorting : DEFAULT_SORTING;
List<String> sortings = useSorting.stream().map(λ -> λ.getLeft() + " " + λ.getRight())
.collect(Collectors.toList());
return KEYWORD_SORT + " " + String.join(", ", sortings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
package org.kitodo.production.services.data.search;

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

import org.kitodo.api.data.*;
import org.kitodo.data.database.beans.BaseBean;
Expand Down Expand Up @@ -40,56 +39,38 @@ public DataService() {
* @param beanClass
* class of the database object. Technically, this always has to
* be a child class of BaseBean.
* @param query
* for counting objects. Must start with {@code FROM} keyword,
* followed by the class name of the base bean.
* @return amount of rows in database according to given query
*/
public <T extends BaseBean> int count(Class<T> beanClass, String query) throws DAOException {
return find(beanClass, "SELECT id ".concat(query), null).size();
public <T extends BaseBean> int count(Class<T> beanClass) throws DAOException {
return find(new BeanQuery<>(beanClass)).size();
}

/**
* Count all rows in database.
*
* @param <T>
* class type of the database object
* @param beanClass
* class of the database object. Technically, this always has to
* be a child class of BaseBean.
* @param query
* for counting objects. Must start with {@code FROM} keyword,
* followed by the class name of the base bean.
* @param parameters
* for query
* for counting objects
* @return amount of rows in database according to given query
*/
public <T extends BaseBean> int count(Class<T> beanClass, String query, Map<String, Object> parameters)
throws DAOException {
return find(beanClass, "SELECT id ".concat(query), parameters).size();
public <T extends BaseBean> int count(BeanQuery<T> query) throws DAOException {
return find(query).size();
}

/**
* Lazily retrieves BaseBean objects from database by given query.
*
* @param <T>
* class type of the database object
* @param beanClass
* class of the database object. Technically, this always has to
* be a child class of BaseBean.
* @param query
* as String. Must start with {@code SELECT id FROM}, followed by
* the case-sensitive class name of the base bean.
* @param parameters
* for query. May be {@code null} if the query doesn’t need
* parameters.
* as BeanQuery
* @return list of bean object IDs in result object
* @throws DAOException
* if a HibernateException is thrown
*/
public <T extends BaseBean> LazyResult<T> find(Class<T> beanClass, String query, Map<String, Object> parameters)
throws DAOException {
return dataManagementModule.find(beanClass, query, parameters);
public <T extends BaseBean> LazyResult<T> find(BeanQuery<T> query) throws DAOException {
return dataManagementModule.find(query.getBeanClass(), query.formIdQuery(), query.getQueryParameters());
}

/**
Expand All @@ -103,7 +84,7 @@ public <T extends BaseBean> LazyResult<T> find(Class<T> beanClass, String query,
* @return all persisted beans
*/
public <T extends BaseBean> List<T> getAll(Class<T> beanClass) throws DAOException {
return find(beanClass, "SELECT id FROM ".concat(beanClass.getName()), null).getAll();
return find(new BeanQuery<>(beanClass)).getAll();
}

/**
Expand All @@ -114,14 +95,14 @@ public <T extends BaseBean> List<T> getAll(Class<T> beanClass) throws DAOExcepti
* @param beanClass
* class of the database object. Technically, this always has to
* be a child class of BaseBean.
* @param offset
* @param first
* result
* @param limit
* @param max
* amount of results
* @return constrained list of persisted beans
*/
public <T extends BaseBean> List<T> getAll(Class<T> beanClass, int offset, int limit) throws DAOException {
return find(beanClass, "SELECT id FROM ".concat(beanClass.getName()), null).get(offset, limit);
public <T extends BaseBean> List<T> getAll(Class<T> beanClass, int first, int max) throws DAOException {
return find(new BeanQuery<>(beanClass)).get(first, max);
}

/**
Expand Down Expand Up @@ -157,30 +138,8 @@ public <T extends BaseBean> T getById(Class<T> beanClass, int id) throws DAOExce
* @throws DAOException
* if a HibernateException is thrown
*/
public <T extends BaseBean> List<T> getByQuery(Class<T> beanClass, String query) throws DAOException {
return find(beanClass, "SELECT id ".concat(query), null).getAll();
}

/**
* Retrieves BaseBean objects from database by given query.
*
* @param <T>
* class type of the database object
* @param beanClass
* class of the database object. Technically, this always has to
* be a child class of BaseBean.
* @param query
* as String. Must start with {@code FROM} keyword, followed by
* the class name of the base bean.
* @param parameters
* for query
* @return list of beans objects
* @throws DAOException
* if a HibernateException is thrown
*/
public <T extends BaseBean> List<T> getByQuery(Class<T> beanClass, String query, Map<String, Object> parameters)
throws DAOException {
return find(beanClass, "SELECT id ".concat(query), parameters).getAll();
public <T extends BaseBean> List<T> getByQuery(BeanQuery<T> query) throws DAOException {
return find(query).getAll();
}

/**
Expand All @@ -204,9 +163,8 @@ public <T extends BaseBean> List<T> getByQuery(Class<T> beanClass, String query,
* @throws DAOException
* if a HibernateException is thrown
*/
public <T extends BaseBean> List<T> getByQuery(Class<T> beanClass, String query, Map<String, Object> parameters,
int first, int max) throws DAOException {
return find(beanClass, "SELECT id ".concat(query), parameters).get(first, max);
public <T extends BaseBean> List<T> getByQuery(BeanQuery<T> query, int first, int max) throws DAOException {
return find(query).get(first, max);
}

/**
Expand Down

0 comments on commit 80d1f8d

Please sign in to comment.