1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
39
40
41
42
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,";
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
71
72
73
74
75
76
77
78
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
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 }