diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java index 50ba0b14..dddc34b4 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java @@ -1,120 +1,52 @@ package eu.dnetlib.is.context; import java.util.List; -import java.util.stream.Collectors; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import eu.dnetlib.common.controller.AbstractDnetController; import eu.dnetlib.is.context.model.Category; -import eu.dnetlib.is.context.model.ConceptLevel0; -import eu.dnetlib.is.context.model.ConceptLevel1; -import eu.dnetlib.is.context.model.ConceptLevel2; import eu.dnetlib.is.context.model.Context; import eu.dnetlib.is.context.model.CtxChildInfo; -import eu.dnetlib.is.context.model.repository.CategoryRepository; -import eu.dnetlib.is.context.model.repository.ConceptLevel0Repository; -import eu.dnetlib.is.context.model.repository.ConceptLevel1Repository; -import eu.dnetlib.is.context.model.repository.ConceptLevel2Repository; -import eu.dnetlib.is.context.model.repository.ContextRepository; +import eu.dnetlib.is.util.InformationServiceException; @RestController @RequestMapping("/api/contexts") -public class ContextRestController { +public class ContextRestController extends AbstractDnetController { @Autowired - private ContextRepository contextRepository; - @Autowired - private CategoryRepository categoryRepository; - @Autowired - private ConceptLevel0Repository conceptLevel0Repository; - @Autowired - private ConceptLevel1Repository conceptLevel1Repository; - @Autowired - private ConceptLevel2Repository conceptLevel2Repository; - - private static final Log log = LogFactory.getLog(ContextRestController.class); + private ContextService contextService; @GetMapping("/") public List listContexts() { - return contextRepository.findAll() - .stream() - .sorted((v1, v2) -> StringUtils.compareIgnoreCase(v1.getId(), v2.getId())) - .collect(Collectors.toList()); + return contextService.listContexts(); } @GetMapping("/{ctxId}") - public Context getContext(@PathVariable final String ctxId) { - return contextRepository.findById(ctxId).get(); + public Context getContext(@PathVariable final String ctxId) throws InformationServiceException { + return contextService.getContext(ctxId); } @GetMapping("/{ctxId}/full") - public ObjectNode getContextFull(@PathVariable final String ctxId) { - - final ObjectMapper mapper = new ObjectMapper(); - final Context ctx = contextRepository.findById(ctxId).get(); - - final ObjectNode ctxNode = mapper.convertValue(ctx, ObjectNode.class); - - final ArrayNode catArray = mapper.createArrayNode(); - - for (final Category cat : categoryRepository.findByParentOrderById(ctxId)) { - final ObjectNode catNode = mapper.convertValue(cat, ObjectNode.class); - catArray.add(catNode); - - final ArrayNode c0Array = mapper.createArrayNode(); - for (final ConceptLevel0 c0 : conceptLevel0Repository.findByParentOrderById(cat.getId())) { - final ObjectNode c0Node = mapper.convertValue(c0, ObjectNode.class); - c0Array.add(c0Node); - - final ArrayNode c1Array = mapper.createArrayNode(); - for (final ConceptLevel1 c1 : conceptLevel1Repository.findByParentOrderById(c0.getId())) { - final ObjectNode c1Node = mapper.convertValue(c1, ObjectNode.class); - c1Array.add(c1Node); - - final ArrayNode c2Array = mapper.createArrayNode(); - for (final ConceptLevel2 c2 : conceptLevel2Repository.findByParentOrderById(c1.getId())) { - final ObjectNode c2Node = mapper.convertValue(c2, ObjectNode.class); - c2Array.add(c2Node); - } - c1Node.set("concepts", c2Array); - } - c0Node.set("concepts", c1Array); - } - catNode.set("concepts", c0Array); - } - ctxNode.set("categories", catArray); - - return ctxNode; + public ObjectNode getContextFull(@PathVariable final String ctxId) throws InformationServiceException { + return contextService.getContextFull(ctxId); } @GetMapping("/{parent}/categories") public Iterable listCategories(@PathVariable final String parent) { - return categoryRepository.findByParentOrderById(parent); + return contextService.listCategories(parent); } @GetMapping("/{level}/{parent}/concepts") - public Iterable listCategories(@PathVariable final int level, @PathVariable final String parent) { - switch (level) { - case 0: - return conceptLevel0Repository.findByParentOrderById(parent); - case 1: - return conceptLevel1Repository.findByParentOrderById(parent); - case 2: - return conceptLevel2Repository.findByParentOrderById(parent); - default: - throw new RuntimeException("Invalid concept level - valid levels are 0, 1, 2"); - } + public Iterable listCategories(@PathVariable final int level, @PathVariable final String parent) + throws InformationServiceException { + return contextService.listCategories(level, parent); } } diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextService.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextService.java new file mode 100644 index 00000000..86e16307 --- /dev/null +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextService.java @@ -0,0 +1,112 @@ +package eu.dnetlib.is.context; + +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import eu.dnetlib.is.context.model.Category; +import eu.dnetlib.is.context.model.ConceptLevel0; +import eu.dnetlib.is.context.model.ConceptLevel1; +import eu.dnetlib.is.context.model.ConceptLevel2; +import eu.dnetlib.is.context.model.Context; +import eu.dnetlib.is.context.model.CtxChildInfo; +import eu.dnetlib.is.context.model.repository.CategoryRepository; +import eu.dnetlib.is.context.model.repository.ConceptLevel0Repository; +import eu.dnetlib.is.context.model.repository.ConceptLevel1Repository; +import eu.dnetlib.is.context.model.repository.ConceptLevel2Repository; +import eu.dnetlib.is.context.model.repository.ContextRepository; +import eu.dnetlib.is.util.InformationServiceException; + +@Service +public class ContextService { + + @Autowired + private ContextRepository contextRepository; + @Autowired + private CategoryRepository categoryRepository; + @Autowired + private ConceptLevel0Repository conceptLevel0Repository; + @Autowired + private ConceptLevel1Repository conceptLevel1Repository; + @Autowired + private ConceptLevel2Repository conceptLevel2Repository; + + private static final Log log = LogFactory.getLog(ContextService.class); + + public List listContexts() { + return contextRepository.findAll() + .stream() + .sorted((v1, v2) -> StringUtils.compareIgnoreCase(v1.getId(), v2.getId())) + .collect(Collectors.toList()); + } + + public Context getContext(final String ctxId) throws InformationServiceException { + return contextRepository.findById(ctxId).orElseThrow(() -> new InformationServiceException("Context Not found")); + } + + public ObjectNode getContextFull(final String ctxId) throws InformationServiceException { + + final ObjectMapper mapper = new ObjectMapper(); + final Context ctx = getContext(ctxId); + + final ObjectNode ctxNode = mapper.convertValue(ctx, ObjectNode.class); + + final ArrayNode catArray = mapper.createArrayNode(); + + for (final Category cat : categoryRepository.findByParentOrderById(ctxId)) { + final ObjectNode catNode = mapper.convertValue(cat, ObjectNode.class); + catArray.add(catNode); + + final ArrayNode c0Array = mapper.createArrayNode(); + for (final ConceptLevel0 c0 : conceptLevel0Repository.findByParentOrderById(cat.getId())) { + final ObjectNode c0Node = mapper.convertValue(c0, ObjectNode.class); + c0Array.add(c0Node); + + final ArrayNode c1Array = mapper.createArrayNode(); + for (final ConceptLevel1 c1 : conceptLevel1Repository.findByParentOrderById(c0.getId())) { + final ObjectNode c1Node = mapper.convertValue(c1, ObjectNode.class); + c1Array.add(c1Node); + + final ArrayNode c2Array = mapper.createArrayNode(); + for (final ConceptLevel2 c2 : conceptLevel2Repository.findByParentOrderById(c1.getId())) { + final ObjectNode c2Node = mapper.convertValue(c2, ObjectNode.class); + c2Array.add(c2Node); + } + c1Node.set("concepts", c2Array); + } + c0Node.set("concepts", c1Array); + } + catNode.set("concepts", c0Array); + } + ctxNode.set("categories", catArray); + + return ctxNode; + } + + public Iterable listCategories(final String parent) { + return categoryRepository.findByParentOrderById(parent); + } + + public Iterable listCategories(final int level, final String parent) throws InformationServiceException { + switch (level) { + case 0: + return conceptLevel0Repository.findByParentOrderById(parent); + case 1: + return conceptLevel1Repository.findByParentOrderById(parent); + case 2: + return conceptLevel2Repository.findByParentOrderById(parent); + default: + throw new InformationServiceException("Invalid concept level - valid levels are 0, 1, 2"); + } + } + +} diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ContextImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ContextImporter.java index 0b72604e..1d5d758d 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ContextImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ContextImporter.java @@ -11,7 +11,7 @@ import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import eu.dnetlib.is.context.model.Category; import eu.dnetlib.is.context.model.ConceptLevel0; @@ -27,7 +27,7 @@ import eu.dnetlib.is.context.model.repository.ConceptLevel1Repository; import eu.dnetlib.is.context.model.repository.ConceptLevel2Repository; import eu.dnetlib.is.context.model.repository.ContextRepository; -@Component +@Service public class ContextImporter { @Autowired diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java index d0f515b6..ef3d7359 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java @@ -17,12 +17,13 @@ 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.resource.model.SimpleResource; import eu.dnetlib.is.vocabulary.model.Vocabulary; @RestController @RequestMapping("/api/import") -public class ImporterController { +public class ImporterController extends AbstractDnetController { @Autowired private ContextImporter contextImporter; diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java index 9246b806..087a7b4e 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java @@ -11,7 +11,7 @@ import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import eu.dnetlib.is.resource.model.SimpleResource; import eu.dnetlib.is.resource.repository.SimpleResourceRepository; @@ -22,7 +22,7 @@ import eu.dnetlib.is.vocabulary.model.VocabularyTerm; import eu.dnetlib.is.vocabulary.repository.VocabularyRepository; import eu.dnetlib.is.vocabulary.repository.VocabularyTermRepository; -@Component +@Service public class OldProfilesImporter { @Autowired diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java index fde3928e..65ddc6b0 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java @@ -12,7 +12,7 @@ import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -20,7 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.is.wf.model.WfProcessExecution; import eu.dnetlib.is.wf.repository.WfProcessExecutionRepository; -@Component +@Service public class WfHistoryImporter { private static final Log log = LogFactory.getLog(WfHistoryImporter.class); diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java index 43465188..e8e5547a 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java @@ -19,12 +19,13 @@ 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.resource.model.SimpleResource; import eu.dnetlib.is.util.InformationServiceException; @RestController @RequestMapping("/api/resources") -public class ResourcesRestController { +public class ResourcesRestController extends AbstractDnetController { @Autowired private SimpleResourceService service; diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java index adf18fb8..8b9050a4 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java @@ -1,11 +1,7 @@ package eu.dnetlib.is.vocabulary; import java.util.List; -import java.util.stream.Collectors; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -16,74 +12,54 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import eu.dnetlib.common.controller.AbstractDnetController; -import eu.dnetlib.is.importer.OldProfilesImporter; +import eu.dnetlib.is.util.InformationServiceException; 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; - - @Autowired - private OldProfilesImporter oldProfilesImporter; - - private static final Log log = LogFactory.getLog(VocabularyRestController.class); + private VocabularyService vocabularyService; @GetMapping("/") public List listVocs() { - return vocabularyRepository.findAll() - .stream() - .sorted((v1, v2) -> StringUtils.compareIgnoreCase(v1.getId(), v2.getId())) - .collect(Collectors.toList()); + return vocabularyService.listVocs(); } @GetMapping("/{vocabulary}") - public Vocabulary getVoc(@PathVariable final String vocabulary) { - return vocabularyRepository.getById(vocabulary); + public Vocabulary getVoc(@PathVariable final String vocabulary) throws InformationServiceException { + return vocabularyService.getVoc(vocabulary); } @DeleteMapping("/{vocabulary}") public List deleteVocs(@PathVariable final String vocabulary) { - log.info("Deleting vocabulary: " + vocabulary); - vocabularyRepository.deleteById(vocabulary); - return listVocs(); + vocabularyService.deleteVocs(vocabulary); + return vocabularyService.listVocs(); } @PostMapping("/") public List saveVoc(@RequestBody final Vocabulary voc) { - log.info("Saving vocabulary: " + voc); - vocabularyRepository.save(voc); - return listVocs(); + vocabularyService.saveVoc(voc); + return vocabularyService.listVocs(); } @GetMapping("/{vocabulary}/terms") public Iterable listTerms(@PathVariable final String vocabulary) { - return vocabularyTermRepository.findByVocabularyOrderByCode(vocabulary); + return vocabularyService.listTerms(vocabulary); } @PostMapping("/{vocabulary}/terms") public Iterable saveTerm(@PathVariable final String vocabulary, @RequestBody final VocabularyTerm term) { - term.setVocabulary(vocabulary); - vocabularyTermRepository.save(term); - return vocabularyTermRepository.findByVocabularyOrderByCode(vocabulary); + vocabularyService.saveTerms(vocabulary, term); + return vocabularyService.listTerms(vocabulary); } @DeleteMapping("/{vocabulary}/terms/{term}") - public Iterable 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); + public Iterable deleteTerms(@PathVariable final String vocabulary, @PathVariable final String term) { + vocabularyService.deleteTerms(vocabulary, term); + return vocabularyService.listTerms(vocabulary); } } diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyService.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyService.java new file mode 100644 index 00000000..a4cb2620 --- /dev/null +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyService.java @@ -0,0 +1,77 @@ +package eu.dnetlib.is.vocabulary; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import eu.dnetlib.is.util.InformationServiceException; +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; + +@Service +public class VocabularyService { + + @Autowired + private VocabularyRepository vocabularyRepository; + + @Autowired + private VocabularyTermRepository vocabularyTermRepository; + + private static final Log log = LogFactory.getLog(VocabularyService.class); + + public List listVocs() { + return vocabularyRepository.findAll() + .stream() + .sorted((v1, v2) -> StringUtils.compareIgnoreCase(v1.getId(), v2.getId())) + .collect(Collectors.toList()); + } + + public Vocabulary getVoc(final String vocabulary) throws InformationServiceException { + return vocabularyRepository.findById(vocabulary).orElseThrow(() -> new InformationServiceException("Vocabulary not found")); + } + + @Transactional + public void deleteVocs(final String... ids) { + for (final String id : ids) { + vocabularyRepository.deleteById(id); + } + } + + public void saveVoc(final Vocabulary voc) { + log.info("Saving vocabulary: " + voc); + vocabularyRepository.save(voc); + } + + public Iterable listTerms(final String vocabulary) { + return vocabularyTermRepository.findByVocabularyOrderByCode(vocabulary); + } + + @Transactional + public void saveTerms(final String vocabulary, final VocabularyTerm... terms) { + for (final VocabularyTerm t : terms) { + t.setVocabulary(vocabulary); + vocabularyTermRepository.save(t); + } + } + + @Transactional + public void deleteTerms(final String vocabulary, final String... terms) { + for (final String t : terms) { + final VocabularyTermPK pk = new VocabularyTermPK(); + pk.setCode(t); + pk.setVocabulary(vocabulary); + vocabularyTermRepository.deleteById(pk); + } + } + +} diff --git a/apps/dnet-is-application/src/main/resources/templates/contextEditor.html b/apps/dnet-is-application/src/main/resources/templates/contextEditor.html index 25ba69e7..68f96b3c 100644 --- a/apps/dnet-is-application/src/main/resources/templates/contextEditor.html +++ b/apps/dnet-is-application/src/main/resources/templates/contextEditor.html @@ -24,7 +24,7 @@ Parameters: [show]

- Return to contexts list + Return to contexts list

    diff --git a/apps/dnet-is-application/src/main/resources/templates/vocabularyEditor.html b/apps/dnet-is-application/src/main/resources/templates/vocabularyEditor.html index 7baa71ce..b9c4a21a 100644 --- a/apps/dnet-is-application/src/main/resources/templates/vocabularyEditor.html +++ b/apps/dnet-is-application/src/main/resources/templates/vocabularyEditor.html @@ -23,7 +23,7 @@ Description:

    - Return to vocabulary list + Return to vocabulary list