class AssetGroupExceptionServiceImpl implements AssetGroupExceptionService {

	@Override
	public String deleteAssetGroupExceptions(final DeleteAssetGroupExceptionRequest assetGroupExceptionRequest, String userId) throws PacManException {
		try {
			Response exceptionResponse = invokeAPI("GET","/exceptions/sticky_exceptions/" + assetGroupExceptionRequest.getExceptionName().replaceAll(" ", "_")+"/_source", null);
			if(exceptionResponse != null) {
				String exceptionBackUp = EntityUtils.toString(exceptionResponse.getEntity());
				Response response = invokeAPI("DELETE","/exceptions/sticky_exceptions/" + assetGroupExceptionRequest.getExceptionName().replaceAll(" ", "_"), null);
				if(response != null) {
					if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201) {
						try {
							List<AssetGroupException> assetGroupExceptions = assetGroupExceptionRepository.findByGroupNameAndExceptionName(assetGroupExceptionRequest.getGroupName(), assetGroupExceptionRequest.getExceptionName());
							for (AssetGroupException assetGroupException : assetGroupExceptions) {
								assetGroupExceptionRepository.delete(assetGroupException);
							}
						} catch (Exception exception) {
							Response exceptionBackUpResponse = invokeAPI("POST", "/exceptions/sticky_exceptions/"+ assetGroupExceptionRequest.getExceptionName().trim().replaceAll(" ", "_"), exceptionBackUp);
							if(exceptionBackUpResponse != null) {
								if (exceptionBackUpResponse.getStatusLine().getStatusCode() == 200 || exceptionBackUpResponse.getStatusLine().getStatusCode() == 201) {
									throw new PacManException("Exception while updating to table as well as updating ES:"+exception.getMessage());
								}
							}
							throw new PacManException("Exception while updating to table "+exception.getMessage());
						}
					}
				} else {
					return EXCEPTION_DELETEION_FAILURE;
				}
				return EXCEPTION_DELETEION_SUCCESS;
			} else {
				return EXCEPTION_DELETEION_FAILURE;
			}
		} catch (Exception exception) {
			throw new PacManException("Exception while deleteing the exception "+exception.getMessage());
		}
	}

}