use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class TestRetirement method reconcileAllOrgs.
private void reconcileAllOrgs() throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
final Task task = createTask("reconcileAllOrgs");
OperationResult result = task.getResult();
ResultHandler<OrgType> handler = new ResultHandler<OrgType>() {
@Override
public boolean handle(PrismObject<OrgType> object, OperationResult parentResult) {
try {
display("reconciling " + object);
reconcileOrg(object.getOid(), task, parentResult);
} catch (SchemaException | PolicyViolationException | ExpressionEvaluationException | ObjectNotFoundException | ObjectAlreadyExistsException | CommunicationException | ConfigurationException | SecurityViolationException e) {
throw new SystemException(e.getMessage(), e);
}
return true;
}
};
display("Reconciling all orgs");
modelService.searchObjectsIterative(OrgType.class, null, handler, null, task, result);
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class ShadowManager method lookupShadowInRepository.
/**
* Locates the appropriate Shadow in repository that corresponds to the
* provided resource object.
*
* DEAD flag is cleared - in memory as well as in repository.
*
* @param parentResult
*
* @return current shadow object that corresponds to provided
* resource object or null if the object does not exist
*/
public PrismObject<ShadowType> lookupShadowInRepository(ProvisioningContext ctx, PrismObject<ShadowType> resourceShadow, OperationResult parentResult) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
ObjectQuery query = createSearchShadowQuery(ctx, resourceShadow, prismContext, parentResult);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Searching for shadow using filter:\n{}", query.debugDump());
}
// PagingType paging = new PagingType();
// TODO: check for errors
List<PrismObject<ShadowType>> results = repositoryService.searchObjects(ShadowType.class, query, null, parentResult);
MiscSchemaUtil.reduceSearchResult(results);
LOGGER.trace("lookupShadow found {} objects", results.size());
if (results.size() == 0) {
return null;
}
if (results.size() > 1) {
for (PrismObject<ShadowType> result : results) {
LOGGER.trace("Search result:\n{}", result.debugDump());
}
LOGGER.error("More than one shadow found for " + resourceShadow);
// TODO: Better error handling later
throw new IllegalStateException("More than one shadow found for " + resourceShadow);
}
PrismObject<ShadowType> shadow = results.get(0);
checkConsistency(shadow);
if (Boolean.TRUE.equals(shadow.asObjectable().isDead())) {
LOGGER.debug("Repository shadow {} is marked as dead - resetting the flag", ObjectTypeUtil.toShortString(shadow));
shadow.asObjectable().setDead(false);
List<ItemDelta<?, ?>> deltas = DeltaBuilder.deltaFor(ShadowType.class, prismContext).item(ShadowType.F_DEAD).replace().asItemDeltas();
try {
repositoryService.modifyObject(ShadowType.class, shadow.getOid(), deltas, parentResult);
} catch (ObjectAlreadyExistsException e) {
throw new SystemException("Unexpected exception when resetting 'dead' flag: " + e.getMessage(), e);
}
}
return shadow;
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class ShadowManager method findOrAddShadowFromChangeGlobalContext.
public PrismObject<ShadowType> findOrAddShadowFromChangeGlobalContext(ProvisioningContext globalCtx, Change change, OperationResult parentResult) throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ObjectNotFoundException, ExpressionEvaluationException {
// Try to locate existing shadow in the repository
List<PrismObject<ShadowType>> accountList = searchShadowByIdenifiers(globalCtx, change, parentResult);
if (accountList.size() > 1) {
String message = "Found more than one shadow with the identifier " + change.getIdentifiers() + ".";
LOGGER.error(message);
parentResult.recordFatalError(message);
throw new IllegalArgumentException(message);
}
PrismObject<ShadowType> newShadow = null;
if (accountList.isEmpty()) {
if (change.getObjectDelta() == null || change.getObjectDelta().getChangeType() != ChangeType.DELETE) {
newShadow = createNewShadowFromChange(globalCtx, change, parentResult);
try {
ConstraintsChecker.onShadowAddOperation(newShadow.asObjectable());
String oid = repositoryService.addObject(newShadow, null, parentResult);
newShadow.setOid(oid);
if (change.getObjectDelta() != null && change.getObjectDelta().getOid() == null) {
change.getObjectDelta().setOid(oid);
}
} catch (ObjectAlreadyExistsException e) {
parentResult.recordFatalError("Can't add " + SchemaDebugUtil.prettyPrint(newShadow) + " to the repository. Reason: " + e.getMessage(), e);
throw new IllegalStateException(e.getMessage(), e);
}
LOGGER.debug("Added new shadow (from global change): {}", newShadow);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Added new shadow (from global change):\n{}", newShadow.debugDump());
}
}
} else {
// Account was found in repository
newShadow = accountList.get(0);
if (change.getObjectDelta() != null && change.getObjectDelta().getChangeType() == ChangeType.DELETE) {
Collection<? extends ItemDelta> deadDeltas = PropertyDelta.createModificationReplacePropertyCollection(ShadowType.F_DEAD, newShadow.getDefinition(), true);
try {
ConstraintsChecker.onShadowModifyOperation(deadDeltas);
repositoryService.modifyObject(ShadowType.class, newShadow.getOid(), deadDeltas, parentResult);
} catch (ObjectAlreadyExistsException e) {
parentResult.recordFatalError("Can't add " + SchemaDebugUtil.prettyPrint(newShadow) + " to the repository. Reason: " + e.getMessage(), e);
throw new IllegalStateException(e.getMessage(), e);
} catch (ObjectNotFoundException e) {
parentResult.recordWarning("Shadow " + SchemaDebugUtil.prettyPrint(newShadow) + " was probably deleted from the repository in the meantime. Exception: " + e.getMessage(), e);
return null;
}
}
}
return newShadow;
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class ShadowManager method updateShadow.
@SuppressWarnings("unchecked")
public Collection<ItemDelta> updateShadow(ProvisioningContext ctx, PrismObject<ShadowType> resourceShadow, Collection<? extends ItemDelta> aprioriDeltas, OperationResult result) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
PrismObject<ShadowType> repoShadow = repositoryService.getObject(ShadowType.class, resourceShadow.getOid(), null, result);
RefinedObjectClassDefinition objectClassDefinition = ctx.getObjectClassDefinition();
Collection<ItemDelta> repoShadowChanges = new ArrayList<ItemDelta>();
CachingStategyType cachingStrategy = ProvisioningUtil.getCachingStrategy(ctx);
for (RefinedAttributeDefinition attrDef : objectClassDefinition.getAttributeDefinitions()) {
if (ProvisioningUtil.shouldStoreAtributeInShadow(objectClassDefinition, attrDef.getName(), cachingStrategy)) {
ResourceAttribute<Object> resourceAttr = ShadowUtil.getAttribute(resourceShadow, attrDef.getName());
PrismProperty<Object> repoAttr = repoShadow.findProperty(new ItemPath(ShadowType.F_ATTRIBUTES, attrDef.getName()));
PropertyDelta attrDelta;
if (repoAttr == null && repoAttr == null) {
continue;
}
if (repoAttr == null) {
attrDelta = attrDef.createEmptyDelta(new ItemPath(ShadowType.F_ATTRIBUTES, attrDef.getName()));
attrDelta.setValuesToReplace(PrismValue.cloneCollection(resourceAttr.getValues()));
} else {
attrDelta = repoAttr.diff(resourceAttr);
// LOGGER.trace("DIFF:\n{}\n-\n{}\n=:\n{}", repoAttr==null?null:repoAttr.debugDump(1), resourceAttr==null?null:resourceAttr.debugDump(1), attrDelta==null?null:attrDelta.debugDump(1));
}
if (attrDelta != null && !attrDelta.isEmpty()) {
normalizeDelta(attrDelta, attrDef);
repoShadowChanges.add(attrDelta);
}
}
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Updating repo shadow {}:\n{}", resourceShadow.getOid(), DebugUtil.debugDump(repoShadowChanges));
}
try {
repositoryService.modifyObject(ShadowType.class, resourceShadow.getOid(), repoShadowChanges, result);
} catch (ObjectAlreadyExistsException e) {
// We are not renaming the object here. This should not happen.
throw new SystemException(e.getMessage(), e);
}
return repoShadowChanges;
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class ShadowManager method findOrAddShadowFromChange.
// beware, may return null if an shadow that was to be marked as DEAD, was deleted in the meantime
public PrismObject<ShadowType> findOrAddShadowFromChange(ProvisioningContext ctx, Change change, OperationResult parentResult) throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ObjectNotFoundException, ExpressionEvaluationException {
// Try to locate existing shadow in the repository
List<PrismObject<ShadowType>> accountList = searchShadowByIdenifiers(ctx, change, parentResult);
if (accountList.size() > 1) {
String message = "Found more than one shadow with the identifier " + change.getIdentifiers() + ".";
LOGGER.error(message);
parentResult.recordFatalError(message);
throw new IllegalArgumentException(message);
}
PrismObject<ShadowType> newShadow = null;
if (accountList.isEmpty()) {
if (change.getObjectDelta() == null || change.getObjectDelta().getChangeType() != ChangeType.DELETE) {
newShadow = createNewShadowFromChange(ctx, change, parentResult);
try {
ConstraintsChecker.onShadowAddOperation(newShadow.asObjectable());
String oid = repositoryService.addObject(newShadow, null, parentResult);
newShadow.setOid(oid);
if (change.getObjectDelta() != null && change.getObjectDelta().getOid() == null) {
change.getObjectDelta().setOid(oid);
}
} catch (ObjectAlreadyExistsException e) {
parentResult.recordFatalError("Can't add " + SchemaDebugUtil.prettyPrint(newShadow) + " to the repository. Reason: " + e.getMessage(), e);
throw new IllegalStateException(e.getMessage(), e);
}
LOGGER.debug("Added new shadow (from change): {}", newShadow);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Added new shadow (from change):\n{}", newShadow.debugDump());
}
}
} else {
// Account was found in repository
newShadow = accountList.get(0);
if (change.getObjectDelta() != null && change.getObjectDelta().getChangeType() == ChangeType.DELETE) {
Collection<? extends ItemDelta> deadDeltas = PropertyDelta.createModificationReplacePropertyCollection(ShadowType.F_DEAD, newShadow.getDefinition(), true);
try {
ConstraintsChecker.onShadowModifyOperation(deadDeltas);
repositoryService.modifyObject(ShadowType.class, newShadow.getOid(), deadDeltas, parentResult);
} catch (ObjectAlreadyExistsException e) {
parentResult.recordFatalError("Can't add " + SchemaDebugUtil.prettyPrint(newShadow) + " to the repository. Reason: " + e.getMessage(), e);
throw new IllegalStateException(e.getMessage(), e);
} catch (ObjectNotFoundException e) {
parentResult.recordWarning("Shadow " + SchemaDebugUtil.prettyPrint(newShadow) + " was probably deleted from the repository in the meantime. Exception: " + e.getMessage(), e);
return null;
}
}
}
return newShadow;
}
Aggregations