com.exxonmobile.ace.hybris.core.search.solrfacetsearch.provider.impl.MultidimentionalProductFlagValueProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.exxonmobile.ace.hybris.core.search.solrfacetsearch.provider.impl.MultidimentionalProductFlagValueProvider.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2013 hybris AG
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of hybris
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with hybris.
 * 
 *  
 */
package com.exxonmobile.ace.hybris.core.search.solrfacetsearch.provider.impl;

import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.solrfacetsearch.config.IndexConfig;
import de.hybris.platform.solrfacetsearch.config.IndexedProperty;
import de.hybris.platform.solrfacetsearch.config.exceptions.FieldValueProviderException;
import de.hybris.platform.solrfacetsearch.provider.FieldNameProvider;
import de.hybris.platform.solrfacetsearch.provider.FieldValue;
import de.hybris.platform.solrfacetsearch.provider.FieldValueProvider;
import de.hybris.platform.solrfacetsearch.provider.impl.AbstractPropertyFieldValueProvider;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Required;

/**
 * Retrives the multidimentional flag for a product model.
 */
public class MultidimentionalProductFlagValueProvider extends AbstractPropertyFieldValueProvider
        implements FieldValueProvider, Serializable {

    private FieldNameProvider fieldNameProvider;

    protected FieldNameProvider getFieldNameProvider() {
        return fieldNameProvider;
    }

    @Required
    public void setFieldNameProvider(final FieldNameProvider fieldNameProvider) {
        this.fieldNameProvider = fieldNameProvider;
    }

    @Override
    public Collection<FieldValue> getFieldValues(final IndexConfig indexConfig,
            final IndexedProperty indexedProperty, final Object model) throws FieldValueProviderException {
        final List<FieldValue> fieldValues = new ArrayList<FieldValue>();
        if (model instanceof ProductModel) {
            final Object variants = modelService.getAttributeValue(model, "variants");

            final boolean isMultidimentional = CollectionUtils.isNotEmpty((Collection) variants);
            addFieldValues(fieldValues, indexedProperty, Boolean.valueOf(isMultidimentional));
        } else {
            throw new FieldValueProviderException("Cannot tell if it is multidimentional for non-product item");
        }

        return fieldValues;
    }

    protected void addFieldValues(final List<FieldValue> fieldValues, final IndexedProperty indexedProperty,
            final Object value) {
        final Collection<String> fieldNames = getFieldNameProvider().getFieldNames(indexedProperty, null);
        for (final String fieldName : fieldNames) {
            fieldValues.add(new FieldValue(fieldName, value));
        }
    }

}