Search in sources :

Example 11 with ISMPBusinessCard

use of com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard in project phoss-smp by phax.

the class BusinessCardServerAPI method getBusinessCard.

@Nonnull
public PD3BusinessCardType getBusinessCard(final String sServiceGroupID) throws SMPServerException {
    final String sLog = LOG_PREFIX + "GET /businesscard/" + sServiceGroupID;
    final String sAction = "getBusinessCard";
    if (LOGGER.isInfoEnabled())
        LOGGER.info(sLog);
    STATS_COUNTER_INVOCATION.increment(sAction);
    try {
        final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
        final IParticipantIdentifier aServiceGroupID = aIdentifierFactory.parseParticipantIdentifier(sServiceGroupID);
        if (aServiceGroupID == null) {
            // Invalid identifier
            throw SMPBadRequestException.failedToParseSG(sServiceGroupID, m_aAPIProvider.getCurrentURI());
        }
        final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
        final ISMPServiceGroup aServiceGroup = aServiceGroupMgr.getSMPServiceGroupOfID(aServiceGroupID);
        if (aServiceGroup == null) {
            // No such service group
            throw new SMPNotFoundException("Unknown Service Group '" + sServiceGroupID + "'", m_aAPIProvider.getCurrentURI());
        }
        final ISMPBusinessCardManager aBusinessCardMgr = SMPMetaManager.getBusinessCardMgr();
        if (aBusinessCardMgr == null) {
            throw new SMPBadRequestException("This SMP server does not support the Business Card API", m_aAPIProvider.getCurrentURI());
        }
        final ISMPBusinessCard aBusinessCard = aBusinessCardMgr.getSMPBusinessCardOfServiceGroup(aServiceGroup);
        if (aBusinessCard == null) {
            // No such business card
            throw new SMPNotFoundException("No Business Card assigned to Service Group '" + sServiceGroupID + "'", m_aAPIProvider.getCurrentURI());
        }
        if (LOGGER.isInfoEnabled())
            LOGGER.info(sLog + " SUCCESS");
        STATS_COUNTER_SUCCESS.increment(sAction);
        return aBusinessCard.getAsJAXBObject();
    } catch (final SMPServerException ex) {
        if (LOGGER.isWarnEnabled())
            LOGGER.warn(sLog + " ERROR - " + ex.getMessage());
        STATS_COUNTER_ERROR.increment(sAction);
        throw ex;
    }
}
Also used : ISMPBusinessCard(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard) ISMPServiceGroupManager(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager) ISMPBusinessCardManager(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCardManager) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonnull(javax.annotation.Nonnull)

Example 12 with ISMPBusinessCard

use of com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard in project phoss-smp by phax.

the class BusinessCardInterfaceTest method testGetCreateV2GetDeleteGet.

@Test
public void testGetCreateV2GetDeleteGet() {
    final ISMPServiceGroupManager aSGMgr = SMPMetaManager.getServiceGroupMgr();
    assertNotNull(aSGMgr);
    final ISMPBusinessCardManager aBCMgr = SMPMetaManager.getBusinessCardMgr();
    assertNotNull(aBCMgr);
    final IParticipantIdentifier aPI = PeppolIdentifierFactory.INSTANCE.createParticipantIdentifierWithDefaultScheme("9999:tester");
    final String sPI = aPI.getURIEncoded();
    final ServiceGroupType aSG = new ServiceGroupType();
    aSG.setParticipantIdentifier(new SimpleParticipantIdentifier(aPI));
    aSG.setServiceMetadataReferenceCollection(new ServiceMetadataReferenceCollectionType());
    final WebTarget aTarget = ClientBuilder.newClient().target(m_aRule.getFullURL());
    Response aResponseMsg;
    try {
        // Create SG
        aResponseMsg = _addCredentials(aTarget.path(sPI).request()).put(Entity.xml(m_aObjFactory.createServiceGroup(aSG)));
        _testResponseJerseyClient(aResponseMsg, 200);
        // Get SG - must work
        assertNotNull(aTarget.path(sPI).request().get(ServiceGroupType.class));
        assertTrue(aSGMgr.containsSMPServiceGroupWithID(aPI));
        // Get BC - not existing
        aResponseMsg = aTarget.path("businesscard").path(sPI).request().get();
        _testResponseJerseyClient(aResponseMsg, 404);
        // Create BC with some entities
        final PD2BusinessCardType aBC = new PD2BusinessCardType();
        aBC.setParticipantIdentifier(PD2APIHelper.createIdentifier(aPI.getScheme(), aPI.getValue()));
        PD2BusinessEntityType aBE = new PD2BusinessEntityType();
        aBE.setName("BusinessEntity1");
        aBE.setCountryCode("AT");
        aBE.setGeographicalInformation("Vienna");
        aBC.addBusinessEntity(aBE);
        aBE = new PD2BusinessEntityType();
        aBE.setName("BusinessEntity2");
        aBE.setCountryCode("DE");
        aBE.setGeographicalInformation("Berlin");
        aBC.addBusinessEntity(aBE);
        aResponseMsg = _addCredentials(aTarget.path("businesscard").path(sPI).request()).put(Entity.xml(m_aBC2ObjFactory.createBusinessCard(aBC)));
        _testResponseJerseyClient(aResponseMsg, 200);
        // Get BC - must work (always V3)
        PD3BusinessCardType aReadBC = aTarget.path("businesscard").path(sPI).request().get(PD3BusinessCardType.class);
        assertNotNull(aReadBC);
        assertEquals(2, aReadBC.getBusinessEntityCount());
        ISMPBusinessCard aGetBC = aBCMgr.getSMPBusinessCardOfID(aPI);
        assertNotNull(aGetBC);
        // Update BC - add entity
        aBE = new PD2BusinessEntityType();
        aBE.setName("BusinessEntity3");
        aBE.setCountryCode("SE");
        aBE.setGeographicalInformation("Stockholm");
        aBC.addBusinessEntity(aBE);
        aResponseMsg = _addCredentials(aTarget.path("businesscard").path(sPI).request()).put(Entity.xml(m_aBC2ObjFactory.createBusinessCard(aBC)));
        _testResponseJerseyClient(aResponseMsg, 200);
        // Get BC - must work (always V3)
        aReadBC = aTarget.path("businesscard").path(sPI).request().get(PD3BusinessCardType.class);
        assertNotNull(aReadBC);
        assertEquals(3, aReadBC.getBusinessEntityCount());
        aGetBC = aBCMgr.getSMPBusinessCardOfID(aPI);
        assertNotNull(aGetBC);
    } finally {
        // Delete Business Card
        aResponseMsg = _addCredentials(aTarget.path("businesscard").path(sPI).request()).delete();
        _testResponseJerseyClient(aResponseMsg, 200, 404);
        // must be deleted
        _testResponseJerseyClient(aTarget.path("businesscard").path(sPI).request().get(), 404);
        assertNull(aBCMgr.getSMPBusinessCardOfID(aPI));
        // Delete service Group
        aResponseMsg = _addCredentials(aTarget.path(sPI).request()).delete();
        _testResponseJerseyClient(aResponseMsg, 200, 404);
        // must be deleted
        _testResponseJerseyClient(aTarget.path(sPI).request().get(), 404);
        assertFalse(aSGMgr.containsSMPServiceGroupWithID(aPI));
    }
}
Also used : ISMPServiceGroupManager(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager) ServiceMetadataReferenceCollectionType(com.helger.xsds.peppol.smp1.ServiceMetadataReferenceCollectionType) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) Response(javax.ws.rs.core.Response) ISMPBusinessCard(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard) ISMPBusinessCardManager(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCardManager) PD2BusinessEntityType(com.helger.pd.businesscard.v2.PD2BusinessEntityType) PD3BusinessCardType(com.helger.pd.businesscard.v3.PD3BusinessCardType) WebTarget(javax.ws.rs.client.WebTarget) ServiceGroupType(com.helger.xsds.peppol.smp1.ServiceGroupType) PD2BusinessCardType(com.helger.pd.businesscard.v2.PD2BusinessCardType) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Test(org.junit.Test)

Example 13 with ISMPBusinessCard

use of com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard 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 14 with ISMPBusinessCard

use of com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard in project phoss-smp by phax.

the class SMPBusinessCardManagerJDBC method getSMPBusinessCardOfID.

@Nullable
public ISMPBusinessCard getSMPBusinessCardOfID(@Nullable final IParticipantIdentifier aID) {
    if (aID == null)
        return null;
    final ICommonsList<DBResultRow> aDBResult = newExecutor().queryAll("SELECT id, name, country, geoinfo, identifiers, websites, contacts, addon, regdate" + " FROM smp_bce" + " WHERE pid=?", new ConstantPreparedStatementDataProvider(aID.getURIEncoded()));
    if (aDBResult == null)
        return null;
    if (aDBResult.isEmpty())
        return null;
    final ICommonsList<SMPBusinessCardEntity> aEntities = new CommonsArrayList<>();
    for (final DBResultRow aRow : aDBResult) {
        final SMPBusinessCardEntity aEntity = new SMPBusinessCardEntity(aRow.getAsString(0));
        // Single name only
        aEntity.names().add(new SMPBusinessCardName(aRow.getAsString(1), null));
        aEntity.setCountryCode(aRow.getAsString(2));
        aEntity.setGeographicalInformation(aRow.getAsString(3));
        aEntity.identifiers().setAll(getJsonAsBCI(aRow.getAsString(4)));
        aEntity.websiteURIs().setAll(getJsonAsString(aRow.getAsString(5)));
        aEntity.contacts().setAll(getJsonAsBCC(aRow.getAsString(6)));
        aEntity.setAdditionalInformation(aRow.getAsString(7));
        aEntity.setRegistrationDate(aRow.get(8).getAsLocalDate());
        aEntities.add(aEntity);
    }
    return new SMPBusinessCard(aID, aEntities);
}
Also used : DBResultRow(com.helger.db.jdbc.executor.DBResultRow) ISMPBusinessCard(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard) SMPBusinessCard(com.helger.phoss.smp.domain.businesscard.SMPBusinessCard) SMPBusinessCardEntity(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardEntity) SMPBusinessCardName(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardName) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 15 with ISMPBusinessCard

use of com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard in project phoss-smp by phax.

the class SMPBusinessCardManagerJDBC method getAllSMPBusinessCards.

@Nonnull
@ReturnsMutableCopy
public ICommonsList<ISMPBusinessCard> getAllSMPBusinessCards() {
    final ICommonsList<ISMPBusinessCard> ret = new CommonsArrayList<>();
    final ICommonsList<DBResultRow> aDBResult = newExecutor().queryAll("SELECT id, pid, name, country, geoinfo, identifiers, websites, contacts, addon, regdate" + " FROM smp_bce");
    if (aDBResult != null) {
        final IIdentifierFactory aIF = SMPMetaManager.getIdentifierFactory();
        // Group by ID
        final ICommonsMap<IParticipantIdentifier, ICommonsList<SMPBusinessCardEntity>> aEntityMap = new CommonsHashMap<>();
        for (final DBResultRow aRow : aDBResult) {
            final SMPBusinessCardEntity aEntity = new SMPBusinessCardEntity(aRow.getAsString(0));
            // Single name only
            aEntity.names().add(new SMPBusinessCardName(aRow.getAsString(2), null));
            aEntity.setCountryCode(aRow.getAsString(3));
            aEntity.setGeographicalInformation(aRow.getAsString(4));
            aEntity.identifiers().setAll(getJsonAsBCI(aRow.getAsString(5)));
            aEntity.websiteURIs().setAll(getJsonAsString(aRow.getAsString(6)));
            aEntity.contacts().setAll(getJsonAsBCC(aRow.getAsString(7)));
            aEntity.setAdditionalInformation(aRow.getAsString(8));
            aEntity.setRegistrationDate(aRow.get(9).getAsLocalDate());
            aEntityMap.computeIfAbsent(aIF.parseParticipantIdentifier(aRow.getAsString(1)), k -> new CommonsArrayList<>()).add(aEntity);
        }
        // Convert
        for (final Map.Entry<IParticipantIdentifier, ICommonsList<SMPBusinessCardEntity>> aEntry : aEntityMap.entrySet()) {
            final IParticipantIdentifier aPID = aEntry.getKey();
            ret.add(new SMPBusinessCard(aPID, aEntry.getValue()));
        }
    }
    return ret;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) Nonnegative(javax.annotation.Nonnegative) ISMPBusinessCard(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard) ISMPBusinessCardManager(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCardManager) ICommonsSet(com.helger.commons.collection.impl.ICommonsSet) LoggerFactory(org.slf4j.LoggerFactory) IJsonObject(com.helger.json.IJsonObject) EChange(com.helger.commons.state.EChange) IJsonWriterSettings(com.helger.json.serialize.IJsonWriterSettings) Supplier(java.util.function.Supplier) JsonWriterSettings(com.helger.json.serialize.JsonWriterSettings) SMPBusinessCard(com.helger.phoss.smp.domain.businesscard.SMPBusinessCard) CallbackList(com.helger.commons.callback.CallbackList) JsonArray(com.helger.json.JsonArray) AbstractJDBCEnabledManager(com.helger.db.jdbc.mgr.AbstractJDBCEnabledManager) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPBusinessCardIdentifier(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardIdentifier) Map(java.util.Map) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) JsonReader(com.helger.json.serialize.JsonReader) SMPBusinessCardEntity(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardEntity) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) SMPMetaManager(com.helger.phoss.smp.domain.SMPMetaManager) Collection(java.util.Collection) JsonObject(com.helger.json.JsonObject) ValueEnforcer(com.helger.commons.ValueEnforcer) DBResultRow(com.helger.db.jdbc.executor.DBResultRow) SMPBusinessCardName(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardName) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) AuditHelper(com.helger.photon.audit.AuditHelper) List(java.util.List) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet) ICommonsList(com.helger.commons.collection.impl.ICommonsList) IJson(com.helger.json.IJson) SMPBusinessCardContact(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardContact) ReturnsMutableObject(com.helger.commons.annotation.ReturnsMutableObject) ISMPBusinessCardCallback(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCardCallback) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) ICommonsList(com.helger.commons.collection.impl.ICommonsList) SMPBusinessCardEntity(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardEntity) SMPBusinessCardName(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardName) ISMPBusinessCard(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard) DBResultRow(com.helger.db.jdbc.executor.DBResultRow) ISMPBusinessCard(com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard) SMPBusinessCard(com.helger.phoss.smp.domain.businesscard.SMPBusinessCard) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) Map(java.util.Map) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Aggregations

ISMPBusinessCard (com.helger.phoss.smp.domain.businesscard.ISMPBusinessCard)19 ISMPBusinessCardManager (com.helger.phoss.smp.domain.businesscard.ISMPBusinessCardManager)14 Nonnull (javax.annotation.Nonnull)10 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)9 SMPBusinessCardEntity (com.helger.phoss.smp.domain.businesscard.SMPBusinessCardEntity)8 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)8 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)6 SMPBusinessCard (com.helger.phoss.smp.domain.businesscard.SMPBusinessCard)6 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)6 ISMPSettings (com.helger.phoss.smp.settings.ISMPSettings)6 HCNodeList (com.helger.html.hc.impl.HCNodeList)5 SMPMetaManager (com.helger.phoss.smp.domain.SMPMetaManager)5 Nullable (javax.annotation.Nullable)5 ICommonsList (com.helger.commons.collection.impl.ICommonsList)4 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)4 ISimpleURL (com.helger.commons.url.ISimpleURL)4 HCRow (com.helger.html.hc.html.tabular.HCRow)4 HCTable (com.helger.html.hc.html.tabular.HCTable)4 HCA (com.helger.html.hc.html.textlevel.HCA)4 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)4