Search in sources :

Example 1 with CommonsTreeSet

use of com.helger.commons.collection.impl.CommonsTreeSet 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 2 with CommonsTreeSet

use of com.helger.commons.collection.impl.CommonsTreeSet in project phoss-directory by phax.

the class ExportAllManager method queryAllContainedParticipantsAsXML.

@Nonnull
public static IMicroDocument queryAllContainedParticipantsAsXML(@Nonnull final EQueryMode eQueryMode) throws IOException {
    final Query aQuery = eQueryMode.getEffectiveQuery(new MatchAllDocsQuery());
    // Query all and group by participant ID
    final ICommonsSortedSet<IParticipantIdentifier> aSet = new CommonsTreeSet<>(Comparator.comparing(IParticipantIdentifier::getURIEncoded));
    PDMetaManager.getStorageMgr().searchAll(aQuery, -1, PDField.PARTICIPANT_ID::getDocValue, aSet::add);
    // XML root
    final IMicroDocument aDoc = new MicroDocument();
    final String sNamespaceURI = "http://www.peppol.eu/schema/pd/participant-generic/201910/";
    final IMicroElement aRoot = aDoc.appendElement(sNamespaceURI, "root");
    aRoot.setAttribute("version", "1");
    aRoot.setAttribute("creationdt", PDTWebDateHelper.getAsStringXSD(PDTFactory.getCurrentZonedDateTimeUTC()));
    aRoot.setAttribute("count", aSet.size());
    // For all participants
    for (final IParticipantIdentifier aParticipantID : aSet) {
        aRoot.appendElement(sNamespaceURI, "participantID").setAttribute("scheme", aParticipantID.getScheme()).setAttribute("value", aParticipantID.getValue());
    }
    return aDoc;
}
Also used : IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CommonsTreeSet(com.helger.commons.collection.impl.CommonsTreeSet) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Example 3 with CommonsTreeSet

use of com.helger.commons.collection.impl.CommonsTreeSet in project phoss-directory by phax.

the class PageSecureParticipantActions method _getDuplicateSourceMap.

@Nonnull
private static ICommonsMap<IParticipantIdentifier, ICommonsSortedSet<String>> _getDuplicateSourceMap() {
    LOGGER.info("_getDuplicateSourceMap () start");
    final ICommonsMap<IParticipantIdentifier, ICommonsSortedSet<String>> aMap = new CommonsHashMap<>();
    final Query aQuery = EQueryMode.NON_DELETED_ONLY.getEffectiveQuery(new MatchAllDocsQuery());
    try {
        final Consumer<Document> aConsumer = aDoc -> {
            final IParticipantIdentifier aResolvedParticipantID = PDField.PARTICIPANT_ID.getDocValue(aDoc);
            // Get the unparsed value
            final String sParticipantID = PDField.PARTICIPANT_ID.getDocField(aDoc).stringValue();
            aMap.computeIfAbsent(aResolvedParticipantID, k -> new CommonsTreeSet<>()).add(sParticipantID);
        };
        PDMetaManager.getStorageMgr().searchAll(aQuery, -1, aConsumer);
    } catch (final IOException ex) {
        LOGGER.error("Error searching for documents with query " + aQuery, ex);
    }
    // Take only the duplicate ones
    final ICommonsMap<IParticipantIdentifier, ICommonsSortedSet<String>> ret = new CommonsHashMap<>();
    for (final Map.Entry<IParticipantIdentifier, ICommonsSortedSet<String>> aEntry : aMap.entrySet()) if (aEntry.getValue().size() > 1) {
        ret.put(aEntry);
        LOGGER.info("  Potential duplicate in: " + aEntry.getKey().getURIEncoded());
    }
    LOGGER.info("_getDuplicateSourceMap () done");
    return ret;
}
Also used : Query(org.apache.lucene.search.Query) EExcelVersion(com.helger.poi.excel.EExcelVersion) CommonsTreeSet(com.helger.commons.collection.impl.CommonsTreeSet) StreamHelper(com.helger.commons.io.stream.StreamHelper) HCOL(com.helger.html.hc.html.grouping.HCOL) WebPageExecutionContext(com.helger.photon.uicore.page.WebPageExecutionContext) ExportAllManager(com.helger.pd.publisher.exportall.ExportAllManager) EDefaultIcon(com.helger.photon.uicore.icon.EDefaultIcon) EIndexerWorkItemType(com.helger.pd.indexer.index.EIndexerWorkItemType) PDMetaManager(com.helger.pd.indexer.mgr.PDMetaManager) LoggerFactory(org.slf4j.LoggerFactory) IMicroDocument(com.helger.xml.microdom.IMicroDocument) BootstrapButtonToolbar(com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar) SyncAllBusinessCardsJob(com.helger.pd.publisher.updater.SyncAllBusinessCardsJob) AjaxFunctionDeclaration(com.helger.photon.ajax.decl.AjaxFunctionDeclaration) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) Document(org.apache.lucene.document.Document) CMimeType(com.helger.commons.mime.CMimeType) CPDPublisher(com.helger.pd.publisher.CPDPublisher) Nonempty(com.helger.commons.annotation.Nonempty) EBootstrapButtonType(com.helger.photon.bootstrap4.button.EBootstrapButtonType) Locale(java.util.Locale) IMicroElement(com.helger.xml.microdom.IMicroElement) Map(java.util.Map) CPageParam(com.helger.photon.uicore.css.CPageParam) IComparator(com.helger.commons.compare.IComparator) PDTToString(com.helger.commons.datetime.PDTToString) HCDiv(com.helger.html.hc.html.grouping.HCDiv) PDField(com.helger.pd.indexer.storage.field.PDField) AbstractAppWebPage(com.helger.pd.publisher.ui.AbstractAppWebPage) Set(java.util.Set) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) StandardCharsets(java.nio.charset.StandardCharsets) WorkbookCreationHelper(com.helger.poi.excel.WorkbookCreationHelper) BootstrapCard(com.helger.photon.bootstrap4.card.BootstrapCard) BootstrapButton(com.helger.photon.bootstrap4.button.BootstrapButton) ExportServlet(com.helger.pd.publisher.servlet.ExportServlet) CSVWriter(com.helger.commons.csv.CSVWriter) PDIndexerManager(com.helger.pd.indexer.mgr.PDIndexerManager) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) BootstrapCardBody(com.helger.photon.bootstrap4.card.BootstrapCardBody) LinkHelper(com.helger.photon.app.url.LinkHelper) LocalDateTime(java.time.LocalDateTime) MicroDocument(com.helger.xml.microdom.MicroDocument) ExportDeliveryHttpHandler(com.helger.pd.publisher.servlet.ExportDeliveryHttpHandler) ExportAllDataJob(com.helger.pd.publisher.exportall.ExportAllDataJob) ICommonsSortedSet(com.helger.commons.collection.impl.ICommonsSortedSet) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull) IHCLI(com.helger.html.hc.html.grouping.IHCLI) HCNodeList(com.helger.html.hc.impl.HCNodeList) IRequestWebScopeWithoutResponse(com.helger.web.scope.IRequestWebScopeWithoutResponse) Logger(org.slf4j.Logger) CBootstrapCSS(com.helger.photon.bootstrap4.CBootstrapCSS) IOException(java.io.IOException) SimpleIdentifierFactory(com.helger.peppolid.factory.SimpleIdentifierFactory) Consumer(java.util.function.Consumer) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) EQueryMode(com.helger.pd.indexer.storage.EQueryMode) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) PDTToString(com.helger.commons.datetime.PDTToString) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Document(org.apache.lucene.document.Document) MicroDocument(com.helger.xml.microdom.MicroDocument) ICommonsSortedSet(com.helger.commons.collection.impl.ICommonsSortedSet) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) Map(java.util.Map) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Example 4 with CommonsTreeSet

use of com.helger.commons.collection.impl.CommonsTreeSet in project phoss-directory by phax.

the class ExportAllManager method queryAllContainedParticipantsAsJSON.

@Nonnull
public static IJsonObject queryAllContainedParticipantsAsJSON(@Nonnull final EQueryMode eQueryMode) throws IOException {
    final Query aQuery = eQueryMode.getEffectiveQuery(new MatchAllDocsQuery());
    // Query all and group by participant ID
    final ICommonsSortedSet<IParticipantIdentifier> aSet = new CommonsTreeSet<>(Comparator.comparing(IParticipantIdentifier::getURIEncoded));
    PDMetaManager.getStorageMgr().searchAll(aQuery, -1, PDField.PARTICIPANT_ID::getDocValue, aSet::add);
    // XML root
    final IJsonObject aObj = new JsonObject();
    aObj.add("version", 1);
    aObj.add("creationdt", PDTWebDateHelper.getAsStringXSD(PDTFactory.getCurrentZonedDateTimeUTC()));
    aObj.add("count", aSet.size());
    // For all participants
    final IJsonArray aArray = new JsonArray();
    for (final IParticipantIdentifier aParticipantID : aSet) aArray.add(aParticipantID.getURIEncoded());
    aObj.addJson("participants", aArray);
    return aObj;
}
Also used : IJsonArray(com.helger.json.IJsonArray) JsonArray(com.helger.json.JsonArray) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CommonsTreeSet(com.helger.commons.collection.impl.CommonsTreeSet) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IJsonObject(com.helger.json.IJsonObject) IJsonArray(com.helger.json.IJsonArray) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Aggregations

CommonsTreeSet (com.helger.commons.collection.impl.CommonsTreeSet)4 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)4 Nonnull (javax.annotation.Nonnull)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Query (org.apache.lucene.search.Query)3 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)2 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)2 ICommonsSortedSet (com.helger.commons.collection.impl.ICommonsSortedSet)2 PDTToString (com.helger.commons.datetime.PDTToString)2 HCOL (com.helger.html.hc.html.grouping.HCOL)2 HCNodeList (com.helger.html.hc.impl.HCNodeList)2 PDIndexerManager (com.helger.pd.indexer.mgr.PDIndexerManager)2 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)2 Nonempty (com.helger.commons.annotation.Nonempty)1 IComparator (com.helger.commons.compare.IComparator)1 CSVWriter (com.helger.commons.csv.CSVWriter)1 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)1 StreamHelper (com.helger.commons.io.stream.StreamHelper)1 CMimeType (com.helger.commons.mime.CMimeType)1 HCDiv (com.helger.html.hc.html.grouping.HCDiv)1