Skip to content

Commit

Permalink
Merge branch 'master' of x:\midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Nov 29, 2016
2 parents 14bd634 + b258679 commit 80a4124
Show file tree
Hide file tree
Showing 38 changed files with 1,941 additions and 1,340 deletions.
Expand Up @@ -34,6 +34,8 @@
<div wicket:id="targetOwnerNameField" />
<label><wicket:message key="PageAuditLogViewer.initiatorNameLabel" /></label>
<div wicket:id="initiatorNameField"/>
<label><wicket:message key="PageAuditLogViewer.changedItem" /></label>
<div wicket:id="changedItem"/>
</div>
<div class="col-md-6" style="padding-left: 5px;">
<label><wicket:message key="PageAuditLogViewer.eventTypeLabel" /></label>
Expand Down
Expand Up @@ -76,6 +76,7 @@ public class AuditLogViewerPanel extends BasePanel{
private static final String ID_EVENT_STAGE = "eventStageField";
private static final String ID_EVENT_STAGE_LABEL = "eventStageLabel";
private static final String ID_OUTCOME = "outcomeField";
private static final String ID_CHANGED_ITEM = "changedItem";

private static final String ID_MAIN_FORM = "mainForm";
private static final String ID_SEARCH_BUTTON = "searchButton";
Expand Down Expand Up @@ -170,6 +171,14 @@ private void initParametersPanel(Form mainForm) {

to.setOutputMarkupId(true);
parametersPanel.add(to);

PropertyModel<String> changedItemModel = new PropertyModel<String>(auditSearchDto,
AuditSearchDto.F_CHANGED_ITEM);
TextPanel<String> changedItemPanel = new TextPanel<String>(ID_CHANGED_ITEM, changedItemModel);
changedItemPanel.getBaseFormComponent().add(new EmptyOnChangeAjaxFormUpdatingBehavior());
changedItemPanel.getBaseFormComponent().add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
changedItemPanel.setOutputMarkupId(true);
parametersPanel.add(changedItemPanel);

PropertyModel<String> hostIdentifierModel = new PropertyModel<String>(auditSearchDto,
AuditSearchDto.F_HOST_IDENTIFIER);
Expand Down Expand Up @@ -377,7 +386,7 @@ public Map<String, Object> getParameters() {
if (search.getTargetName() != null) {
parameters.put("targetName", search.getTargetName().getOid());
}

parameters.put("changedItem", search.getChangedItem());
parameters.put("eventType", search.getEventType());
parameters.put("eventStage", search.getEventStage());
parameters.put("outcome", search.getOutcome());
Expand Down
@@ -1,5 +1,14 @@
package com.evolveum.midpoint.web.page.admin.reports.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;

import com.evolveum.midpoint.audit.api.AuditEventRecord;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
Expand All @@ -8,10 +17,6 @@
import com.evolveum.midpoint.web.component.data.BaseSortableDataProvider;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType;
import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;

import java.util.*;

/**
* Created by honchar.
Expand Down Expand Up @@ -68,26 +73,27 @@ public Iterator<AuditEventRecordType> internalIterator(long first, long count) {
}


@Override
protected int internalSize() {
String query = generateFullQuery(AUDIT_RECORDS_QUERY_COUNT + auditEventQuery, false);
String query = generateFullQuery(AUDIT_RECORDS_QUERY_COUNT + auditEventQuery, false, true);
long count;
try {
count = getAuditService().countObjects(query, parameters, new OperationResult("internalSize"));
} catch (SecurityViolationException | SchemaException e) {
// TODO: proper error handling (MID-3536)
throw new SystemException(e.getMessage(), e);
}

return ((Long)count).intValue();
}


return ((Long)count).intValue();
}

private List<AuditEventRecordType> listRecords(String query, boolean orderBy){
return listRecords(query, orderBy, 0, getPage().getItemsPerPage(UserProfileStorage.TableId.PAGE_AUDIT_LOG_VIEWER));
}

private List<AuditEventRecordType> listRecords(String query, boolean orderBy, long first, long count){
String parameterQuery = generateFullQuery(query, orderBy);
String parameterQuery = generateFullQuery(query, orderBy, false);

if (parameters.containsKey(SET_FIRST_RESULT_PARAMETER)){
parameters.remove(SET_FIRST_RESULT_PARAMETER);
Expand Down Expand Up @@ -126,8 +132,23 @@ public void setAuditEventQuery(String auditEventQuery) {
this.auditEventQuery = auditEventQuery;
}

private String generateFullQuery(String query, boolean orderBy){
private String generateFullQuery(String query, boolean orderBy, boolean isCount){
parameters = getParameters();
if (parameters.get("changedItem") != null) {
if (isCount) {
query = "select count(*) from RAuditEventRecord as aer right join aer.changedItems as item where 1=1 and ";
} else {
query = "from RAuditEventRecord as aer right join aer.changedItems as item where 1=1 and ";
}
// query += "INNER JOIN aer.changedItems as item on item.record_id = aer.id WHERE 1=1 and "
// + "(item.changedItemPath = :changedItem) and ";
query += "(item.changedItemPath = :changedItem) and ";

} else {
parameters.remove("changedItem");
// query += "where 1=1 and ";
}

if (parameters.get("from") != null) {
query += "(aer.timestamp >= :from) and ";
} else {
Expand Down Expand Up @@ -183,6 +204,7 @@ private String generateFullQuery(String query, boolean orderBy){
} else {
parameters.remove("taskIdentifier");
}

query = query.substring(0, query.length()-5); // remove trailing " and "
if (orderBy){
query += AUDIT_RECORDS_ORDER_BY;
Expand Down
Expand Up @@ -43,6 +43,7 @@ public class AuditSearchDto implements Serializable {
public static final String F_EVENT_TYPE = "eventType";
public static final String F_EVENT_STAGE = "eventStage";
public static final String F_OUTCOME = "outcome";
public static final String F_CHANGED_ITEM = "changedItem";

private XMLGregorianCalendar from;
private XMLGregorianCalendar to;
Expand All @@ -54,6 +55,7 @@ public class AuditSearchDto implements Serializable {
private AuditEventTypeType eventType;
private AuditEventStageType eventStage;
private OperationResultStatusType outcome;
private String changedItem;

public XMLGregorianCalendar getFrom() {
return from;
Expand Down Expand Up @@ -134,5 +136,13 @@ public OperationResultStatusType getOutcome() {
public void setOutcome(OperationResultStatusType outcome) {
this.outcome = outcome;
}

public String getChangedItem() {
return changedItem;
}


public void setChangedItem(String changedItem) {
this.changedItem = changedItem;
}
}
Expand Up @@ -3378,6 +3378,7 @@ PageAuditLogViewer.eventTypeShortLabel=Type
PageAuditLogViewer.eventStageLabel=Event Stage
PageAuditLogViewer.eventStageShortLabel=Stage
PageAuditLogViewer.outcomeLabel=Outcome
PageAuditLogViewer.changedItem=Item changed
AuditLogViewerPanel.dateValidatorMessage=From date must be before To date.
AuditEventRecordType.timestamp=Time
AuditEventRecordType.initiatorRef=Initiator
Expand Down
Expand Up @@ -48,5 +48,6 @@ public class OperationConstants {

public static final String CHECK_SHADOW_INTEGRITY = PREFIX + ".checkShadowIntegrity";
public static final String REINDEX = PREFIX + ".reindex";
public static final String AUDIT_REINDEX = PREFIX + ".auditReindex";

}
Expand Up @@ -325,7 +325,7 @@
<li>archived: Inactive historical definition. It is no longer used.
It is maintained only for historical, auditing and
sentimental reasons.</li>
<li>failed: Unexpected error has occured during object lifecycle. Result
<li>failed: Unexpected error has occurred during object lifecycle. Result
of that event is that the object is rendered inactive.
The situation cannot be automatically remedied. Manual action
is needed.</li>
Expand Down
Expand Up @@ -30,6 +30,7 @@ public class ModelPublicConstants {

public static final String DELETE_TASK_HANDLER_URI = NS_SYNCHRONIZATION_TASK_PREFIX + "/delete/handler-3"; // TODO why "synchronization"?
public static final String REINDEX_TASK_HANDLER_URI = SchemaConstants.NS_MODEL + "/reindex/handler-3";
public static final String AUDIT_REINDEX_TASK_HANDLER_URI = SchemaConstants.NS_MODEL + "/auditReindex/handler-3";
public static final String CLEANUP_TASK_HANDLER_URI = SchemaConstants.NS_MODEL + "/cleanup/handler-3";
public static final String SHADOW_INTEGRITY_CHECK_TASK_HANDLER_URI = SchemaConstants.NS_MODEL + "/shadow-integrity-check/handler-3";
public static final String FOCUS_VALIDITY_SCANNER_TASK_HANDLER_URI = NS_SYNCHRONIZATION_TASK_PREFIX + "/focus-validation-scanner/handler-3"; // TODO why synchronization?
Expand Down
@@ -0,0 +1,177 @@
package com.evolveum.midpoint.model.impl.util;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.evolveum.midpoint.audit.api.AuditEventRecord;
import com.evolveum.midpoint.audit.api.AuditResultHandler;
import com.evolveum.midpoint.audit.api.AuditService;
import com.evolveum.midpoint.model.api.ModelPublicConstants;
import com.evolveum.midpoint.schema.result.OperationConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskHandler;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.task.api.TaskRunResult;
import com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

@Component
public class AuditReindexTaskHandler implements TaskHandler {

static final Trace LOGGER = TraceManager.getTrace(AuditReindexTaskHandler.class);

public static final String HANDLER_URI = ModelPublicConstants.AUDIT_REINDEX_TASK_HANDLER_URI;

private static final String taskName = "AuditReindex";

@Autowired
protected AuditService auditService;

@Autowired
protected TaskManager taskManager;

@PostConstruct
private void initialize() {
taskManager.registerHandler(HANDLER_URI, this);
}

@Override
public TaskRunResult run(Task coordinatorTask) {
OperationResult opResult = new OperationResult(OperationConstants.AUDIT_REINDEX + ".run");
opResult.setStatus(OperationResultStatus.IN_PROGRESS);
TaskRunResult runResult = new TaskRunResult();
runResult.setOperationResult(opResult);

final Long expectedTotal = auditService.countObjects("select count(*) from RAuditEventRecord as aer where 1=1", null);
AuditResultHandler resultHandler = new AuditResultHandler() {

private AtomicInteger processedObjects = new AtomicInteger();

@Override
public boolean handle(AuditEventRecord auditRecord) {

auditService.reindexEntry(auditRecord);
processedObjects.incrementAndGet();

return true;
}

@Override
public int getProgress() {
return processedObjects.get();
}
};
if (resultHandler == null) {
// the error should already be in the runResult
return runResult;
}


try {
LOGGER.trace("{}: expecting {} objects to be processed", taskName, expectedTotal);

runResult.setProgress(0);
coordinatorTask.setProgress(0);
if (expectedTotal != null) {
coordinatorTask.setExpectedTotal(expectedTotal);
}
try {
coordinatorTask.savePendingModifications(opResult);
} catch (ObjectAlreadyExistsException e) { // other exceptions are
// handled in the outer
// try block
throw new IllegalStateException(
"Unexpected ObjectAlreadyExistsException when updating task progress/expectedTotal",
e);
}

auditService.listRecordsIterative(null, null, resultHandler);

opResult.recordSuccess();

} catch (ObjectNotFoundException e) {
// This is bad. The resource does not exist. Permanent problem.
logErrorAndSetResult(runResult, resultHandler, "Object not found", e,
OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
return runResult;
} catch (SchemaException e) {
// Not sure about this. But most likely it is a misconfigured
// resource or connector
// It may be worth to retry. Error is fatal, but may not be
// permanent.
logErrorAndSetResult(runResult, resultHandler, "Error dealing with schema", e,
OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.TEMPORARY_ERROR);
return runResult;
} catch (RuntimeException e) {
// Can be anything ... but we can't recover from that.
// It is most likely a programming error. Does not make much sense
// to retry.
logErrorAndSetResult(runResult, resultHandler, "Internal error", e,
OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
return runResult;
}

// TODO: check last handler status

runResult.setProgress(resultHandler.getProgress());
runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);

String finishMessage = "Finished " + taskName + " (" + coordinatorTask + "). ";
String statistics = "Processed " + resultHandler.getProgress() + " objects";

opResult.createSubresult(OperationConstants.AUDIT_REINDEX + ".statistics")
.recordStatus(OperationResultStatus.SUCCESS, statistics);

LOGGER.info(finishMessage + statistics);

LOGGER.trace("{} run finished (task {}, run result {})", taskName, coordinatorTask, runResult);

return runResult;

}

@Override
public Long heartbeat(Task task) {
return task.getProgress();
}

@Override
public void refreshStatus(Task task) {
// TODO Auto-generated method stub

}

@Override
public String getCategoryName(Task task) {
return taskName;
}

@Override
public List<String> getCategoryNames() {
// TODO Auto-generated method stub
return null;
}

// TODO: copied from abstract search iterative handler
private TaskRunResult logErrorAndSetResult(TaskRunResult runResult, AuditResultHandler resultHandler,
String message, Throwable e, OperationResultStatus opStatus, TaskRunResultStatus status) {
LOGGER.error("{}: {}: {}", taskName, message, e.getMessage(), e);
runResult.getOperationResult().recordStatus(opStatus, message + ": " + e.getMessage(), e);
runResult.setRunResultStatus(status);
runResult.setProgress(resultHandler.getProgress());
return runResult;

}

}

0 comments on commit 80a4124

Please sign in to comment.