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;
}
}
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));
}
}
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;
}
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);
}
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;
}
Aggregations