Search in sources :

Example 1 with Auditable

use of eu.bcvsolutions.idm.core.api.domain.Auditable in project CzechIdMng by bcvsolutions.

the class AuditableListener method onPreInsert.

@Override
public boolean onPreInsert(PreInsertEvent event) {
    if (event.getEntity() instanceof Auditable) {
        DateTime date = new DateTime();
        Auditable entity = (Auditable) event.getEntity();
        // 
        setValue(event.getState(), event, Auditable.PROPERTY_CREATED, date);
        entity.setCreated(date);
        // 
        AbstractAuthentication authentication = securityService.getAuthentication();
        IdmIdentityDto currentIdentity = authentication == null ? null : authentication.getCurrentIdentity();
        IdmIdentityDto originalIdentity = authentication == null ? null : authentication.getOriginalIdentity();
        if (entity.getCreator() == null) {
            String creator = currentIdentity == null ? securityService.getUsername() : currentIdentity.getUsername();
            setValue(event.getState(), event, Auditable.PROPERTY_CREATOR, creator);
            entity.setCreator(creator);
            // 
            UUID creatorId = currentIdentity == null ? null : currentIdentity.getId();
            setValue(event.getState(), event, Auditable.PROPERTY_CREATOR_ID, creatorId);
            entity.setCreatorId(creatorId);
        }
        // could be filled in wf (applicant) ...
        if (entity.getOriginalCreator() == null) {
            String originalCreator = originalIdentity == null ? null : originalIdentity.getUsername();
            setValue(event.getState(), event, Auditable.PROPERTY_ORIGINAL_CREATOR, originalCreator);
            entity.setOriginalCreator(originalCreator);
            // 
            UUID originalCreatorId = originalIdentity == null ? null : originalIdentity.getId();
            setValue(event.getState(), event, Auditable.PROPERTY_ORIGINAL_CREATOR_ID, originalCreatorId);
            entity.setOriginalCreatorId(originalCreatorId);
        }
    }
    return false;
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) AbstractAuthentication(eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) DateTime(org.joda.time.DateTime)

Example 2 with Auditable

use of eu.bcvsolutions.idm.core.api.domain.Auditable in project CzechIdMng by bcvsolutions.

the class AuditableListener method onPreUpdate.

@Override
public boolean onPreUpdate(PreUpdateEvent event) {
    if (event.getEntity() instanceof Auditable) {
        DateTime date = new DateTime();
        Auditable entity = (Auditable) event.getEntity();
        // 
        setValue(event.getState(), event, Auditable.PROPERTY_MODIFIED, date);
        entity.setModified(date);
        // 
        AbstractAuthentication authentication = securityService.getAuthentication();
        // 
        IdmIdentityDto currentIdentity = authentication == null ? null : authentication.getCurrentIdentity();
        IdmIdentityDto originalIdentity = authentication == null ? null : authentication.getOriginalIdentity();
        // 
        String modifier = currentIdentity == null ? securityService.getUsername() : currentIdentity.getUsername();
        setValue(event.getState(), event, Auditable.PROPERTY_MODIFIER, modifier);
        entity.setModifier(modifier);
        // 
        UUID modifierId = currentIdentity == null ? null : currentIdentity.getId();
        setValue(event.getState(), event, Auditable.PROPERTY_MODIFIER_ID, modifierId);
        entity.setModifierId(modifierId);
        // could be filled in wf (applicant) ...
        if (entity.getOriginalModifier() == null) {
            String originalModifier = originalIdentity == null ? null : originalIdentity.getUsername();
            setValue(event.getState(), event, Auditable.PROPERTY_ORIGINAL_MODIFIER, originalModifier);
            entity.setOriginalModifier(originalModifier);
            // 
            UUID originalModifierId = originalIdentity == null ? null : originalIdentity.getId();
            setValue(event.getState(), event, Auditable.PROPERTY_ORIGINAL_MODIFIER_ID, originalModifierId);
            entity.setOriginalModifierId(originalModifierId);
        }
    }
    return false;
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) AbstractAuthentication(eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) DateTime(org.joda.time.DateTime)

Example 3 with Auditable

use of eu.bcvsolutions.idm.core.api.domain.Auditable in project CzechIdMng by bcvsolutions.

the class AuditableEntityListener method touchForUpdate.

/**
 * Sets modification date and modifier on the target object in case it implements {@link Auditable} on
 * update events.
 *
 * @param target
 */
@PreUpdate
public void touchForUpdate(Object target) {
    if (!(target instanceof Auditable)) {
        return;
    }
    // 
    AutowireHelper.autowire(this, this.securityService);
    // 
    ZonedDateTime date = ZonedDateTime.now();
    Auditable entity = (Auditable) target;
    // 
    entity.setModified(date);
    // 
    AbstractAuthentication authentication = securityService.getAuthentication();
    // 
    IdmIdentityDto currentIdentity = authentication == null ? null : authentication.getCurrentIdentity();
    IdmIdentityDto originalIdentity = authentication == null ? null : authentication.getOriginalIdentity();
    // 
    String modifier = currentIdentity == null ? securityService.getUsername() : currentIdentity.getUsername();
    entity.setModifier(modifier);
    // 
    UUID modifierId = currentIdentity == null ? null : currentIdentity.getId();
    entity.setModifierId(modifierId);
    // 
    String originalModifier = originalIdentity == null ? null : originalIdentity.getUsername();
    entity.setOriginalModifier(originalModifier);
    UUID originalModifierId = originalIdentity == null ? null : originalIdentity.getId();
    entity.setOriginalModifierId(originalModifierId);
    // 
    // set transaction id from context holder
    entity.setTransactionId(TransactionContextHolder.getContext().getTransactionId());
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) ZonedDateTime(java.time.ZonedDateTime) AbstractAuthentication(eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) PreUpdate(javax.persistence.PreUpdate)

Example 4 with Auditable

use of eu.bcvsolutions.idm.core.api.domain.Auditable in project CzechIdMng by bcvsolutions.

the class AuditableEntityListener method touchForCreate.

/**
 * Sets creation date and creator on the target object in case it implements {@link Auditable} on
 * persist events.
 *
 * @param target
 */
@PrePersist
public void touchForCreate(Object target) {
    if (!(target instanceof Auditable)) {
        return;
    }
    // 
    AutowireHelper.autowire(this, this.securityService);
    // 
    ZonedDateTime date = ZonedDateTime.now();
    Auditable entity = (Auditable) target;
    // set created date - can be preset (e.g. preserve created date of original operation moved some where info archive)
    if (entity.getCreated() == null) {
        entity.setCreated(date);
    }
    // 
    AbstractAuthentication authentication = securityService.getAuthentication();
    IdmIdentityDto currentIdentity = authentication == null ? null : authentication.getCurrentIdentity();
    IdmIdentityDto originalIdentity = authentication == null ? null : authentication.getOriginalIdentity();
    if (entity.getCreator() == null) {
        String creator = currentIdentity == null ? securityService.getUsername() : currentIdentity.getUsername();
        entity.setCreator(creator);
        // 
        UUID creatorId = currentIdentity == null ? null : currentIdentity.getId();
        entity.setCreatorId(creatorId);
    }
    // could be filled in wf (applicant) ...
    if (entity.getOriginalCreator() == null) {
        String originalCreator = originalIdentity == null ? null : originalIdentity.getUsername();
        entity.setOriginalCreator(originalCreator);
        // 
        UUID originalCreatorId = originalIdentity == null ? null : originalIdentity.getId();
        entity.setOriginalCreatorId(originalCreatorId);
    }
    // set transaction id from context holder
    entity.setTransactionId(TransactionContextHolder.getContext().getTransactionId());
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) ZonedDateTime(java.time.ZonedDateTime) AbstractAuthentication(eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) PrePersist(javax.persistence.PrePersist)

Aggregations

Auditable (eu.bcvsolutions.idm.core.api.domain.Auditable)4 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)4 AbstractAuthentication (eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication)4 UUID (java.util.UUID)4 ZonedDateTime (java.time.ZonedDateTime)2 DateTime (org.joda.time.DateTime)2 PrePersist (javax.persistence.PrePersist)1 PreUpdate (javax.persistence.PreUpdate)1