Skip to content

Commit

Permalink
using of JDBC instead of hibernate
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Jun 27, 2019
1 parent e0b4b3c commit 76f046a
Show file tree
Hide file tree
Showing 9 changed files with 539 additions and 69 deletions.
Expand Up @@ -82,13 +82,15 @@ public class AuditEventRecordProvider extends BaseSortableDataProvider<AuditEven
@Nullable private final IModel<ObjectCollectionType> objectCollectionModel;
@NotNull private final SerializableSupplier<Map<String, Object>> parametersSupplier;

private static final String AUDIT_RECORDS_QUERY_CORE = "from RAuditEventRecord as aer";
private static final String AUDIT_RECORDS_QUERY_SELECT = "select * from m_audit_event ";
private static final String AUDIT_RECORDS_QUERY_CORE = " as aer";
private static final String AUDIT_RECORDS_QUERY_ITEMS_CHANGED = " right join aer.changedItems as item";
private static final String AUDIT_RECORDS_QUERY_REF_VALUES = " left outer join aer.referenceValues as rv";
private static final String AUDIT_RECORDS_QUERY_COUNT = "select count(*) ";
private static final String AUDIT_RECORDS_ORDER_BY = " order by aer.timestamp desc";
private static final String AUDIT_RECORDS_QUERY_COUNT = "select count(*) from RAuditEventRecord ";
private static final String AUDIT_RECORDS_ORDER_BY = " order by aer.timestampValue desc";
private static final String SET_FIRST_RESULT_PARAMETER = "setFirstResult";
private static final String SET_MAX_RESULTS_PARAMETER = "setMaxResults";

// private static final String TIMESTAMP_VALUE_NAME = "aer.timestamp";

public AuditEventRecordProvider(Component component, @Nullable IModel<ObjectCollectionType> objectCollectionModel, @NotNull SerializableSupplier<Map<String, Object>> parametersSupplier) {
Expand Down Expand Up @@ -171,12 +173,12 @@ private String generateFullQuery(Map<String, Object> parameters, boolean ordered
boolean filteredOnValueRefTargetNames = filteredOnValueRefTargetNames(parameters);
List<String> conditions = new ArrayList<>();
if (parameters.get(PARAMETER_FROM) != null) {
conditions.add("aer.timestamp >= :from");
conditions.add("aer.timestampValue >= :from");
} else {
parameters.remove(PARAMETER_FROM);
}
if (parameters.get(PARAMETER_TO) != null) {
conditions.add("aer.timestamp <= :to");
conditions.add("aer.timestampValue <= :to");
} else {
parameters.remove(PARAMETER_TO);
}
Expand Down Expand Up @@ -266,6 +268,8 @@ private String generateFullQuery(Map<String, Object> parameters, boolean ordered
}
if (isCount) {
query = AUDIT_RECORDS_QUERY_COUNT + query;
} else {
query = AUDIT_RECORDS_QUERY_SELECT + query;
}
query += conditions.stream().collect(Collectors.joining(" and "));
if (ordered) {
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -19,6 +19,8 @@
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.repo.sql.helpers.modify.Ignore;
import com.evolveum.midpoint.audit.api.AuditEventRecord;
import com.evolveum.midpoint.audit.api.AuditEventStage;
import com.evolveum.midpoint.audit.api.AuditEventType;
import com.evolveum.midpoint.audit.api.AuditReferenceValue;
import com.evolveum.midpoint.audit.api.AuditService;
import com.evolveum.midpoint.prism.PrismContext;
Expand All @@ -34,6 +36,7 @@
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.schema.result.OperationResultStatus;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import org.apache.commons.lang.Validate;
Expand All @@ -51,6 +54,8 @@
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;
Expand All @@ -73,6 +78,35 @@ public class RAuditEventRecord implements Serializable {
public static final String COLUMN_TIMESTAMP = "timestampValue";

private static final long serialVersionUID = 621116861556252436L;

public static final String ID_COLUMN_NAME = "id";
public static final String ATTORNEY_NAME_COLUMN_NAME = "attorneyName";
public static final String ATTORNEY_OID_COLUMN_NAME = "attorneyOid";
private static final String CHANNEL_COLUMN_NAME = "channel";
private static final String EVENT_IDENTIFIER_COLUMN_NAME = "eventIdentifier";
private static final String EVENT_STAGE_COLUMN_NAME = "eventStage";
private static final String EVENT_TYPE_COLUMN_NAME = "eventType";
private static final String HOST_IDENTIFIER_COLUMN_NAME = "hostIdentifier";
public static final String INITIATOR_NAME_COLUMN_NAME = "initiatorName";
public static final String INITIATOR_OID_COLUMN_NAME = "initiatorOid";
public static final String INITIATOR_TYPE_COLUMN_NAME = "initiatorType";
private static final String MESSAGE_COLUMN_NAME = "message";
private static final String NODE_IDENTIFIER_COLUMN_NAME = "nodeIdentifier";
private static final String OUTCOME_COLUMN_NAME = "outcome";
private static final String PARAMETER_COLUMN_NAME = "parameter";
private static final String REMOTE_HOST_ADDRESS_COLUMN_NAME = "remoteHostAddress";
private static final String REQUEST_IDENTIFIER_COLUMN_NAME = "requestIdentifier";
private static final String RESULT_COLUMN_NAME = "result";
private static final String SESSION_IDENTIFIER_COLUMN_NAME = "sessionIdentifier";
public static final String TARGET_NAME_COLUMN_NAME = "targetName";
public static final String TARGET_OID_COLUMN_NAME = "targetOid";
public static final String TARGET_OWNER_NAME_COLUMN_NAME = "targetOwnerName";
public static final String TARGET_OWNER_OID_COLUMN_NAME = "targetOwnerOid";
public static final String TARGET_OWNER_TYPE_COLUMN_NAME = "targetOwnerType";
public static final String TARGET_TYPE_COLUMN_NAME = "targetType";
private static final String TASK_IDENTIFIER_COLUMN_NAME = "taskIdentifier";
private static final String TASK_OID_COLUMN_NAME = "taskOID";
private static final String TIMESTAMP_VALUE_COLUMN_NAME = "timestampValue";

private long id;
private Timestamp timestamp;
Expand Down Expand Up @@ -113,7 +147,7 @@ public class RAuditEventRecord implements Serializable {
private Set<RAuditItem> changedItems;
private Set<RAuditPropertyValue> propertyValues;
private Set<RAuditReferenceValue> referenceValues;
private Set<RResourceOid> resourceOids;
private Set<RTargetResourceOid> resourceOids;

private String result;

Expand Down Expand Up @@ -177,7 +211,7 @@ public Set<RAuditReferenceValue> getReferenceValues() {
@ForeignKey(name = "fk_audit_resource")
@OneToMany(mappedBy = "record", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public Set<RResourceOid> getResourceOids() {
public Set<RTargetResourceOid> getResourceOids() {
if (resourceOids == null) {
resourceOids = new HashSet<>();
}
Expand Down Expand Up @@ -317,7 +351,7 @@ public void setChangedItems(Set<RAuditItem> changedItems) {
this.changedItems = changedItems;
}

public void setResourceOids(Set<RResourceOid> resourceOids) {
public void setResourceOids(Set<RTargetResourceOid> resourceOids) {
this.resourceOids = resourceOids;
}

Expand Down Expand Up @@ -512,7 +546,7 @@ public static RAuditEventRecord toRepo(AuditEventRecord record, PrismContext pri
repo.setResult(record.getResult());

for(String resourceOid : record.getResourceOids()) {
repo.getResourceOids().add(RResourceOid.toRepo(repo, resourceOid));
repo.getResourceOids().add(RTargetResourceOid.toRepo(repo, resourceOid));
}

try {
Expand Down Expand Up @@ -616,7 +650,7 @@ public static AuditEventRecord fromRepo(RAuditEventRecord repo, PrismContext pri
audit.setTimestamp(repo.getTimestamp().getTime());
}

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

Expand Down Expand Up @@ -649,6 +683,40 @@ public static AuditEventRecord fromRepo(RAuditEventRecord repo, PrismContext pri
// initiator, attorney, target, targetOwner

}

public static AuditEventRecord fromRepo(ResultSet resultSet) throws SQLException {

AuditEventRecord audit = new AuditEventRecord();
audit.setChannel(resultSet.getString(CHANNEL_COLUMN_NAME));
audit.setEventIdentifier(resultSet.getString(EVENT_IDENTIFIER_COLUMN_NAME));
if (resultSet.getObject(EVENT_STAGE_COLUMN_NAME) != null) {
audit.setEventStage(AuditEventStage.values()[resultSet.getInt(EVENT_STAGE_COLUMN_NAME)]);
}
if (resultSet.getObject(EVENT_TYPE_COLUMN_NAME) != null) {
audit.setEventType(AuditEventType.values()[resultSet.getInt(EVENT_TYPE_COLUMN_NAME)]);
}
audit.setHostIdentifier(resultSet.getString(HOST_IDENTIFIER_COLUMN_NAME));
audit.setRemoteHostAddress(resultSet.getString(REMOTE_HOST_ADDRESS_COLUMN_NAME));
audit.setNodeIdentifier(resultSet.getString(NODE_IDENTIFIER_COLUMN_NAME));
audit.setMessage(resultSet.getString(MESSAGE_COLUMN_NAME));

if (resultSet.getObject(OUTCOME_COLUMN_NAME) != null) {
audit.setOutcome(OperationResultStatus.values()[resultSet.getInt(OUTCOME_COLUMN_NAME)]);
}
audit.setParameter(resultSet.getString(PARAMETER_COLUMN_NAME));
audit.setResult(resultSet.getString(RESULT_COLUMN_NAME));
audit.setSessionIdentifier(resultSet.getString(SESSION_IDENTIFIER_COLUMN_NAME));
audit.setRequestIdentifier(resultSet.getString(REQUEST_IDENTIFIER_COLUMN_NAME));
audit.setTaskIdentifier(resultSet.getString(TASK_IDENTIFIER_COLUMN_NAME));
audit.setTaskOID(resultSet.getString(TASK_OID_COLUMN_NAME));
if (resultSet.getTimestamp(TIMESTAMP_VALUE_COLUMN_NAME) != null) {
audit.setTimestamp(resultSet.getTimestamp(TIMESTAMP_VALUE_COLUMN_NAME).getTime());
}

audit.setRepoId(Long.valueOf(resultSet.getString(ID_COLUMN_NAME)));

return audit;
}

private static String getOrigName(PrismObject object) {
PolyString name = (PolyString) object.getPropertyRealValue(ObjectType.F_NAME, PolyString.class);
Expand Down
Expand Up @@ -35,6 +35,9 @@ public class RAuditPropertyValue implements EntityState {

public static final String TABLE_NAME = "m_audit_prop_value";
public static final String COLUMN_RECORD_ID = "record_id";

public static final String NAME_COLUMN_NAME = "name";
public static final String VALUE_COLUMN_NAME = "value";

private Boolean trans;

Expand Down
Expand Up @@ -17,12 +17,16 @@
package com.evolveum.midpoint.repo.sql.data.audit;

import com.evolveum.midpoint.audit.api.AuditReferenceValue;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString;
import com.evolveum.midpoint.repo.sql.helpers.modify.Ignore;
import com.evolveum.midpoint.repo.sql.util.EntityState;
import com.evolveum.midpoint.repo.sql.util.RUtil;

import javax.persistence.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

import static com.evolveum.midpoint.repo.sql.data.audit.RAuditReferenceValue.COLUMN_RECORD_ID;
Expand All @@ -36,6 +40,12 @@ public class RAuditReferenceValue implements EntityState {

public static final String TABLE_NAME = "m_audit_ref_value";
public static final String COLUMN_RECORD_ID = "record_id";

public static final String NAME_COLUMN_NAME = "name";
private static final String OID_COLUMN_NAME = "oid";
private static final String TARGET_NAME_NORM_COLUMN_NAME = "targetName_norm";
private static final String TARGET_NAME_ORIG_COLUMN_NAME = "targetName_orig";
private static final String TYPE_COLUMN_NAME = "type";

private Boolean trans;

Expand Down Expand Up @@ -145,6 +155,17 @@ public static RAuditReferenceValue toRepo(RAuditEventRecord record, String name,
public AuditReferenceValue fromRepo() {
return new AuditReferenceValue(oid, RUtil.stringToQName(type), RPolyString.fromRepo(targetName));
}

public static AuditReferenceValue fromRepo(ResultSet resultSet) throws SQLException {
PolyString targetName = null;
if(resultSet.getString(TARGET_NAME_ORIG_COLUMN_NAME)!= null
|| resultSet.getString(TARGET_NAME_NORM_COLUMN_NAME) != null) {
targetName = new PolyString(resultSet.getString(TARGET_NAME_ORIG_COLUMN_NAME),
resultSet.getString(TARGET_NAME_NORM_COLUMN_NAME));
}
return new AuditReferenceValue(resultSet.getString(OID_COLUMN_NAME),
RUtil.stringToQName(resultSet.getString(TYPE_COLUMN_NAME)), targetName);
}


@Override
Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.sql.data.common.OperationResultFull;
import com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString;
import com.evolveum.midpoint.repo.sql.data.common.enums.RChangeType;
Expand All @@ -40,6 +41,8 @@

import static com.evolveum.midpoint.repo.sql.data.audit.RObjectDeltaOperation.COLUMN_RECORD_ID;

import java.sql.ResultSet;

/**
* @author lazyman
*/
Expand All @@ -54,6 +57,18 @@ public class RObjectDeltaOperation implements OperationResultFull, EntityState {

public static final String TABLE_NAME = "m_audit_delta";
public static final String COLUMN_RECORD_ID = "record_id";

private static final String CHECKSUM_COLUMN_NAME = "checksum";
private static final String DELTA_COLUMN_NAME = "delta";
private static final String DELTA_OID_COLUMN_NAME = "deltaOid";
private static final String DELTA_TYPE_COLUMN_NAME = "deltaType";
private static final String FULL_RESULT_COLUMN_NAME = "fullResult";
private static final String OBJECT_NAME_NORM_COLUMN_NAME = "objectName_norm";
private static final String OBJECT_NAME_ORIG_COLUMN_NAME = "objectName_orig";
private static final String RESOURCE_NAME_NORM_COLUMN_NAME = "resourceName_norm";
private static final String RESOURCE_NAME_ORIG_COLUMN_NAME = "resourceName_orig";
private static final String RESOURCE_OID_COLUMN_NAME = "resourceOid";
private static final String STATUS_COLUMN_NAME = "status";

private Boolean trans;

Expand Down Expand Up @@ -315,4 +330,37 @@ public static ObjectDeltaOperation fromRepo(RObjectDeltaOperation operation, Pri

return odo;
}

public static ObjectDeltaOperation fromRepo(ResultSet resultSet, PrismContext prismContext, boolean useUtf16)
throws DtoTranslationException {

ObjectDeltaOperation odo = new ObjectDeltaOperation();
try {
if (resultSet.getBytes(DELTA_COLUMN_NAME) != null) {
byte[] data = resultSet.getBytes(DELTA_COLUMN_NAME);
String xmlDelta = RUtil.getXmlFromByteArray(data, true, useUtf16);

ObjectDeltaType delta = prismContext.parserFor(xmlDelta).parseRealValue(ObjectDeltaType.class);
odo.setObjectDelta(DeltaConvertor.createObjectDelta(delta, prismContext));
}
if (resultSet.getBytes(FULL_RESULT_COLUMN_NAME) != null) {
byte[] data = resultSet.getBytes(FULL_RESULT_COLUMN_NAME);
String xmlResult = RUtil.getXmlFromByteArray(data, true, useUtf16);

OperationResultType resultType = prismContext.parserFor(xmlResult).parseRealValue(OperationResultType.class);
odo.setExecutionResult(OperationResult.createOperationResult(resultType));
}
if(resultSet.getString(OBJECT_NAME_ORIG_COLUMN_NAME) != null || resultSet.getString(OBJECT_NAME_NORM_COLUMN_NAME) != null) {
odo.setObjectName(new PolyString(resultSet.getString(OBJECT_NAME_ORIG_COLUMN_NAME), resultSet.getString(OBJECT_NAME_NORM_COLUMN_NAME)));
}
odo.setResourceOid(resultSet.getString(RESOURCE_OID_COLUMN_NAME));
if(resultSet.getString(RESOURCE_NAME_ORIG_COLUMN_NAME) != null || resultSet.getString(RESOURCE_NAME_NORM_COLUMN_NAME) != null) {
odo.setResourceName(new PolyString(resultSet.getString(RESOURCE_NAME_ORIG_COLUMN_NAME), resultSet.getString(RESOURCE_NAME_NORM_COLUMN_NAME)));
}
} catch (Exception ex) {
throw new DtoTranslationException(ex.getMessage(), ex);
}

return odo;
}
}
Expand Up @@ -22,18 +22,20 @@
import com.evolveum.midpoint.repo.sql.util.EntityState;
import org.hibernate.annotations.ForeignKey;

import static com.evolveum.midpoint.repo.sql.data.audit.RResourceOid.COLUMN_RECORD_ID;
import static com.evolveum.midpoint.repo.sql.data.audit.RTargetResourceOid.COLUMN_RECORD_ID;

@Ignore
@Entity
@IdClass(RResourceOidId.class)
@Table(name = RResourceOid.TABLE_NAME, indexes = {
@Index(name = "iresourceOid", columnList = "resourceOid"),
@Index(name = "iResourceOidRecordId", columnList = COLUMN_RECORD_ID)})
public class RResourceOid implements EntityState {
@IdClass(RTargetResourceOidId.class)
@Table(name = RTargetResourceOid.TABLE_NAME, indexes = {
@Index(name = "iAuditResourceOid", columnList = "resourceOid"),
@Index(name = "iAuditResourceOidRecordId", columnList = COLUMN_RECORD_ID)})
public class RTargetResourceOid implements EntityState {

public static final String TABLE_NAME = "m_audit_resource";
public static final String COLUMN_RECORD_ID = "record_id";

public static final String RESOURCE_OID_COLUMN_NAME = "resourceOid";

private Boolean trans;

Expand Down Expand Up @@ -93,8 +95,8 @@ public void setresourceOid(String resourceOid) {
this.resourceOid = resourceOid;
}

public static RResourceOid toRepo(RAuditEventRecord record, String resourceOid) {
RResourceOid resourceOidObject = new RResourceOid();
public static RTargetResourceOid toRepo(RAuditEventRecord record, String resourceOid) {
RTargetResourceOid resourceOidObject = new RTargetResourceOid();
resourceOidObject.setRecord(record);
resourceOidObject.setresourceOid(resourceOid);
return resourceOidObject;
Expand All @@ -106,7 +108,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

RResourceOid that = (RResourceOid) o;
RTargetResourceOid that = (RTargetResourceOid) o;

if (resourceOid != null ? !resourceOid.equals(that.resourceOid) : that.resourceOid != null) return false;
return true;
Expand Down
Expand Up @@ -18,7 +18,7 @@

import java.io.Serializable;

public class RResourceOidId implements Serializable{
public class RTargetResourceOidId implements Serializable{

private static final long serialVersionUID = 1L;
private Long recordId;
Expand Down Expand Up @@ -46,7 +46,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

RResourceOidId that = (RResourceOidId) o;
RTargetResourceOidId that = (RTargetResourceOidId) o;

if (recordId != null ? !recordId.equals(that.recordId) : that.recordId != null) return false;
if (resourceOid != null ? !resourceOid.equals(that.resourceOid) : that.resourceOid != null) return false;
Expand Down

0 comments on commit 76f046a

Please sign in to comment.