package org.leialearns.neo4jproblem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class AlphabetDAO {
    private final Logger logger = LoggerFactory.getLogger(AlphabetDAO.class);

    @Autowired
    AlphabetRepository repository;

    public AlphabetDTO find(String uri) {
        AlphabetDTO result = repository.getAlphabetByUri(uri);
        logger.debug("Find: {}: {}", uri, result);
        return result;
    }

    public AlphabetDTO findOrCreate(String uri) {
        AlphabetDTO alphabet = find(uri);
        if (alphabet == null) {
            alphabet = new AlphabetDTO();
            alphabet.setURI(uri);
            logger.debug("Alphabet: {}", alphabet);
            alphabet = repository.save(alphabet);
            logger.debug("Alphabet: {}", alphabet);
        }
        logger.debug("Alphabet: " + alphabet.toString());
        return alphabet;
    }
}
