View Javadoc
1   /*
2    * Copyright 2018-2022 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * Author Petr Vcelak (vcelak@kiv.zcu.cz).
7    *
8    * This file is part of MRECore project.
9    *
10   * MRECore is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation, either version 3 of the License.
13   *
14   * MRECore is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with MRECore. If not, see <http://www.gnu.org/licenses/>.
21   */
22  package cz.zcu.mre.service.data;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  import jakarta.servlet.ServletContext;
27  import org.apache.jena.rdf.model.Property;
28  import org.apache.jena.rdf.model.ResourceFactory;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.context.annotation.Scope;
33  import org.springframework.context.annotation.ScopedProxyMode;
34  import org.springframework.context.event.ContextRefreshedEvent;
35  import org.springframework.context.event.EventListener;
36  
37  /**
38   * Property Label Service implementation.
39   *
40   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
41   */
42  //@Component
43  @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
44  public class LabelServiceImpl implements LabelService {
45  
46      private static final Logger LOG = LoggerFactory.getLogger(LabelServiceImpl.class);
47  
48      private static final String WARN_MSG = "Missing label for {}";
49      private static final String DEFAULT_PREFERED_LANGUAGES = "cs,en,"; // TODO move to properties file
50  
51      @Autowired
52      private OntologyService ontologyService;
53  
54      @Autowired
55      private ServletContext servletContext;
56  
57      private String prefLangs = null;
58      private final Map<Property, String> labelMapProperty = new HashMap<>();
59      private final Map<String, String> labelMapLocalName = new HashMap<>();
60  
61      public LabelServiceImpl() {
62          prefLangs = DEFAULT_PREFERED_LANGUAGES;
63      }
64  
65      public LabelServiceImpl(String prefLangs) {
66          this.prefLangs = prefLangs;
67      }
68  
69      /**
70       * Handle context refreshed event and register (set) attributes in the
71       * servlet context.
72       *
73       * It is possible to use placeholder with the local name of property:
74       * ${application.llln.label('country')} or property URI like:
75       * ${application.ll.label('country')} or property URI like:
76       * ${application.ll.label('https://mre.zcu.cz/ontology/tdb.owl#tissue')}.
77       *
78       * @param e Context refreshed event.
79       */
80      @EventListener
81      protected void handleContextRefresh(ContextRefreshedEvent e) {
82          LOG.info("Add LabelService on the event ContextRefreshedEvent.");
83  
84          labelMapProperty.clear();
85          labelMapProperty.putAll(ontologyService.mapOfPropertyAndLabel(prefLangs));
86          // set attribute ll (application.prop.get())
87          servletContext.setAttribute("ll", this);
88  
89          labelMapLocalName.clear();
90          labelMapLocalName.putAll(ontologyService.mapOfLocalNameAndLabel(prefLangs));
91          servletContext.setAttribute("llln", labelMapLocalName);
92      }
93  
94      @Override
95      public Map<Property, String> getLabelMap() {
96          return labelMapProperty;
97      }
98  
99      @Deprecated
100     @Override
101     public Map<String, String> getLocalNameMap() {
102         return labelMapLocalName;
103     }
104 
105     @Override
106     public String label(Property property) {
107         return getLabel(property);
108     }
109 
110     @Override
111     public String getLabel(Property property) {
112 
113         if (labelMapProperty.containsKey(property)) {
114             LOG.debug("Get prepared label for property {}", property.getURI());
115             return labelMapProperty.get(property);
116         }
117 
118         LOG.warn(WARN_MSG, property.getURI());
119         return property.getLocalName();
120     }
121 
122     @Override
123     public String label(String property) {
124         return getLabel(property);
125     }
126 
127     @Override
128     public String getLabel(String propertyString) {
129 
130         if (propertyString.startsWith("http")) {
131             Property property = ResourceFactory.createProperty(propertyString);
132             if (labelMapProperty.containsKey(property)) {
133                 LOG.debug("Get prepared label for property {}", property.getURI());
134                 return labelMapProperty.get(property);
135             }
136         } else {
137             return getLabelLocalName(propertyString);
138         }
139 
140         LOG.warn(WARN_MSG, propertyString);
141         return propertyString;
142     }
143 
144     @Deprecated
145     @Override
146     public String getLabelLocalName(String propertyLocalName) {
147 
148         if (labelMapLocalName.containsKey(propertyLocalName)) {
149             LOG.debug("Get prepared label for property localName {}", propertyLocalName);
150             return labelMapLocalName.get(propertyLocalName);
151         }
152 
153         LOG.warn(WARN_MSG, propertyLocalName);
154         return propertyLocalName;
155     }
156 
157 }