Enable push for update target, create/update/delete ds

Renamed constant MAX_TARGET_TABLE_ENTRIES

Signed-off-by: Asharani <asharani.murugesh@in.bosch.com>
This commit is contained in:
Asharani
2016-06-28 12:07:26 +05:30
parent 5fb86bbb2f
commit 4f7bb98587
30 changed files with 788 additions and 337 deletions

View File

@@ -28,6 +28,7 @@ import org.eclipse.hawkbit.repository.DistributionSetMetadataFields;
import org.eclipse.hawkbit.repository.DistributionSetTypeFields;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.TagManagement;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionDeletedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionSetTagAssigmentResultEvent;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityLockedException;
@@ -54,6 +55,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.DistributionSetTagAssignmentResult;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
@@ -102,6 +104,9 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Autowired
private AfterTransactionCommitExecutor afterCommit;
@Autowired
private TenantAware tenantAware;
@Override
public DistributionSet findDistributionSetByIdWithDetails(final Long distid) {
return distributionSetRepository.findOne(DistributionSetSpecification.byId(distid));
@@ -168,32 +173,39 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Override
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteDistributionSet(final Long... distributionSetIDs) {
final List<Long> toHardDelete = new ArrayList<>();
public void deleteDistributionSet(final Long... distributionSetIDs) {
final List<Long> toHardDelete = new ArrayList<>();
final List<Long> assigned = distributionSetRepository
.findAssignedToTargetDistributionSetsById(distributionSetIDs);
assigned.addAll(distributionSetRepository.findAssignedToRolloutDistributionSetsById(distributionSetIDs));
final List<Long> assigned = distributionSetRepository
.findAssignedToTargetDistributionSetsById(distributionSetIDs);
assigned.addAll(distributionSetRepository
.findAssignedToRolloutDistributionSetsById(distributionSetIDs));
// soft delete assigned
if (!assigned.isEmpty()) {
distributionSetRepository.deleteDistributionSet(assigned.toArray(new Long[assigned.size()]));
}
// soft delete assigned
if (!assigned.isEmpty()) {
distributionSetRepository.deleteDistributionSet(assigned
.toArray(new Long[assigned.size()]));
}
// mark the rest as hard delete
for (final Long setId : distributionSetIDs) {
if (!assigned.contains(setId)) {
toHardDelete.add(setId);
}
}
// mark the rest as hard delete
for (final Long setId : distributionSetIDs) {
if (!assigned.contains(setId)) {
toHardDelete.add(setId);
}
}
// hard delete the rest if exixts
if (!toHardDelete.isEmpty()) {
// don't give the delete statement an empty list, JPA/Oracle cannot
// handle the empty list
distributionSetRepository.deleteByIdIn(toHardDelete);
}
}
// hard delete the rest if exixts
if (!toHardDelete.isEmpty()) {
// don't give the delete statement an empty list, JPA/Oracle cannot
// handle the empty list
distributionSetRepository.deleteByIdIn(toHardDelete);
}
afterCommit.afterCommit(() -> eventBus
.post(new DistributionDeletedEvent(tenantAware
.getCurrentTenant(), distributionSetIDs)));
}
@Override
@Modifying

View File

@@ -29,6 +29,7 @@ import javax.persistence.criteria.Root;
import org.eclipse.hawkbit.repository.TargetFields;
import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.eventbus.event.TargetDeletedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetTagAssigmentResultEvent;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.jpa.configuration.Constants;
@@ -48,6 +49,7 @@ import org.eclipse.hawkbit.repository.model.TargetIdName;
import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.repository.model.TargetTagAssignmentResult;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
@@ -96,6 +98,10 @@ public class JpaTargetManagement implements TargetManagement {
@Autowired
private AfterTransactionCommitExecutor afterCommit;
@Autowired
private TenantAware tenantAware;
@Override
public Target findTargetByControllerID(final String controllerId) {
@@ -202,12 +208,14 @@ public class JpaTargetManagement implements TargetManagement {
// hibernate session.
final List<Long> targetsForCurrentTenant = targetRepository.findAll(Lists.newArrayList(targetIDs)).stream()
.map(Target::getId).collect(Collectors.toList());
if (!targetsForCurrentTenant.isEmpty()) {
targetInfoRepository.deleteByTargetIdIn(targetsForCurrentTenant);
targetRepository.deleteByIdIn(targetsForCurrentTenant);
}
if (!targetsForCurrentTenant.isEmpty()) {
targetInfoRepository.deleteByTargetIdIn(targetsForCurrentTenant);
targetRepository.deleteByIdIn(targetsForCurrentTenant);
targetsForCurrentTenant.forEach(targetId -> notifyTargetDeleted(
tenantAware.getCurrentTenant(), targetId));
}
}
@Override
public Page<Target> findTargetByAssignedDistributionSet(final Long distributionSetID, final Pageable pageReq) {
return targetRepository.findByAssignedDistributionSetId(pageReq, distributionSetID);
@@ -651,5 +659,9 @@ public class JpaTargetManagement implements TargetManagement {
}
return resultList;
}
private void notifyTargetDeleted(final String tenant, final Long targetId) {
afterCommit.afterCommit(() -> eventBus.post(new TargetDeletedEvent(tenant, targetId)));
}
}

View File

@@ -1,154 +0,0 @@
/**
* Copyright (c) 2015 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.eventbus;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.eclipse.hawkbit.eventbus.event.TargetDeletedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetInfoUpdateEvent;
import org.eclipse.hawkbit.repository.jpa.TargetRepository;
import org.eclipse.hawkbit.repository.jpa.executor.AfterTransactionCommitExecutor;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetInfo;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetInfo;
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.eventbus.EventBus;
/**
* An aspect implementation which wraps the necessary repository services for
* saving {@link TenantAwareBaseEntity}s to publish create or update events.
*
*
*
*
*/
@Service
@Aspect
public class EntityChangeEventListener {
@Autowired
private EventBus eventBus;
@Autowired
private TenantAware tenantAware;
@Autowired
private EntityManager entityManager;
@Autowired
private AfterTransactionCommitExecutor afterCommit;
/**
* In case the a {@link Target} is created a corresponding
* {@link TargetInfo} is created as well. We need the {@link TargetInfo}
* information in the target created event. So we are listening to the
* {@link TargetInfo} creation to indicate if an Target has been created.
*
* @param joinpoint
* the aspect join point
* @return the object of the {@link ProceedingJoinPoint#proceed()}
* @throws Throwable
* in case exception happens in the
* {@link ProceedingJoinPoint#proceed()}
*/
@Around("execution(* org.eclipse.hawkbit.repository.jpa.TargetInfoRepository.save(..))")
// Exception squid:S00112 - Is aspectJ proxy
@SuppressWarnings({ "squid:S00112" })
public Object targetCreated(final ProceedingJoinPoint joinpoint) throws Throwable {
final boolean isNew = isTargetInfoNew(joinpoint.getArgs()[0]);
final Object result = joinpoint.proceed();
if (result instanceof TargetInfo) {
if (isNew) {
notifyTargetCreated(entityManager.merge(entityManager.merge(((TargetInfo) result).getTarget())));
} else {
notifyTargetInfoChanged((TargetInfo) result);
}
}
return result;
}
/**
* Proxy method around the delete method of the {@link TargetRepository} to
* notify the {@link TargetDeletedEvent} in case targets has been deleted.
*
* @param joinpoint
* the aspect join point
* @return the object of the {@link ProceedingJoinPoint#proceed()}
* @throws Throwable
* in case exception happens in the
* {@link ProceedingJoinPoint#proceed()}
*/
@Around("execution(* org.eclipse.hawkbit.repository.jpa.TargetRepository.deleteByIdIn(..))")
// Exception squid:S00112 - Is aspectJ proxy
@SuppressWarnings({ "squid:S00112" })
public Object targetDeletedById(final ProceedingJoinPoint joinpoint) throws Throwable {
final String currentTenant = tenantAware.getCurrentTenant();
final Object result = joinpoint.proceed();
final Collection<Long> targetIds = (Collection<Long>) joinpoint.getArgs()[0];
targetIds.forEach(targetId -> notifyTargetDeleted(currentTenant, targetId));
return result;
}
/**
* Proxy method around the delete method of the {@link TargetRepository} to
* notify the {@link TargetDeletedEvent} in case targets has been deleted.
*
* @param joinpoint
* the aspect join point
* @return the object of the {@link ProceedingJoinPoint#proceed()}
* @throws Throwable
* in case exception happens in the
* {@link ProceedingJoinPoint#proceed()}
*/
@Around("execution(* org.eclipse.hawkbit.repository.jpa.TargetRepository.delete(..))")
// Exception squid:S00112 - Is aspectJ proxy
@SuppressWarnings({ "squid:S00112", "unchecked" })
public Object targetDeleted(final ProceedingJoinPoint joinpoint) throws Throwable {
final String currentTenant = tenantAware.getCurrentTenant();
final Object result = joinpoint.proceed();
final Object param = joinpoint.getArgs()[0];
// delete by id
if (param instanceof Long) {
notifyTargetDeleted(currentTenant, (Long) param);
} else if (param instanceof Target) {
notifyTargetDeleted(currentTenant, ((Target) param).getId());
} else if (param instanceof Iterable) {
((Iterable<Target>) param).forEach(target -> notifyTargetDeleted(currentTenant, target.getId()));
}
return result;
}
private void notifyTargetCreated(final Target t) {
afterCommit.afterCommit(() -> eventBus.post(new TargetCreatedEvent(t)));
}
private void notifyTargetInfoChanged(final TargetInfo targetInfo) {
afterCommit.afterCommit(() -> eventBus.post(new TargetInfoUpdateEvent(targetInfo)));
}
private void notifyTargetDeleted(final String tenant, final Long targetId) {
afterCommit.afterCommit(() -> eventBus.post(new TargetDeletedEvent(tenant, targetId)));
}
private static boolean isTargetInfoNew(final Object targetInfo) {
return ((JpaTargetInfo) targetInfo).isNew();
}
}

View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2015 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.model;
/**
*
* Interface defining the action to be performed after entity is
* created/updated.
*
*/
public interface AbstractDescriptorEventVisitor {
void publishEventPostAction(DescriptorEventDetails eventDetails);
}

View File

@@ -0,0 +1,189 @@
/**
* Copyright (c) 2015 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.model;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.stream.Collectors;
import org.eclipse.hawkbit.repository.eventbus.event.AbstractPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.ActionCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.ActionPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionSetUpdateEvent;
import org.eclipse.hawkbit.repository.eventbus.event.RolloutGroupPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.RolloutPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetInfoUpdateEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.jpa.executor.AfterTransactionCommitExecutor;
import org.eclipse.hawkbit.repository.jpa.model.DescriptorEventDetails.ActionType;
import org.eclipse.hawkbit.repository.jpa.model.helper.AfterTransactionCommitExecutorHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.EventBusHolder;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.RolloutGroup;
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
import org.eclipse.persistence.descriptors.DescriptorEvent;
import org.eclipse.persistence.internal.sessions.ObjectChangeSet;
import org.eclipse.persistence.queries.UpdateObjectQuery;
import org.eclipse.persistence.sessions.changesets.DirectToFieldChangeRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.eventbus.EventBus;
/**
* Implementation of @link{AbstractDescriptorEventVisitor} .Publishes the
* appropriate event after any action on entity like create/update.
*
*/
public class AbstractDescriptorEventVisitorImpl implements
AbstractDescriptorEventVisitor {
private static final String COMPLETE = "complete";
private static final Logger LOG = LoggerFactory
.getLogger(AbstractDescriptorEventVisitorImpl.class);
@Override
public void publishEventPostAction(final DescriptorEventDetails event) {
Method method = null;
DescriptorEvent descriptorEvent = event.getDescriptorEvent();
ActionType actiontype = event.getActiontype();
try {
method = getMethod(descriptorEvent, actiontype);
if (method != null) {
method.invoke(this, descriptorEvent.getObject(),
descriptorEvent);
}
} catch (NoSuchMethodException | SecurityException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
LOG.info(
"Exception when invoking approriate method to publis event {}",
e);
}
}
private Method getMethod(DescriptorEvent descriptorEvent,
ActionType actiontype) throws NoSuchMethodException {
if (actiontype == ActionType.UPDATE) {
return this.getClass().getMethod("publishEventAfterUpdate",
descriptorEvent.getObject().getClass(),
DescriptorEvent.class);
} else if (actiontype == ActionType.CREATE) {
return this.getClass().getMethod("publishEventAfterCreate",
descriptorEvent.getObject().getClass(),
DescriptorEvent.class);
}
return null;
}
public void publishEventAfterCreate(JpaAction action, DescriptorEvent event) {
if (action.getRollout() != null) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(new ActionCreatedEvent(action)));
}
}
public void publishEventAfterCreate(JpaTarget target, DescriptorEvent event) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(new TargetCreatedEvent(target)));
}
public void publishEventAfterCreate(JpaDistributionSet ds,
DescriptorEvent event) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(new DistributionCreatedEvent(ds)));
}
public void publishEventAfterUpdate(JpaAction action, DescriptorEvent event) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(
new ActionPropertyChangeEvent(action, getChangeSet(
Action.class, event))));
}
public void publishEventAfterUpdate(JpaTarget target, DescriptorEvent event) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(new TargetUpdatedEvent(target)));
}
public void publishEventAfterUpdate(JpaTargetInfo targetInfo,
DescriptorEvent event) {
getAfterTransactionCommmitExecutor()
.afterCommit(
() -> getEventBus().post(
new TargetInfoUpdateEvent(targetInfo)));
}
public void publishEventAfterUpdate(JpaRollout entity, DescriptorEvent event) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(
new RolloutPropertyChangeEvent(entity, getChangeSet(
Rollout.class, event))));
}
public void publishEventAfterUpdate(JpaRolloutGroup entity,
DescriptorEvent event) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(
new RolloutGroupPropertyChangeEvent(entity,
getChangeSet(RolloutGroup.class, event))));
}
public void publishEventAfterUpdate(JpaDistributionSet entity,
DescriptorEvent event) {
Map<String, AbstractPropertyChangeEvent<JpaDistributionSet>.Values> changeSet = getChangeSet(
JpaDistributionSet.class, event);
if (changeSet.containsKey(COMPLETE)
&& changeSet.get(COMPLETE).getOldValue().equals(false)
&& changeSet.get(COMPLETE).getNewValue().equals(true)) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(
new DistributionCreatedEvent(entity)));
}
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus()
.post(new DistributionSetUpdateEvent(entity)));
}
private <T extends TenantAwareBaseEntity> Map<String, AbstractPropertyChangeEvent<T>.Values> getChangeSet(
final Class<T> clazz, final DescriptorEvent event) {
final T rolloutGroup = clazz.cast(event.getObject());
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())));
}
private AfterTransactionCommitExecutor getAfterTransactionCommmitExecutor() {
return AfterTransactionCommitExecutorHolder.getInstance()
.getAfterCommit();
}
private EventBus getEventBus() {
return EventBusHolder.getInstance().getEventBus();
}
}

View File

@@ -32,7 +32,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@MappedSuperclass
@Access(AccessType.FIELD)
@EntityListeners({ AuditingEntityListener.class, CacheFieldEntityListener.class, EntityPropertyChangeListener.class })
public abstract class AbstractJpaBaseEntity implements BaseEntity {
public abstract class AbstractJpaBaseEntity implements BaseEntity,AcceptVisitor {
private static final long serialVersionUID = 1L;
@Id
@@ -178,4 +178,9 @@ public abstract class AbstractJpaBaseEntity implements BaseEntity {
return true;
}
@Override
public void postActionOnEntity(AbstractDescriptorEventVisitor visitor, DescriptorEventDetails eventDetails){
visitor.publishEventPostAction(eventDetails);
}
}

View File

@@ -0,0 +1,18 @@
/**
* Copyright (c) 2015 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.model;
/**
* Interface to accept visitor @link{AbstractDescriptorEventVisitor}.
*
*/
public interface AcceptVisitor {
public void postActionOnEntity(AbstractDescriptorEventVisitor visitor,
DescriptorEventDetails eventDetails);
}

View File

@@ -0,0 +1,42 @@
/**
* Copyright (c) 2015 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.model;
import org.eclipse.persistence.descriptors.DescriptorEvent;
/**
*
* Holds details of action(Create/Update) and @link{DescriptorEvent}.
*
*/
public class DescriptorEventDetails {
enum ActionType {
CREATE, UPDATE;
}
private DescriptorEvent descriptorEvent;
private ActionType actiontype;
public DescriptorEventDetails(ActionType actionType,
DescriptorEvent descriptorEvent) {
this.descriptorEvent = descriptorEvent;
this.actiontype = actionType;
}
public DescriptorEvent getDescriptorEvent() {
return descriptorEvent;
}
public ActionType getActiontype() {
return actiontype;
}
}

View File

@@ -8,28 +8,9 @@
*/
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.ActionCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.ActionPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.RolloutGroupPropertyChangeEvent;
import org.eclipse.hawkbit.repository.eventbus.event.RolloutPropertyChangeEvent;
import org.eclipse.hawkbit.repository.jpa.executor.AfterTransactionCommitExecutor;
import org.eclipse.hawkbit.repository.jpa.model.helper.AfterTransactionCommitExecutorHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.EventBusHolder;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.RolloutGroup;
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
import org.eclipse.hawkbit.repository.jpa.model.DescriptorEventDetails.ActionType;
import org.eclipse.persistence.descriptors.DescriptorEvent;
import org.eclipse.persistence.descriptors.DescriptorEventAdapter;
import org.eclipse.persistence.internal.sessions.ObjectChangeSet;
import org.eclipse.persistence.queries.UpdateObjectQuery;
import org.eclipse.persistence.sessions.changesets.DirectToFieldChangeRecord;
import com.google.common.eventbus.EventBus;
/**
* Listens to change in property values of an entity.
@@ -37,50 +18,19 @@ import com.google.common.eventbus.EventBus;
*/
public class EntityPropertyChangeListener extends DescriptorEventAdapter {
@Override
public void postInsert(final DescriptorEvent event) {
if (event.getObject().getClass().equals(Action.class)) {
final Action action = (Action) event.getObject();
if (action.getRollout() != null) {
final EventBus eventBus = getEventBus();
final AfterTransactionCommitExecutor afterCommit = getAfterTransactionCommmitExecutor();
afterCommit.afterCommit(() -> eventBus.post(new ActionCreatedEvent(action)));
}
}
@Override
public void postInsert(final DescriptorEvent event) {
AbstractDescriptorEventVisitor visitor = new AbstractDescriptorEventVisitorImpl();
((AbstractJpaBaseEntity) event.getObject()).postActionOnEntity(visitor,
new DescriptorEventDetails(ActionType.CREATE, event));
}
}
@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))));
} else if (event.getObject().getClass().equals(JpaRollout.class)) {
getAfterTransactionCommmitExecutor().afterCommit(() -> getEventBus().post(
new RolloutPropertyChangeEvent((Rollout) event.getObject(), getChangeSet(Rollout.class, event))));
} else if (event.getObject().getClass().equals(JpaRolloutGroup.class)) {
getAfterTransactionCommmitExecutor().afterCommit(
() -> getEventBus().post(new RolloutGroupPropertyChangeEvent((RolloutGroup) event.getObject(),
getChangeSet(RolloutGroup.class, 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());
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())));
}
private AfterTransactionCommitExecutor getAfterTransactionCommmitExecutor() {
return AfterTransactionCommitExecutorHolder.getInstance().getAfterCommit();
}
private EventBus getEventBus() {
return EventBusHolder.getInstance().getEventBus();
}
@Override
public void postUpdate(final DescriptorEvent event) {
AbstractDescriptorEventVisitor visitor = new AbstractDescriptorEventVisitorImpl();
((AbstractJpaBaseEntity) event.getObject()).postActionOnEntity(visitor,
new DescriptorEventDetails(ActionType.UPDATE, event));
}
}