Fix issues from static code analysis

Signed-off-by: Marcel Mager (INST-IOT/ESB) <Marcel.Mager@bosch-si.com>
This commit is contained in:
Marcel Mager (INST-IOT/ESB)
2016-09-22 10:28:08 +02:00
parent b790072d1f
commit 07a158534f
8 changed files with 215 additions and 83 deletions

View File

@@ -27,6 +27,7 @@ import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.eclipse.hawkbit.repository.FilterParams;
import org.eclipse.hawkbit.repository.TargetFields;
import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.eventbus.event.TargetDeletedEvent;
@@ -281,8 +282,10 @@ public class JpaTargetManagement implements TargetManagement {
public Slice<Target> findTargetByFilters(final Pageable pageable, final Collection<TargetUpdateStatus> status,
final Boolean overdueState, final String searchText, final Long installedOrAssignedDistributionSetId,
final Boolean selectTargetWithNoTag, final String... tagNames) {
final List<Specification<JpaTarget>> specList = buildSpecificationList(status, overdueState, searchText,
installedOrAssignedDistributionSetId, selectTargetWithNoTag, true, tagNames);
final List<Specification<JpaTarget>> specList = buildSpecificationList(
new FilterParams(installedOrAssignedDistributionSetId, status, overdueState, searchText,
selectTargetWithNoTag, tagNames),
true);
return findByCriteriaAPI(pageable, specList);
}
@@ -290,31 +293,35 @@ public class JpaTargetManagement implements TargetManagement {
public Long countTargetByFilters(final Collection<TargetUpdateStatus> status, final Boolean overdueState,
final String searchText, final Long installedOrAssignedDistributionSetId,
final Boolean selectTargetWithNoTag, final String... tagNames) {
final List<Specification<JpaTarget>> specList = buildSpecificationList(status, overdueState, searchText,
installedOrAssignedDistributionSetId, selectTargetWithNoTag, true, tagNames);
final List<Specification<JpaTarget>> specList = buildSpecificationList(
new FilterParams(installedOrAssignedDistributionSetId, status, overdueState, searchText,
selectTargetWithNoTag, tagNames),
true);
return countByCriteriaAPI(specList);
}
private static List<Specification<JpaTarget>> buildSpecificationList(final Collection<TargetUpdateStatus> status,
final Boolean overdueState, final String searchText, final Long installedOrAssignedDistributionSetId,
final Boolean selectTargetWithNoTag, final boolean fetch, final String... tagNames) {
private static List<Specification<JpaTarget>> buildSpecificationList(final FilterParams filterParams,
final boolean fetch) {
final List<Specification<JpaTarget>> specList = new ArrayList<>();
if (status != null && !status.isEmpty()) {
specList.add(TargetSpecifications.hasTargetUpdateStatus(status, fetch));
if (filterParams.getFilterByStatus() != null && !filterParams.getFilterByStatus().isEmpty()) {
specList.add(TargetSpecifications.hasTargetUpdateStatus(filterParams.getFilterByStatus(), fetch));
}
if (overdueState != null) {
if (filterParams.getOverdueState() != null) {
specList.add(
TargetSpecifications.isOverdue(new TimestampCalculator().calculateOverdueTimestamp()));
}
if (installedOrAssignedDistributionSetId != null) {
if (filterParams.getFilterByDistributionId() != null) {
specList.add(
TargetSpecifications.hasInstalledOrAssignedDistributionSet(installedOrAssignedDistributionSetId));
TargetSpecifications
.hasInstalledOrAssignedDistributionSet(filterParams.getFilterByDistributionId()));
}
if (!Strings.isNullOrEmpty(searchText)) {
specList.add(TargetSpecifications.likeNameOrDescriptionOrIp(searchText));
if (!Strings.isNullOrEmpty(filterParams.getFilterBySearchText())) {
specList.add(TargetSpecifications.likeNameOrDescriptionOrIp(filterParams.getFilterBySearchText()));
}
if (selectTargetWithNoTag != null && (selectTargetWithNoTag || (tagNames != null && tagNames.length > 0))) {
specList.add(TargetSpecifications.hasTags(tagNames, selectTargetWithNoTag));
if (filterParams.getSelectTargetWithNoTag() != null && (filterParams.getSelectTargetWithNoTag()
|| (filterParams.getFilterByTagNames() != null && filterParams.getFilterByTagNames().length > 0))) {
specList.add(TargetSpecifications.hasTags(filterParams.getFilterByTagNames(),
filterParams.getSelectTargetWithNoTag()));
}
return specList;
}
@@ -428,10 +435,8 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Slice<Target> findTargetsAllOrderByLinkedDistributionSet(final Pageable pageable,
final Long orderByDistributionId, final Long filterByDistributionId,
final Collection<TargetUpdateStatus> filterByStatus, final Boolean overdueState,
final String filterBySearchText,
final Boolean selectTargetWithNoTag, final String... filterByTagNames) {
final Long orderByDistributionId, final FilterParams filterParams) {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<JpaTarget> query = cb.createQuery(JpaTarget.class);
final Root<JpaTarget> targetRoot = query.from(JpaTarget.class);
@@ -454,8 +459,7 @@ public class JpaTargetManagement implements TargetManagement {
// build the specifications and then to predicates necessary by the
// given filters
final Predicate[] specificationsForMultiSelect = specificationsToPredicate(
buildSpecificationList(filterByStatus, overdueState, filterBySearchText, filterByDistributionId,
selectTargetWithNoTag, true, filterByTagNames),
buildSpecificationList(filterParams, true),
targetRoot, query, cb);
// if we have some predicates then add it to the where clause of the
@@ -529,9 +533,9 @@ public class JpaTargetManagement implements TargetManagement {
targetRoot.get(JpaTarget_.controllerId), targetRoot.get(JpaTarget_.name), targetRoot.get(sortProperty));
final Predicate[] specificationsForMultiSelect = specificationsToPredicate(
buildSpecificationList(filterByStatus, overdueState, filterBySearchText,
installedOrAssignedDistributionSetId,
selectTargetWithNoTag, false, filterByTagNames),
buildSpecificationList(new FilterParams(installedOrAssignedDistributionSetId, filterByStatus,
overdueState, filterBySearchText,
selectTargetWithNoTag, filterByTagNames), false),
targetRoot, multiselect, cb);
// if we have some predicates then add it to the where clause of the

View File

@@ -13,6 +13,7 @@ package org.eclipse.hawkbit.repository.jpa.rsql;
* <p>
* This is used in context of string replacement.
*/
@FunctionalInterface
public interface VirtualPropertyLookup {
/**

View File

@@ -50,9 +50,9 @@ public class VirtualPropertyResolver implements VirtualPropertyLookup {
public String lookup(String rhs) {
String resolved = null;
if ("now_ts".equals(rhs.toLowerCase())) {
if ("now_ts".equalsIgnoreCase(rhs)) {
resolved = String.valueOf(Instant.now().toEpochMilli());
} else if ("overdue_ts".equals(rhs.toLowerCase())) {
} else if ("overdue_ts".equalsIgnoreCase(rhs)) {
resolved = String.valueOf(getTimestampCalculator().calculateOverdueTimestamp());
}
return resolved;

View File

@@ -16,21 +16,16 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.eclipse.hawkbit.ControllerPollProperties;
import org.eclipse.hawkbit.repository.FilterParams;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetFilterQuery;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag;
import org.eclipse.hawkbit.repository.jpa.model.helper.TenantConfigurationManagementHolder;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.eclipse.hawkbit.repository.model.ActionStatus;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.Target;
@@ -39,7 +34,6 @@ import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Slice;
import com.google.common.collect.Lists;
@@ -746,7 +740,7 @@ public class TargetManagementSearchTest extends AbstractJpaIntegrationTest {
targInstalled = sendUpdateActionStatusToTargets(ds, targInstalled, Status.FINISHED, "installed");
final Slice<Target> result = targetManagement.findTargetsAllOrderByLinkedDistributionSet(pageReq, ds.getId(),
null, null, null, null, Boolean.FALSE, new String[0]);
new FilterParams(null, null, null, null, Boolean.FALSE, new String[0]));
final Comparator<TenantAwareBaseEntity> byId = (e1, e2) -> Long.compare(e2.getId(), e1.getId());
@@ -801,7 +795,7 @@ public class TargetManagementSearchTest extends AbstractJpaIntegrationTest {
targInstalled = sendUpdateActionStatusToTargets(ds, targInstalled, Status.FINISHED, "installed");
final Slice<Target> result = targetManagement.findTargetsAllOrderByLinkedDistributionSet(pageReq, ds.getId(),
null, null, Boolean.TRUE, null, Boolean.FALSE, new String[0]);
new FilterParams(null, null, Boolean.TRUE, null, Boolean.FALSE, new String[0]));
final Comparator<TenantAwareBaseEntity> byId = (e1, e2) -> Long.compare(e2.getId(), e1.getId());