Search in sources :

Example 16 with ConstantPreparedStatementDataProvider

use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.

the class SMPRedirectManagerJDBC method deleteAllSMPRedirectsOfServiceGroup.

@Nonnull
public EChange deleteAllSMPRedirectsOfServiceGroup(@Nullable final ISMPServiceGroup aServiceGroup) {
    if (aServiceGroup == null)
        return EChange.UNCHANGED;
    // Remember all existing
    final ICommonsList<ISMPRedirect> aDeletedRedirects = getAllSMPRedirectsOfServiceGroup(aServiceGroup);
    // Now delete
    final IParticipantIdentifier aParticipantID = aServiceGroup.getParticipantIdentifier();
    final long nDeleted = newExecutor().insertOrUpdateOrDelete("DELETE FROM smp_service_metadata_red" + " WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue()));
    if (nDeleted == 0) {
        return EChange.UNCHANGED;
    }
    // Callback only, if all were deleted
    if (nDeleted == aDeletedRedirects.size()) {
        for (final ISMPRedirect aSMPRedirect : aDeletedRedirects) {
            AuditHelper.onAuditDeleteSuccess(SMPRedirect.OT, aSMPRedirect.getID(), aSMPRedirect.getServiceGroupID(), aSMPRedirect.getDocumentTypeIdentifier().getURIEncoded());
            m_aCallbacks.forEach(x -> x.onSMPRedirectDeleted(aSMPRedirect));
        }
    } else {
        LOGGER.warn(nDeleted + " SMP redirects were deleted, but " + aDeletedRedirects.size() + " were found previously. Because of this inconsistency, no callbacks are triggered");
    }
    return EChange.CHANGED;
}
Also used : ISMPRedirect(com.helger.phoss.smp.domain.redirect.ISMPRedirect) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Example 17 with ConstantPreparedStatementDataProvider

use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.

the class SMPRedirectManagerJDBC method createOrUpdateSMPRedirect.

@Nullable
public ISMPRedirect createOrUpdateSMPRedirect(@Nonnull final ISMPServiceGroup aServiceGroup, @Nonnull final IDocumentTypeIdentifier aDocTypeID, @Nonnull @Nonempty final String sRedirectUrl, @Nonnull @Nonempty final String sSubjectUniqueIdentifier, @Nullable final X509Certificate aCertificate, @Nullable final String sExtension) {
    ValueEnforcer.notNull(aServiceGroup, "ServiceGroup");
    ValueEnforcer.notNull(aDocTypeID, "DocumentTypeIdentifier");
    final MutableBoolean aCreatedNew = new MutableBoolean(true);
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        final ISMPRedirect aDBRedirect = getSMPRedirectOfServiceGroupAndDocumentType(aServiceGroup, aDocTypeID);
        final IParticipantIdentifier aParticipantID = aServiceGroup.getParticipantIdentifier();
        final String sCertificate = aCertificate == null ? null : CertificateHelper.getPEMEncodedCertificate(aCertificate);
        if (aDBRedirect == null) {
            // Create new
            final long nCreated = aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_service_metadata_red (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, redirectionUrl, certificateUID, certificate, extension) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), sRedirectUrl, sSubjectUniqueIdentifier, sCertificate, sExtension));
            if (nCreated != 1)
                throw new IllegalStateException("Failed to create new DB entry (" + nCreated + ")");
            aCreatedNew.set(true);
        } else {
            // Update existing
            final long nUpdated = aExecutor.insertOrUpdateOrDelete("UPDATE smp_service_metadata_red" + " SET redirectionUrl=?, certificateUID=?, certificate=?, extension=?" + " WHERE businessIdentifierScheme=? AND businessIdentifier=? AND documentIdentifierScheme=? AND documentIdentifier=?", new ConstantPreparedStatementDataProvider(sRedirectUrl, sSubjectUniqueIdentifier, sCertificate, sExtension, aParticipantID.getScheme(), aParticipantID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue()));
            if (nUpdated != 1)
                throw new IllegalStateException("Failed to update existing DB entry (" + nUpdated + ")");
            aCreatedNew.set(false);
        }
    });
    if (eSuccess.isFailure()) {
        return null;
    }
    final SMPRedirect aSMPRedirect = new SMPRedirect(aServiceGroup, aDocTypeID, sRedirectUrl, sSubjectUniqueIdentifier, aCertificate, sExtension);
    if (aCreatedNew.booleanValue()) {
        AuditHelper.onAuditCreateSuccess(SMPRedirect.OT, aSMPRedirect.getID(), aSMPRedirect.getServiceGroupID(), aSMPRedirect.getDocumentTypeIdentifier().getURIEncoded(), aSMPRedirect.getTargetHref(), aSMPRedirect.getSubjectUniqueIdentifier(), aSMPRedirect.getCertificate(), aSMPRedirect.getExtensionsAsString());
        m_aCallbacks.forEach(x -> x.onSMPRedirectCreated(aSMPRedirect));
    } else {
        AuditHelper.onAuditModifySuccess(SMPRedirect.OT, "set-all", aSMPRedirect.getID(), aSMPRedirect.getServiceGroupID(), aSMPRedirect.getDocumentTypeIdentifier().getURIEncoded(), aSMPRedirect.getTargetHref(), aSMPRedirect.getSubjectUniqueIdentifier(), aSMPRedirect.getCertificate(), aSMPRedirect.getExtensionsAsString());
        m_aCallbacks.forEach(x -> x.onSMPRedirectUpdated(aSMPRedirect));
    }
    return aSMPRedirect;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) SMPRedirect(com.helger.phoss.smp.domain.redirect.SMPRedirect) ISMPRedirect(com.helger.phoss.smp.domain.redirect.ISMPRedirect) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ISMPRedirect(com.helger.phoss.smp.domain.redirect.ISMPRedirect) MutableBoolean(com.helger.commons.mutable.MutableBoolean) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nullable(javax.annotation.Nullable)

Example 18 with ConstantPreparedStatementDataProvider

use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.

the class SMPServiceGroupManagerJDBC method getSMPServiceGroupOfID.

@Nullable
public SMPServiceGroup getSMPServiceGroupOfID(@Nullable final IParticipantIdentifier aParticipantID) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("getSMPServiceGroupOfID(" + (aParticipantID == null ? "null" : aParticipantID.getURIEncoded()) + ")");
    if (aParticipantID == null)
        return null;
    // Use cache
    SMPServiceGroup ret = m_aCache == null ? null : m_aCache.get(aParticipantID.getURIEncoded());
    if (ret != null)
        return ret;
    // Not in cache
    final Wrapper<DBResultRow> aResult = new Wrapper<>();
    newExecutor().querySingle("SELECT sg.extension, so.username" + " FROM smp_service_group sg, smp_ownership so" + " WHERE sg.businessIdentifierScheme=? AND sg.businessIdentifier=?" + " AND so.businessIdentifierScheme=sg.businessIdentifierScheme AND so.businessIdentifier=sg.businessIdentifier", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue()), aResult::set);
    if (aResult.isNotSet())
        return null;
    ret = new SMPServiceGroup(aResult.get().getAsString(1), aParticipantID, aResult.get().getAsString(0));
    if (m_aCache != null)
        m_aCache.put(aParticipantID.getURIEncoded(), ret);
    return ret;
}
Also used : Wrapper(com.helger.commons.wrapper.Wrapper) DBResultRow(com.helger.db.jdbc.executor.DBResultRow) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) Nullable(javax.annotation.Nullable)

Example 19 with ConstantPreparedStatementDataProvider

use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.

the class SMPServiceGroupManagerJDBC method updateSMPServiceGroup.

@Nonnull
public EChange updateSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, @Nonnull @Nonempty final String sNewOwnerID, @Nullable final String sNewExtension) throws SMPServerException {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    ValueEnforcer.notEmpty(sNewOwnerID, "NewOwnerID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("updateSMPServiceGroup (" + aParticipantID.getURIEncoded() + ", " + sNewOwnerID + ", " + (StringHelper.hasText(sNewExtension) ? "with extension" : "without extension") + ")");
    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 (!EqualsHelper.equals(sNewOwnerID, aDBServiceGroup.getOwnerID())) {
            // Update ownership
            final long nCount = aExecutor.insertOrUpdateOrDelete("UPDATE smp_ownership SET username=? WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(sNewOwnerID, aDBServiceGroup.getParticipantIdentifier().getScheme(), aDBServiceGroup.getParticipantIdentifier().getValue()));
            if (nCount != 1)
                throw new IllegalStateException("Failed to update the ownership username to '" + sNewOwnerID + "'");
            aWrappedChange.set(EChange.CHANGED);
        }
        if (!EqualsHelper.equals(sNewExtension, aDBServiceGroup.getExtensionsAsString())) {
            // Update extension
            final long nCount = aExecutor.insertOrUpdateOrDelete("UPDATE smp_service_group SET extension=? WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(sNewExtension, aDBServiceGroup.getParticipantIdentifier().getScheme(), aDBServiceGroup.getParticipantIdentifier().getValue()));
            if (nCount != 1)
                throw new IllegalStateException("Failed to update the service_group extension to '" + sNewExtension + "'");
            aWrappedChange.set(EChange.CHANGED);
        }
    }, aCaughtException::set);
    if (eSuccess.isFailure() || aCaughtException.isSet()) {
        AuditHelper.onAuditModifyFailure(SMPServiceGroup.OT, "set-all", aParticipantID.getURIEncoded(), sNewOwnerID, sNewExtension);
        final Exception ex = aCaughtException.get();
        if (ex instanceof SMPServerException)
            throw (SMPServerException) ex;
        throw new SMPInternalErrorException("Failed to update ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
    }
    final EChange eChange = aWrappedChange.get();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("updateSMPServiceGroup succeeded. Change=" + eChange.isChanged());
    AuditHelper.onAuditModifySuccess(SMPServiceGroup.OT, "set-all", aParticipantID.getURIEncoded(), sNewOwnerID, sNewExtension);
    // Callback only if something changed
    if (eChange.isChanged())
        m_aCBs.forEach(x -> x.onSMPServiceGroupUpdated(aParticipantID));
    return eChange;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) ESuccess(com.helger.commons.state.ESuccess) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) Nonnegative(javax.annotation.Nonnegative) ICommonsSet(com.helger.commons.collection.impl.ICommonsSet) CIdentifier(com.helger.peppolid.CIdentifier) LoggerFactory(org.slf4j.LoggerFactory) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) EChange(com.helger.commons.state.EChange) CheckForSigned(javax.annotation.CheckForSigned) Supplier(java.util.function.Supplier) CallbackList(com.helger.commons.callback.CallbackList) AbstractJDBCEnabledManager(com.helger.db.jdbc.mgr.AbstractJDBCEnabledManager) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonempty(com.helger.commons.annotation.Nonempty) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) RegistrationHookFactory(com.helger.phoss.smp.smlhook.RegistrationHookFactory) Logger(org.slf4j.Logger) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) StringHelper(com.helger.commons.string.StringHelper) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) ISMPServiceGroupCallback(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupCallback) ExpiringMap(net.jodah.expiringmap.ExpiringMap) EqualsHelper(com.helger.commons.equals.EqualsHelper) ValueEnforcer(com.helger.commons.ValueEnforcer) DBResultRow(com.helger.db.jdbc.executor.DBResultRow) TimeUnit(java.util.concurrent.TimeUnit) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) AuditHelper(com.helger.photon.audit.AuditHelper) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Wrapper(com.helger.commons.wrapper.Wrapper) ReturnsMutableObject(com.helger.commons.annotation.ReturnsMutableObject) ExpirationPolicy(net.jodah.expiringmap.ExpirationPolicy) ISMPServiceGroupManager(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager) Wrapper(com.helger.commons.wrapper.Wrapper) 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) 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 20 with ConstantPreparedStatementDataProvider

use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider 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)

Aggregations

ConstantPreparedStatementDataProvider (com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider)33 Nonnull (javax.annotation.Nonnull)20 DBExecutor (com.helger.db.jdbc.executor.DBExecutor)16 ESuccess (com.helger.commons.state.ESuccess)15 Nullable (javax.annotation.Nullable)15 Wrapper (com.helger.commons.wrapper.Wrapper)14 DBResultRow (com.helger.db.jdbc.executor.DBResultRow)14 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)14 MutableBoolean (com.helger.commons.mutable.MutableBoolean)9 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)8 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)8 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)8 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)7 EChange (com.helger.commons.state.EChange)6 ISMPTransportProfile (com.helger.peppol.smp.ISMPTransportProfile)5 IProcessIdentifier (com.helger.peppolid.IProcessIdentifier)5 SimpleDocumentTypeIdentifier (com.helger.peppolid.simple.doctype.SimpleDocumentTypeIdentifier)5 SimpleParticipantIdentifier (com.helger.peppolid.simple.participant.SimpleParticipantIdentifier)5 ValueEnforcer (com.helger.commons.ValueEnforcer)4 ReturnsMutableObject (com.helger.commons.annotation.ReturnsMutableObject)4