Minor code improvements

Signed-off-by: Dominic Schabel dominic.schabel@bosch-si.com
This commit is contained in:
Dominic Schabel
2016-08-15 11:30:21 +02:00
parent 7f156f05a4
commit 3accac9ade
50 changed files with 639 additions and 696 deletions

View File

@@ -82,7 +82,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@@ -599,11 +598,16 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public Page<Action> findActionsByTarget(final String rsqlParam, final Target target, final Pageable pageable) {
final Specification<JpaAction> specification = RSQLUtility.parse(rsqlParam, ActionFields.class);
return convertAcPage(actionRepository.findAll((Specification<JpaAction>) (root, query, cb) -> cb
.and(specification.toPredicate(root, query, cb), cb.equal(root.get(JpaAction_.target), target)),
pageable), pageable);
final Specification<JpaAction> byTargetSpec = createSpecificationFor(target, rsqlParam);
final Page<JpaAction> actions = actionRepository.findAll(byTargetSpec, pageable);
return convertAcPage(actions, pageable);
}
private Specification<JpaAction> createSpecificationFor(final Target target, final String rsqlParam) {
final Specification<JpaAction> spec = RSQLUtility.parse(rsqlParam, ActionFields.class);
return (root, query, cb) -> cb.and(spec.toPredicate(root, query, cb),
cb.equal(root.get(JpaAction_.target), target));
}
private static Page<Action> convertAcPage(final Page<JpaAction> findAll, final Pageable pageable) {
@@ -642,10 +646,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public Long countActionsByTarget(final String rsqlParam, final Target target) {
final Specification<JpaAction> spec = RSQLUtility.parse(rsqlParam, ActionFields.class);
return actionRepository.count((root, query, cb) -> cb.and(spec.toPredicate(root, query, cb),
cb.equal(root.get(JpaAction_.target), target)));
return actionRepository.count(createSpecificationFor(target, rsqlParam));
}
@Override

View File

@@ -599,7 +599,7 @@ public class JpaRolloutManagement implements RolloutManagement {
/**
* Get count of targets in different status in rollout.
*
* @param page
* @param pageable
* the page request to sort and limit the result
* @return a list of rollouts with details of targets count for different
* statuses

View File

@@ -26,7 +26,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

View File

@@ -46,6 +46,7 @@ import org.springframework.transaction.TransactionSystemException;
*/
@Aspect
public class ExceptionMappingAspectHandler implements Ordered {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionMappingAspectHandler.class);
private static final Map<String, String> EXCEPTION_MAPPING = new HashMap<>();
@@ -63,6 +64,7 @@ public class ExceptionMappingAspectHandler implements Ordered {
private final SQLStateSQLExceptionTranslator sqlStateExceptionTranslator = new SQLStateSQLExceptionTranslator();
static {
MAPPED_EXCEPTION_ORDER.add(DuplicateKeyException.class);
MAPPED_EXCEPTION_ORDER.add(DataIntegrityViolationException.class);
MAPPED_EXCEPTION_ORDER.add(ConcurrencyFailureException.class);
@@ -90,9 +92,11 @@ public class ExceptionMappingAspectHandler implements Ordered {
+ " || execution( * org.eclipse.hawkbit.controller.*.*(..)) "
+ " || execution( * org.eclipse.hawkbit.rest.resource.*.*(..)) "
+ " || execution( * org.eclipse.hawkbit.service.*.*(..)) )", throwing = "ex")
// Exception squid:S00112 - Is aspectJ proxy
@SuppressWarnings({ "squid:S00112" })
// Exception for squid:S00112, squid:S1162
// It is a AspectJ proxy which deals with exceptions.
@SuppressWarnings({ "squid:S00112", "squid:S1162" })
public void catchAndWrapJpaExceptionsService(final Exception ex) throws Throwable {
LOG.trace("exception occured", ex);
Exception translatedAccessException = translateEclipseLinkExceptionIfPossible(ex);
@@ -122,6 +126,7 @@ public class ExceptionMappingAspectHandler implements Ordered {
break;
}
}
LOG.trace("mapped exception {} to {}", translatedAccessException.getClass(), mappingException.getClass());
throw mappingException;
}
@@ -138,7 +143,7 @@ public class ExceptionMappingAspectHandler implements Ordered {
* translate the exception by the sql error code. Luckily, there we can use
* {@link SQLStateSQLExceptionTranslator} if we can get a
* {@link SQLException}.
*
*
* @param accessException
* the base access exception from jpa
* @return the translated accessException

View File

@@ -11,7 +11,7 @@ package org.eclipse.hawkbit.repository.jpa.model;
import java.util.Map;
import java.util.stream.Collectors;
import org.eclipse.hawkbit.repository.eventbus.event.AbstractPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.AbstractPropertyChangeEvent.PropertyChange;
import org.eclipse.hawkbit.repository.eventbus.event.ActionCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.ActionPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.RolloutGroupPropertyChangeEvent;
@@ -33,7 +33,6 @@ import com.google.common.eventbus.EventBus;
/**
* Listens to change in property values of an entity.
*
*/
public class EntityPropertyChangeListener extends DescriptorEventAdapter {
@@ -53,27 +52,23 @@ public class EntityPropertyChangeListener extends DescriptorEventAdapter {
@Override
public void postUpdate(final DescriptorEvent event) {
if (event.getObject().getClass().equals(JpaAction.class)) {
getAfterTransactionCommmitExecutor().afterCommit(() -> getEventBus().post(
new ActionPropertyChangeEvent((Action) event.getObject(), getChangeSet(Action.class, event))));
getAfterTransactionCommmitExecutor().afterCommit(() -> getEventBus()
.post(new ActionPropertyChangeEvent((Action) event.getObject(), getChangeSet(event))));
} else if (event.getObject().getClass().equals(JpaRollout.class)) {
getAfterTransactionCommmitExecutor().afterCommit(() -> getEventBus().post(
new RolloutPropertyChangeEvent((Rollout) event.getObject(), getChangeSet(Rollout.class, event))));
getAfterTransactionCommmitExecutor().afterCommit(() -> getEventBus()
.post(new RolloutPropertyChangeEvent((Rollout) event.getObject(), getChangeSet(event))));
} else if (event.getObject().getClass().equals(JpaRolloutGroup.class)) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(new RolloutGroupPropertyChangeEvent((RolloutGroup) event.getObject(),
getChangeSet(RolloutGroup.class, event))));
getAfterTransactionCommmitExecutor().afterCommit(() -> getEventBus()
.post(new RolloutGroupPropertyChangeEvent((RolloutGroup) event.getObject(), getChangeSet(event))));
}
}
private <T extends TenantAwareBaseEntity> Map<String, AbstractPropertyChangeEvent<T>.Values> getChangeSet(
final Class<T> clazz, final DescriptorEvent event) {
final T rolloutGroup = clazz.cast(event.getObject());
private <T extends TenantAwareBaseEntity> Map<String, PropertyChange> getChangeSet(final DescriptorEvent event) {
final ObjectChangeSet changeSet = ((UpdateObjectQuery) event.getQuery()).getObjectChangeSet();
return changeSet.getChanges().stream().filter(record -> record instanceof DirectToFieldChangeRecord)
.map(record -> (DirectToFieldChangeRecord) record)
.collect(Collectors.toMap(record -> record.getAttribute(),
record -> new AbstractPropertyChangeEvent<T>(rolloutGroup, null).new Values(
record.getOldValue(), record.getNewValue())));
record -> new PropertyChange(record.getOldValue(), record.getNewValue())));
}
private AfterTransactionCommitExecutor getAfterTransactionCommmitExecutor() {

View File

@@ -98,8 +98,8 @@ public class JpaActionStatus extends AbstractJpaTenantAwareBaseEntity implements
* the status for this action status
* @param occurredAt
* the occurred timestamp
* @param messages
* the messages which should be added to this action status
* @param message
* the message which should be added to this action status
*/
public JpaActionStatus(final JpaAction action, final Status status, final Long occurredAt, final String message) {
this.action = action;

View File

@@ -65,6 +65,7 @@ import org.springframework.data.domain.Persistable;
// sub entities
@SuppressWarnings("squid:S2160")
public class JpaTarget extends AbstractJpaNamedEntity implements Persistable<Long>, Target {
private static final long serialVersionUID = 1L;
@Column(name = "controller_id", length = 64)
@@ -180,7 +181,7 @@ public class JpaTarget extends AbstractJpaNamedEntity implements Persistable<Lon
}
/**
* @param isNew
* @param entityNew
* the isNew to set
*/
public void setNew(final boolean entityNew) {
@@ -222,6 +223,7 @@ public class JpaTarget extends AbstractJpaNamedEntity implements Persistable<Lon
* @param securityToken
* the securityToken to set
*/
@Override
public void setSecurityToken(final String securityToken) {
this.securityToken = securityToken;
}

View File

@@ -117,7 +117,7 @@ public class JpaTargetInfo implements Persistable<Long>, TargetInfo {
private boolean requestControllerAttributes = true;
/**
* Constructor for {@link TargetStatus}.
* Constructor for {@link JpaTargetInfo}.
*
* @param target
* related to this status.
@@ -144,7 +144,7 @@ public class JpaTargetInfo implements Persistable<Long>, TargetInfo {
}
/**
* @param isNew
* @param entityNew
* the isNew to set
*/
public void setNew(final boolean entityNew) {

View File

@@ -8,6 +8,8 @@
*/
package org.eclipse.hawkbit.repository.jpa.rsql;
import static org.eclipse.hawkbit.repository.FieldNameProvider.SUB_ATTRIBUTE_SEPERATOR;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -15,7 +17,6 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
@@ -49,7 +50,7 @@ import cz.jirutka.rsql.parser.ast.RSQLVisitor;
* A utility class which is able to parse RSQL strings into an spring data
* {@link Specification} which then can be enhanced sql queries to filter
* entities. RSQL parser library: https://github.com/jirutka/rsql-parser
*
*
* <ul>
* <li>Equal to : ==</li>
* <li>Not equal to : !=</li>
@@ -83,14 +84,12 @@ public final class RSQLUtility {
/**
* 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 entityManager
* {@link EntityManager}
* @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
@@ -105,10 +104,10 @@ public final class RSQLUtility {
/**
* Validate the given rsql string regarding existence and correct syntax.
*
*
* @param rsql
* the rsql string to get validated
*
*
*/
public static void isValid(final String rsql) {
parseRsql(rsql);
@@ -156,7 +155,7 @@ public final class RSQLUtility {
/**
* An implementation of the {@link RSQLVisitor} to visit the parsed tokens
* and build jpa where clauses.
*
*
*
*
* @param <A>
@@ -205,7 +204,7 @@ public final class RSQLUtility {
}
private String getAndValidatePropertyFieldName(final A propertyEnum, final ComparisonNode node) {
String finalProperty = propertyEnum.getFieldName();
final String[] graph = node.getSelector().split("\\" + FieldNameProvider.SUB_ATTRIBUTE_SEPERATOR);
validateMapParamter(propertyEnum, node, graph);
@@ -215,9 +214,12 @@ public final class RSQLUtility {
throw createRSQLParameterUnsupportedException(node);
}
final StringBuilder fieldNameBuilder = new StringBuilder(propertyEnum.getFieldName());
for (int i = 1; i < graph.length; i++) {
final String propertyField = graph[i];
finalProperty += FieldNameProvider.SUB_ATTRIBUTE_SEPERATOR + propertyField;
fieldNameBuilder.append(SUB_ATTRIBUTE_SEPERATOR).append(propertyField);
// the key of map is not in the graph
if (propertyEnum.isMap() && graph.length == (i + 1)) {
@@ -229,7 +231,7 @@ public final class RSQLUtility {
}
}
return finalProperty;
return fieldNameBuilder.toString();
}
private void validateMapParamter(final A propertyEnum, final ComparisonNode node, final String[] graph) {