Search in sources :

Example 1 with SMPServiceGroup

use of com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup in project phoss-smp by phax.

the class SMPServiceGroupManagerJDBC method createSMPServiceGroup.

@Nonnull
public SMPServiceGroup createSMPServiceGroup(@Nonnull @Nonempty final String sOwnerID, @Nonnull final IParticipantIdentifier aParticipantID, @Nullable final String sExtension, final boolean bCreateInSML) throws SMPServerException {
    ValueEnforcer.notEmpty(sOwnerID, "OwnerID");
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup (" + sOwnerID + ", " + aParticipantID.getURIEncoded() + ", " + (StringHelper.hasText(sExtension) ? "with extension" : "without extension") + ", " + bCreateInSML + ")");
    final MutableBoolean aCreatedSGHook = new MutableBoolean(false);
    final MutableBoolean aCreatedSGDB = new MutableBoolean(false);
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    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 IllegalStateException("The service group with ID " + aParticipantID.getURIEncoded() + " already exists!");
        if (bCreateInSML) {
            // It's a new service group - Create in SML and remember that
            // Throws exception in case of an error
            aHook.createServiceGroup(aParticipantID);
            aCreatedSGHook.set(true);
        }
        // Did not exist. Create it.
        if (aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_service_group (businessIdentifierScheme, businessIdentifier, extension) VALUES (?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), sExtension)) > 0) {
            aCreatedSGDB.set(true);
            aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_ownership (businessIdentifierScheme, businessIdentifier, username) VALUES (?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), sOwnerID));
        }
    }, aCaughtException::set);
    if (aCreatedSGHook.booleanValue() && !aCreatedSGDB.booleanValue()) {
        // Undo creation in SML again
        try {
            aHook.undoCreateServiceGroup(aParticipantID);
        } catch (final RegistrationHookException ex) {
            LOGGER.error("Failed to undoCreateServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex);
        }
    }
    if (eSuccess.isFailure() || aCaughtException.isSet() || !aCreatedSGDB.booleanValue()) {
        AuditHelper.onAuditCreateFailure(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), sOwnerID, sExtension, Boolean.valueOf(bCreateInSML));
        // Propagate contained exception
        final Exception ex = aCaughtException.get();
        if (ex instanceof SMPServerException)
            throw (SMPServerException) ex;
        if (ex instanceof RegistrationHookException)
            throw new SMPSMLException("Failed to create '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        throw new SMPInternalErrorException("Error creating ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
    }
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup succeeded");
    AuditHelper.onAuditCreateSuccess(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), sOwnerID, sExtension, Boolean.valueOf(bCreateInSML));
    final SMPServiceGroup aServiceGroup = new SMPServiceGroup(sOwnerID, aParticipantID, sExtension);
    if (m_aCache != null)
        m_aCache.put(aParticipantID.getURIEncoded(), aServiceGroup);
    m_aCBs.forEach(x -> x.onSMPServiceGroupCreated(aServiceGroup, bCreateInSML));
    return aServiceGroup;
}
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) 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) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonnull(javax.annotation.Nonnull)

Example 2 with SMPServiceGroup

use of com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup in project phoss-smp by phax.

the class SMPServiceGroupManagerJDBC method getAllSMPServiceGroups.

@Nonnull
@ReturnsMutableCopy
public ICommonsList<ISMPServiceGroup> getAllSMPServiceGroups() {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("getAllSMPServiceGroups()");
    final ICommonsList<DBResultRow> aDBResult = newExecutor().queryAll("SELECT sg.businessIdentifierScheme, sg.businessIdentifier, sg.extension, so.username" + " FROM smp_service_group sg, smp_ownership so" + " WHERE so.businessIdentifierScheme=sg.businessIdentifierScheme AND so.businessIdentifier=sg.businessIdentifier");
    final ICommonsList<ISMPServiceGroup> ret = new CommonsArrayList<>();
    if (aDBResult != null)
        for (final DBResultRow aRow : aDBResult) ret.add(new SMPServiceGroup(aRow.getAsString(3), new SimpleParticipantIdentifier(aRow.getAsString(0), aRow.getAsString(1)), aRow.getAsString(2)));
    return ret;
}
Also used : DBResultRow(com.helger.db.jdbc.executor.DBResultRow) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 3 with SMPServiceGroup

use of com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup in project phoss-smp by phax.

the class SMPServiceGroupManagerMongoDB method toDomain.

@Nonnull
@ReturnsMutableCopy
public static SMPServiceGroup toDomain(@Nonnull final Document aDoc) {
    final String sOwnerID = aDoc.getString(BSON_OWNER_ID);
    final IParticipantIdentifier aParticipantIdentifier = toParticipantID(aDoc.get(BSON_PARTICIPANT_ID, Document.class));
    final String sExtension = aDoc.getString(BSON_EXTENSION);
    return new SMPServiceGroup(sOwnerID, aParticipantIdentifier, sExtension);
}
Also used : ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) Document(org.bson.Document) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 4 with SMPServiceGroup

use of com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup in project phoss-smp by phax.

the class SMPServiceGroupManagerXML method updateSMPServiceGroup.

@Nonnull
public EChange updateSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, @Nonnull @Nonempty final String sNewOwnerID, @Nullable final String sExtension) throws SMPServerException {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    ValueEnforcer.notEmpty(sNewOwnerID, "NewOwnerID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("updateSMPServiceGroup (" + aParticipantID.getURIEncoded() + ", " + sNewOwnerID + ", " + (StringHelper.hasText(sExtension) ? "with extension" : "without extension") + ")");
    final String sServiceGroupID = SMPServiceGroup.createSMPServiceGroupID(aParticipantID);
    final SMPServiceGroup aSMPServiceGroup = getOfID(sServiceGroupID);
    if (aSMPServiceGroup == null) {
        AuditHelper.onAuditModifyFailure(SMPServiceGroup.OT, "set-all", sServiceGroupID, "no-such-id");
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("updateSMPServiceGroup - failure");
        throw new SMPNotFoundException("No such service group '" + sServiceGroupID + "'");
    }
    m_aRWLock.writeLock().lock();
    try {
        EChange eChange = EChange.UNCHANGED;
        eChange = eChange.or(aSMPServiceGroup.setOwnerID(sNewOwnerID));
        eChange = eChange.or(aSMPServiceGroup.setExtensionAsString(sExtension));
        if (eChange.isUnchanged()) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("updateSMPServiceGroup - unchanged");
            return EChange.UNCHANGED;
        }
        internalUpdateItem(aSMPServiceGroup);
    } finally {
        m_aRWLock.writeLock().unlock();
    }
    AuditHelper.onAuditModifySuccess(SMPServiceGroup.OT, "set-all", sServiceGroupID, sNewOwnerID, sExtension);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("updateSMPServiceGroup - success");
    m_aCBs.forEach(x -> x.onSMPServiceGroupUpdated(aParticipantID));
    return EChange.CHANGED;
}
Also used : SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) EChange(com.helger.commons.state.EChange) Nonnull(javax.annotation.Nonnull)

Example 5 with SMPServiceGroup

use of com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup in project phoss-smp by phax.

the class SMPRedirectTest method testCaseSensitivity.

@Test
public void testCaseSensitivity() {
    final IParticipantIdentifier aPI = new SimpleParticipantIdentifier(PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, "0088:UpperCase");
    final IDocumentTypeIdentifier aDocTypeID = new SimpleDocumentTypeIdentifier(PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS, "testDocType");
    final SMPServiceGroup aSG = new SMPServiceGroup(CSecurity.USER_ADMINISTRATOR_ID, aPI, null);
    // Create new one
    final ISMPRedirect aRedirect = new SMPRedirect(aSG, aDocTypeID, "target", "suid", null, "<extredirect/>");
    assertSame(aSG, aRedirect.getServiceGroup());
    assertEquals(aDocTypeID, aRedirect.getDocumentTypeIdentifier());
    assertEquals("target", aRedirect.getTargetHref());
    assertEquals("suid", aRedirect.getSubjectUniqueIdentifier());
    assertNull(aRedirect.getCertificate());
    assertFalse(aRedirect.hasCertificate());
    assertEquals("[{\"Any\":\"<extredirect />\"}]", aRedirect.getExtensionsAsString());
}
Also used : SimpleDocumentTypeIdentifier(com.helger.peppolid.simple.doctype.SimpleDocumentTypeIdentifier) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Test(org.junit.Test)

Aggregations

SMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup)16 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)13 Nonnull (javax.annotation.Nonnull)10 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)7 SimpleParticipantIdentifier (com.helger.peppolid.simple.participant.SimpleParticipantIdentifier)7 SMPSMLException (com.helger.phoss.smp.exception.SMPSMLException)6 IRegistrationHook (com.helger.phoss.smp.smlhook.IRegistrationHook)6 RegistrationHookException (com.helger.phoss.smp.smlhook.RegistrationHookException)6 ConstantPreparedStatementDataProvider (com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider)5 SMPNotFoundException (com.helger.phoss.smp.exception.SMPNotFoundException)5 Test (org.junit.Test)5 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)4 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)4 Wrapper (com.helger.commons.wrapper.Wrapper)4 DBResultRow (com.helger.db.jdbc.executor.DBResultRow)4 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)4 SimpleDocumentTypeIdentifier (com.helger.peppolid.simple.doctype.SimpleDocumentTypeIdentifier)4 MutableBoolean (com.helger.commons.mutable.MutableBoolean)3 EChange (com.helger.commons.state.EChange)3 ESuccess (com.helger.commons.state.ESuccess)3