Search in sources :

Example 6 with DBExecutor

use of com.helger.db.jdbc.executor.DBExecutor in project phoss-smp by phax.

the class SMPSettingsManagerJDBC method setSettingsValues.

@Nonnull
public ESuccess setSettingsValues(@Nonnull @Nonempty final Map<String, String> aEntries) {
    ValueEnforcer.notEmpty(aEntries, "Entries");
    final DBExecutor aExecutor = newExecutor();
    return aExecutor.performInTransaction(() -> {
        for (final Map.Entry<String, String> aEntry : aEntries.entrySet()) {
            final String sKey = aEntry.getKey();
            final String sValue = aEntry.getValue();
            setSettingsValue(aExecutor, sKey, sValue);
        }
    });
}
Also used : DBExecutor(com.helger.db.jdbc.executor.DBExecutor) StringMap(com.helger.commons.collection.attr.StringMap) Map(java.util.Map) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IStringMap(com.helger.commons.collection.attr.IStringMap) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) Nonnull(javax.annotation.Nonnull)

Example 7 with DBExecutor

use of com.helger.db.jdbc.executor.DBExecutor in project phoss-smp by phax.

the class SMPUserManagerJDBC method updateOwnershipsAndKillUsers.

public void updateOwnershipsAndKillUsers(@Nonnull final ICommonsMap<String, String> aOldToNewUserNameMap) {
    ValueEnforcer.notNull(aOldToNewUserNameMap, "OldToNewUserNameMap");
    final DBExecutor aExecutor = newExecutor();
    aExecutor.performInTransaction(() -> {
        // Drop the Foreign Key Constraint - do this all the time
        try {
            final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
            switch(eDBType) {
                case MYSQL:
                    aExecutor.executeStatement("ALTER TABLE smp_ownership DROP FOREIGN KEY FK_smp_ownership_username;");
                    break;
                case POSTGRESQL:
                    aExecutor.executeStatement("ALTER TABLE smp_ownership DROP CONSTRAINT FK_smp_ownership_username;");
                    break;
                case ORACLE:
                case DB2:
                    // No such constraint
                    break;
                default:
                    throw new IllegalStateException("The migration code for DB type " + eDBType + " is missing");
            }
        } catch (final RuntimeException ex) {
            LOGGER.warn("Error in droping constraints", ex);
        }
        // Update user names
        for (final Map.Entry<String, String> aEntry : aOldToNewUserNameMap.entrySet()) {
            final String sOld = aEntry.getKey();
            final String sNew = aEntry.getValue();
            aExecutor.insertOrUpdateOrDelete("UPDATE smp_ownership SET username=? WHERE username=?", new ConstantPreparedStatementDataProvider(sNew, sOld));
        }
        try {
            aExecutor.executeStatement("DROP TABLE smp_user;");
        } catch (final RuntimeException ex) {
            LOGGER.warn("Error in droping table smp_user", ex);
        }
    });
}
Also used : DBExecutor(com.helger.db.jdbc.executor.DBExecutor) EDatabaseType(com.helger.phoss.smp.backend.sql.EDatabaseType) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) Map(java.util.Map) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap)

Example 8 with DBExecutor

use of com.helger.db.jdbc.executor.DBExecutor 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 9 with DBExecutor

use of com.helger.db.jdbc.executor.DBExecutor 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 10 with DBExecutor

use of com.helger.db.jdbc.executor.DBExecutor 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

DBExecutor (com.helger.db.jdbc.executor.DBExecutor)16 ConstantPreparedStatementDataProvider (com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider)14 ESuccess (com.helger.commons.state.ESuccess)13 Nonnull (javax.annotation.Nonnull)10 MutableBoolean (com.helger.commons.mutable.MutableBoolean)7 Wrapper (com.helger.commons.wrapper.Wrapper)6 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)6 Nullable (javax.annotation.Nullable)5 EChange (com.helger.commons.state.EChange)4 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)4 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)4 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)3 MutableLong (com.helger.commons.mutable.MutableLong)3 IProcessIdentifier (com.helger.peppolid.IProcessIdentifier)3 SMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup)3 Map (java.util.Map)3 ValueEnforcer (com.helger.commons.ValueEnforcer)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)2 ReturnsMutableObject (com.helger.commons.annotation.ReturnsMutableObject)2 CallbackList (com.helger.commons.callback.CallbackList)2