Skip to content

Commit

Permalink
Apply Lift changes
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Kesterson <rkesters@gmail.com>
  • Loading branch information
rkesters committed Apr 27, 2023
1 parent fbfbd62 commit 1c1924f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,37 @@
import alpine.common.logging.Logger;
import alpine.event.framework.Event;
import alpine.model.ApiKey;
import alpine.model.IConfigProperty;
import alpine.model.Team;
import alpine.model.UserPrincipal;
import alpine.persistence.PaginatedResult;
import alpine.resources.AlpineRequest;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import org.dependencytrack.event.IndexEvent;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.ComponentIdentity;
import org.dependencytrack.model.ComponentProperty;
import org.dependencytrack.model.ConfigPropertyConstants;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.RepositoryMetaComponent;
import org.dependencytrack.model.RepositoryType;

import javax.jdo.FetchPlan;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.HashSet;
import javax.jdo.FetchPlan;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.json.Json;
import javax.json.JsonValue;
import javax.json.JsonArray;
import javax.json.JsonValue;
import org.datanucleus.exceptions.NucleusObjectNotFoundException;
import org.dependencytrack.event.IndexEvent;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.ComponentIdentity;
import org.dependencytrack.model.ComponentProperty;
import org.dependencytrack.model.ConfigPropertyConstants;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.RepositoryMetaComponent;
import org.dependencytrack.model.RepositoryType;

final class ComponentQueryManager extends QueryManager implements IQueryManager {

Expand All @@ -75,7 +78,7 @@ final class ComponentQueryManager extends QueryManager implements IQueryManager
* Returns a list of all Components defined in the datastore.
* @return a List of Components
*/
public PaginatedResult getComponents(final boolean includeMetrics) {
@Override public PaginatedResult getComponents(final boolean includeMetrics) {
final PaginatedResult result;
final Query<Component> query = pm.newQuery(Component.class);
if (orderBy == null) {
Expand All @@ -102,18 +105,18 @@ public PaginatedResult getComponents(final boolean includeMetrics) {
* Returns a list of all Components defined in the datastore.
* @return a List of Components
*/
public PaginatedResult getComponents() {
@Override public PaginatedResult getComponents() {
return getComponents(false);
}

/**
* Returns a ProjectProperty with the specified groupName and propertyName.
* @param component the project the property belongs to
* Returns a ComponentProperty with the specified groupName and propertyName.
* @param component the component the property belongs to
* @param groupName the group name of the config property
* @param propertyName the name of the property
* @return a ProjectProperty object
* @return a ComponentProperty object
*/
public ComponentProperty getComponentProperty(final Component component, final String groupName, final String propertyName) {
@Override public ComponentProperty getComponentProperty(final Component component, final String groupName, final String propertyName) {
final Query<ComponentProperty> query = this.pm.newQuery(ComponentProperty.class, "component == :component && groupName == :groupName && propertyName == :propertyName");
query.setRange(0, 1);
return singleResult(query.execute(component, groupName, propertyName));
Expand All @@ -124,7 +127,7 @@ public ComponentProperty getComponentProperty(final Component component, final S
* @param component the project the property belongs to
* @return a List ProjectProperty objects
*/
@SuppressWarnings("unchecked")
@Override @SuppressWarnings("unchecked")
public List<ComponentProperty> getComponentProperties(final Component component) {
final Query<ComponentProperty> query = this.pm.newQuery(ComponentProperty.class, "component == :component");
query.setOrdering("groupName asc, propertyName asc");
Expand All @@ -141,8 +144,8 @@ public List<ComponentProperty> getComponentProperties(final Component component)
* @param description a description of the property
* @return the created ComponentProperty object
*/
public ComponentProperty createComponentProperty(final Component component, final String groupName, final String propertyName,
final String propertyValue, final ComponentProperty.PropertyType propertyType,
@Override public ComponentProperty createComponentProperty(final Component component, final String groupName, final String propertyName,
final String propertyValue, final IConfigProperty.PropertyType propertyType,
final String description) {
final ComponentProperty property = new ComponentProperty();
property.setComponent(component);
Expand All @@ -159,7 +162,7 @@ public ComponentProperty createComponentProperty(final Component component, fina
* This method if designed NOT to provide paginated results.
* @return a List of Components
*/
public List<Component> getAllComponents() {
@Override public List<Component> getAllComponents() {
final Query<Component> query = pm.newQuery(Component.class);
query.setOrdering("id asc");
return query.executeList();
Expand All @@ -171,7 +174,7 @@ public List<Component> getAllComponents() {
* @param project the Project to retrieve dependencies of
* @return a List of Component objects
*/
@SuppressWarnings("unchecked")
@Override @SuppressWarnings("unchecked")
public List<Component> getAllComponents(Project project) {
final Query<Component> query = pm.newQuery(Component.class, "project == :project");
query.getFetchPlan().setMaxFetchDepth(2);
Expand All @@ -184,7 +187,7 @@ public List<Component> getAllComponents(Project project) {
* @param project the Project to retrieve dependencies of
* @return a List of Dependency objects
*/
public PaginatedResult getComponents(final Project project, final boolean includeMetrics) {
@Override public PaginatedResult getComponents(final Project project, final boolean includeMetrics) {
final PaginatedResult result;
final Query<Component> query = pm.newQuery(Component.class, "project == :project");
query.getFetchPlan().setMaxFetchDepth(2);
Expand All @@ -206,7 +209,7 @@ public PaginatedResult getComponents(final Project project, final boolean includ
final PackageURL purl = component.getPurl();
if (purl != null) {
final RepositoryType type = RepositoryType.resolve(purl);
if (RepositoryType.UNSUPPORTED != type) {
if (type != RepositoryType.UNSUPPORTED) {
final RepositoryMetaComponent repoMetaComponent = getRepositoryMetaComponent(type, purl.getNamespace(), purl.getName());
component.setRepositoryMeta(repoMetaComponent);
}
Expand All @@ -221,7 +224,7 @@ public PaginatedResult getComponents(final Project project, final boolean includ
* @param hash the hash of the component to retrieve
* @return a list of components
*/
public PaginatedResult getComponentByHash(String hash) {
@Override public PaginatedResult getComponentByHash(String hash) {
if (hash == null) {
return null;
}
Expand All @@ -246,11 +249,11 @@ public PaginatedResult getComponentByHash(String hash) {
* @param identity the ComponentIdentity to query against
* @return a list of components
*/
public PaginatedResult getComponents(ComponentIdentity identity) {
@Override public PaginatedResult getComponents(ComponentIdentity identity) {
return getComponents(identity, null, false);
}

public PaginatedResult getComponents(ComponentIdentity identity, boolean includeMetrics) {
@Override public PaginatedResult getComponents(ComponentIdentity identity, boolean includeMetrics) {
return getComponents(identity, null, includeMetrics);
}

Expand All @@ -261,7 +264,7 @@ public PaginatedResult getComponents(ComponentIdentity identity, boolean include
* @param includeMetrics whether or not to include component metrics or not
* @return a list of components
*/
public PaginatedResult getComponents(ComponentIdentity identity, Project project, boolean includeMetrics) {
@Override public PaginatedResult getComponents(ComponentIdentity identity, Project project, boolean includeMetrics) {
if (identity == null) {
return null;
}
Expand Down Expand Up @@ -317,7 +320,7 @@ public PaginatedResult getComponents(ComponentIdentity identity, Project project
final PackageURL purl = component.getPurl();
if (purl != null) {
final RepositoryType type = RepositoryType.resolve(purl);
if (RepositoryType.UNSUPPORTED != type) {
if (type != RepositoryType.UNSUPPORTED) {
final RepositoryMetaComponent repoMetaComponent = getRepositoryMetaComponent(type, purl.getNamespace(), purl.getName());
component.setRepositoryMeta(repoMetaComponent);
}
Expand Down Expand Up @@ -351,14 +354,14 @@ private PaginatedResult loadComponents(String queryFilter, Map<String, Object> p
* @param commitIndex specifies if the search index should be committed (an expensive operation)
* @return a new Component
*/
public Component createComponent(Component component, boolean commitIndex) {
@Override public Component createComponent(Component component, boolean commitIndex) {
final Component result = persist(component);
Event.dispatch(new IndexEvent(IndexEvent.Action.CREATE, pm.detachCopy(result)));
commitSearchIndex(commitIndex, Component.class);
return result;
}

public Component cloneComponent(Component sourceComponent, Project destinationProject, boolean commitIndex) {
@Override public Component cloneComponent(Component sourceComponent, Project destinationProject, boolean commitIndex) {
final Component component = new Component();
component.setGroup(sourceComponent.getGroup());
component.setName(sourceComponent.getName());
Expand Down Expand Up @@ -398,7 +401,7 @@ public Component cloneComponent(Component sourceComponent, Project destinationPr
* @param commitIndex specifies if the search index should be committed (an expensive operation)
* @return a Component
*/
public Component updateComponent(Component transientComponent, boolean commitIndex) {
@Override public Component updateComponent(Component transientComponent, boolean commitIndex) {
final Component component = getObjectByUuid(Component.class, transientComponent.getUuid());
component.setName(transientComponent.getName());
component.setVersion(transientComponent.getVersion());
Expand Down Expand Up @@ -429,7 +432,7 @@ public Component updateComponent(Component transientComponent, boolean commitInd
* Deletes all components for the specified Project.
* @param project the Project to delete components of
*/
protected void deleteComponents(Project project) {
@Override protected void deleteComponents(Project project) {
final Query<Component> query = pm.newQuery(Component.class, "project == :project");
query.deletePersistentAll(project);
}
Expand All @@ -439,7 +442,7 @@ protected void deleteComponents(Project project) {
* @param component the Component to delete
* @param commitIndex specifies if the search index should be committed (an expensive operation)
*/
public void recursivelyDelete(Component component, boolean commitIndex) {
@Override public void recursivelyDelete(Component component, boolean commitIndex) {
if (component.getChildren() != null) {
for (final Component child: component.getChildren()) {
recursivelyDelete(child, false);
Expand All @@ -456,7 +459,7 @@ public void recursivelyDelete(Component component, boolean commitIndex) {
deletePolicyViolations(component);
delete(component);
commitSearchIndex(commitIndex, Component.class);
} catch (javax.jdo.JDOObjectNotFoundException | org.datanucleus.exceptions.NucleusObjectNotFoundException e) {
} catch (JDOObjectNotFoundException | NucleusObjectNotFoundException e) {
LOGGER.warn("Deletion of component failed because it didn't exist anymore.");
}

Expand All @@ -468,7 +471,7 @@ public void recursivelyDelete(Component component, boolean commitIndex) {
* @param cid the identity values of the component
* @return a Component object, or null if not found
*/
public Component matchSingleIdentity(final Project project, final ComponentIdentity cid) {
@Override public Component matchSingleIdentity(final Project project, final ComponentIdentity cid) {
String purlString = null;
String purlCoordinates = null;
if (cid.getPurl() != null) {
Expand All @@ -490,7 +493,7 @@ public Component matchSingleIdentity(final Project project, final ComponentIdent
* @param cid the identity values of the component
* @return a List of Component objects, or null if not found
*/
@SuppressWarnings("unchecked")
@Override @SuppressWarnings("unchecked")
public List<Component> matchIdentity(final Project project, final ComponentIdentity cid) {
String purlString = null;
String purlCoordinates = null;
Expand All @@ -511,7 +514,7 @@ public List<Component> matchIdentity(final Project project, final ComponentIdent
* @param cid the identity values of the component
* @return a List of Component objects
*/
@SuppressWarnings("unchecked")
@Override @SuppressWarnings("unchecked")
public List<Component> matchIdentity(final ComponentIdentity cid) {
String purlString = null;
String purlCoordinates = null;
Expand All @@ -535,7 +538,7 @@ public List<Component> matchIdentity(final ComponentIdentity cid) {
* @param existingProjectComponents the complete list of existing dependent components
* @param components the complete list of components that should be dependencies of the project
*/
public void reconcileComponents(Project project, List<Component> existingProjectComponents, List<Component> components) {
@Override public void reconcileComponents(Project project, List<Component> existingProjectComponents, List<Component> components) {
// Removes components as dependencies to the project for all
// components not included in the list provided
List<Component> markedForDeletion = new ArrayList<>();
Expand Down Expand Up @@ -601,7 +604,7 @@ private void preprocessACLs(final Query<Component> query, final String inputFilt
}
}

public Map<String, Component> getDependencyGraphForComponent(Project project, Component component) {
@Override public Map<String, Component> getDependencyGraphForComponent(Project project, Component component) {
Map<String, Component> dependencyGraph = new HashMap<>();
if (project.getDirectDependencies() == null || project.getDirectDependencies().isBlank()) {
return dependencyGraph;
Expand Down Expand Up @@ -638,7 +641,7 @@ public Map<String, Component> getDependencyGraphForComponent(Project project, Co
transientComponent.setExpandDependencyGraph(entry.getValue().isExpandDependencyGraph());
if (transientComponent.getPurl() != null) {
final RepositoryType type = RepositoryType.resolve(transientComponent.getPurl());
if (RepositoryType.UNSUPPORTED != type) {
if (type != RepositoryType.UNSUPPORTED) {
final RepositoryMetaComponent repoMetaComponent = getRepositoryMetaComponent(type, transientComponent.getPurl().getNamespace(), transientComponent.getPurl().getName());
if (repoMetaComponent != null) {
RepositoryMetaComponent transientRepoMetaComponent = new RepositoryMetaComponent();
Expand All @@ -657,7 +660,7 @@ private void getParentDependenciesOfComponent(Project project, Component parentN
final Query<Component> query = pm.newQuery(Component.class, "directDependencies.matches(:queryUuid) && project == :project");
List<Component> components = (List<Component>) query.executeWithArray(queryUuid, project);
for (Component component : components) {
if (component.getUuid() != searchedComponent.getUuid()) {
if (!Objects.equals(component.getUuid(), searchedComponent.getUuid())) {
component.setExpandDependencyGraph(true);
if (dependencyGraph.containsKey(component.getUuid().toString())) {
if (component.getDependencyGraph().add(component.getUuid().toString())) {
Expand Down
Loading

0 comments on commit 1c1924f

Please sign in to comment.