Fix findings from overdue review:

* Introduce VirtualPropertyLookup interface
* Remove Apache StrLookup from signature and use VirtualPropertyLookup
* Use (internal) adapter to adapt VirtualPropertyLookup to StrLookup
* Use interface instead of implementation for auto-wiring
* Extract calculation for overdue_ts to service TimestampCalculator

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-15 11:38:38 +02:00
parent 76c3c96075
commit 00aaed3027
16 changed files with 283 additions and 181 deletions

View File

@@ -52,7 +52,6 @@ import org.eclipse.hawkbit.repository.jpa.model.helper.SystemManagementHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.SystemSecurityContextHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.TenantAwareHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.TenantConfigurationManagementHolder;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyMakroResolver;
import org.eclipse.hawkbit.security.SecurityTokenGenerator;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.tenancy.TenantAware;
@@ -392,14 +391,4 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
return new JpaEntityFactory();
}
/**
* {@link VirtualPropertyMakroResolver} bean.
*
* @return a new {@link VirtualPropertyMakroResolver}
*/
@Bean
@ConditionalOnMissingBean
public VirtualPropertyMakroResolver virtualPropertyMakroResolver() {
return new VirtualPropertyMakroResolver();
}
}

View File

@@ -54,6 +54,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetInfo;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.jpa.specifications.TargetSpecifications;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.ActionType;
@@ -134,6 +135,9 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Autowired
private SystemSecurityContext systemSecurityContext;
@Autowired
private VirtualPropertyLookup virtualPropertyLookup;
@Override
@Transactional(isolation = Isolation.READ_COMMITTED)
@Modifying
@@ -605,7 +609,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
}
private Specification<JpaAction> createSpecificationFor(final Target target, final String rsqlParam) {
final Specification<JpaAction> spec = RSQLUtility.parse(rsqlParam, ActionFields.class);
final Specification<JpaAction> spec = RSQLUtility.parse(rsqlParam, ActionFields.class, virtualPropertyLookup);
return (root, query, cb) -> cb.and(spec.toPredicate(root, query, cb),
cb.equal(root.get(JpaAction_.target), target));
}

View File

@@ -43,6 +43,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetMetadata_;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetType;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet_;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetSpecification;
import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetTypeSpecification;
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
@@ -107,6 +108,9 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Autowired
private TenantAware tenantAware;
@Autowired
private VirtualPropertyLookup virtualPropertyLookup;
@Override
public DistributionSet findDistributionSetByIdWithDetails(final Long distid) {
return distributionSetRepository.findOne(DistributionSetSpecification.byId(distid));
@@ -286,7 +290,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Override
public Page<DistributionSetType> findDistributionSetTypesAll(final String rsqlParam, final Pageable pageable) {
final Specification<JpaDistributionSetType> spec = RSQLUtility.parse(rsqlParam,
DistributionSetTypeFields.class);
DistributionSetTypeFields.class, virtualPropertyLookup);
return convertDsTPage(distributionSetTypeRepository.findAll(spec, pageable));
}
@@ -352,7 +356,8 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
public Page<DistributionSet> findDistributionSetsAll(final String rsqlParam, final Pageable pageReq,
final Boolean deleted) {
final Specification<JpaDistributionSet> spec = RSQLUtility.parse(rsqlParam, DistributionSetFields.class);
final Specification<JpaDistributionSet> spec = RSQLUtility.parse(rsqlParam, DistributionSetFields.class,
virtualPropertyLookup);
final List<Specification<JpaDistributionSet>> specList = new ArrayList<>();
if (deleted != null) {
@@ -530,7 +535,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
/**
* Method to get the latest distribution set based on ds ID after the
* metadata changes for that distribution set.
*
*
* @param distributionSet
* Distribution set
*/
@@ -566,7 +571,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
final String rsqlParam, final Pageable pageable) {
final Specification<JpaDistributionSetMetadata> spec = RSQLUtility.parse(rsqlParam,
DistributionSetMetadataFields.class);
DistributionSetMetadataFields.class, virtualPropertyLookup);
return convertMdPage(
distributionSetMetadataRepository

View File

@@ -33,6 +33,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTarget_;
import org.eclipse.hawkbit.repository.jpa.model.RolloutTargetGroup;
import org.eclipse.hawkbit.repository.jpa.model.RolloutTargetGroup_;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.Rollout.RolloutStatus;
@@ -70,6 +71,9 @@ public class JpaRolloutGroupManagement implements RolloutGroupManagement {
@Autowired
private EntityManager entityManager;
@Autowired
private VirtualPropertyLookup virtualPropertyLookup;
@Override
public RolloutGroup findRolloutGroupById(final Long rolloutGroupId) {
return rolloutGroupRepository.findOne(rolloutGroupId);
@@ -92,7 +96,8 @@ public class JpaRolloutGroupManagement implements RolloutGroupManagement {
public Page<RolloutGroup> findRolloutGroupsAll(final Rollout rollout, final String rsqlParam,
final Pageable pageable) {
final Specification<JpaRolloutGroup> specification = RSQLUtility.parse(rsqlParam, RolloutGroupFields.class);
final Specification<JpaRolloutGroup> specification = RSQLUtility.parse(rsqlParam, RolloutGroupFields.class,
virtualPropertyLookup);
return convertPage(
rolloutGroupRepository
@@ -145,7 +150,8 @@ public class JpaRolloutGroupManagement implements RolloutGroupManagement {
public Page<Target> findRolloutGroupTargets(final RolloutGroup rolloutGroup, final String rsqlParam,
final Pageable pageable) {
final Specification<JpaTarget> rsqlSpecification = RSQLUtility.parse(rsqlParam, TargetFields.class);
final Specification<JpaTarget> rsqlSpecification = RSQLUtility.parse(rsqlParam, TargetFields.class,
virtualPropertyLookup);
return convertTPage(targetRepository.findAll((root, query, criteriaBuilder) -> {
final ListJoin<JpaTarget, RolloutTargetGroup> rolloutTargetJoin = root.join(JpaTarget_.rolloutTargetGroup);

View File

@@ -32,6 +32,7 @@ import org.eclipse.hawkbit.repository.jpa.model.RolloutTargetGroup;
import org.eclipse.hawkbit.repository.jpa.rollout.condition.RolloutGroupActionEvaluator;
import org.eclipse.hawkbit.repository.jpa.rollout.condition.RolloutGroupConditionEvaluator;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.ActionType;
import org.eclipse.hawkbit.repository.model.DistributionSet;
@@ -114,6 +115,9 @@ public class JpaRolloutManagement implements RolloutManagement {
@Autowired
private CacheWriteNotify cacheWriteNotify;
@Autowired
private VirtualPropertyLookup virtualPropertyLookup;
@Autowired
@Qualifier("asyncExecutor")
private Executor executor;
@@ -150,7 +154,8 @@ public class JpaRolloutManagement implements RolloutManagement {
@Override
public Page<Rollout> findAllWithDetailedStatusByPredicate(final String rsqlParam, final Pageable pageable) {
final Specification<JpaRollout> specification = RSQLUtility.parse(rsqlParam, RolloutFields.class);
final Specification<JpaRollout> specification = RSQLUtility.parse(rsqlParam, RolloutFields.class,
virtualPropertyLookup);
final Page<JpaRollout> findAll = rolloutRepository.findAll(specification, pageable);
setRolloutStatusDetails(findAll);

View File

@@ -42,6 +42,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModuleType;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule_;
import org.eclipse.hawkbit.repository.jpa.model.SwMetadataCompositeKey;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.jpa.specifications.SoftwareModuleSpecification;
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.model.AssignedSoftwareModule;
@@ -102,6 +103,9 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Autowired
private ArtifactManagement artifactManagement;
@Autowired
private VirtualPropertyLookup virtualPropertyLookup;
@Override
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@@ -312,7 +316,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Override
public Page<SoftwareModule> findSoftwareModulesByPredicate(final String rsqlParam, final Pageable pageable) {
final Specification<JpaSoftwareModule> spec = RSQLUtility.parse(rsqlParam, SoftwareModuleFields.class);
final Specification<JpaSoftwareModule> spec = RSQLUtility.parse(rsqlParam, SoftwareModuleFields.class,
virtualPropertyLookup);
return convertSmPage(softwareModuleRepository.findAll(spec, pageable), pageable);
}
@@ -320,7 +325,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Override
public Page<SoftwareModuleType> findSoftwareModuleTypesAll(final String rsqlParam, final Pageable pageable) {
final Specification<JpaSoftwareModuleType> spec = RSQLUtility.parse(rsqlParam, SoftwareModuleTypeFields.class);
final Specification<JpaSoftwareModuleType> spec = RSQLUtility.parse(rsqlParam, SoftwareModuleTypeFields.class,
virtualPropertyLookup);
return convertSmTPage(softwareModuleTypeRepository.findAll(spec, pageable), pageable);
}
@@ -609,7 +615,7 @@ public class JpaSoftwareManagement implements SoftwareManagement {
final String rsqlParam, final Pageable pageable) {
final Specification<JpaSoftwareModuleMetadata> spec = RSQLUtility.parse(rsqlParam,
SoftwareModuleMetadataFields.class);
SoftwareModuleMetadataFields.class, virtualPropertyLookup);
return convertSmMdPage(
softwareModuleMetadataRepository
.findAll(

View File

@@ -30,6 +30,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetTag;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.tenancy.TenantAware;
@@ -74,6 +75,9 @@ public class JpaTagManagement implements TagManagement {
@Autowired
private AfterTransactionCommitExecutor afterCommit;
@Autowired
private VirtualPropertyLookup virtualPropertyLookup;
@Override
public TargetTag findTargetTag(final String name) {
return targetTagRepository.findByNameEquals(name);
@@ -147,7 +151,7 @@ public class JpaTagManagement implements TagManagement {
@Override
public Page<TargetTag> findAllTargetTags(final String rsqlParam, final Pageable pageable) {
final Specification<JpaTargetTag> spec = RSQLUtility.parse(rsqlParam, TagFields.class);
final Specification<JpaTargetTag> spec = RSQLUtility.parse(rsqlParam, TagFields.class, virtualPropertyLookup);
return convertTPage(targetTagRepository.findAll(spec, pageable), pageable);
}
@@ -279,7 +283,8 @@ public class JpaTagManagement implements TagManagement {
@Override
public Page<DistributionSetTag> findAllDistributionSetTags(final String rsqlParam, final Pageable pageable) {
final Specification<JpaDistributionSetTag> spec = RSQLUtility.parse(rsqlParam, TagFields.class);
final Specification<JpaDistributionSetTag> spec = RSQLUtility.parse(rsqlParam, TagFields.class,
virtualPropertyLookup);
return convertDsPage(distributionSetTagRepository.findAll(spec, pageable), pageable);
}

View File

@@ -16,7 +16,7 @@ import org.eclipse.hawkbit.repository.TargetFilterQueryManagement;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetFilterQuery;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyMakroResolver;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.jpa.specifications.TargetFilterQuerySpecification;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
@@ -46,7 +46,7 @@ public class JpaTargetFilterQueryManagement implements TargetFilterQueryManageme
private TargetFilterQueryRepository targetFilterQueryRepository;
@Autowired
private VirtualPropertyMakroResolver virtualPropMakroResolver;
private VirtualPropertyLookup virtualPropertyLookup;
@Override
@Modifying
@@ -115,7 +115,7 @@ public class JpaTargetFilterQueryManagement implements TargetFilterQueryManageme
@Override
public boolean verifyTargetFilterQuerySyntax(final String query) {
RSQLUtility.parse(query, TargetFields.class, virtualPropMakroResolver);
RSQLUtility.parse(query, TargetFields.class, virtualPropertyLookup);
return true;
}

View File

@@ -41,7 +41,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTargetInfo_;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget_;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyMakroResolver;
import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyLookup;
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.jpa.specifications.TargetSpecifications;
import org.eclipse.hawkbit.repository.model.Target;
@@ -104,7 +104,7 @@ public class JpaTargetManagement implements TargetManagement {
private AfterTransactionCommitExecutor afterCommit;
@Autowired
private VirtualPropertyMakroResolver virtualPropMakroResolver;
private VirtualPropertyLookup virtualPropertyLookup;
@Override
public Target findTargetByControllerID(final String controllerId) {
@@ -157,13 +157,13 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Slice<Target> findTargetsAll(final TargetFilterQuery targetFilterQuery, final Pageable pageable) {
return findTargetsBySpec(
RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class, virtualPropMakroResolver),
RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class, virtualPropertyLookup),
pageable);
}
@Override
public Page<Target> findTargetsAll(final String targetFilterQuery, final Pageable pageable) {
return findTargetsBySpec(RSQLUtility.parse(targetFilterQuery, TargetFields.class, virtualPropMakroResolver),
return findTargetsBySpec(RSQLUtility.parse(targetFilterQuery, TargetFields.class, virtualPropertyLookup),
pageable);
}
@@ -232,7 +232,7 @@ public class JpaTargetManagement implements TargetManagement {
final Pageable pageReq) {
final Specification<JpaTarget> spec = RSQLUtility.parse(rsqlParam, TargetFields.class,
virtualPropMakroResolver);
virtualPropertyLookup);
return convertPage(
targetRepository
@@ -261,7 +261,7 @@ public class JpaTargetManagement implements TargetManagement {
final Pageable pageable) {
final Specification<JpaTarget> spec = RSQLUtility.parse(rsqlParam, TargetFields.class,
virtualPropMakroResolver);
virtualPropertyLookup);
return convertPage(
targetRepository
@@ -304,7 +304,7 @@ public class JpaTargetManagement implements TargetManagement {
}
if (overdueState != null) {
specList.add(
TargetSpecifications.isOverdue(new VirtualPropertyMakroResolver().calculateOverdueTimestamp()));
TargetSpecifications.isOverdue(new TimestampCalculator().calculateOverdueTimestamp()));
}
if (installedOrAssignedDistributionSetId != null) {
specList.add(
@@ -561,7 +561,7 @@ public class JpaTargetManagement implements TargetManagement {
targetRoot.get(JpaTarget_.controllerId), targetRoot.get(JpaTarget_.name), targetRoot.get(sortProperty));
final Specification<JpaTarget> spec = RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class,
virtualPropMakroResolver);
virtualPropertyLookup);
final List<Specification<JpaTarget>> specList = new ArrayList<>();
specList.add(spec);
@@ -645,14 +645,14 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Long countTargetByTargetFilterQuery(final TargetFilterQuery targetFilterQuery) {
final Specification<JpaTarget> specs = RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class,
virtualPropMakroResolver);
virtualPropertyLookup);
return targetRepository.count(specs);
}
@Override
public Long countTargetByTargetFilterQuery(final String targetFilterQuery) {
final Specification<JpaTarget> specs = RSQLUtility.parse(targetFilterQuery, TargetFields.class,
virtualPropMakroResolver);
virtualPropertyLookup);
return targetRepository.count(specs);
}

View File

@@ -0,0 +1,45 @@
package org.eclipse.hawkbit.repository.jpa;
import java.time.Duration;
import java.time.Instant;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.repository.jpa.model.helper.TenantConfigurationManagementHolder;
import org.eclipse.hawkbit.tenancy.configuration.DurationHelper;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.springframework.stereotype.Service;
@Service
public class TimestampCalculator {
/**
* Calculates the overdue timestamp (<em>overdue_ts</em>) based on the
* current timestamp and the intervals for polling and poll-overdue:
* <p>
* <em>overdue_ts = now_ts - pollingInterval -
* pollingOverdueInterval</em>;<br>
* <em>pollingInterval</em> and <em>pollingOverdueInterval</em> are
* retrieved from tenant-specific system configuration.
*
* @return <em>overdue_ts</em> in milliseconds since Unix epoch as long
* value
*/
public long calculateOverdueTimestamp() {
long overdueTs = Instant.now().toEpochMilli() //
- getDurationForKey(TenantConfigurationKey.POLLING_TIME_INTERVAL).toMillis() //
- getDurationForKey(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL).toMillis();
return overdueTs;
}
private Duration getDurationForKey(TenantConfigurationKey key) {
return DurationHelper.formattedStringToDuration(getRawStringForKey(key));
}
private String getRawStringForKey(TenantConfigurationKey key) {
return getTenantConfigurationManagement().getConfigurationValue(key, String.class).getValue();
}
protected TenantConfigurationManagement getTenantConfigurationManagement() {
return TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement();
}
}

View File

@@ -70,12 +70,12 @@ import cz.jirutka.rsql.parser.ast.RSQLVisitor;
* <li>name==targetId1 or description==plugAndPlay or updateStatus==UNKNOWN</li>
* </ul>
* <p>
* There is also a mechanism that allows to refer to known makros that can resolved by an optional {@link StrLookup}
* (cp. {@link VirtualPropertyMakroResolver}).<br>
* There is also a mechanism that allows to refer to known macros that can resolved by an optional {@link StrLookup}
* (cp. {@link VirtualPropertyResolver}).<br>
* An example that queries for all overdue targets using the ${OVERDUE_TS} placeholder introduced by
* {@link VirtualPropertyMakroResolver} looks like this:<br>
* {@link VirtualPropertyResolver} looks like this:<br>
* <em>lastControllerRequestAt=le=${OVERDUE_TS}</em><br>
* It is possible to escape a makro expression by using a second '$': $${OVERDUE_TS} would prevent the ${OVERDUE_TS}
* It is possible to escape a macro expression by using a second '$': $${OVERDUE_TS} would prevent the ${OVERDUE_TS}
* token from being expanded.
*
*/
@@ -99,6 +99,9 @@ public final class RSQLUtility {
* @param fieldNameProvider
* the enum class type which implements the
* {@link FieldNameProvider}
* @param virtualPropertyLookup
* holds the logic how the known macros have to be resolved; may
* be <code>null</code>
* @return an specification which can be used with JPA
* @throws RSQLParameterUnsupportedFieldException
* if a field in the RSQL string is used but not provided by the
@@ -107,29 +110,8 @@ public final class RSQLUtility {
* if the RSQL syntax is wrong
*/
public static <A extends Enum<A> & FieldNameProvider, T> Specification<T> parse(final String rsql,
final Class<A> fieldNameProvider) {
return new RSQLSpecification<>(rsql.toLowerCase(), fieldNameProvider, new VirtualPropertyMakroResolver());
}
/**
* parses an RSQL valid string into an JPA {@link Specification} which then can be used to filter for JPA entities
* with the given RSQL query.
*
* @param rsql
* the rsql query
* @param fieldNameProvider
* the enum class type which implements the {@link FieldNameProvider}
* @param makroLookup
* holds the logic how the known makros have to be resolved; may be <code>null</code>
* @return an specification which can be used with JPA
* @throws RSQLParameterUnsupportedFieldException
* if a field in the RSQL string is used but not provided by the given {@code fieldNameProvider}
* @throws RSQLParameterSyntaxException
* if the RSQL syntax is wrong
*/
public static <A extends Enum<A> & FieldNameProvider, T> Specification<T> parse(final String rsql,
final Class<A> fieldNameProvider, StrLookup<String> makroLookup) {
return new RSQLSpecification<>(rsql.toLowerCase(), fieldNameProvider, makroLookup);
final Class<A> fieldNameProvider, VirtualPropertyLookup virtualPropertyLookup) {
return new RSQLSpecification<>(rsql.toLowerCase(), fieldNameProvider, virtualPropertyLookup);
}
/**
@@ -160,12 +142,13 @@ public final class RSQLUtility {
private final String rsql;
private final Class<A> enumType;
private final StrLookup<String> makroLookup;
private final VirtualPropertyLookup virtualPropertyLookup;
private RSQLSpecification(final String rsql, final Class<A> enumType, StrLookup<String> makroLookup) {
private RSQLSpecification(final String rsql, final Class<A> enumType,
VirtualPropertyLookup virtualPropertyLookup) {
this.rsql = rsql;
this.enumType = enumType;
this.makroLookup = makroLookup;
this.virtualPropertyLookup = virtualPropertyLookup;
}
@Override
@@ -174,7 +157,7 @@ public final class RSQLUtility {
final Node rootNode = parseRsql(rsql);
final JpqQueryRSQLVisitor<A, T> jpqQueryRSQLVisitor = new JpqQueryRSQLVisitor<>(root, cb, enumType,
makroLookup);
virtualPropertyLookup);
final List<Predicate> accept = rootNode.<List<Predicate>, String> accept(jpqQueryRSQLVisitor);
if (accept != null && !accept.isEmpty()) {
@@ -204,18 +187,19 @@ public final class RSQLUtility {
private final Root<T> root;
private final CriteriaBuilder cb;
private final Class<A> enumType;
private final StrLookup<String> makroLookup;
private final VirtualPropertyLookup virtualPropertyLookup;
private final StrSubstitutor substitutor;
private final SimpleTypeConverter simpleTypeConverter;
private JpqQueryRSQLVisitor(final Root<T> root, final CriteriaBuilder cb, final Class<A> enumType,
StrLookup<String> makroLookup) {
VirtualPropertyLookup virtualPropertyLookup) {
this.root = root;
this.cb = cb;
this.enumType = enumType;
this.makroLookup = makroLookup;
this.substitutor = new StrSubstitutor(makroLookup, StrSubstitutor.DEFAULT_PREFIX,
this.virtualPropertyLookup = virtualPropertyLookup;
this.substitutor = new StrSubstitutor(new StrLookupAdapter(virtualPropertyLookup),
StrSubstitutor.DEFAULT_PREFIX,
StrSubstitutor.DEFAULT_SUFFIX, StrSubstitutor.DEFAULT_ESCAPE);
simpleTypeConverter = new SimpleTypeConverter();
}
@@ -387,13 +371,10 @@ public final class RSQLUtility {
private Object convertValueIfNecessary(final ComparisonNode node, final A fieldName, final String value,
final Path<Object> fieldPath) {
// in case the value of an rsql query e.g. type==application is an
// enum we need to
// handle it separately because JPA needs the correct java-type to
// build an
// expression. So String and numeric values JPA can do it by it's
// own but not for
// classes like enums. So we need to transform the given value
// string into the enum
// enum we need to handle it separately because JPA needs the
// correct java-type to build an expression. So String and numeric
// values JPA can do it by it's own but not for classes like enums.
// So we need to transform the given value string into the enum
// class.
final Class<? extends Object> javaType = fieldPath.getJavaType();
if (javaType != null && javaType.isEnum()) {
@@ -445,8 +426,7 @@ public final class RSQLUtility {
return Enum.valueOf(tmpEnumType, value.toUpperCase());
} catch (final IllegalArgumentException e) {
// we could not transform the given string value into the enum
// type, so ignore
// it and return null and do not filter
// type, so ignore it and return null and do not filter
LOGGER.info("given value {} cannot be transformed into the correct enum type {}", value.toUpperCase(),
javaType);
LOGGER.debug("value cannot be transformed to an enum", e);
@@ -461,12 +441,13 @@ public final class RSQLUtility {
private List<Predicate> mapToPredicate(final ComparisonNode node, final Path<Object> fieldPath,
final List<String> values, final List<Object> transformedValues, final A enumField) {
// only 'equal' and 'notEqual' can handle transformed value like
// enums. The JPA API
// cannot handle object types for greaterThan etc methods.
// enums. The JPA API cannot handle object types for greaterThan etc
// methods.
final Object transformedValue = transformedValues.get(0);
final String value;
if (makroLookup != null) { // if substitutor is available, replace makros ...
if (virtualPropertyLookup != null) { // if lookup is available,
// replace macros ...
value = substitutor.replace(values.get(0));
} else {
value = values.get(0);
@@ -611,4 +592,28 @@ public final class RSQLUtility {
}
}
/**
* Adapts the <code>VirtualPropertyLookup</code> to <code>StrLookup</code>.
*/
final static class StrLookupAdapter extends StrLookup<String> {
private VirtualPropertyLookup virtualPropertyLookup;
/**
* Constructor.
*
* @param virtualPropertyLookup
* the lookup to adapt.
*/
StrLookupAdapter(VirtualPropertyLookup virtualPropertyLookup) {
this.virtualPropertyLookup = virtualPropertyLookup;
}
@Override
public String lookup(String key) {
return virtualPropertyLookup.lookup(key);
}
}
}

View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2016 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa.rsql;
/**
* Implementations map a placeholder to the associated value.
* <p>
* This is used in context of string replacement.
*/
public interface VirtualPropertyLookup {
/**
* Looks up a placeholder to the associated value.
*
* @param placeholder
* the virtual property that should be resolved by a value
* @return the value for the placeholder; may be <code>null</code> if no
* value could be found for the given placeholder;
*/
public String lookup(String placeholder);
}

View File

@@ -1,76 +0,0 @@
/**
* Copyright (c) 2016 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa.rsql;
import java.time.Duration;
import java.time.Instant;
import org.apache.commons.lang3.text.StrLookup;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.repository.jpa.model.helper.TenantConfigurationManagementHolder;
import org.eclipse.hawkbit.tenancy.configuration.DurationHelper;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
/**
* Adds makro capabilities to RSQL expressions that are used to filter for targets.
* <p>
* Some (virtual) properties do not have a representation in the database (in general these properties are time-related,
* or more explicitly, they deal with time intervals).<br>
* Such a virtual property needs to be calculated on Java-side before it may be used in a target filter query that is
* passed to the database. Therefore a placeholder is used in the RSQL expression that is expanded in the
* {@link RSQLUtility} by a {@link StrSubstitutor}. This {@link StrSubstitutor} is configured with an instance of
* {@link VirtualPropertyMakroResolver} to resolve the known makros.
* <p>
* A virtual property may either be a system value like the current date (aka <em>now_ts</em>) or a value derived from
* (tenant-specific) system configuration (e.g. <em>overdue_ts</em>).
* <p>
* Known values are:<br>
* <ul>
* <li><em>now_ts</em>: maps to system UTC time in milliseconds since Unix epoch as long value</li>
* <li><em>overdue_ts</em>: is a calculated value: <em>overdue_ts = now_ts - pollingInterval - pollingOverdueInterval
* </em>; pollingInterval and pollingOverdueInterval are retrieved from tenant-specific system configuration.</li>
* </ul>
*
*/
public class VirtualPropertyMakroResolver extends StrLookup<String> {
@Override
public String lookup(String rhs) {
String resolved = null;
if ("now_ts".equals(rhs.toLowerCase())) {
resolved = String.valueOf(Instant.now().toEpochMilli());
} else if ("overdue_ts".equals(rhs.toLowerCase())) {
resolved = String.valueOf(calculateOverdueTimestamp());
}
return resolved;
}
public long calculateOverdueTimestamp() {
long overdueTs = Instant.now().toEpochMilli() //
- getDurationForKey(TenantConfigurationKey.POLLING_TIME_INTERVAL).toMillis() //
- getDurationForKey(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL).toMillis();
return overdueTs;
}
private Duration getDurationForKey(TenantConfigurationKey key) {
return DurationHelper.formattedStringToDuration(getRawStringForKey(key));
}
private String getRawStringForKey(TenantConfigurationKey key) {
return getTenantConfigurationManagement().getConfigurationValue(key, String.class).getValue();
}
TenantConfigurationManagement getTenantConfigurationManagement() {
return TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement();
}
}

View File

@@ -0,0 +1,64 @@
/**
* Copyright (c) 2016 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa.rsql;
import java.time.Instant;
import org.eclipse.hawkbit.repository.jpa.TimestampCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Adds macro capabilities to RSQL expressions that are used to filter for
* targets.
* <p>
* Some (virtual) properties do not have a representation in the database (in
* general these properties are time-related, or more explicitly, they deal with
* time intervals).<br>
* Such a virtual property needs to be calculated on Java-side before it may be
* used in a target filter query that is passed to the database. Therefore a
* placeholder is used in the RSQL expression that is expanded in the
* {@link RSQLUtility}.
* <p>
* A virtual property may either be a system value like the current date (aka
* <em>now_ts</em>) or a value derived from (tenant-specific) system
* configuration (e.g. <em>overdue_ts</em>).
* <p>
* Known values are:<br>
* <ul>
* <li><em>now_ts</em>: maps to system UTC time in milliseconds since Unix epoch
* as long value</li>
* <li><em>overdue_ts</em>: is a calculated value: <em>overdue_ts = now_ts -
* pollingInterval - pollingOverdueInterval</em>; pollingInterval and
* pollingOverdueInterval are retrieved from tenant-specific system
* configuration.</li>
* </ul>
*/
@Component
public class VirtualPropertyResolver implements VirtualPropertyLookup {
@Autowired
private TimestampCalculator timestampCalculator;
@Override
public String lookup(String rhs) {
String resolved = null;
if ("now_ts".equals(rhs.toLowerCase())) {
resolved = String.valueOf(Instant.now().toEpochMilli());
} else if ("overdue_ts".equals(rhs.toLowerCase())) {
resolved = String.valueOf(getTimestampCalculator().calculateOverdueTimestamp());
}
return resolved;
}
TimestampCalculator getTimestampCalculator() {
return timestampCalculator;
}
}