Search in sources :

Example 1 with IParticipantIdentifier

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;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) IPDBusinessCardProvider(com.helger.pd.indexer.businesscard.IPDBusinessCardProvider) PDExtendedBusinessCard(com.helger.pd.indexer.businesscard.PDExtendedBusinessCard) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Example 2 with IParticipantIdentifier

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nullable(javax.annotation.Nullable)

Example 3 with IParticipantIdentifier

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));
    }
}
Also used : Locale(java.util.Locale) HCNodeList(com.helger.html.hc.impl.HCNodeList) CommonsTreeSet(com.helger.commons.collection.impl.CommonsTreeSet) PDTToString(com.helger.commons.datetime.PDTToString) ICommonsSortedSet(com.helger.commons.collection.impl.ICommonsSortedSet) HCOL(com.helger.html.hc.html.grouping.HCOL) Map(java.util.Map) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) PDIndexerManager(com.helger.pd.indexer.mgr.PDIndexerManager) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Example 4 with IParticipantIdentifier

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"));
}
Also used : Locale(java.util.Locale) HCDiv(com.helger.html.hc.html.grouping.HCDiv) HCNodeList(com.helger.html.hc.impl.HCNodeList) PDTToString(com.helger.commons.datetime.PDTToString) ICommonsSortedSet(com.helger.commons.collection.impl.ICommonsSortedSet) HCOL(com.helger.html.hc.html.grouping.HCOL) Map(java.util.Map) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Example 5 with IParticipantIdentifier

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;
}
Also used : PDIdentifier(com.helger.pd.businesscard.generic.PDIdentifier) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) ICommonsList(com.helger.commons.collection.impl.ICommonsList) PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) PDStoredBusinessEntity(com.helger.pd.indexer.storage.PDStoredBusinessEntity) IMicroElement(com.helger.xml.microdom.IMicroElement) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) IMicroDocument(com.helger.xml.microdom.IMicroDocument) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) Map(java.util.Map) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Aggregations

IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)218 Test (org.junit.Test)70 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)58 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)57 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)51 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)47 File (java.io.File)46 Nonnull (javax.annotation.Nonnull)43 ESimpleUserMessageSendResult (com.helger.phase4.sender.AbstractAS4UserMessageBuilder.ESimpleUserMessageSendResult)42 Element (org.w3c.dom.Element)41 SMPClientReadOnly (com.helger.smpclient.peppol.SMPClientReadOnly)40 AS4RawResponseConsumerWriteToFile (com.helger.phase4.dump.AS4RawResponseConsumerWriteToFile)38 AS4IncomingDumperFileBased (com.helger.phase4.dump.AS4IncomingDumperFileBased)35 AS4OutgoingDumperFileBased (com.helger.phase4.dump.AS4OutgoingDumperFileBased)35 SimpleParticipantIdentifier (com.helger.peppolid.simple.participant.SimpleParticipantIdentifier)30 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)24 ISMPServiceInformationManager (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager)24 SMPServerException (com.helger.phoss.smp.exception.SMPServerException)24 ISMPServiceInformation (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation)19 ICommonsList (com.helger.commons.collection.impl.ICommonsList)18