package com.evolveum.midpoint.provisioning.consistency.impl;

import java.util.Collection;

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

import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.provisioning.consistency.api.ErrorHandler;
import com.evolveum.midpoint.provisioning.consistency.api.ErrorHandler.FailedOperation;
import com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
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.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ResourceObjectShadowType;

@Component
public class SchemaExceptionHandler extends ErrorHandler{

	@Autowired
	@Qualifier("cacheRepositoryService")
	private RepositoryService cacheRepositoryService;
	
	@Override
	public <T extends ResourceObjectShadowType> T handleError(T shadow, FailedOperation op, Exception ex, OperationResult parentResult) throws SchemaException,
			GenericFrameworkException, CommunicationException, ObjectNotFoundException,
			ObjectAlreadyExistsException, ConfigurationException, SecurityViolationException {
		
		if (shadow.getOid() == null){
			throw new SchemaException("Schema violation during processing shadow: "+ ObjectTypeUtil.toShortString(shadow)+": "+ex.getMessage(), ex);
		}
		
		Collection<ItemDelta> modification = createAttemptModification(shadow, null);
		cacheRepositoryService.modifyObject(shadow.asPrismObject().getCompileTimeClass(), shadow.getOid(), modification, parentResult);
		
		throw new SchemaException("Schema violation during processing shadow: "+ ObjectTypeUtil.toShortString(shadow)+": "+ex.getMessage(), ex);
	}	

}

@@ -2034,16 +2034,30 @@
 ll);%0D%0A%09%09
+%0D%0A%09%09try %7B%0D%0A%09%09%09
 cacheRep
@@ -2146,17 +2146,23 @@
 etOid(),
- 
+%0D%0A%09%09%09%09%09
 modifica
@@ -2179,24 +2179,297 @@
 ntResult);%0D%0A
+%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09//this should not happen. But if it happens, we should return original exception%0D%0A//%09%09%09throw new SchemaException(%22Schema violation during processing shadow: %22%0D%0A//%09%09%09%09%09+ ObjectTypeUtil.toShortString(shadow) + %22: %22 + ex.getMessage(), ex);%0D%0A%09%09%7D%0D%0A
 %09%09%0D%0A%09%09throw 

fix for provisioning tests..