use of com.helger.peppolid.IParticipantIdentifier in project phoss-directory by phax.
the class PDIndexExecutor method executeWorkItem.
/**
* This method is responsible for executing the specified work item depending
* on its type.
*
* @param aStorageMgr
* Storage manager.
* @param aWorkItem
* The work item to be executed. May not be <code>null</code>.
* @param nRetryCount
* The retry count. For the initial indexing it is 0, for the first
* retry 1 etc.
* @param aSuccessHandler
* A callback that is invoked upon success only.
* @param aFailureHandler
* A callback that is invoked upon failure only.
* @return {@link ESuccess}
*/
@Nonnull
public static ESuccess executeWorkItem(@Nonnull final IPDStorageManager aStorageMgr, @Nonnull final IIndexerWorkItem aWorkItem, @Nonnegative final int nRetryCount, @Nonnull final Consumer<? super IIndexerWorkItem> aSuccessHandler, @Nonnull final Consumer<? super IIndexerWorkItem> aFailureHandler) {
LOGGER.info("Execute work item " + aWorkItem.getLogText() + " - " + (nRetryCount > 0 ? "retry #" + nRetryCount : "initial try"));
final IPDBusinessCardProvider aBCProvider = PDMetaManager.getBusinessCardProviderOrNull();
if (aBCProvider == null) {
// Maybe null upon shutdown - in that case ignore it and don't reindex
return ESuccess.FAILURE;
}
try {
final IParticipantIdentifier aParticipantID = aWorkItem.getParticipantID();
ESuccess eSuccess;
switch(aWorkItem.getType()) {
case CREATE_UPDATE:
{
// Get BI from participant (e.g. from SMP)
final PDExtendedBusinessCard aBI = aBCProvider.getBusinessCard(aParticipantID);
if (aBI == null) {
// No/invalid extension present - no need to try again
eSuccess = ESuccess.FAILURE;
} else {
// Got data - put in storage
eSuccess = aStorageMgr.createOrUpdateEntry(aParticipantID, aBI, aWorkItem.getAsMetaData());
}
break;
}
case DELETE:
{
// Really delete it
eSuccess = ESuccess.valueOf(aStorageMgr.deleteEntry(aParticipantID, aWorkItem.getAsMetaData(), true) >= 0);
break;
}
case SYNC:
{
// Get BI from participant (e.g. from SMP)
final PDExtendedBusinessCard aBI = aBCProvider.getBusinessCard(aParticipantID);
if (aBI == null) {
// No/invalid extension present - delete from index
eSuccess = ESuccess.valueOf(aStorageMgr.deleteEntry(aParticipantID, aWorkItem.getAsMetaData(), true) >= 0);
} else {
// Got data - put in storage
eSuccess = aStorageMgr.createOrUpdateEntry(aParticipantID, aBI, aWorkItem.getAsMetaData());
}
break;
}
default:
throw new IllegalStateException("Unsupported work item type: " + aWorkItem);
}
if (eSuccess.isSuccess()) {
// Item handled - remove from overall list
aSuccessHandler.accept(aWorkItem);
// And we're done
return ESuccess.SUCCESS;
}
// else error storing data
} catch (final Exception ex) {
LOGGER.error("Error in executing work item " + aWorkItem.getLogText(), ex);
// Fall through
}
// Invoke failure handler
aFailureHandler.accept(aWorkItem);
return ESuccess.FAILURE;
}
use of com.helger.peppolid.IParticipantIdentifier in project phoss-directory by phax.
the class IndexerWorkItemMicroTypeConverter method convertToNative.
@Nullable
public IndexerWorkItem convertToNative(@Nonnull final IMicroElement aElement) {
final String sID = aElement.getAttributeValue(ATTR_ID);
final LocalDateTime aCreationDT = aElement.getAttributeValueWithConversion(ATTR_CREATION_DATE_TIME, LocalDateTime.class);
final String sParticipantID = StringHelper.trim(aElement.getAttributeValue(ATTR_PARTICIPANT_ID));
IParticipantIdentifier aParticipantID = PDMetaManager.getIdentifierFactory().parseParticipantIdentifier(sParticipantID);
if (aParticipantID == null) {
LOGGER.warn("Failed to parse '" + sParticipantID + "' with the configured IdentifierFactory: " + PDMetaManager.getIdentifierFactory().toString());
// For read bogus entries - we need to stick with the simple one for now.
// TODO hard coded use of SimpleIdentifierFactory
aParticipantID = SimpleIdentifierFactory.INSTANCE.parseParticipantIdentifier(sParticipantID);
}
if (aParticipantID == null)
throw new IllegalStateException("Failed to parse participant identifier '" + sParticipantID + "'");
final String sTypeID = aElement.getAttributeValue(ATTR_TYPE);
final EIndexerWorkItemType eType = EIndexerWorkItemType.getFromIDOrNull(sTypeID);
if (eType == null)
throw new IllegalStateException("Failed to parse type ID '" + sTypeID + "'");
final String sOwnerID = aElement.getAttributeValue(ATTR_OWNER_ID);
final String sRequestingHost = aElement.getAttributeValue(ATTR_HOST);
return new IndexerWorkItem(sID, aCreationDT, aParticipantID, eType, sOwnerID, sRequestingHost);
}
use of com.helger.peppolid.IParticipantIdentifier in project phoss-directory by phax.
the class PageSecureParticipantActions method _deleteDuplicateIDs.
private void _deleteDuplicateIDs(@Nonnull final WebPageExecutionContext aWPEC) {
LOGGER.info("Deleting all duplicate participant identifiers");
final Locale aDisplayLocale = aWPEC.getDisplayLocale();
final ICommonsMap<IParticipantIdentifier, ICommonsSortedSet<String>> aDupMap = _getDuplicateSourceMap();
final ICommonsSortedSet<String> aPIsToDelete = new CommonsTreeSet<>();
final ICommonsSortedSet<String> aPIsToAdd = new CommonsTreeSet<>();
for (final Map.Entry<IParticipantIdentifier, ICommonsSortedSet<String>> aEntry : aDupMap.entrySet()) {
final ICommonsSortedSet<String> aSet = aEntry.getValue();
final IParticipantIdentifier aPI = aEntry.getKey();
final String sDesiredVersion = aPI.getURIEncoded();
if (aSet.contains(sDesiredVersion)) {
// Simple kill the other ones
aPIsToDelete.addAll(aSet, x -> !x.equals(sDesiredVersion));
} else {
// Remove all and index the correct version
aPIsToDelete.addAll(aSet);
aPIsToAdd.add(sDesiredVersion);
}
}
if (aPIsToDelete.isNotEmpty()) {
final HCNodeList aNL = new HCNodeList();
// Important to use this identifier factory so that the correct key is
// created
final IIdentifierFactory aIF = SimpleIdentifierFactory.INSTANCE;
final PDIndexerManager aIndexerMgr = PDMetaManager.getIndexerMgr();
String sMsg = "Deleting " + aPIsToDelete.size() + " participant ID(s):";
LOGGER.info(sMsg);
aNL.addChild(h2(sMsg));
HCOL aOL = aNL.addAndReturnChild(new HCOL());
for (final String s : aPIsToDelete.getSorted(IComparator.getComparatorCollating(aDisplayLocale))) {
aOL.addItem(s);
aIndexerMgr.queueWorkItem(aIF.parseParticipantIdentifier(s), EIndexerWorkItemType.DELETE, "duplicate-elimination", PDIndexerManager.HOST_LOCALHOST);
}
if (aPIsToAdd.isNotEmpty()) {
sMsg = "Adding " + aPIsToAdd.size() + " participant ID(s) instead:";
LOGGER.info(sMsg);
aNL.addChild(h2(sMsg));
aOL = aNL.addAndReturnChild(new HCOL());
for (final String s : aPIsToAdd.getSorted(IComparator.getComparatorCollating(aDisplayLocale))) {
aOL.addItem(s);
aIndexerMgr.queueWorkItem(aIF.parseParticipantIdentifier(s), EIndexerWorkItemType.CREATE_UPDATE, "duplicate-elimination", PDIndexerManager.HOST_LOCALHOST);
}
}
aWPEC.postRedirectGetInternal(aNL);
} else {
final String sMsg = "Found no duplicate entries to remove";
LOGGER.info(sMsg);
aWPEC.postRedirectGetInternal(success(sMsg));
}
}
use of com.helger.peppolid.IParticipantIdentifier in project phoss-directory by phax.
the class PageSecureParticipantActions method _showDuplicateIDs.
private void _showDuplicateIDs(@Nonnull final WebPageExecutionContext aWPEC) {
// This method can take a couple of minutes
LOGGER.info("Showing all duplicate participant identifiers");
final Locale aDisplayLocale = aWPEC.getDisplayLocale();
final ICommonsMap<IParticipantIdentifier, ICommonsSortedSet<String>> aDupMap = _getDuplicateSourceMap();
final HCNodeList aNL = new HCNodeList();
for (final Map.Entry<IParticipantIdentifier, ICommonsSortedSet<String>> aEntry : aDupMap.entrySet()) {
final ICommonsSortedSet<String> aSet = aEntry.getValue();
final IParticipantIdentifier aPI = aEntry.getKey();
final String sDesiredVersion = aPI.getURIEncoded();
final HCDiv aDiv = div("Found " + aSet.size() + " duplicate IDs for ").addChild(code(sDesiredVersion)).addChild(":");
final HCOL aOL = aDiv.addAndReturnChild(new HCOL());
for (final String sVersion : aSet.getSorted(IComparator.getComparatorCollating(aDisplayLocale))) {
final boolean bIsDesired = sDesiredVersion.equals(sVersion);
final IHCLI<?> aLI = aOL.addAndReturnItem(code(sVersion));
if (bIsDesired)
aLI.addChild(" ").addChild(badgeSuccess("desired version"));
}
aNL.addChild(aDiv);
}
if (aNL.hasChildren()) {
final String sMsg = "Found duplicate entries for " + aDupMap.size() + " " + (aDupMap.size() == 1 ? "participant" : "participants");
LOGGER.info(sMsg);
aNL.addChildAt(0, h2(sMsg));
aWPEC.postRedirectGetInternal(aNL);
} else
aWPEC.postRedirectGetInternal(success("Found no duplicate entries"));
}
use of com.helger.peppolid.IParticipantIdentifier in project phoss-directory by phax.
the class ExportHelper method getAsXML.
@Nonnull
public static IMicroDocument getAsXML(@Nonnull final ICommonsOrderedMap<IParticipantIdentifier, ICommonsList<PDStoredBusinessEntity>> aMap, final boolean bIncludeDocTypes) {
// XML root
final IMicroDocument aDoc = new MicroDocument();
final IMicroElement aRoot = aDoc.appendElement(XML_EXPORT_NS_URI, "root");
aRoot.setAttribute("version", "2");
aRoot.setAttribute("creationdt", PDTWebDateHelper.getAsStringXSD(PDTFactory.getCurrentZonedDateTimeUTC()));
// For all BCs
for (final Map.Entry<IParticipantIdentifier, ICommonsList<PDStoredBusinessEntity>> aEntry : aMap.entrySet()) {
final IParticipantIdentifier aParticipantID = aEntry.getKey();
final PDBusinessCard aBC = new PDBusinessCard();
aBC.setParticipantIdentifier(new PDIdentifier(aParticipantID.getScheme(), aParticipantID.getValue()));
for (final PDStoredBusinessEntity aSBE : aEntry.getValue()) aBC.businessEntities().add(aSBE.getAsBusinessEntity());
final IMicroElement eBC = aBC.getAsMicroXML(XML_EXPORT_NS_URI, "businesscard");
// New in v2 - add all Document types
if (bIncludeDocTypes && aEntry.getValue().isNotEmpty())
for (final IDocumentTypeIdentifier aDocTypeID : aEntry.getValue().getFirst().documentTypeIDs()) eBC.appendChild(_createMicroElement(aDocTypeID));
aRoot.appendChild(eBC);
}
return aDoc;
}
Aggregations