Skip to content

Commit

Permalink
MID-6319: audit/R* classes + SqlAuditServiceImpl cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Jun 23, 2020
1 parent 665fe83 commit d4ff5b6
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 238 deletions.
Expand Up @@ -17,10 +17,6 @@
import javax.persistence.criteria.CriteriaQuery;
import javax.xml.datatype.Duration;

import com.evolveum.midpoint.prism.util.CloneUtil;

import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationAuditType;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.hibernate.FlushMode;
Expand All @@ -41,6 +37,7 @@
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.CanonicalItemPath;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.util.CloneUtil;
import com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration.Database;
import com.evolveum.midpoint.repo.sql.data.BatchSqlQuery;
import com.evolveum.midpoint.repo.sql.data.SelectQueryBuilder;
Expand All @@ -64,6 +61,7 @@
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationAuditType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

/**
Expand Down
Expand Up @@ -7,8 +7,18 @@

package com.evolveum.midpoint.repo.sql.data.audit;

import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.repo.sql.helpers.modify.Ignore;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;
import java.util.Map.Entry;
import javax.persistence.*;

import org.apache.commons.lang.Validate;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.ForeignKey;

import com.evolveum.midpoint.audit.api.AuditEventRecord;
import com.evolveum.midpoint.audit.api.AuditReferenceValue;
import com.evolveum.midpoint.audit.api.AuditService;
Expand All @@ -18,55 +28,28 @@
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.CanonicalItemPath;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.sql.data.InsertQueryBuilder;
import com.evolveum.midpoint.repo.sql.data.SingleSqlQuery;
import com.evolveum.midpoint.repo.sql.data.common.enums.ROperationResultStatus;
import com.evolveum.midpoint.repo.sql.data.common.other.RObjectType;
import com.evolveum.midpoint.repo.sql.helpers.modify.Ignore;
import com.evolveum.midpoint.repo.sql.util.ClassMapper;
import com.evolveum.midpoint.repo.sql.util.DtoTranslationException;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.schema.ObjectDeltaOperation;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationAuditType;

import org.apache.commons.lang.Validate;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.ForeignKey;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.Map.Entry;

/**
* @author lazyman
*/
@Ignore
@Entity
@Table(name = RAuditEventRecord.TABLE_NAME, indexes = {
@Index(name = "iTimestampValue", columnList = RAuditEventRecord.COLUMN_TIMESTAMP)}) // TODO correct index name
@Index(name = "iTimestampValue", columnList = RAuditEventRecord.COLUMN_TIMESTAMP) }) // TODO correct index name
public class RAuditEventRecord implements Serializable {

public static final String TABLE_NAME = "m_audit_event";
Expand Down Expand Up @@ -165,7 +148,7 @@ public String getChannel() {

@ForeignKey(name = "fk_audit_delta")
@OneToMany(mappedBy = "record", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<RObjectDeltaOperation> getDeltas() {
if (deltas == null) {
deltas = new HashSet<>();
Expand All @@ -175,7 +158,7 @@ public Set<RObjectDeltaOperation> getDeltas() {

@ForeignKey(name = "fk_audit_item")
@OneToMany(mappedBy = "record", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<RAuditItem> getChangedItems() {
if (changedItems == null) {
changedItems = new HashSet<>();
Expand All @@ -185,7 +168,7 @@ public Set<RAuditItem> getChangedItems() {

@ForeignKey(name = "fk_audit_prop_value")
@OneToMany(mappedBy = "record", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<RAuditPropertyValue> getPropertyValues() {
if (propertyValues == null) {
propertyValues = new HashSet<>();
Expand All @@ -195,7 +178,7 @@ public Set<RAuditPropertyValue> getPropertyValues() {

@ForeignKey(name = "fk_audit_ref_value")
@OneToMany(mappedBy = "record", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<RAuditReferenceValue> getReferenceValues() {
if (referenceValues == null) {
referenceValues = new HashSet<>();
Expand All @@ -205,7 +188,7 @@ public Set<RAuditReferenceValue> getReferenceValues() {

@ForeignKey(name = "fk_audit_resource")
@OneToMany(mappedBy = "record", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<RTargetResourceOid> getResourceOids() {
if (resourceOids == null) {
resourceOids = new HashSet<>();
Expand Down Expand Up @@ -454,61 +437,9 @@ public void setResult(String result) {
this.result = result;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof RAuditEventRecord))
return false;
RAuditEventRecord that = (RAuditEventRecord) o;
return id == that.id &&
Objects.equals(timestamp, that.timestamp) &&
Objects.equals(eventIdentifier, that.eventIdentifier) &&
Objects.equals(sessionIdentifier, that.sessionIdentifier) &&
Objects.equals(requestIdentifier, that.requestIdentifier) &&
Objects.equals(taskIdentifier, that.taskIdentifier) &&
Objects.equals(taskOID, that.taskOID) &&
Objects.equals(hostIdentifier, that.hostIdentifier) &&
Objects.equals(remoteHostAddress, that.remoteHostAddress) &&
Objects.equals(nodeIdentifier, that.nodeIdentifier) &&
Objects.equals(initiatorOid, that.initiatorOid) &&
Objects.equals(initiatorName, that.initiatorName) &&
Objects.equals(initiatorType, that.initiatorType) &&
Objects.equals(attorneyOid, that.attorneyOid) &&
Objects.equals(attorneyName, that.attorneyName) &&
Objects.equals(targetOid, that.targetOid) &&
Objects.equals(targetName, that.targetName) &&
targetType == that.targetType &&
Objects.equals(targetOwnerOid, that.targetOwnerOid) &&
Objects.equals(targetOwnerName, that.targetOwnerName) &&
Objects.equals(targetOwnerType, that.targetOwnerType) &&
eventType == that.eventType &&
eventStage == that.eventStage &&
Objects.equals(deltas, that.deltas) &&
Objects.equals(channel, that.channel) &&
outcome == that.outcome &&
Objects.equals(parameter, that.parameter) &&
Objects.equals(message, that.message) &&
Objects.equals(changedItems, that.changedItems) &&
Objects.equals(resourceOids, that.resourceOids) &&
Objects.equals(propertyValues, that.propertyValues) &&
Objects.equals(referenceValues, that.referenceValues) &&
Objects.equals(result, that.result);
}

@Override
public int hashCode() {
return Objects
.hash(id, timestamp, eventIdentifier, sessionIdentifier, requestIdentifier, taskIdentifier, taskOID, hostIdentifier,
remoteHostAddress, nodeIdentifier, initiatorOid, initiatorName, initiatorType,
attorneyOid, attorneyName, targetOid, targetName, targetType, targetOwnerOid,
targetOwnerName, targetOwnerType, eventType, eventStage, deltas, channel, outcome, parameter, message,
changedItems, resourceOids, propertyValues, referenceValues, result);
}

public static RAuditEventRecord toRepo(AuditEventRecord record, PrismContext prismContext, Boolean isTransient,
SystemConfigurationAuditType auditConfiguration)
throws DtoTranslationException {
throws DtoTranslationException {

Validate.notNull(record, "Audit event record must not be null.");
Validate.notNull(prismContext, "Prism context must not be null.");
Expand All @@ -534,14 +465,14 @@ public static RAuditEventRecord toRepo(AuditEventRecord record, PrismContext pri
repo.setMessage(RUtil.trimString(record.getMessage(), AuditService.MAX_MESSAGE_SIZE));
if (record.getOutcome() != null) {
repo.setOutcome(RUtil.getRepoEnumValue(record.getOutcome().createStatusType(),
ROperationResultStatus.class));
ROperationResultStatus.class));
}
repo.setRequestIdentifier(record.getRequestIdentifier());
repo.setTaskIdentifier(record.getTaskIdentifier());
repo.setTaskOID(record.getTaskOid());
repo.setResult(record.getResult());

for(String resourceOid : record.getResourceOids()) {
for (String resourceOid : record.getResourceOids()) {
RTargetResourceOid targetResourceOid = RTargetResourceOid.toRepo(repo, resourceOid);
targetResourceOid.setTransient(isTransient);
repo.getResourceOids().add(targetResourceOid);
Expand Down Expand Up @@ -600,7 +531,7 @@ public static RAuditEventRecord toRepo(AuditEventRecord record, PrismContext pri
for (Map.Entry<String, Set<String>> propertyEntry : record.getProperties().entrySet()) {
for (String propertyValue : propertyEntry.getValue()) {
RAuditPropertyValue val = RAuditPropertyValue.toRepo(
repo, propertyEntry.getKey(), RUtil.trimString(propertyValue, AuditService.MAX_PROPERTY_SIZE));
repo, propertyEntry.getKey(), RUtil.trimString(propertyValue, AuditService.MAX_PROPERTY_SIZE));
val.setTransient(isTransient);
repo.getPropertyValues().add(val);
}
Expand Down Expand Up @@ -648,7 +579,7 @@ public static AuditEventRecord fromRepo(RAuditEventRecord repo, PrismContext pri
audit.setTimestamp(repo.getTimestamp().getTime());
}

for(RTargetResourceOid resourceOID : repo.getResourceOids()) {
for (RTargetResourceOid resourceOID : repo.getResourceOids()) {
audit.getResourceOids().add(resourceOID.getResourceOid());
}

Expand Down Expand Up @@ -687,7 +618,6 @@ public static SingleSqlQuery toRepo(AuditEventRecord record, Map<String, String>

Validate.notNull(record, "Audit event record must not be null.");
InsertQueryBuilder queryBulder = new InsertQueryBuilder(TABLE_NAME);
Map<Integer, Object> parameters = new HashMap<Integer, Object>();
if (record.getRepoId() != null) {
queryBulder.addParameter(ID_COLUMN_NAME, record.getRepoId());
}
Expand All @@ -702,9 +632,9 @@ public static SingleSqlQuery toRepo(AuditEventRecord record, Map<String, String>
queryBulder.addParameter(NODE_IDENTIFIER_COLUMN_NAME, record.getNodeIdentifier());
queryBulder.addParameter(PARAMETER_COLUMN_NAME, record.getParameter());
queryBulder.addParameter(MESSAGE_COLUMN_NAME, RUtil.trimString(record.getMessage(), AuditService.MAX_MESSAGE_SIZE));
if(record.getOutcome() != null) {
queryBulder.addParameter(OUTCOME_COLUMN_NAME, RUtil.getRepoEnumValue(record.getOutcome().createStatusType(),
ROperationResultStatus.class));
if (record.getOutcome() != null) {
queryBulder.addParameter(OUTCOME_COLUMN_NAME, RUtil.getRepoEnumValue(record.getOutcome().createStatusType(),
ROperationResultStatus.class));
} else {
queryBulder.addParameter(OUTCOME_COLUMN_NAME, null);
}
Expand Down Expand Up @@ -738,9 +668,9 @@ public static SingleSqlQuery toRepo(AuditEventRecord record, Map<String, String>
queryBulder.addParameter(ATTORNEY_OID_COLUMN_NAME, attorney.getOid());
}

if(!customColumn.isEmpty()) {
for(Entry<String, String> property : record.getCustomColumnProperty().entrySet()) {
if(!customColumn.containsKey(property.getKey())) {
if (!customColumn.isEmpty()) {
for (Entry<String, String> property : record.getCustomColumnProperty().entrySet()) {
if (!customColumn.containsKey(property.getKey())) {
throw new IllegalArgumentException("Audit event record table don't contains column for property " + property.getKey());
}
queryBulder.addParameter(customColumn.get(property.getKey()), property.getValue());
Expand Down Expand Up @@ -806,4 +736,17 @@ public void merge(RAuditEventRecord repoRecord) {
this.id = repoRecord.id;
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (!(o instanceof RAuditEventRecord)) { return false; }

RAuditEventRecord that = (RAuditEventRecord) o;
return id == that.id;
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Expand Up @@ -18,7 +18,7 @@ public enum RAuditEventStage {

EXECUTION(AuditEventStage.EXECUTION);

private AuditEventStage stage;
private final AuditEventStage stage;

RAuditEventStage(AuditEventStage stage) {
this.stage = stage;
Expand Down
Expand Up @@ -42,8 +42,7 @@ public enum RAuditEventType {

RUN_TASK_IMMEDIATELY(AuditEventType.RUN_TASK_IMMEDIATELY);


private AuditEventType type;
private final AuditEventType type;

RAuditEventType(AuditEventType type) {
this.type = type;
Expand All @@ -53,7 +52,6 @@ public AuditEventType getType() {
return type;
}


public static RAuditEventType toRepo(AuditEventType type) {
if (type == null) {
return null;
Expand Down

0 comments on commit d4ff5b6

Please sign in to comment.