use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.
the class ExportObjects method execute.
public boolean execute() throws UnsupportedEncodingException, FileNotFoundException {
System.out.println("Starting objects export.");
File file = new File(filePath);
if (file.exists() || file.canRead()) {
System.out.println("XML file already exists, export won't be done.");
return false;
}
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(CONTEXTS);
final OutputStreamWriter stream = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
try {
System.out.println("Loading spring contexts.");
System.out.println("Set repository.");
RepositoryService repository = context.getBean("repositoryService", RepositoryService.class);
ResultHandler<ObjectType> handler = new ResultHandler<ObjectType>() {
PrismContext prismContext = context.getBean(PrismContext.class);
@Override
public boolean handle(PrismObject<ObjectType> object, OperationResult parentResult) {
String displayName = getDisplayName(object);
System.out.println("Exporting object " + displayName);
OperationResult resultExport = new OperationResult("Export " + displayName);
try {
String stringObject = prismContext.serializeObjectToString(object, PrismContext.LANG_XML);
stream.write("\t" + stringObject + "\n");
} catch (Exception ex) {
System.out.println("Failed to parse objects to string for xml. Reason: " + ex);
resultExport.recordFatalError("Failed to parse objects to string for xml. Reason: ", ex);
}
return true;
}
};
stream.write(createHeaderForXml());
OperationResult result = new OperationResult("search set");
System.out.println("Creating xml file " + file.getName());
repository.searchObjectsIterative(ObjectType.class, null, handler, null, false, result);
stream.write("</objects>");
System.out.println("Created xml file " + file.getName());
} catch (Exception ex) {
System.out.println("Exception occurred during context loading, reason: " + ex.getMessage());
ex.printStackTrace();
} finally {
destroyContext(context);
if (stream != null) {
IOUtils.closeQuietly(stream);
}
}
System.out.println("Objects export finished.");
return true;
}
use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.
the class TestResources method test125SearchResourcesIterative.
@Test
public void test125SearchResourcesIterative() throws Exception {
given();
Task task = getTestTask();
OperationResult result = task.getResult();
preTestCleanup(AssignmentPolicyEnforcementType.POSITIVE);
// precondition
assertSteadyResources();
final List<PrismObject<ResourceType>> resources = new ArrayList<>();
ResultHandler<ResourceType> handler = (resource, parentResult) -> {
assertResource(resource, true);
resources.add(resource);
return true;
};
when();
modelService.searchObjectsIterative(ResourceType.class, null, handler, null, task, result);
then();
assertSuccess(result);
assertFalse("Empty search return", resources.isEmpty());
assertEquals("Unexpected number of resources found", 2, resources.size());
assertSteadyResources();
}
use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.
the class SearchEvaluator method evaluate.
public <T extends ObjectType> PipelineData evaluate(SearchExpressionType searchExpression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException, SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, SecurityViolationException, ExpressionEvaluationException {
Validate.notNull(searchExpression.getType());
ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
List<PipelineItem> data = input.getData();
if (data.isEmpty()) {
// TODO fix this brutal hack (with dummyValue)
PrismContainerValue<?> dummyValue = prismContext.itemFactory().createContainerValue();
PipelineItem dummyItem = new PipelineItem(dummyValue, PipelineData.newOperationResult(), context.getInitialVariables());
data = Collections.singletonList(dummyItem);
}
final PipelineData outputData = PipelineData.createEmpty();
final MutableBoolean atLeastOne = new MutableBoolean(false);
for (PipelineItem item : data) {
// TODO variables from current item
// TODO operation result handling (global vs local)
boolean noFetch = expressionHelper.getArgumentAsBoolean(searchExpression.getParameter(), PARAM_NO_FETCH, input, context, false, "search", globalResult);
Class<T> objectClass = ObjectTypes.getObjectTypeFromTypeQName(searchExpression.getType()).getClassDefinition();
ObjectQuery unresolvedObjectQuery = null;
if (searchExpression.getQuery() != null) {
try {
unresolvedObjectQuery = context.getQueryConverter().createObjectQuery(objectClass, searchExpression.getQuery());
} catch (SchemaException e) {
throw new ScriptExecutionException("Couldn't parse object query. Reason: " + e.getMessage(), e);
}
} else if (searchExpression.getSearchFilter() != null) {
unresolvedObjectQuery = prismContext.queryFactory().createQuery();
try {
ObjectFilter filter = prismContext.getQueryConverter().parseFilter(searchExpression.getSearchFilter(), objectClass);
unresolvedObjectQuery.setFilter(filter);
} catch (SchemaException e) {
throw new ScriptExecutionException("Couldn't parse object query. Reason: " + e.getMessage(), e);
}
}
ObjectQuery objectQuery;
if (unresolvedObjectQuery != null) {
VariablesMap variables = new VariablesMap();
// noinspection unchecked
item.getVariables().forEach((name, value) -> variables.put(name, cloneIfNecessary(name, value)));
try {
objectQuery = ExpressionUtil.evaluateQueryExpressions(unresolvedObjectQuery, variables, expressionProfile, expressionFactory, prismContext, "bulk action query", context.getTask(), globalResult);
} catch (SchemaException | ObjectNotFoundException | ExpressionEvaluationException | CommunicationException | ConfigurationException | SecurityViolationException e) {
// TODO continue on any error?
throw new ScriptExecutionException("Couldn't evaluate expressions in object query: " + e.getMessage(), e);
}
} else {
objectQuery = null;
}
final String variableName = searchExpression.getVariable();
ResultHandler<T> handler = (object, parentResult) -> {
context.checkTaskStop();
atLeastOne.setValue(true);
if (searchExpression.getScriptingExpression() != null) {
if (variableName != null) {
// TODO
}
JAXBElement<?> childExpression = searchExpression.getScriptingExpression();
try {
PipelineData expressionResult = scriptingExpressionEvaluator.evaluateExpression((ScriptingExpressionType) childExpression.getValue(), PipelineData.create(object.getValue(), item.getVariables()), context, globalResult);
if (!BooleanUtils.isFalse(searchExpression.isAggregateOutput())) {
outputData.addAllFrom(expressionResult);
}
globalResult.setSummarizeSuccesses(true);
globalResult.summarize();
} catch (ScriptExecutionException | SchemaException | ConfigurationException | ObjectNotFoundException | CommunicationException | SecurityViolationException | ExpressionEvaluationException e) {
// todo think about this
if (context.isContinueOnAnyError()) {
LoggingUtils.logUnexpectedException(LOGGER, "Exception when evaluating item from search result list.", e);
} else {
throw new SystemException(e);
}
}
} else {
outputData.addValue(object.getValue(), item.getVariables());
}
return true;
};
try {
Collection<SelectorOptions<GetOperationOptions>> options = operationsHelper.createGetOptions(searchExpression.getOptions(), noFetch);
modelService.searchObjectsIterative(objectClass, objectQuery, handler, options, context.getTask(), globalResult);
} catch (SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
// TODO continue on any error?
throw new ScriptExecutionException("Couldn't execute searchObjects operation: " + e.getMessage(), e);
}
}
if (atLeastOne.isFalse()) {
// temporary hack, this will be configurable
context.println("Warning: no matching object found");
}
return outputData;
}
use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.
the class TestBrokenResources method testListResources.
public void testListResources(int expectedNumber, Collection<SelectorOptions<GetOperationOptions>> options) throws Exception {
// GIVEN (1)
Task task = getTestTask();
OperationResult result = task.getResult();
// WHEN (1)
final SearchResultList<PrismObject<ResourceType>> resources = modelService.searchObjects(ResourceType.class, null, options, task, result);
// THEN (1)
result.computeStatus();
display("getObject result", result);
if (options == null) {
assertEquals("Expected partial error (search)", OperationResultStatus.PARTIAL_ERROR, result.getStatus());
} else if (GetOperationOptions.isNoFetch(SelectorOptions.findRootOptions(options))) {
TestUtil.assertSuccess(result);
} else {
AssertJUnit.fail("unexpected");
}
display("Got resources: " + resources);
assertEquals("Wrong number of resources", expectedNumber, resources.size());
// GIVEN (2)
resources.clear();
task = getTestTask();
result = task.getResult();
ResultHandler<ResourceType> handler = (object, parentResult) -> {
resources.add(object);
return true;
};
// WHEN (2)
modelService.searchObjectsIterative(ResourceType.class, null, handler, options, task, result);
// THEN (2)
result.computeStatus();
display("getObject result", result);
if (options == null) {
assertEquals("Expected partial error (searchIterative)", OperationResultStatus.PARTIAL_ERROR, result.getStatus());
} else if (GetOperationOptions.isNoFetch(SelectorOptions.findRootOptions(options))) {
TestUtil.assertSuccess(result);
} else {
AssertJUnit.fail("unexpected");
}
display("Got resources: " + resources);
assertEquals("Wrong number of resources", expectedNumber, resources.size());
}
use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.
the class SearchIterativeTest method test100SimpleSequentialIteration.
@Test
public void test100SimpleSequentialIteration() throws Exception {
OperationResult result = new OperationResult("test100SimpleSequentialIteration");
final List<PrismObject<UserType>> objects = new ArrayList<>();
ResultHandler<UserType> handler = (object, parentResult) -> {
objects.add(object);
return true;
};
SearchOpAsserter asserter = new SearchOpAsserter();
repositoryService.searchObjectsIterative(UserType.class, null, handler, null, true, result);
result.recomputeStatus();
// extra search is to make sure no other objects are there
asserter.assertIncrement(COUNT / BATCH + 1);
assertTrue(result.isSuccess());
assertObjects(objects, COUNT);
}
Aggregations