Search in sources :

Example 1 with IPDBusinessCardProvider

use of com.helger.pd.indexer.businesscard.IPDBusinessCardProvider 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 IPDBusinessCardProvider

use of com.helger.pd.indexer.businesscard.IPDBusinessCardProvider in project phoss-directory by phax.

the class PageSecureIndexManually method fillContent.

@Override
protected void fillContent(final WebPageExecutionContext aWPEC) {
    final HCNodeList aNodeList = aWPEC.getNodeList();
    final IIdentifierFactory aIdentifierFactory = PDMetaManager.getIdentifierFactory();
    final FormErrorList aFormErrors = new FormErrorList();
    {
        final IPDBusinessCardProvider aBCProv = PDMetaManager.getBusinessCardProvider();
        if (aBCProv instanceof SMPBusinessCardProvider) {
            final SMPBusinessCardProvider aSMPBCProv = (SMPBusinessCardProvider) aBCProv;
            if (aSMPBCProv.isFixedSMP()) {
                aNodeList.addChild(info("Fixed SMP URI " + aSMPBCProv.getFixedSMPURI() + " is used."));
            } else {
                aNodeList.addChild(info("The following SMLs are crawled for entries: " + StringHelper.getImplodedMapped(", ", aSMPBCProv.getAllSMLsToUse(), ISMLInfo::getDisplayName)));
            }
        }
    }
    if (aWPEC.hasAction(CPageParam.ACTION_PERFORM)) {
        final String sParticipantID = aWPEC.params().getAsString(FIELD_PARTICIPANT_ID);
        final IParticipantIdentifier aParticipantID = aIdentifierFactory.parseParticipantIdentifier(sParticipantID);
        if (StringHelper.hasNoText(sParticipantID))
            aFormErrors.addFieldError(FIELD_PARTICIPANT_ID, "A participant ID must be provided.");
        else if (aParticipantID == null)
            aFormErrors.addFieldError(FIELD_PARTICIPANT_ID, "The provided participant ID is syntactically invalid.");
        if (aFormErrors.isEmpty()) {
            if (PDMetaManager.getIndexerMgr().queueWorkItem(aParticipantID, EIndexerWorkItemType.CREATE_UPDATE, "manually-triggered", PDIndexerManager.HOST_LOCALHOST).isChanged()) {
                aWPEC.postRedirectGetInternal(success("The indexing of participant ID '" + sParticipantID + "' was successfully triggered!"));
            } else {
                aWPEC.postRedirectGetInternal(warn("Participant ID '" + sParticipantID + "' is already in the indexing queue!"));
            }
        }
    }
    final BootstrapForm aForm = aNodeList.addAndReturnChild(getUIHandler().createFormSelf(aWPEC));
    aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Participant ID").setCtrl(new HCEdit(new RequestField(FIELD_PARTICIPANT_ID, PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME + CIdentifier.URL_SCHEME_VALUE_SEPARATOR))).setHelpText(span().addChild("Enter the fully qualified Peppol participant ID (including the scheme) you want to index.\nExample identifier layout: ").addChild(code(aIdentifierFactory.createParticipantIdentifier(PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, "9999:test").getURIEncoded()))).setErrorList(aFormErrors.getListOfField(FIELD_PARTICIPANT_ID)));
    final BootstrapButtonToolbar aToolbar = aForm.addAndReturnChild(new BootstrapButtonToolbar(aWPEC));
    aToolbar.addHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM);
    aToolbar.addSubmitButton("Add to queue", EDefaultIcon.YES);
}
Also used : SMPBusinessCardProvider(com.helger.pd.indexer.businesscard.SMPBusinessCardProvider) HCNodeList(com.helger.html.hc.impl.HCNodeList) ISMLInfo(com.helger.peppol.sml.ISMLInfo) FormErrorList(com.helger.photon.core.form.FormErrorList) HCEdit(com.helger.html.hc.html.forms.HCEdit) BootstrapForm(com.helger.photon.bootstrap4.form.BootstrapForm) IPDBusinessCardProvider(com.helger.pd.indexer.businesscard.IPDBusinessCardProvider) BootstrapButtonToolbar(com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) RequestField(com.helger.photon.core.form.RequestField)

Example 3 with IPDBusinessCardProvider

use of com.helger.pd.indexer.businesscard.IPDBusinessCardProvider in project phoss-directory by phax.

the class PageSecureIndexImport method fillContent.

@Override
protected void fillContent(final WebPageExecutionContext aWPEC) {
    final HCNodeList aNodeList = aWPEC.getNodeList();
    final Locale aDisplayLocale = aWPEC.getDisplayLocale();
    final IIdentifierFactory aIdentifierFactory = PDMetaManager.getIdentifierFactory();
    final FormErrorList aFormErrors = new FormErrorList();
    {
        final IPDBusinessCardProvider aBCProv = PDMetaManager.getBusinessCardProvider();
        if (aBCProv instanceof SMPBusinessCardProvider) {
            final SMPBusinessCardProvider aSMPBCProv = (SMPBusinessCardProvider) aBCProv;
            if (aSMPBCProv.isFixedSMP()) {
                aNodeList.addChild(info("Fixed SMP URI " + aSMPBCProv.getFixedSMPURI() + " is used."));
            } else {
                aNodeList.addChild(info("The following SMLs are crawled for entries: " + StringHelper.getImplodedMapped(", ", aSMPBCProv.getAllSMLsToUse(), ISMLInfo::getDisplayName)));
            }
        }
    }
    final boolean bIsFormSubmitted = aWPEC.hasAction(CPageParam.ACTION_PERFORM);
    if (bIsFormSubmitted) {
        final IFileItem aFile = aWPEC.params().getAsFileItem(FIELD_FILE);
        if (aFile == null || StringHelper.hasNoText(aFile.getName()))
            aFormErrors.addFieldError(FIELD_FILE, "No file was selected");
        if (aFormErrors.isEmpty()) {
            final HCNodeList aResultNL = new HCNodeList();
            final SAXReaderSettings aSettings = new SAXReaderSettings();
            aSettings.setFeatureValues(EXMLParserFeature.AVOID_DOS_SETTINGS);
            final CollectingSAXErrorHandler aErrorHandler = new CollectingSAXErrorHandler();
            aSettings.setErrorHandler(aErrorHandler);
            final ICommonsList<IParticipantIdentifier> aQueued = new CommonsArrayList<>();
            final ICommonsList<IParticipantIdentifier> aNotQueued = new CommonsArrayList<>();
            aSettings.setContentHandler(new DefaultHandler() {

                @Override
                public void startElement(final String sURI, final String sLocalName, final String sQName, final Attributes aAttributes) throws SAXException {
                    if (sQName.equals("participant")) {
                        final String sScheme = aAttributes.getValue("scheme");
                        final String sValue = aAttributes.getValue("value");
                        final IParticipantIdentifier aParticipantID = aIdentifierFactory.createParticipantIdentifier(sScheme, sValue);
                        if (aParticipantID != null) {
                            if (PDMetaManager.getIndexerMgr().queueWorkItem(aParticipantID, EIndexerWorkItemType.CREATE_UPDATE, "import-triggered", PDIndexerManager.HOST_LOCALHOST).isChanged()) {
                                aQueued.add(aParticipantID);
                            } else {
                                aNotQueued.add(aParticipantID);
                            }
                        } else
                            LOGGER.error("Failed to convert '" + sScheme + "' and '" + sValue + "' to a participant identifier");
                    }
                }
            });
            LOGGER.info("Importing participant IDs from '" + aFile.getNameSecure() + "'");
            final ESuccess eSuccess = SAXReader.readXMLSAX(new FileItemResource(aFile), aSettings);
            LOGGER.info("Finished reading XML file. Queued " + aQueued.size() + "; not queued: " + aNotQueued.size() + "; errors: " + aErrorHandler.getErrorList().size());
            // Some things may have been queued even in case of error
            if (aQueued.isNotEmpty()) {
                final HCUL aUL = new HCUL();
                for (final IParticipantIdentifier aPI : aQueued) aUL.addItem(aPI.getURIEncoded());
                aResultNL.addChild(success(div("The following identifiers were successfully queued for indexing:")).addChild(aUL));
            }
            if (aNotQueued.isNotEmpty()) {
                final HCUL aUL = new HCUL();
                for (final IParticipantIdentifier aPI : aNotQueued) aUL.addItem(aPI.getURIEncoded());
                aResultNL.addChild(warn(div("The following identifiers could not be queued (because they are already in the queue):")).addChild(aUL));
            }
            if (eSuccess.isFailure()) {
                final HCUL aUL = new HCUL();
                for (final IError aError : aErrorHandler.getErrorList()) {
                    final String sMsg = aError.getAsString(AppCommonUI.DEFAULT_LOCALE);
                    LOGGER.error("  " + sMsg);
                    aUL.addItem(sMsg);
                }
                aResultNL.addChild(error(div("Error parsing provided XML:")).addChild(aUL));
            }
            aWPEC.postRedirectGetInternal(aResultNL);
        }
    }
    final BootstrapForm aForm = aNodeList.addAndReturnChild(getUIHandler().createFormFileUploadSelf(aWPEC, bIsFormSubmitted));
    aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Import file").setCtrl(new BootstrapFileUpload(FIELD_FILE, aDisplayLocale)).setHelpText("Select a file that was created from a full XML export to index of all them manually.").setErrorList(aFormErrors.getListOfField(FIELD_FILE)));
    final BootstrapButtonToolbar aToolbar = aForm.addAndReturnChild(new BootstrapButtonToolbar(aWPEC));
    aToolbar.addHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM);
    aToolbar.addSubmitButton("Import all", EDefaultIcon.YES);
}
Also used : Locale(java.util.Locale) HCNodeList(com.helger.html.hc.impl.HCNodeList) CollectingSAXErrorHandler(com.helger.xml.sax.CollectingSAXErrorHandler) ISMLInfo(com.helger.peppol.sml.ISMLInfo) FormErrorList(com.helger.photon.core.form.FormErrorList) Attributes(org.xml.sax.Attributes) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) SAXException(org.xml.sax.SAXException) BootstrapFileUpload(com.helger.photon.bootstrap4.uictrls.ext.BootstrapFileUpload) IFileItem(com.helger.web.fileupload.IFileItem) BootstrapButtonToolbar(com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) SMPBusinessCardProvider(com.helger.pd.indexer.businesscard.SMPBusinessCardProvider) ESuccess(com.helger.commons.state.ESuccess) IError(com.helger.commons.error.IError) FileItemResource(com.helger.web.fileupload.FileItemResource) DefaultHandler(org.xml.sax.helpers.DefaultHandler) BootstrapForm(com.helger.photon.bootstrap4.form.BootstrapForm) IPDBusinessCardProvider(com.helger.pd.indexer.businesscard.IPDBusinessCardProvider) HCUL(com.helger.html.hc.html.grouping.HCUL) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Aggregations

IPDBusinessCardProvider (com.helger.pd.indexer.businesscard.IPDBusinessCardProvider)3 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)3 ESuccess (com.helger.commons.state.ESuccess)2 HCNodeList (com.helger.html.hc.impl.HCNodeList)2 SMPBusinessCardProvider (com.helger.pd.indexer.businesscard.SMPBusinessCardProvider)2 ISMLInfo (com.helger.peppol.sml.ISMLInfo)2 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)2 BootstrapButtonToolbar (com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar)2 BootstrapForm (com.helger.photon.bootstrap4.form.BootstrapForm)2 BootstrapFormGroup (com.helger.photon.bootstrap4.form.BootstrapFormGroup)2 FormErrorList (com.helger.photon.core.form.FormErrorList)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 IError (com.helger.commons.error.IError)1 HCEdit (com.helger.html.hc.html.forms.HCEdit)1 HCUL (com.helger.html.hc.html.grouping.HCUL)1 PDExtendedBusinessCard (com.helger.pd.indexer.businesscard.PDExtendedBusinessCard)1 BootstrapFileUpload (com.helger.photon.bootstrap4.uictrls.ext.BootstrapFileUpload)1 RequestField (com.helger.photon.core.form.RequestField)1 FileItemResource (com.helger.web.fileupload.FileItemResource)1 IFileItem (com.helger.web.fileupload.IFileItem)1