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