From 35315f913b8d7d3e987709f053255ff3817312a3 Mon Sep 17 00:00:00 2001 From: starfishfive <161029169+starfishfive@users.noreply.github.com> Date: Fri, 17 May 2024 16:56:12 +0200 Subject: [PATCH] Create ComponentQueryFilterBuilder.java Signed-off-by: starfishfive <161029169+starfishfive@users.noreply.github.com> --- .../ComponentQueryFilterBuilder.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/org/dependencytrack/persistence/ComponentQueryFilterBuilder.java diff --git a/src/main/java/org/dependencytrack/persistence/ComponentQueryFilterBuilder.java b/src/main/java/org/dependencytrack/persistence/ComponentQueryFilterBuilder.java new file mode 100644 index 0000000000..349c10b624 --- /dev/null +++ b/src/main/java/org/dependencytrack/persistence/ComponentQueryFilterBuilder.java @@ -0,0 +1,63 @@ +/* + * This file is part of Dependency-Track. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright (c) OWASP Foundation. All Rights Reserved. + */ +package org.dependencytrack.persistence; + +import org.dependencytrack.model.Classifier; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Builder for filters meant to be used with {@link javax.jdo.Query#setFilter} and the query's + * parameters that can be passed to {@link alpine.persistence.AbstractAlpineQueryManager#execute} + *
+ * Mutable and not threadsafe! + */ +class ComponentQueryFilterBuilder { + + private final Map params; + private final List filterCriteria; + + ComponentQueryFilterBuilder() { + this.params = new HashMap<>(); + this.filterCriteria = new ArrayList<>(); + } + + ComponentQueryFilterBuilder withAuthor(string author) { + params.put("author", author); + filterCriteria.add("(author == :author)"); + return this; + } + + ComponentQueryFilterBuilder withClassifier(Classifier classifier) { + params.put("classifier", classifier); + filterCriteria.add("(classifier == :classifier)"); + return this; + } + + String buildFilter() { + return String.join(" && ", this.filterCriteria); + } + + Map getParams() { + return params; + } +}