package com.izouma.nineth.converter;

import com.alibaba.fastjson.JSON;
import com.izouma.nineth.domain.DropTarget;
import com.izouma.nineth.domain.FileObject;
import org.apache.commons.lang3.StringUtils;

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

@Converter
public class DropTargetConverter implements AttributeConverter<List<DropTarget>, String> {
    @Override
    public String convertToDatabaseColumn(List<DropTarget> dropTargets) {
        if (dropTargets != null) {
            return JSON.toJSONString(dropTargets);
        }
        return null;
    }

    @Override
    public List<DropTarget> convertToEntityAttribute(String s) {
        if (StringUtils.isNotEmpty(s)) {
            try {
                return JSON.parseArray(s, DropTarget.class);
            } catch (Exception ignored) {
            }
        }
        return null;
    }
}
