View Javadoc
1   package de.dlr.shepard.context.semantic.services;
2   
3   import de.dlr.shepard.common.exceptions.InvalidBodyException;
4   import de.dlr.shepard.context.semantic.SemanticRepositoryConnectorFactory;
5   import de.dlr.shepard.context.semantic.daos.SemanticAnnotationDAO;
6   import de.dlr.shepard.context.semantic.entities.SemanticAnnotation;
7   import de.dlr.shepard.context.semantic.entities.SemanticRepository;
8   import de.dlr.shepard.context.semantic.io.SemanticAnnotationIO;
9   import de.dlr.shepard.context.version.daos.VersionableEntityConcreteDAO;
10  import de.dlr.shepard.context.version.entities.VersionableEntity;
11  import io.quarkus.logging.Log;
12  import jakarta.enterprise.context.RequestScoped;
13  import jakarta.inject.Inject;
14  import jakarta.ws.rs.NotFoundException;
15  import java.nio.file.InvalidPathException;
16  import java.util.List;
17  
18  @RequestScoped
19  public class SemanticAnnotationService {
20  
21    @Inject
22    SemanticAnnotationDAO semanticAnnotationDAO;
23  
24    @Inject
25    SemanticRepositoryService semanticRepositoryService;
26  
27    @Inject
28    VersionableEntityConcreteDAO versionableEntityConcreteDAO;
29  
30    @Inject
31    SemanticRepositoryConnectorFactory semanticRepositoryConnectorFactory;
32  
33    public List<SemanticAnnotation> getAllAnnotationsByNeo4jId(long entityId) {
34      return semanticAnnotationDAO.findAllSemanticAnnotationsByNeo4jId(entityId);
35    }
36  
37    public List<SemanticAnnotation> getAllAnnotationsByShepardId(long shepardId) {
38      return semanticAnnotationDAO.findAllSemanticAnnotationsByShepardId(shepardId);
39    }
40  
41    /**
42     * Gets semantic annotation by neo4j id
43     *
44     * @param id
45     * @return
46     * @throws NotFoundException if semantic annotation is null or deleted
47     */
48    public SemanticAnnotation getAnnotationByNeo4jId(long id) {
49      var annotation = semanticAnnotationDAO.findByNeo4jId(id);
50      if (annotation == null) {
51        String errorMsg = String.format("ID ERROR - Semantic Annotation with id %s is null or deleted", id);
52        Log.error(errorMsg);
53        throw new NotFoundException(errorMsg);
54      }
55      return annotation;
56    }
57  
58    public SemanticAnnotation createAnnotationByShepardId(long entityShepardId, SemanticAnnotationIO annotationIO) {
59      VersionableEntity entity = versionableEntityConcreteDAO.findByShepardId(entityShepardId);
60      if (entity == null || entity.isDeleted()) throw new InvalidBodyException("invalid entity");
61  
62      SemanticRepository propertyRepository = getRepository(annotationIO.getPropertyRepositoryId());
63      SemanticRepository valueRepository = getRepository(annotationIO.getValueRepositoryId());
64      var propertyName = validateTerm(propertyRepository, annotationIO.getPropertyIRI());
65      var valueName = validateTerm(valueRepository, annotationIO.getValueIRI());
66  
67      SemanticAnnotation toCreate = new SemanticAnnotation();
68      toCreate.setPropertyIRI(annotationIO.getPropertyIRI());
69      toCreate.setValueIRI(annotationIO.getValueIRI());
70      toCreate.setPropertyRepository(propertyRepository);
71      toCreate.setValueRepository(valueRepository);
72      toCreate.setPropertyName(propertyName);
73      toCreate.setValueName(valueName);
74  
75      SemanticAnnotation created = semanticAnnotationDAO.createOrUpdate(toCreate);
76      entity.addAnnotation(created);
77      versionableEntityConcreteDAO.createOrUpdate(entity);
78      return created;
79    }
80  
81    public boolean deleteAnnotationByNeo4jId(long id) {
82      getAnnotationByNeo4jId(id);
83  
84      var result = semanticAnnotationDAO.deleteByNeo4jId(id);
85      return result;
86    }
87  
88    private SemanticRepository getRepository(long id) {
89      try {
90        return semanticRepositoryService.getRepository(id);
91      } catch (InvalidPathException ex) {
92        Log.error(ex.getMessage());
93        throw new NotFoundException(ex.getMessage());
94      }
95    }
96  
97    public String validateTerm(SemanticRepository repository, String iri) {
98      var src = semanticRepositoryConnectorFactory.getRepositoryService(repository.getType(), repository.getEndpoint());
99      var term = src.getTerm(iri);
100     if (term == null || term.isEmpty()) throw new InvalidBodyException("term could not be found");
101     // Prefer the default label
102     if (term.containsKey("")) return term.get("");
103     // Then prefer the English label
104     if (term.containsKey("en")) return term.get("en");
105     // Fall back to the first label in the list
106     return term.values().iterator().next();
107   }
108 }