use of com.evolveum.midpoint.common.validator.Validator in project midpoint by Evolveum.
the class ObjectImporter method importObjectsInternal.
private void importObjectsInternal(InputStream input, final ImportOptionsType options, final boolean raw, final Task task, final OperationResult parentResult) {
EventHandler handler = new EventHandler() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public <T extends Objectable> EventResult postMarshall(PrismObject<T> prismObjectObjectable, Element objectElement, OperationResult objectResult) {
LOGGER.debug("Importing object {}", prismObjectObjectable);
T objectable = prismObjectObjectable.asObjectable();
if (!(objectable instanceof ObjectType)) {
String message = "Cannot process type " + objectable.getClass() + " as it is not a subtype of " + ObjectType.class;
objectResult.recordFatalError(message);
LOGGER.error("Import of object {} failed: {}", new Object[] { prismObjectObjectable, message });
return EventResult.skipObject(message);
}
PrismObject<? extends ObjectType> object = (PrismObject<? extends ObjectType>) prismObjectObjectable;
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("IMPORTING object:\n{}", object.debugDump());
}
object = migrator.migrate(object);
Utils.resolveReferences(object, repository, (options == null || options.isReferentialIntegrity() == null) ? false : options.isReferentialIntegrity(), false, EvaluationTimeType.IMPORT, false, prismContext, objectResult);
objectResult.computeStatus();
if (!objectResult.isAcceptable()) {
return EventResult.skipObject(objectResult.getMessage());
}
generateIdentifiers(object, repository, objectResult);
objectResult.computeStatus();
if (!objectResult.isAcceptable()) {
return EventResult.skipObject(objectResult.getMessage());
}
if (options != null && BooleanUtils.isTrue(options.isValidateDynamicSchema())) {
validateWithDynamicSchemas(object, objectElement, repository, objectResult);
}
objectResult.computeStatus();
if (!objectResult.isAcceptable()) {
return EventResult.skipObject(objectResult.getMessage());
}
if (options != null && BooleanUtils.isTrue(options.isEncryptProtectedValues())) {
OperationResult opResult = objectResult.createMinorSubresult(ObjectImporter.class.getName() + ".encryptValues");
try {
CryptoUtil.encryptValues(protector, object);
opResult.recordSuccess();
} catch (EncryptionException e) {
opResult.recordFatalError(e);
}
}
if (options == null || (options != null && !BooleanUtils.isTrue(options.isKeepMetadata()))) {
MetadataType metaData = new MetadataType();
String channel = SchemaConstants.CHANNEL_OBJECT_IMPORT_URI;
metaData.setCreateChannel(channel);
metaData.setCreateTimestamp(clock.currentTimeXMLGregorianCalendar());
if (task.getOwner() != null) {
metaData.setCreatorRef(ObjectTypeUtil.createObjectRef(task.getOwner()));
}
object.asObjectable().setMetadata(metaData);
}
objectResult.computeStatus();
if (!objectResult.isAcceptable()) {
return EventResult.skipObject(objectResult.getMessage());
}
try {
importObjectToRepository(object, options, raw, task, objectResult);
LOGGER.info("Imported object {}", object);
} catch (SchemaException e) {
objectResult.recordFatalError("Schema violation: " + e.getMessage(), e);
LOGGER.error("Import of object {} failed: Schema violation: {}", object, e.getMessage(), e);
} catch (ObjectAlreadyExistsException e) {
objectResult.recordFatalError("Object already exists: " + e.getMessage(), e);
LOGGER.error("Import of object {} failed: Object already exists: {}", object, e.getMessage(), e);
LOGGER.error("Object already exists", e);
} catch (RuntimeException e) {
objectResult.recordFatalError("Unexpected problem: " + e.getMessage(), e);
LOGGER.error("Import of object {} failed: Unexpected problem: {}", object, e.getMessage(), e);
} catch (ObjectNotFoundException e) {
LOGGER.error("Import of object {} failed: Object referred from this object was not found: {}", object, e.getMessage(), e);
} catch (ExpressionEvaluationException e) {
LOGGER.error("Import of object {} failed: Expression evaluation error: {}", object, e.getMessage(), e);
} catch (CommunicationException e) {
LOGGER.error("Import of object {} failed: Communication error: {}", object, e.getMessage(), e);
} catch (ConfigurationException e) {
LOGGER.error("Import of object {} failed: Configuration error: {}", object, e.getMessage(), e);
} catch (PolicyViolationException e) {
LOGGER.error("Import of object {} failed: Policy violation: {}", object, e.getMessage(), e);
} catch (SecurityViolationException e) {
LOGGER.error("Import of object {} failed: Security violation: {}", object, e.getMessage(), e);
}
objectResult.recordSuccessIfUnknown();
if (objectResult.isAcceptable()) {
// Continue import
return EventResult.cont();
} else {
return EventResult.skipObject(objectResult.getMessage());
}
}
@Override
public void handleGlobalError(OperationResult currentResult) {
// No reaction
}
};
Validator validator = new Validator(prismContext, handler);
validator.setVerbose(true);
if (options != null) {
validator.setValidateSchema(BooleanUtils.isTrue(options.isValidateStaticSchema()));
if (options.getStopAfterErrors() != null) {
validator.setStopAfterErrors(options.getStopAfterErrors().longValue());
}
if (BooleanUtils.isTrue(options.isSummarizeErrors())) {
parentResult.setSummarizeErrors(true);
}
if (BooleanUtils.isTrue(options.isSummarizeSucceses())) {
parentResult.setSummarizeSuccesses(true);
}
}
validator.validate(input, parentResult, OperationConstants.IMPORT_OBJECT);
}
Aggregations