Skip to content

Commit

Permalink
Got rid of code smell,primitive obsession,followed DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Jun 14, 2024
1 parent 29eebe5 commit 6f304c4
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 218 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package org.avni.server.dao;

import org.avni.server.domain.metabase.Collection;
import org.avni.server.domain.metabase.CollectionPermissions;
import org.avni.server.domain.metabase.CollectionResponse;
import org.avni.server.domain.metabase.Database;
import org.avni.server.domain.metabase.DatabaseResponse;
import org.avni.server.domain.metabase.Permissions;
import org.avni.server.domain.metabase.PermissionsGroup;
import org.avni.server.domain.metabase.PermissionsGroupResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -8,16 +16,12 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

@Repository
public class MetabaseRepository {

private final Logger logger = LoggerFactory.getLogger(MetabaseRepository.class);
private final RestTemplate restTemplate;

@Value("${metabase.api.key}")
Expand All @@ -26,154 +30,69 @@ public class MetabaseRepository {
@Value("${metabase.api.url}")
private String metabaseApiUrl;

@Value("${database.host}")
private String dbHost;

@Value("${database.port}")
private String dbPort;

@Value("${database.name}")
private String dbName;

@Value("${database.engine}")
private String dbEngine;

public MetabaseRepository(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

public int createDatabase(String dbUser) {
public int createDatabase(Database database) {
String url = metabaseApiUrl + "/database";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);

Map<String, Object> details = new HashMap<>();
details.put("host", dbHost);
details.put("port", dbPort);
details.put("db", dbName);
details.put("user", dbUser);

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("engine", dbEngine);
requestBody.put("name", dbUser);
requestBody.put("details", details);

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
logger.info("Sending request to create database for user: {}", dbUser);
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class);
logger.info("Response from Metabase API (create database): " + response.getBody());

return (Integer) response.getBody().get("id");
HttpEntity<Database> entity = createHttpEntity(database);
DatabaseResponse response = restTemplate.postForObject(url, entity, DatabaseResponse.class);
return response.getId();
}

public int createCollection(String name) {
public int createCollection(Collection collection) {
String url = metabaseApiUrl + "/collection";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("name", name);
requestBody.put("description", name + " collection");

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
logger.info("Sending request to create collection for name: {}", name);
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class);
logger.info("Response from Metabase API (create collection): " + response.getBody());

return (Integer) response.getBody().get("id");
HttpEntity<Collection> entity = createHttpEntity(collection);
CollectionResponse response = restTemplate.postForObject(url, entity, CollectionResponse.class);
return response.getId();
}

public int createPermissionsGroup(String name) {
public int createPermissionsGroup(PermissionsGroup permissionsGroup) {
String url = metabaseApiUrl + "/permissions/group";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("name", name);
HttpEntity<PermissionsGroup> entity = createHttpEntity(permissionsGroup);
PermissionsGroupResponse response = restTemplate.postForObject(url, entity, PermissionsGroupResponse.class);
return response.getId();
}

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
logger.info("Request body for creating permissions group: {}", requestBody);
logger.info("Sending request to create permissions group for name: {}", name);
public Map<String, Object> getPermissionsGraph() {
String url = metabaseApiUrl + "/permissions/graph";
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, createHttpEntity(null), Map.class);
return response.getBody();
}

ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class);
logger.info("Response from Metabase API (create permissions group): " + response.getBody());
return (Integer) response.getBody().get("id");
public Map<String, Object> getCollectionPermissionsGraph() {
String url = metabaseApiUrl + "/collection/graph";
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, createHttpEntity(null), Map.class);
return response.getBody();
}

public void assignDatabasePermissions(int groupId, int databaseId) {
logger.info("Assigning database permissions for group ID: {}", groupId);
public void assignDatabasePermissions(Permissions permissions, int groupId, int databaseId) {
permissions.updatePermissionsGraph(groupId, databaseId);
String url = metabaseApiUrl + "/permissions/graph";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);

ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), Map.class);
Map<String, Object> permissionsGraph = response.getBody();

Map<String, Object> groups = (Map<String, Object>) permissionsGraph.get("groups");
if (groups == null) {
throw new RuntimeException("Groups not found in the permissions graph.");
}

Map<String, Object> databasePermissions = new HashMap<>();
databasePermissions.put("data", new HashMap<String, String>() {{
put("schemas", "all");
}});

if (!groups.containsKey(String.valueOf(groupId))) {
groups.put(String.valueOf(groupId), new HashMap<>());
}
Map<String, Object> groupPermissions = (Map<String, Object>) groups.get(String.valueOf(groupId));
groupPermissions.put(String.valueOf(databaseId), databasePermissions);

if (groups.containsKey("1")) {
Map<String, Object> group1Permissions = (Map<String, Object>) groups.get("1");
if (group1Permissions.containsKey(String.valueOf(databaseId))) {
Map<String, Object> group1DatabasePermissions = (Map<String, Object>) group1Permissions.get(String.valueOf(databaseId));
if (group1DatabasePermissions.containsKey("data")) {
Map<String, String> dataPermissions = (Map<String, String>) group1DatabasePermissions.get("data");
dataPermissions.put("native", "none");
dataPermissions.put("schemas", "none");
}
}
}

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(permissionsGraph, headers);
response = restTemplate.exchange(url, HttpMethod.PUT, entity, Map.class);
logger.info("Response from Metabase API (assign database permissions): " + response.getBody());
sendPutRequest(url, permissions.getPermissionsGraph());
}

public void updateCollectionPermissions(int groupId, int collectionId) {
logger.info("Updating collection permissions for group ID: {} and collection ID: {}", groupId, collectionId);
String graphUrl = metabaseApiUrl + "/collection/graph";
public void updateCollectionPermissions(CollectionPermissions collectionPermissions, int groupId, int collectionId) {
collectionPermissions.updatePermissionsGraph(groupId, collectionId);
String url = metabaseApiUrl + "/collection/graph";
sendPutRequest(url, collectionPermissions.getPermissionsGraph());
}

private HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);
return headers;
}

ResponseEntity<Map> graphResponse = restTemplate.exchange(graphUrl, HttpMethod.GET, new HttpEntity<>(headers), Map.class);
Map<String, Object> collectionGraph = graphResponse.getBody();

logger.info("Current collection permissions graph: {}", collectionGraph);

Map<String, Map<String, String>> groups = (Map<String, Map<String, String>>) collectionGraph.get("groups");

groups.computeIfAbsent(String.valueOf(groupId), k -> new HashMap<>());

Map<String, String> groupPermissions = groups.get(String.valueOf(groupId));
groupPermissions.put(String.valueOf(collectionId), "write");

if (groups.containsKey("1")) {
Map<String, String> group1Permissions = (Map<String, String>) groups.get("1");
group1Permissions.put(String.valueOf(collectionId), "none"); // Set the permission for the specified collection ID to "none"
}

logger.info("Updated collection permissions graph: {}", collectionGraph);
private <T> HttpEntity<T> createHttpEntity(T body) {
HttpHeaders headers = getHeaders();
return new HttpEntity<>(body, headers);
}

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(collectionGraph, headers);
graphResponse = restTemplate.exchange(graphUrl, HttpMethod.PUT, entity, Map.class);
logger.info("Response from Metabase API (update collection permissions): " + graphResponse.getBody());
private void sendPutRequest(String url, Map<String, Object> requestBody) {
HttpEntity<Map<String, Object>> entity = createHttpEntity(requestBody);
restTemplate.exchange(url, HttpMethod.PUT, entity, Map.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.avni.server.domain.metabase;

public class Collection {
private String name;
private String description;

public Collection(String name, String description) {
this.name = name;
this.description = description;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.avni.server.domain.metabase;

import java.util.HashMap;
import java.util.Map;

public class CollectionPermissions {
private Map<String, Object> permissionsGraph;

public CollectionPermissions(Map<String, Object> permissionsGraph) {
this.permissionsGraph = permissionsGraph;
}

public Map<String, Object> getPermissionsGraph() {
return permissionsGraph;
}

public void updatePermissionsGraph(int groupId, int collectionId) {
Map<String, Map<String, String>> groups = (Map<String, Map<String, String>>) permissionsGraph.get("groups");

groups.computeIfAbsent(String.valueOf(groupId), k -> new HashMap<>());
Map<String, String> groupPermissions = groups.get(String.valueOf(groupId));
groupPermissions.put(String.valueOf(collectionId), "write");

if (groups.containsKey("1")) {
Map<String, String> allGroupPermissions = groups.get("1");
allGroupPermissions.put(String.valueOf(collectionId), "none");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.avni.server.domain.metabase;

public class CollectionResponse {
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.avni.server.domain.metabase;

public class Database {
private String name;
private String engine;
private DatabaseDetails details;

public Database(String name, String engine, DatabaseDetails details) {
this.name = name;
this.engine = engine;
this.details = details;
}

public String getName() {
return name;
}

public String getEngine() {
return engine;
}

public DatabaseDetails getDetails() {
return details;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.avni.server.domain.metabase;

public class DatabaseDetails {
private String host;
private String port;
private String db;
private String user;

public DatabaseDetails(String host, String port, String db, String user) {
this.host = host;
this.port = port;
this.db = db;
this.user = user;
}


public String getHost() {
return host;
}

public String getPort() {
return port;
}

public String getDb() {
return db;
}

public String getUser() {
return user;
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.avni.server.domain.metabase;

public class DatabaseResponse {
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.avni.server.domain.metabase;

public class Group {
private int id;
private boolean hasPermission;

public Group(int id, boolean hasPermission) {
this.id = id;
this.hasPermission = hasPermission;
}

public int getId() {
return id;
}

public boolean hasPermission() {
return hasPermission;
}

public void setPermission(boolean hasPermission) {
this.hasPermission = hasPermission;
}
}
Loading

0 comments on commit 6f304c4

Please sign in to comment.