Skip to content

Commit

Permalink
Create ComponentQueryFilterBuilder.java
Browse files Browse the repository at this point in the history
Signed-off-by: starfishfive <[email protected]>
  • Loading branch information
starfishfive committed May 17, 2024
1 parent d271a94 commit 35315f9
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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}
* <br>
* Mutable and not threadsafe!
*/
class ComponentQueryFilterBuilder {

private final Map<String, Object> params;
private final List<String> 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<String, Object> getParams() {
return params;
}
}

0 comments on commit 35315f9

Please sign in to comment.