Search in sources :

Example 11 with DBExecutor

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

the class SMPServiceInformationManagerJDBC method mergeSMPServiceInformation.

@Nonnull
public ESuccess mergeSMPServiceInformation(@Nonnull final ISMPServiceInformation aSMPServiceInformation) {
    ValueEnforcer.notNull(aSMPServiceInformation, "ServiceInformation");
    final MutableBoolean aUpdated = new MutableBoolean(false);
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        // Simply delete the old one
        final EChange eDeleted = _deleteSMPServiceInformationNoCallback(aSMPServiceInformation);
        aUpdated.set(eDeleted.isChanged());
        // Insert new processes
        final IParticipantIdentifier aPID = aSMPServiceInformation.getServiceGroup().getParticipantIdentifier();
        final IDocumentTypeIdentifier aDocTypeID = aSMPServiceInformation.getDocumentTypeIdentifier();
        aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_service_metadata (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, extension) VALUES (?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aPID.getScheme(), aPID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), aSMPServiceInformation.getExtensionsAsString()));
        for (final ISMPProcess aProcess : aSMPServiceInformation.getAllProcesses()) {
            final IProcessIdentifier aProcessID = aProcess.getProcessIdentifier();
            aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_process (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, processIdentifierType, processIdentifier, extension) VALUES (?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aPID.getScheme(), aPID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), aProcessID.getScheme(), aProcessID.getValue(), aProcess.getExtensionsAsString()));
            // Insert new endpoints
            for (final ISMPEndpoint aEndpoint : aProcess.getAllEndpoints()) {
                aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_endpoint (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, processIdentifierType, processIdentifier," + " certificate, endpointReference, minimumAuthenticationLevel, requireBusinessLevelSignature, serviceActivationDate, serviceDescription, serviceExpirationDate, technicalContactUrl, technicalInformationUrl, transportProfile," + " extension) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aPID.getScheme(), aPID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), aProcessID.getScheme(), aProcessID.getValue(), aEndpoint.getCertificate(), aEndpoint.getEndpointReference(), aEndpoint.getMinimumAuthenticationLevel(), Boolean.valueOf(aEndpoint.isRequireBusinessLevelSignature()), DBValueHelper.toTimestamp(aEndpoint.getServiceActivationDateTime()), aEndpoint.getServiceDescription(), DBValueHelper.toTimestamp(aEndpoint.getServiceExpirationDateTime()), aEndpoint.getTechnicalContactUrl(), aEndpoint.getTechnicalInformationUrl(), aEndpoint.getTransportProfile(), aEndpoint.getExtensionsAsString()));
            }
        }
    });
    if (eSuccess.isFailure())
        return ESuccess.FAILURE;
    // Callback outside of transaction
    if (aUpdated.booleanValue()) {
        AuditHelper.onAuditModifySuccess(SMPServiceInformation.OT, "set-all", aSMPServiceInformation.getID(), aSMPServiceInformation.getServiceGroupID(), aSMPServiceInformation.getDocumentTypeIdentifier().getURIEncoded(), aSMPServiceInformation.getAllProcesses(), aSMPServiceInformation.getExtensionsAsString());
        m_aCBs.forEach(x -> x.onSMPServiceInformationUpdated(aSMPServiceInformation));
    } else {
        AuditHelper.onAuditCreateSuccess(SMPServiceInformation.OT, aSMPServiceInformation.getID(), aSMPServiceInformation.getServiceGroupID(), aSMPServiceInformation.getDocumentTypeIdentifier().getURIEncoded(), aSMPServiceInformation.getAllProcesses(), aSMPServiceInformation.getExtensionsAsString());
        m_aCBs.forEach(x -> x.onSMPServiceInformationCreated(aSMPServiceInformation));
    }
    return ESuccess.SUCCESS;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) MutableBoolean(com.helger.commons.mutable.MutableBoolean) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) ISMPEndpoint(com.helger.phoss.smp.domain.serviceinfo.ISMPEndpoint) EChange(com.helger.commons.state.EChange) ISMPProcess(com.helger.phoss.smp.domain.serviceinfo.ISMPProcess) IProcessIdentifier(com.helger.peppolid.IProcessIdentifier) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Example 12 with DBExecutor

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

the class SMPBusinessCardManagerJDBC method createOrUpdateSMPBusinessCard.

@Nullable
public ISMPBusinessCard createOrUpdateSMPBusinessCard(@Nonnull final IParticipantIdentifier aParticipantID, @Nonnull final Collection<SMPBusinessCardEntity> aEntities) {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    ValueEnforcer.notNull(aEntities, "Entities");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createOrUpdateSMPBusinessCard (" + aParticipantID.getURIEncoded() + ", " + aEntities.size() + " entities" + ")");
    final MutableBoolean aUpdated = new MutableBoolean(false);
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSucces = aExecutor.performInTransaction(() -> {
        // Delete all existing entities
        final String sPID = aParticipantID.getURIEncoded();
        final long nDeleted = aExecutor.insertOrUpdateOrDelete("DELETE FROM smp_bce" + " WHERE pid=?", new ConstantPreparedStatementDataProvider(sPID));
        if (nDeleted > 0) {
            aUpdated.set(true);
            if (LOGGER.isDebugEnabled())
                LOGGER.info("Deleted " + nDeleted + " existing DBBusinessCardEntity rows");
        }
        for (final SMPBusinessCardEntity aEntity : aEntities) {
            // Single name only
            aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_bce (id, pid, name, country, geoinfo, identifiers, websites, contacts, addon, regdate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aEntity.getID(), sPID, aEntity.names().getFirst().getName(), aEntity.getCountryCode(), aEntity.getGeographicalInformation(), getBCIAsJson(aEntity.identifiers()).getAsJsonString(JWS), getStringAsJson(aEntity.websiteURIs()).getAsJsonString(JWS), getBCCAsJson(aEntity.contacts()).getAsJsonString(JWS), aEntity.getAdditionalInformation(), aEntity.getRegistrationDate()));
        }
    });
    if (eSucces.isFailure()) {
        if (aUpdated.booleanValue())
            AuditHelper.onAuditModifyFailure(SMPBusinessCard.OT, "set-all", aParticipantID.getURIEncoded());
        else
            AuditHelper.onAuditCreateFailure(SMPBusinessCard.OT, aParticipantID.getURIEncoded());
        return null;
    }
    final SMPBusinessCard aNewBusinessCard = new SMPBusinessCard(aParticipantID, aEntities);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Finished createOrUpdateSMPBusinessCard");
    if (aUpdated.booleanValue())
        AuditHelper.onAuditModifySuccess(SMPBusinessCard.OT, "set-all", aParticipantID.getURIEncoded(), Integer.valueOf(aEntities.size()));
    else
        AuditHelper.onAuditCreateSuccess(SMPBusinessCard.OT, aParticipantID.getURIEncoded(), Integer.valueOf(aEntities.size()));
    // Invoke generic callbacks
    m_aCBs.forEach(x -> x.onSMPBusinessCardCreatedOrUpdated(aNewBusinessCard));
    return aNewBusinessCard;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) ISMPBusinessCard(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard) SMPBusinessCard(com.helger.phoss.smp.domain.businesscard.SMPBusinessCard) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPBusinessCardEntity(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardEntity) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) Nullable(javax.annotation.Nullable)

Example 13 with DBExecutor

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

the class SMPIDFactoryJDBC method readAndUpdateIDCounter.

@Override
protected long readAndUpdateIDCounter(@Nonnegative final int nReserveCount) {
    final MutableLong aReadValue = new MutableLong(0);
    final DBExecutor aExecutor = new SMPDBExecutor();
    aExecutor.performInTransaction(() -> {
        // Read existing value
        final String sExistingValue = SMPSettingsManagerJDBC.getSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID);
        final long nRead = StringParser.parseLong(sExistingValue, m_nInitialCount);
        aReadValue.set(nRead);
        // Write new value
        final long nNewValue = nRead + nReserveCount;
        SMPSettingsManagerJDBC.setSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID, Long.toString(nNewValue));
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Updated SQL ID from " + sExistingValue + " to " + nNewValue);
    });
    return aReadValue.longValue();
}
Also used : MutableLong(com.helger.commons.mutable.MutableLong) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Example 14 with DBExecutor

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

the class SMPParticipantMigrationManagerJDBC method _createParticipantMigration.

@Nullable
private ISMPParticipantMigration _createParticipantMigration(@Nonnull final SMPParticipantMigration aSMPParticipantMigration) {
    ValueEnforcer.notNull(aSMPParticipantMigration, "SMPParticipantMigration");
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        // Create new
        final long nCreated = aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_pmigration (id, direction, state, pid, initdt, migkey)" + " VALUES (?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aSMPParticipantMigration.getID(), aSMPParticipantMigration.getDirection().getID(), aSMPParticipantMigration.getState().getID(), aSMPParticipantMigration.getParticipantIdentifier().getURIEncoded(), DBValueHelper.toTimestamp(aSMPParticipantMigration.getInitiationDateTime()), aSMPParticipantMigration.getMigrationKey()));
        if (nCreated != 1)
            throw new IllegalStateException("Failed to create new DB entry (" + nCreated + ")");
    });
    if (eSuccess.isFailure()) {
        AuditHelper.onAuditCreateFailure(SMPParticipantMigration.OT, aSMPParticipantMigration.getID(), "database-error");
        return null;
    }
    AuditHelper.onAuditCreateSuccess(SMPParticipantMigration.OT, aSMPParticipantMigration.getID(), aSMPParticipantMigration.getDirection(), aSMPParticipantMigration.getParticipantIdentifier().getURIEncoded(), aSMPParticipantMigration.getInitiationDateTime(), aSMPParticipantMigration.getMigrationKey());
    return aSMPParticipantMigration;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) Nullable(javax.annotation.Nullable)

Example 15 with DBExecutor

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

the class SMPTransportProfileManagerJDBC method updateSMPTransportProfile.

@Nonnull
public EChange updateSMPTransportProfile(@Nullable final String sSMPTransportProfileID, @Nonnull @Nonempty final String sName, final boolean bIsDeprecated) {
    final MutableLong aUpdated = new MutableLong(-1);
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        // Update existing
        final long nUpdated = aExecutor.insertOrUpdateOrDelete("UPDATE smp_tprofile SET name=?, deprecated=? WHERE id=?", new ConstantPreparedStatementDataProvider(sName, Boolean.valueOf(bIsDeprecated), sSMPTransportProfileID));
        aUpdated.set(nUpdated);
    });
    if (eSuccess.isFailure()) {
        // DB error
        AuditHelper.onAuditModifyFailure(SMPTransportProfile.OT, "update", sSMPTransportProfileID, "database-error");
        return EChange.UNCHANGED;
    }
    if (aUpdated.is0()) {
        // No such transport profile ID
        AuditHelper.onAuditModifyFailure(SMPTransportProfile.OT, "update", sSMPTransportProfileID, "no-such-id");
        return EChange.UNCHANGED;
    }
    AuditHelper.onAuditModifySuccess(SMPTransportProfile.OT, "update", sSMPTransportProfileID, sName, Boolean.valueOf(bIsDeprecated));
    return EChange.CHANGED;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) MutableLong(com.helger.commons.mutable.MutableLong) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) 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