All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hp.autonomy.searchcomponents.idol.search.fields.FieldsParserImpl Maven / Gradle / Ivy

/*
 * Copyright 2015 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.searchcomponents.idol.search.fields;

import com.hp.autonomy.frontend.configuration.ConfigService;
import com.hp.autonomy.searchcomponents.core.config.FieldAssociations;
import com.hp.autonomy.searchcomponents.core.config.FieldInfo;
import com.hp.autonomy.searchcomponents.core.config.FieldType;
import com.hp.autonomy.searchcomponents.core.config.FieldsInfo;
import com.hp.autonomy.searchcomponents.core.search.PromotionCategory;
import com.hp.autonomy.searchcomponents.idol.configuration.IdolSearchCapable;
import com.hp.autonomy.searchcomponents.idol.search.IdolSearchResult;
import com.hp.autonomy.types.idol.DocContent;
import com.hp.autonomy.types.idol.Hit;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class FieldsParserImpl implements FieldsParser {
    private final ConfigService configService;

    @Autowired
    public FieldsParserImpl(final ConfigService configService) {
        this.configService = configService;
    }

    @Override
    public void parseDocumentFields(final Hit hit, final IdolSearchResult.Builder searchResultBuilder) {
        final FieldsInfo fieldsInfo = configService.getConfig().getFieldsInfo();
        final FieldAssociations fieldAssociations = fieldsInfo.getFieldAssociations();
        final Set> customFields = fieldsInfo.getCustomFields();

        final DocContent content = hit.getContent();
        Map> fieldMap = Collections.emptyMap();
        String qmsId = null;
        PromotionCategory promotionCategory = PromotionCategory.NONE;
        if (content != null) {
            final Element docContent = (Element) content.getContent().get(0);
            if (docContent.hasChildNodes()) {
                final NodeList childNodes = docContent.getChildNodes();
                fieldMap = new HashMap<>(childNodes.getLength());

                for (final FieldInfo customField : customFields) {
                    final List values = parseFields(docContent, customField.getName(), customField.getType(), customField.getType().getType());
                    if (CollectionUtils.isNotEmpty(values)) {
                        addFieldToResultMap(fieldMap, customField, values);
                    }
                }

                qmsId = parseField(docContent, IdolDocumentFieldsService.QMS_ID_FIELD_INFO, String.class);
                promotionCategory = determinePromotionCategory(docContent, hit.getPromotionname(), hit.getDatabase());
            }
        }

        searchResultBuilder
                .setFieldMap(fieldMap)
                .setContentType(readField(fieldAssociations.getMediaContentType(), fieldMap, String.class))
                .setUrl(readField(fieldAssociations.getMediaUrl(), fieldMap, String.class))
                .setOffset(readField(fieldAssociations.getMediaOffset(), fieldMap, String.class))
                .setAuthors(readFields(fieldAssociations.getAuthor(), fieldMap, String.class))
                .setMmapUrl(readField(fieldAssociations.getMmapUrl(), fieldMap, String.class))
                .setThumbnail(readField(fieldAssociations.getThumbnail(), fieldMap, String.class))
                .setQmsId(qmsId)
                .setPromotionCategory(promotionCategory);
    }

    private void addFieldToResultMap(final Map> fieldMap, final FieldInfo fieldInfo, final List values) {
        fieldMap.put(fieldInfo.getName(), new FieldInfo<>(fieldInfo.getName(), fieldInfo.getDisplayName(), fieldInfo.getType(), values));
    }

    private  T readField(final String name, final Map> fieldMap, final Class type) {
        return fieldMap.containsKey(name) ? type.cast(fieldMap.get(name).getValues().iterator().next()) : null;
    }

    @SuppressWarnings("UnusedParameters")
    private  List readFields(final String name, final Map> fieldMap, final Class type) {
        //noinspection unchecked
        return fieldMap.containsKey(name) ? (List) fieldMap.get(name).getValues() : Collections.emptyList();
    }

    private PromotionCategory determinePromotionCategory(final Element docContent, final CharSequence promotionName, final CharSequence database) {
        final PromotionCategory promotionCategory;
        final Boolean injectedPromotion = parseField(docContent, IdolDocumentFieldsService.INJECTED_PROMOTION_FIELD_INFO, Boolean.class);
        if (injectedPromotion != null && injectedPromotion) {
            promotionCategory = PromotionCategory.CARDINAL_PLACEMENT;
        } else if (StringUtils.isNotEmpty(promotionName)) {
            // If the database isn't found, then assume it is a static content promotion
            promotionCategory = StringUtils.isNotEmpty(database) ? PromotionCategory.SPOTLIGHT : PromotionCategory.STATIC_CONTENT_PROMOTION;
        } else {
            promotionCategory = PromotionCategory.NONE;
        }

        return promotionCategory;
    }

    private  List parseFields(final Element node, final String name, final FieldType fieldType, final Class type) {
        final NodeList childNodes = node.getElementsByTagName(name.toUpperCase());
        final int length = childNodes.getLength();
        final List values = new ArrayList<>(length);

        for (int i = 0; i < length; i++) {
            final Node childNode = childNodes.item(i);
            values.add(fieldType.parseValue(type, childNode.getFirstChild().getNodeValue()));
        }

        return values;
    }

    private  T parseField(final Element node, final FieldInfo fieldInfo, final Class type) {
        final List fields = parseFields(node, fieldInfo.getName(), fieldInfo.getType(), type);
        return CollectionUtils.isNotEmpty(fields) ? fields.get(0) : null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy