Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / query / QueryService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.schemaservice.query;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.stream.Stream;
29
30 import javax.annotation.PostConstruct;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.stereotype.Service;
36
37 @Service
38 public class QueryService {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(QueryService.class);
41
42     private String queryLocation;
43
44     private String storedQueriesContent;
45
46     public QueryService(@Value("${schema.query.location}") String queryLocation) {
47         this.queryLocation = queryLocation;
48     }
49
50     @PostConstruct
51     public void initialize() throws IOException {
52
53         String fileName = queryLocation + File.separator + "stored-queries.json";
54         LOGGER.debug("Loading the following stored queries file {}", fileName);
55
56         StringBuilder contentBuilder = new StringBuilder();
57
58         try (Stream<String> stream = Files.lines(Paths.get(fileName), StandardCharsets.UTF_8)) {
59             stream.forEach(s -> contentBuilder.append(s));
60         }
61
62         storedQueriesContent = contentBuilder.toString();
63
64         LOGGER.trace("Contents of the stored query file {}", storedQueriesContent);
65     }
66
67     public String getStoredQueries() {
68         return storedQueriesContent;
69     }
70 }