dnet-applications/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java

137 lines
4.6 KiB
Java

package eu.dnetlib.is.vocabulary;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.is.vocabulary.model.Vocabulary;
import eu.dnetlib.is.vocabulary.model.VocabularyTerm;
import eu.dnetlib.is.vocabulary.model.VocabularyTermPK;
import eu.dnetlib.is.vocabulary.repository.VocabularyRepository;
import eu.dnetlib.is.vocabulary.repository.VocabularyTermRepository;
@RestController
@RequestMapping("/api/vocs")
public class VocabularyRestController extends AbstractDnetController {
@Autowired
private VocabularyRepository vocabularyRepository;
@Autowired
private VocabularyTermRepository vocabularyTermRepository;
private static final Log log = LogFactory.getLog(VocabularyRestController.class);
@GetMapping("/")
public List<Vocabulary> listVocs() {
return vocabularyRepository.findAll()
.stream()
.sorted((v1, v2) -> StringUtils.compare(v1.getName(), v2.getName()))
.collect(Collectors.toList());
}
@GetMapping("/{vocabulary}")
public Vocabulary getVoc(@PathVariable final String vocabulary) {
return vocabularyRepository.getById(vocabulary);
}
@DeleteMapping("/{vocabulary}")
public List<Vocabulary> deleteVocs(@PathVariable final String vocabulary) {
log.info("Deleting vocabulary: " + vocabulary);
vocabularyRepository.deleteById(vocabulary);
return listVocs();
}
@PostMapping("/")
public List<Vocabulary> saveVoc(@RequestBody final Vocabulary voc) {
log.info("Saving vocabulary: " + voc);
vocabularyRepository.save(voc);
return listVocs();
}
@PostMapping(value = "/load", consumes = "text/plain")
public Vocabulary loadFromOldProfile(final HttpServletRequest request) throws DocumentException, IOException {
final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
System.out.println(xml);
final SAXReader reader = new SAXReader();
final Document doc = reader.read(new StringReader(xml));
final Vocabulary voc = new Vocabulary();
final String vocId = doc.valueOf("//VOCABULARY_NAME/@code");
final String vocName = doc.valueOf("//VOCABULARY_NAME");
final String vocDesc = doc.valueOf("//VOCABULARY_DESCRIPTION");
voc.setId(vocId);
voc.setName(vocName);
voc.setDescription(vocDesc);
vocabularyRepository.save(voc);
for (final Node n : doc.selectNodes("//TERM")) {
final VocabularyTerm term = new VocabularyTerm();
term.setVocabulary(vocId);
term.setCode(n.valueOf("@code"));
term.setEnglishName(n.valueOf("@english_name"));
term.setNativeName(n.valueOf("@native_name"));
term.setSynonyms(n.selectNodes(".//SYNONYM")
.stream()
.map(ns -> ns.valueOf("@term"))
.sorted()
.distinct()
.toArray(String[]::new));
vocabularyTermRepository.save(term);
}
return voc;
}
@GetMapping("/{vocabulary}/terms")
public Iterable<VocabularyTerm> listTerms(@PathVariable final String vocabulary) {
return vocabularyTermRepository.findByVocabularyOrderByCode(vocabulary);
}
@PostMapping("/{vocabulary}/terms")
public Iterable<VocabularyTerm> saveTerm(@PathVariable final String vocabulary, @RequestParam final VocabularyTerm term) {
term.setVocabulary(vocabulary);
vocabularyTermRepository.save(term);
return vocabularyTermRepository.findByVocabularyOrderByCode(vocabulary);
}
@DeleteMapping("/{vocabulary}/terms/{termId}")
public Iterable<VocabularyTerm> listTerms(@PathVariable final String vocabulary, @PathVariable final String term) {
final VocabularyTermPK pk = new VocabularyTermPK();
pk.setCode(term);
pk.setVocabulary(vocabulary);
vocabularyTermRepository.deleteById(pk);
return vocabularyTermRepository.findByVocabularyOrderByCode(vocabulary);
}
}