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.qbuilder.config;
23
24 import cz.zcu.mre.converter.ConversionServiceProvider;
25 import cz.zcu.mre.dao.OntologyRepository;
26 import cz.zcu.mre.dao.OntologyRepositoryImpl;
27 import cz.zcu.mre.dao.SPARQLRepository;
28 import cz.zcu.mre.dao.SPARQLRepositoryImpl;
29 import cz.zcu.mre.qbuilder.QBuilderApplication;
30 import cz.zcu.mre.qbuilder.core.QBuilder;
31 import cz.zcu.mre.qbuilder.core.QBuilderImpl;
32 import cz.zcu.mre.service.data.LabelService;
33 import cz.zcu.mre.service.data.LabelServiceImpl;
34 import cz.zcu.mre.service.data.MREDataService;
35 import cz.zcu.mre.service.data.MREDataServiceImpl;
36 import cz.zcu.mre.service.data.OntologyService;
37 import cz.zcu.mre.service.data.OntologyServiceImpl;
38 import cz.zcu.mre.service.data.SPARQLBuilder;
39 import cz.zcu.mre.service.data.SPARQLBuilderImpl;
40 import cz.zcu.mre.service.data.VocabularyService;
41 import cz.zcu.mre.service.data.VocabularyServiceImpl;
42 import cz.zcu.mre.vocab.MRE;
43 import cz.zcu.mre.vocab.STROKE;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.context.annotation.ComponentScan;
48 import org.springframework.context.annotation.Configuration;
49 import org.springframework.context.annotation.Primary;
50 import org.springframework.context.annotation.PropertySource;
51 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
52 import org.springframework.core.convert.ConversionService;
53 import org.springframework.format.datetime.DateFormatter;
54 import org.springframework.format.datetime.DateFormatterRegistrar;
55 import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
56 import org.springframework.format.support.FormattingConversionService;
57
58
59
60
61
62
63 @Configuration
64 @PropertySource("classpath:application.properties")
65 @ComponentScan(basePackageClasses = QBuilderApplication.class)
66 public class QBuilderApplicationConfig {
67
68 private static final Logger LOG = LoggerFactory.getLogger(QBuilderApplicationConfig.class);
69
70
71
72
73 private static final String QBUILDER_DEFAULT_SERVICE_URL = "http://127.0.0.1:3030/stroke";
74
75
76
77
78 private final static String ONTOLOGY_STROKE_URI = STROKE.NS;
79
80
81
82
83
84 private final static String ONTOLOGY_STROKE_LOCAL_FILE_COPY = "ontology/stroke.owl";
85
86
87
88
89 private final static String ONTOLOGY_MRE_URI = MRE.NS;
90
91
92
93
94
95 private final static String ONTOLOGY_MRE_LOCAL_FILE_COPY = "ontology/mre.owl";
96 private static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer;
97
98 @Bean
99 public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
100
101 if (propertySourcesPlaceholderConfigurer == null) {
102 LOG.info("Create bean PropertySourcesPlaceholderConfigurer");
103 propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
104 }
105
106 return propertySourcesPlaceholderConfigurer;
107 }
108
109 private QBuilder queryBuilder;
110
111 private SPARQLBuilder sparqlBuilder;
112
113 private OntologyRepository ontologyRepository;
114 private SPARQLRepository sparqlRepository;
115 private MREDataService dataService;
116
117 private ConversionService conversionService;
118
119 @Bean("qBuilder")
120 public QBuilder queryBuilder() {
121
122 if (queryBuilder == null) {
123 LOG.info("Create bean queryBuilder (in qBuilder)");
124
125 queryBuilder = new QBuilderImpl(ontologyRepository());
126 }
127
128 return queryBuilder;
129 }
130
131 @Bean
132 @Primary
133 public SPARQLRepository sparqlRepository() {
134
135 if (sparqlRepository == null) {
136 LOG.info("Create bean sparqlRepository with ServiceURL: {}", QBUILDER_DEFAULT_SERVICE_URL);
137 sparqlRepository = new SPARQLRepositoryImpl();
138 sparqlRepository.setServiceURL(QBUILDER_DEFAULT_SERVICE_URL);
139 }
140
141 return sparqlRepository;
142 }
143
144 @Bean
145 public MREDataService dataService() {
146
147 if (dataService == null) {
148 LOG.info("Create bean dataService");
149 dataService = new MREDataServiceImpl(sparqlBuilder(), sparqlRepository(), conversionService());
150 ((SPARQLBuilderImpl) sparqlBuilder).setDataService(dataService);
151 }
152
153 return dataService;
154 }
155
156 @Primary
157 @Bean
158 public SPARQLBuilder sparqlBuilder() {
159
160 if (sparqlBuilder == null) {
161 LOG.info("Create bean sparqlBuilder");
162 sparqlBuilder = new SPARQLBuilderImpl();
163 }
164
165 return sparqlBuilder;
166 }
167
168 @Bean(name = "ontologyRepositoryQB")
169 @Primary
170 public OntologyRepository ontologyRepository() {
171
172 if (ontologyRepository == null) {
173 LOG.info("Create bean ontologyRepository");
174 ontologyRepository = new OntologyRepositoryImpl();
175 ontologyRepository.addOntology(ONTOLOGY_STROKE_URI, ONTOLOGY_STROKE_LOCAL_FILE_COPY);
176 ontologyRepository.addOntology(ONTOLOGY_MRE_URI, ONTOLOGY_MRE_LOCAL_FILE_COPY);
177 }
178
179 return ontologyRepository;
180 }
181
182 @Bean(name = "ontologyService")
183 public OntologyService ontologyService() {
184 return new OntologyServiceImpl();
185 }
186
187 @Bean(name = "labelService")
188 public LabelService labelService() {
189 return new LabelServiceImpl();
190 }
191
192 @Bean(name = "vocabularyService")
193 public VocabularyService vocabularyService() {
194 return new VocabularyServiceImpl();
195 }
196
197
198
199
200
201
202
203
204
205 @Primary
206 @Bean(name = "conversionService")
207 public ConversionService conversionService() {
208
209 if (conversionService == null) {
210 LOG.info("Create bean conversionService");
211
212
213
214
215 FormattingConversionService service = new ConversionServiceProvider();
216
217
218
219 service.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
220
221
222 DateFormatterRegistrar registrar = new DateFormatterRegistrar();
223 registrar.setFormatter(new DateFormatter("dd. MM. yyyy"));
224 registrar.registerFormatters(service);
225
226 conversionService = service;
227 LOG.debug("Created conversion service is {}", conversionService);
228 }
229
230 return conversionService;
231 }
232
233 }