Cleanup sparky-be pom
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / filters / config / FiltersConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.search.filters.config;
22
23 import java.io.File;
24 import java.io.IOException;
25
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.sparky.config.SparkyResourceLoader;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
30
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 public class FiltersConfig {
34
35   private static final Logger LOG = LoggerFactory.getInstance().getLogger(FiltersConfig.class);
36   
37   private String filtersFileName;
38   
39   private String viewsFileName;
40   
41   private FiltersForViewsConfig viewsConfig;
42   
43   private FiltersDetailsConfig filtersConfig;
44   
45   private SparkyResourceLoader resourceLoader;
46   
47   public FiltersConfig() {
48     //exposed for testing
49   }
50   
51   public FiltersConfig(String filtersFileName, String viewsFileName, SparkyResourceLoader resourceLoader) {
52     this.filtersFileName = filtersFileName;
53     this.viewsFileName = viewsFileName;
54     this.resourceLoader = resourceLoader;
55
56     initializeFilters();
57   }
58   
59   /**
60    * Initialize config.
61    */
62   private void initializeFilters() {
63     viewsConfig = this.readUiViewsConfig();
64     filtersConfig = this.readUiFiltersConfig();
65   }
66
67   public String getViewsFileName() {
68     return viewsFileName;
69   }
70
71   public void setViewsFileName(String viewsFileName) {
72     this.viewsFileName = viewsFileName;
73   }
74
75   public String getFiltersFileName() {
76     return filtersFileName;
77   }
78
79   public void setFiltersFileName(String filtersFileName) {
80     this.filtersFileName = filtersFileName;
81   }
82
83   public FiltersForViewsConfig getViewsConfig() {
84     return viewsConfig;
85   }
86
87   public void setViewsConfig(FiltersForViewsConfig filtersMapEntity) {
88     this.viewsConfig = filtersMapEntity;
89   }
90
91   public FiltersDetailsConfig getFiltersConfig() {
92     return filtersConfig;
93   }
94   
95   public UiFilterConfig getFilterById(String filterId) {
96     for ( UiFilterConfig filter : filtersConfig.getFilters()) {
97       if ( filter.getFilterId().equals(filterId)) {
98         return filter;
99       }
100     }
101     
102     return null;
103   }
104   
105   public void setFiltersConfig(FiltersDetailsConfig filtersConfig) {
106     this.filtersConfig = filtersConfig;
107   }
108
109   public FiltersDetailsConfig readUiFiltersConfig() {
110     ObjectMapper mapper = new ObjectMapper();
111     FiltersDetailsConfig filtersConfig = null;
112     try{ 
113       filtersConfig = mapper.readValue(resourceLoader.getResourceAsFile(this.getFiltersFileName(), true), FiltersDetailsConfig.class);
114     } catch (Exception e){
115       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, this.getFiltersFileName());
116     }
117
118     return filtersConfig;
119   }
120
121   public FiltersForViewsConfig readUiViewsConfig() {
122     ObjectMapper mapper = new ObjectMapper();
123     FiltersForViewsConfig viewsConfig = null;
124     
125     try {
126       viewsConfig = mapper.readValue(resourceLoader.getResourceAsFile(this.getViewsFileName(), true), FiltersForViewsConfig.class);
127     } catch (Exception e){
128       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, this.getViewsFileName());
129     }
130
131     return viewsConfig;
132   }
133   
134   public void initializeFiltersDetailsConfig(File filtersFile) {
135     ObjectMapper mapper = new ObjectMapper();
136     try{ 
137       this.filtersConfig = mapper.readValue(filtersFile, FiltersDetailsConfig.class);
138     } catch (Exception e){
139       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, filtersFile.getAbsolutePath());
140     }
141   }
142
143   public void initializeFiltersForViewsConfig(File viewsFile) {
144     ObjectMapper mapper = new ObjectMapper();
145     
146     try {
147       this.viewsConfig = mapper.readValue(viewsFile, FiltersForViewsConfig.class);
148     } catch (Exception e){
149       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, viewsFile.getAbsolutePath());
150     }
151   }
152   
153   public File getFiltersFile() {
154     File toReturn = null;
155     try {
156       toReturn = resourceLoader.getResourceAsFile(this.getFiltersFileName(), true);
157     } catch (IOException e) {
158       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, this.getFiltersFileName());
159     }
160     return toReturn;
161   }
162   
163   public File getViewsFile() {
164     File toReturn = null;
165     
166     try {
167       toReturn = resourceLoader.getResourceAsFile(this.getViewsFileName(), true);
168     } catch (IOException e) {
169       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, this.getViewsFileName());
170     }
171     
172     return toReturn;
173   }
174 }
175