package com.izouma.nineth.converter;

import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Converter
public class StringArrayConverter implements AttributeConverter<List<String>, String> {
    @Override
    public String convertToDatabaseColumn(List<String> strings) {
        if (strings != null && !strings.isEmpty())
            return StringUtils.join(strings, ",");
        return null;
    }

    @Override
    public List<String> convertToEntityAttribute(String s) {
        if (StringUtils.isNotEmpty(s)) {
            return Arrays.asList(s.split(","));
        }
        return new ArrayList<>();
    }
}
