001package springdao.support;
002
003import java.beans.PropertyEditorSupport;
004import org.springframework.util.StringUtils;
005import springdao.RepositoryManager;
006
007/**
008 *
009 * @author Kent Yeh
010 */
011public class DaoPropertyEditor extends PropertyEditorSupport {
012
013    private final RepositoryManager<?> daoManager;
014
015    public DaoPropertyEditor(RepositoryManager<?> daoManager) {
016        this.daoManager = daoManager;
017    }
018
019    public RepositoryManager<?> getDaoManager() {
020        return daoManager;
021    }
022
023    @Override
024    public String getAsText() {
025        return getValue() == null ? "" : getValue().toString();
026    }
027
028    @Override
029    public void setAsText(String text) throws IllegalArgumentException {
030        try {
031            setValue(StringUtils.hasText(text) ? daoManager.findByPrimaryKey(text) : null);
032        } catch (RuntimeException e) {
033            setValue(null);
034        }
035    }
036}