converters.StringToValuedConverter.java Source code

Java tutorial

Introduction

Here is the source code for converters.StringToValuedConverter.java

Source

/* StringToCurriculumConverter.java
 *
 * Copyright (C) 2016 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package converters;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import repositories.ValuedRepository;
import domain.Valued;

@Component
@Transactional
public class StringToValuedConverter implements Converter<String, Valued> {

    @Autowired
    ValuedRepository valuedRepository;

    @Override
    public Valued convert(String text) {
        Valued result;
        int id;

        try {
            if (StringUtils.isEmpty(text))
                result = null;
            else {
                id = Integer.valueOf(text);
                result = valuedRepository.findOne(id);
            }
        } catch (Throwable oops) {
            throw new IllegalArgumentException(oops);
        }

        return result;
    }

}