View Javadoc
1   /*
2    * Copyright 2018-2024 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 MRE SPARQL QueryBuilder project.
9    *
10   * MRE SPARQL QueryBuilder 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   * MRE SPARQL QueryBuilder 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 MRE SPARQL QueryBuilder. If not, see <http://www.gnu.org/licenses/>.
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   * Application Configuration.
60   *
61   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
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       * SPARQL Endpoint URL.
72       */
73      private static final String QBUILDER_DEFAULT_SERVICE_URL = "http://127.0.0.1:3030/stroke";
74  
75      /**
76       * Stroke ontology URI.
77       */
78      private final static String ONTOLOGY_STROKE_URI = STROKE.NS;
79  
80      /**
81       * Local copy of Ontology (stroke.owl) file (backup) in
82       * src/main/resources/ontology/stroke.owl.
83       */
84      private final static String ONTOLOGY_STROKE_LOCAL_FILE_COPY = "ontology/stroke.owl";
85  
86      /**
87       * MRE ontology URI.
88       */
89      private final static String ONTOLOGY_MRE_URI = MRE.NS;
90  
91      /**
92       * Local copy of MRE Ontology (mre.owl) file (backup) in
93       * src/main/resources/ontology/mre.owl.
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      * Conversion Service.
199      *
200      * Be careful! Spring has its own (@link WebMvcConfig.addFormatters()) way
201      * how to add formatters.
202      *
203      * @return Conversion service.
204      */
205     @Primary
206     @Bean(name = "conversionService")
207     public ConversionService conversionService() {
208 
209         if (conversionService == null) {
210             LOG.info("Create bean conversionService");
211 
212             // Uses the DefaultFormattingConversionService, but do not register defaults.
213             //FormattingConversionService conversionService = new DefaultFormattingConversionService(false);
214             // Use own conversion service.
215             FormattingConversionService service = new ConversionServiceProvider();
216             //this.conversionService = service;
217 
218             // Ensure @NumberFormat is still supported
219             service.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
220 
221             // Register date conversion with a specific global format
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 }