Update license and poms
[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
25 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.eelf.LoggerFactory;
27 import org.onap.aai.sparky.config.SparkyResourceLoader;
28 import org.onap.aai.sparky.logging.AaiUiMsgs;
29
30 import com.fasterxml.jackson.databind.ObjectMapper;
31
32 public class FiltersConfig {
33
34   private static final Logger LOG = LoggerFactory.getInstance().getLogger(FiltersConfig.class);
35   
36   private String filtersFileName;
37   
38   private String viewsFileName;
39   
40   private FiltersForViewsConfig viewsConfig;
41   
42   private FiltersDetailsConfig filtersConfig;
43   
44   private SparkyResourceLoader resourceLoader;
45   
46   public FiltersConfig() {
47     //exposed for testing
48   }
49   
50   public FiltersConfig(String filtersFileName, String viewsFileName, SparkyResourceLoader resourceLoader) {
51     this.filtersFileName = filtersFileName;
52     this.viewsFileName = viewsFileName;
53     this.resourceLoader = resourceLoader;
54
55     initializeFilters();
56   }
57   
58   /**
59    * Initialize config.
60    */
61   private void initializeFilters() {
62     viewsConfig = this.readUiViewsConfig();
63     filtersConfig = this.readUiFiltersConfig();
64   }
65
66   public String getViewsFileName() {
67     return viewsFileName;
68   }
69
70   public void setViewsFileName(String viewsFileName) {
71     this.viewsFileName = viewsFileName;
72   }
73
74   public String getFiltersFileName() {
75     return filtersFileName;
76   }
77
78   public void setFiltersFileName(String filtersFileName) {
79     this.filtersFileName = filtersFileName;
80   }
81
82   public FiltersForViewsConfig getViewsConfig() {
83     return viewsConfig;
84   }
85
86   public void setViewsConfig(FiltersForViewsConfig filtersMapEntity) {
87     this.viewsConfig = filtersMapEntity;
88   }
89
90   public FiltersDetailsConfig getFiltersConfig() {
91     return filtersConfig;
92   }
93   
94   public UiFilterConfig getFilterById(String filterId) {
95     for ( UiFilterConfig filter : filtersConfig.getFilters()) {
96       if ( filter.getFilterId().equals(filterId)) {
97         return filter;
98       }
99     }
100     
101     return null;
102   }
103   
104   public void setFiltersConfig(FiltersDetailsConfig filtersConfig) {
105     this.filtersConfig = filtersConfig;
106   }
107
108   public FiltersDetailsConfig readUiFiltersConfig() {
109     ObjectMapper mapper = new ObjectMapper();
110     FiltersDetailsConfig filtersConfig = null;
111     try{ 
112       filtersConfig = mapper.readValue(resourceLoader.getResourceAsFile(this.getFiltersFileName(),true), FiltersDetailsConfig.class);
113     } catch (Exception e){
114       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, this.getFiltersFileName());
115     }
116
117     return filtersConfig;
118   }
119
120   public FiltersForViewsConfig readUiViewsConfig() {
121     ObjectMapper mapper = new ObjectMapper();
122     FiltersForViewsConfig viewsConfig = null;
123     
124     try {
125       viewsConfig = mapper.readValue(resourceLoader.getResourceAsFile(this.getViewsFileName(),true), FiltersForViewsConfig.class);
126     } catch (Exception e){
127       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, this.getViewsFileName());
128     }
129
130     return viewsConfig;
131   }
132   
133   public void initializeFiltersDetailsConfig(File filtersFile) {
134     ObjectMapper mapper = new ObjectMapper();
135     try{ 
136       this.filtersConfig = mapper.readValue(filtersFile, FiltersDetailsConfig.class);
137     } catch (Exception e){
138       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, filtersFile.getAbsolutePath());
139     }
140   }
141
142   public void initializeFiltersForViewsConfig(File viewsFile) {
143     ObjectMapper mapper = new ObjectMapper();
144     
145     try {
146       this.viewsConfig = mapper.readValue(viewsFile, FiltersForViewsConfig.class);
147     } catch (Exception e){
148       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, viewsFile.getAbsolutePath());
149     }
150     
151   }
152
153 }
154