tv.arte.resteventapi.core.querying.convertion.QueryOpParamGenericConverter.java Source code

Java tutorial

Introduction

Here is the source code for tv.arte.resteventapi.core.querying.convertion.QueryOpParamGenericConverter.java

Source

package tv.arte.resteventapi.core.querying.convertion;

/*
 * #%L
 * RestEventAPI
 * %%
 * Copyright (C) 2014 ARTE G.E.I.E
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of The MIT License (MIT) as published by the Open Source 
 * Initiative.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See The 
 * MIT License (MIT) for more details.
 * 
 * You should have received a copy of The MIT License (MIT) 
 * along with this program.  If not, see <http://opensource.org/licenses/MIT>
 * #L%
 */

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.stereotype.Component;

import tv.arte.resteventapi.core.RestEventApiRuntimeException;
import tv.arte.resteventapi.core.querying.QueryOp;
import tv.arte.resteventapi.core.querying.QueryOpParamValue;

/**
 * Custom converter for QueryOp
 * 
 * @author Simeon Petev
 * @since 0.1
 */
@Component
@RestEventApiConverter
public class QueryOpParamGenericConverter extends AbstractBaseRestEventApiParamValueConverter
        implements GenericConverter {

    /**
     * {@inheritDoc}
     */
    public Set<ConvertiblePair> getConvertibleTypes() {
        HashSet<GenericConverter.ConvertiblePair> convertibleTypes = new HashSet<GenericConverter.ConvertiblePair>();

        convertibleTypes.add(new ConvertiblePair(String.class, QueryOpParamValue.class));
        convertibleTypes.add(new ConvertiblePair(QueryOpParamValue.class, String.class));

        return convertibleTypes;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

        Object result = null;

        if (sourceType.getResolvableType().resolve().equals(String.class)
                && targetType.getResolvableType().resolve().equals(QueryOpParamValue.class)) {
            QueryOpParamValue queryOpParamValue = new QueryOpParamValue();

            ResolvableType genericResolvableType = targetType.getResolvableType().getGeneric(0);
            TypeDescriptor finalTargerTypeDescriptor = null;

            if (ResolvableType.NONE.equals(genericResolvableType)) {
                finalTargerTypeDescriptor = TypeDescriptor.valueOf(String.class);
            } else {
                finalTargerTypeDescriptor = new RestEventApiTypeDescriptor(genericResolvableType, null,
                        genericResolvableType.resolve().getAnnotations());
            }

            int queryOpSeparatorPosition = this.queryOpValueSeparatorPosition((String) source);
            QueryOp queryOp = this.extractQueryOp((String) source);
            String valuePart = this.extractValueString(queryOpSeparatorPosition, queryOp, (String) source);

            queryOpParamValue.setQueryOp(queryOp);
            queryOpParamValue.setValue(conversionService.convert(valuePart, sourceType, finalTargerTypeDescriptor));

            result = queryOpParamValue;
        } else if (sourceType.getResolvableType().resolve().equals(QueryOpParamValue.class)
                && targetType.getResolvableType().resolve().equals(String.class)) {
            QueryOpParamValue valToVonvert = (QueryOpParamValue) source;

            if (valToVonvert.getValue() != null) {
                String valueStr = conversionService.convert(valToVonvert.getValue(), String.class);
                if (StringUtils.isNotBlank(valueStr)) {
                    result = this.assembleQueryOpValue(valToVonvert.getQueryOp(), valueStr);
                }
            }
        } else {
            throw new RestEventApiRuntimeException("Uncompatible conversion");
        }

        return result;
    }

}