forked from kitodo/kitodo-production
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1bbea7
commit 3cf372b
Showing
9 changed files
with
715 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Kitodo/src/main/java/org/kitodo/production/services/data/DatabaseQueryPart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* (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; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A part of the filter that is resolved on the database. Searching is only | ||
* possible by ID or by ID range. | ||
*/ | ||
class DatabaseQueryPart implements UserSpecifiedFilter { | ||
|
||
private static final String SECOND_PARAMETER_EXTENSION = "upTo"; | ||
|
||
private FilterField filterField; | ||
private Integer firstId; | ||
private Integer upToId; | ||
|
||
/** | ||
* Constructor, creates a new DatabaseQueryPart. | ||
* | ||
* @param filterField | ||
* field to search on | ||
* @param firstId | ||
* first or only ID | ||
* @param upToId | ||
* {@code null} or last ID | ||
*/ | ||
DatabaseQueryPart(FilterField filterField, String firstId, String upToId) { | ||
this.filterField = filterField; | ||
this.firstId = Integer.valueOf(firstId); | ||
this.upToId = Objects.nonNull(upToId) ? Integer.valueOf(upToId) : null; | ||
} | ||
|
||
@Override | ||
public FilterField getFilterField() { | ||
return filterField; | ||
} | ||
|
||
/** | ||
* Returns the part HQL query. If the query contains the keyword "IN", it | ||
* must be queried using JOIN, otherwise using WHERE. | ||
* | ||
* @param className | ||
* "Task" for a query on the tasks table, else for a query on the | ||
* process table. | ||
* @param varName | ||
* variable name to use in the query | ||
* @param parameterName | ||
* parameter name to use in the query | ||
* @return the part HQL query | ||
*/ | ||
String getDatabaseQuery(String className, String varName, String parameterName) { | ||
String query = Objects.equals(className, "Task") ? filterField.getTaskQuery() : filterField.getProcessQuery(); | ||
return varName + '.' + query + (upToId == null ? " = :" + parameterName | ||
: " BETWEEN :" + parameterName + " AND :" + parameterName + SECOND_PARAMETER_EXTENSION); | ||
} | ||
|
||
/** | ||
* Puts the parameters necessary for the query. | ||
* | ||
* @param parameterName | ||
* parameter name used in the query | ||
* @param parameters | ||
* map to put the parameters into | ||
*/ | ||
void addParameters(String parameterName, Map<String, Object> parameters) { | ||
if (Objects.nonNull(filterField.getQueryObject())) { | ||
parameters.put("queryObject", filterField.getQueryObject()); | ||
} | ||
parameters.put(parameterName, firstId); | ||
if (Objects.nonNull(upToId)) { | ||
parameters.put(parameterName.concat(SECOND_PARAMETER_EXTENSION), upToId); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return filterField + (upToId == null ? " = " + firstId : " BETWEEN " + firstId + " AND " + upToId); | ||
} | ||
} |
Oops, something went wrong.