package com.barclays.oadf.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.util.*;
@Component
public class ViewColumnMappingConfig {
private Map\<String, Set\<String\>\> viewColumnMap = new HashMap\<\>();
private Map\<Integer, String\> viewMap = new HashMap\<\>();
// Load both the mappings from the YAML file
public void loadMappings() {
try {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
ViewColumnConfig config = objectMapper.readValue(
new File("src/main/resources/view_column_mapping.yml"),
ViewColumnConfig.class
);
this.viewColumnMap = config.getViews(); // This is the column mapping
loadOtherConfig(); // Load your other config (like msg-format-table-map)
} catch (Exception e) {
throw new RuntimeException("Failed to load view-column mappings", e);
}
}
private void loadOtherConfig() {
// Manually load other configurations like msg-format-table-map if necessary
viewMap.put(1, "oadf_mr_master_ds.vw_onerisk_master_ds");
viewMap.put(2, "oadf_mr_master_ds.vw_second_example_ds");
}
// Validate if the column is valid for the provided view
public boolean isColumnValidForView(String view, String column) {
return viewColumnMap.getOrDefault(view, Collections.emptySet()).contains(column);
}
// Getters
public Map\<String, Set\<String\>\> getViewColumnMap() {
return viewColumnMap;
}
public Map\<Integer, String\> getViewMap() {
return viewMap;
}
}
package com.barclays.oadf.config;
import java.util.Map;
import java.util.Set;
public class ViewColumnConfig {
private Map\<String, Set\<String\>\> views;
public Map\<String, Set\<String\>\> getViews() {
return views;
}
public void setViews(Map\<String, Set\<String\>\> views) {
this.views = views;
}
}
package com.barclays.oadf.service;
import com.barclays.oadf.config.ViewColumnMappingConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class QueryService {
private final ViewColumnMappingConfig viewColumnMappingConfig;
@Autowired
public QueryService(ViewColumnMappingConfig viewColumnMappingConfig) {
this.viewColumnMappingConfig = viewColumnMappingConfig;
}
public void processClientRequest(String view, List\<String\> selectedColumns) {
for (String column : selectedColumns) {
if (!viewColumnMappingConfig.isColumnValidForView(view, column)) {
throw new IllegalArgumentException("Column " + column + " is not allowed for view " + view);
}
}
// Proceed with query execution or other logic
}
}
Done