Search in sources :

Example 21 with SMPNotFoundException

use of com.helger.phoss.smp.exception.SMPNotFoundException in project phoss-smp by phax.

the class SMPServiceGroupManagerJDBC method deleteSMPServiceGroup.

@Nonnull
public EChange deleteSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, final boolean bDeleteInSML) throws SMPServerException {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup (" + aParticipantID.getURIEncoded() + ")");
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    final MutableBoolean aDeletedServiceGroupInSML = new MutableBoolean(false);
    final Wrapper<EChange> aWrappedChange = new Wrapper<>(EChange.UNCHANGED);
    final Wrapper<Exception> aCaughtException = new Wrapper<>();
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        // Check if the passed service group ID is already in use
        final SMPServiceGroup aDBServiceGroup = getSMPServiceGroupOfID(aParticipantID);
        if (aDBServiceGroup == null)
            throw new SMPNotFoundException("The service group with ID " + aParticipantID.getURIEncoded() + " does not exist!");
        if (bDeleteInSML) {
            // Delete in SML - and remember that
            // throws exception in case of error
            aHook.deleteServiceGroup(aParticipantID);
            aDeletedServiceGroupInSML.set(true);
        }
        final long nCount = aExecutor.insertOrUpdateOrDelete("DELETE FROM smp_service_group" + " WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue()));
        if (nCount != 1)
            throw new IllegalStateException("Failed to delete service group");
        aWrappedChange.set(EChange.CHANGED);
    }, aCaughtException::set);
    if (eSuccess.isFailure()) {
        // Error writing to the DB
        if (bDeleteInSML && aDeletedServiceGroupInSML.booleanValue()) {
            // Undo deletion in SML!
            try {
                aHook.undoDeleteServiceGroup(aParticipantID);
            } catch (final RegistrationHookException ex) {
                LOGGER.error("Failed to undoDeleteServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex);
            }
        }
    }
    if (aCaughtException.isSet()) {
        AuditHelper.onAuditDeleteFailure(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), "database-error");
        final Exception ex = aCaughtException.get();
        if (ex instanceof SMPServerException)
            throw (SMPServerException) ex;
        if (ex instanceof RegistrationHookException)
            throw new SMPSMLException("Failed to delete '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        throw new SMPInternalErrorException("Failed to delete ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
    }
    final EChange eChange = aWrappedChange.get();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup succeeded. Change=" + eChange.isChanged());
    if (eChange.isChanged()) {
        AuditHelper.onAuditDeleteSuccess(SMPServiceGroup.OT, aParticipantID.getURIEncoded());
        if (m_aCache != null)
            m_aCache.remove(aParticipantID.getURIEncoded());
        m_aCBs.forEach(x -> x.onSMPServiceGroupDeleted(aParticipantID, bDeleteInSML));
    }
    return eChange;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) Wrapper(com.helger.commons.wrapper.Wrapper) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) EChange(com.helger.commons.state.EChange) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonnull(javax.annotation.Nonnull)

Example 22 with SMPNotFoundException

use of com.helger.phoss.smp.exception.SMPNotFoundException in project phoss-smp by phax.

the class SMPServiceGroupManagerXML method deleteSMPServiceGroup.

@Nonnull
public EChange deleteSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, final boolean bDeleteInSML) throws SMPServerException {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup (" + aParticipantID.getURIEncoded() + ", " + bDeleteInSML + ")");
    final String sServiceGroupID = SMPServiceGroup.createSMPServiceGroupID(aParticipantID);
    final SMPServiceGroup aSMPServiceGroup = getOfID(sServiceGroupID);
    if (aSMPServiceGroup == null) {
        AuditHelper.onAuditDeleteFailure(SMPServiceGroup.OT, aParticipantID, "no-such-id");
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("deleteSMPServiceGroup - failure");
        throw new SMPNotFoundException("No such service group '" + aParticipantID.getURIEncoded() + "'");
    }
    // Delete in SML - throws exception in case of error
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    if (bDeleteInSML)
        try {
            aHook.deleteServiceGroup(aParticipantID);
        } catch (final RegistrationHookException ex) {
            throw new SMPSMLException("Failed to delete '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        }
    final ISMPRedirectManager aRedirectMgr = SMPMetaManager.getRedirectMgr();
    final ISMPServiceInformationManager aServiceInfoMgr = SMPMetaManager.getServiceInformationMgr();
    ICommonsList<ISMPRedirect> aOldRedirects = null;
    ICommonsList<ISMPServiceInformation> aOldServiceInformation = null;
    m_aRWLock.writeLock().lock();
    try {
        if (internalDeleteItem(aSMPServiceGroup.getID()) == null) {
            AuditHelper.onAuditDeleteFailure(SMPServiceGroup.OT, aSMPServiceGroup.getID(), "no-such-id");
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("deleteSMPServiceGroup - failure");
            // Restore in SML again
            if (bDeleteInSML) {
                try {
                    aHook.undoDeleteServiceGroup(aParticipantID);
                } catch (final RegistrationHookException ex2) {
                    LOGGER.error("Failed to undoDeleteServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex2);
                }
            }
            return EChange.UNCHANGED;
        }
        // Remember all redirects (in case of an error) and delete them
        aOldRedirects = aRedirectMgr.getAllSMPRedirectsOfServiceGroup(aSMPServiceGroup);
        aRedirectMgr.deleteAllSMPRedirectsOfServiceGroup(aSMPServiceGroup);
        // Remember all service information (in case of an error) and delete them
        aOldServiceInformation = aServiceInfoMgr.getAllSMPServiceInformationOfServiceGroup(aSMPServiceGroup);
        aServiceInfoMgr.deleteAllSMPServiceInformationOfServiceGroup(aSMPServiceGroup);
    } catch (final RuntimeException ex) {
        // Try to rollback the actions
        if (!containsWithID(aSMPServiceGroup.getID()))
            internalCreateItem(aSMPServiceGroup);
        // Restore redirects (if any)
        if (aOldRedirects != null)
            for (final ISMPRedirect aOldRedirect : aOldRedirects) {
                // ignore return value - we cannot do anything anyway
                aRedirectMgr.createOrUpdateSMPRedirect(aSMPServiceGroup, aOldRedirect.getDocumentTypeIdentifier(), aOldRedirect.getTargetHref(), aOldRedirect.getSubjectUniqueIdentifier(), aOldRedirect.getCertificate(), aOldRedirect.getExtensionsAsString());
            }
        // Restore service information (if any)
        if (aOldServiceInformation != null)
            for (final ISMPServiceInformation aOldServiceInfo : aOldServiceInformation) {
                // ignore return value - we cannot do anything anyway
                aServiceInfoMgr.mergeSMPServiceInformation(aOldServiceInfo);
            }
        // An error occurred - restore in SML again
        try {
            aHook.undoDeleteServiceGroup(aParticipantID);
        } catch (final RegistrationHookException ex2) {
            LOGGER.error("Failed to undoDeleteServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex2);
        }
        throw ex;
    } finally {
        m_aRWLock.writeLock().unlock();
    }
    AuditHelper.onAuditDeleteSuccess(SMPServiceGroup.OT, aSMPServiceGroup.getID(), Boolean.valueOf(bDeleteInSML));
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup - success");
    m_aCBs.forEach(x -> x.onSMPServiceGroupDeleted(aParticipantID, bDeleteInSML));
    return EChange.CHANGED;
}
Also used : RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) ISMPServiceInformationManager(com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager) ISMPServiceInformation(com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) ISMPRedirectManager(com.helger.phoss.smp.domain.redirect.ISMPRedirectManager) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) ISMPRedirect(com.helger.phoss.smp.domain.redirect.ISMPRedirect) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) Nonnull(javax.annotation.Nonnull)

Aggregations

SMPNotFoundException (com.helger.phoss.smp.exception.SMPNotFoundException)22 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)21 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)17 SMPServerException (com.helger.phoss.smp.exception.SMPServerException)17 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)16 Nonnull (javax.annotation.Nonnull)15 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)14 ISMPServiceInformationManager (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager)14 ISMPServiceInformation (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation)11 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)10 ISMPRedirectManager (com.helger.phoss.smp.domain.redirect.ISMPRedirectManager)10 IUser (com.helger.photon.security.user.IUser)8 EChange (com.helger.commons.state.EChange)7 ISMPRedirect (com.helger.phoss.smp.domain.redirect.ISMPRedirect)5 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)5 SMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup)4 SMPSMLException (com.helger.phoss.smp.exception.SMPSMLException)4 IRegistrationHook (com.helger.phoss.smp.smlhook.IRegistrationHook)4 RegistrationHookException (com.helger.phoss.smp.smlhook.RegistrationHookException)4 ICommonsList (com.helger.commons.collection.impl.ICommonsList)3