Java tutorial
/* * 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.parametricvalues; import com.autonomy.aci.client.services.AciErrorException; import com.autonomy.aci.client.services.AciService; import com.autonomy.aci.client.services.Processor; import com.autonomy.aci.client.util.AciParameters; import com.hp.autonomy.idolutils.processors.AciResponseJaxbProcessorFactory; import com.hp.autonomy.searchcomponents.core.fields.FieldsService; import com.hp.autonomy.searchcomponents.core.parametricvalues.ParametricValuesService; import com.hp.autonomy.searchcomponents.idol.fields.IdolFieldsRequest; import com.hp.autonomy.searchcomponents.idol.search.HavenSearchAciParameterHandler; import com.hp.autonomy.types.idol.FlatField; import com.hp.autonomy.types.idol.GetQueryTagValuesResponseData; import com.hp.autonomy.types.idol.RecursiveField; import com.hp.autonomy.types.idol.TagValue; import com.hp.autonomy.types.requests.idol.actions.tags.QueryTagCountInfo; import com.hp.autonomy.types.requests.idol.actions.tags.QueryTagInfo; import com.hp.autonomy.types.requests.idol.actions.tags.TagActions; import com.hp.autonomy.types.requests.idol.actions.tags.params.GetQueryTagValuesParams; import com.hp.autonomy.types.requests.idol.actions.tags.params.SortParam; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.xml.bind.JAXBElement; import java.io.Serializable; import java.util.*; @Service public class IdolParametricValuesService implements ParametricValuesService<IdolParametricRequest, String, AciErrorException> { private static final String VALUE_NODE_NAME = "value"; private final HavenSearchAciParameterHandler parameterHandler; private final FieldsService<IdolFieldsRequest, AciErrorException> fieldsService; private final AciService contentAciService; private final Processor<GetQueryTagValuesResponseData> queryTagValuesResponseProcessor; @Autowired public IdolParametricValuesService(final HavenSearchAciParameterHandler parameterHandler, final FieldsService<IdolFieldsRequest, AciErrorException> fieldsService, final AciService contentAciService, final AciResponseJaxbProcessorFactory aciResponseProcessorFactory) { this.parameterHandler = parameterHandler; this.fieldsService = fieldsService; this.contentAciService = contentAciService; queryTagValuesResponseProcessor = aciResponseProcessorFactory .createAciResponseProcessor(GetQueryTagValuesResponseData.class); } @Override public Set<QueryTagInfo> getAllParametricValues(final IdolParametricRequest parametricRequest) throws AciErrorException { final Collection<String> fieldNames = new HashSet<>(); fieldNames.addAll(parametricRequest.getFieldNames()); if (fieldNames.isEmpty()) { fieldNames.addAll(fieldsService.getParametricFields(new IdolFieldsRequest.Builder().build())); } final Set<QueryTagInfo> results; if (fieldNames.isEmpty()) { results = Collections.emptySet(); } else { final AciParameters aciParameters = new AciParameters(TagActions.GetQueryTagValues.name()); parameterHandler.addSearchRestrictions(aciParameters, parametricRequest.getQueryRestrictions()); if (parametricRequest.isModified()) { parameterHandler.addQmsParameters(aciParameters, parametricRequest.getQueryRestrictions()); } aciParameters.add(GetQueryTagValuesParams.DocumentCount.name(), true); aciParameters.add(GetQueryTagValuesParams.MaxValues.name(), parametricRequest.getMaxValues()); aciParameters.add(GetQueryTagValuesParams.FieldName.name(), StringUtils.join(fieldNames.toArray(), ',')); aciParameters.add(GetQueryTagValuesParams.Sort.name(), SortParam.DocumentCount.name()); final GetQueryTagValuesResponseData responseData = contentAciService.executeAction(aciParameters, queryTagValuesResponseProcessor); final List<FlatField> fields = responseData.getField(); results = new LinkedHashSet<>(fields.size()); for (final FlatField field : fields) { final List<JAXBElement<? extends Serializable>> valueElements = field.getValueOrSubvalueOrValues(); final LinkedHashSet<QueryTagCountInfo> values = new LinkedHashSet<>(valueElements.size()); for (final JAXBElement<?> element : valueElements) { if (VALUE_NODE_NAME.equals(element.getName().getLocalPart())) { final TagValue tagValue = (TagValue) element.getValue(); values.add(new QueryTagCountInfo(tagValue.getValue(), tagValue.getCount())); } } final String fieldName = getFieldNameFromPath(field.getName().get(0)); if (!values.isEmpty()) { results.add(new QueryTagInfo(fieldName, values)); } } } return results; } @Override public List<RecursiveField> getDependentParametricValues(final IdolParametricRequest parametricRequest) throws AciErrorException { final List<String> fieldNames = new ArrayList<>(); fieldNames.addAll(parametricRequest.getFieldNames()); if (fieldNames.isEmpty()) { fieldNames.addAll(fieldsService.getParametricFields(new IdolFieldsRequest.Builder().build())); } final List<RecursiveField> results; if (fieldNames.isEmpty()) { results = Collections.emptyList(); } else { final AciParameters aciParameters = new AciParameters(TagActions.GetQueryTagValues.name()); parameterHandler.addSearchRestrictions(aciParameters, parametricRequest.getQueryRestrictions()); if (parametricRequest.isModified()) { parameterHandler.addQmsParameters(aciParameters, parametricRequest.getQueryRestrictions()); } aciParameters.add(GetQueryTagValuesParams.DocumentCount.name(), true); aciParameters.add(GetQueryTagValuesParams.FieldName.name(), StringUtils.join(fieldNames.toArray(), ',')); aciParameters.add(GetQueryTagValuesParams.FieldDependence.name(), true); aciParameters.add(GetQueryTagValuesParams.FieldDependenceMultiLevel.name(), true); final GetQueryTagValuesResponseData responseData = contentAciService.executeAction(aciParameters, queryTagValuesResponseProcessor); results = responseData.getValues().getField(); } return results; } private String getFieldNameFromPath(final String value) { return value.contains("/") ? value.substring(value.lastIndexOf('/') + 1) : value; } }