Skip to content

Commit

Permalink
avniproject#786 | Added checks to ensure DB collection and group are …
Browse files Browse the repository at this point in the history
…only made if they are not already present
  • Loading branch information
ombhardwajj committed Sep 13, 2024
1 parent f621d61 commit d336318
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public Database getDatabaseByName(Database database) {
String jsonResponse = getForObject(url, String.class);

try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonResponse);
JsonNode dataArray = rootNode.path("data");

Expand All @@ -43,7 +42,7 @@ public Database getDatabaseByName(Database database) {
return db;
}
}
throw new RuntimeException("Database with name " + database.getName() + " not found.");
return null;
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve database", e);
}
Expand Down Expand Up @@ -74,16 +73,14 @@ public CollectionInfoResponse getCollectionByName(Database database) {
String url = metabaseApiUrl + "/collection";
try {
String jsonResponse = getForObject(url, String.class);

ObjectMapper objectMapper = new ObjectMapper();
List<CollectionInfoResponse> collections = objectMapper.readValue(
jsonResponse, new TypeReference<List<CollectionInfoResponse>>() {}
);

return collections.stream()
.filter(collection -> collection.getName().equals(database.getName()))
.findFirst()
.orElseThrow(() -> new RuntimeException("Collection with name " + database.getName() + " not found."));
.orElseThrow(null);
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve collection", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.GroupPermissionsService;
import org.avni.server.domain.metabase.GroupPermissionsGraphResponse;
import org.avni.server.domain.metabase.Group;
import org.avni.server.domain.metabase.GroupPermissionsBody;
import org.avni.server.domain.metabase.*;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Repository
Expand All @@ -21,7 +20,6 @@ public Group save(Group permissionsGroup) {
String url = metabaseApiUrl + "/permissions/group";
GroupPermissionsBody body = new GroupPermissionsBody(permissionsGroup.getName());
HttpEntity<Map<String, Object>> entity = createJsonEntity(body);

Group response = restTemplate.postForObject(url, entity, Group.class);
return response;
}
Expand All @@ -36,4 +34,10 @@ public void updatePermissionsGraph(GroupPermissionsService permissions, int grou
Map<String, Object> requestBody = permissions.getUpdatedPermissionsGraph();
sendPutRequest(url, requestBody);
}

public List<GroupPermissionResponse> getAllGroups() {
String url = metabaseApiUrl + "/permissions/group";
GroupPermissionResponse[] response = getForObject(url, GroupPermissionResponse[].class);
return Arrays.asList(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class GroupPermissionResponse {
private int id;
private String name;
private int memberCount;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getMemberCount() {
return memberCount;
}

public void setMemberCount(int memberCount) {
this.memberCount = memberCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
import org.avni.server.domain.metabase.*;
import org.avni.server.service.OrganisationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MetabaseService {

private final OrganisationService organisationService;
private final AvniDatabase avniDatabase;
private final DatabaseRepository databaseRepository;
private final DatabaseService databaseService;
private final GroupPermissionsRepository groupPermissionsRepository;
private final CollectionPermissionsRepository collectionPermissionsRepository;
private final CollectionRepository collectionRepository;
Expand All @@ -29,14 +29,12 @@ public class MetabaseService {
public MetabaseService(OrganisationService organisationService,
AvniDatabase avniDatabase,
DatabaseRepository databaseRepository,
@Lazy DatabaseService databaseService,
GroupPermissionsRepository groupPermissionsRepository,
CollectionPermissionsRepository collectionPermissionsRepository,
CollectionRepository collectionRepository) {
this.organisationService = organisationService;
this.avniDatabase = avniDatabase;
this.databaseRepository = databaseRepository;
this.databaseService = databaseService;
this.groupPermissionsRepository = groupPermissionsRepository;
this.collectionPermissionsRepository = collectionPermissionsRepository;
this.collectionRepository = collectionRepository;
Expand All @@ -47,36 +45,60 @@ public void setupMetabase() {
String name = currentOrganisation.getName();
String dbUser = currentOrganisation.getDbUser();

Database database = databaseRepository.save(new Database(name, "postgres", new DatabaseDetails(avniDatabase, dbUser)));
this.globalDatabase = database;
globalDatabase = databaseRepository.getDatabaseByName(new Database(name));
if (globalDatabase == null) {
Database newDatabase = new Database(name, "postgres", new DatabaseDetails(avniDatabase, dbUser));
globalDatabase = databaseRepository.save(newDatabase);
}

CollectionResponse metabaseCollection = collectionRepository.save(new CreateCollectionRequest(name, name + " collection"));
this.globalCollection = new CollectionInfoResponse(null, metabaseCollection.getId(), false);
globalCollection = databaseRepository.getCollectionByName(globalDatabase);
if (globalCollection == null) {
CollectionResponse metabaseCollection = collectionRepository.save(new CreateCollectionRequest(name, name + " collection"));
globalCollection = new CollectionInfoResponse(null, metabaseCollection.getId(), false);
}

Group metabaseGroup = groupPermissionsRepository.save(new Group(name));
Group metabaseGroup = findOrCreateGroup(name);

GroupPermissionsService groupPermissions = new GroupPermissionsService(groupPermissionsRepository.getPermissionsGraph());
groupPermissions.updatePermissions(metabaseGroup.getId(), database.getId());
groupPermissionsRepository.updatePermissionsGraph(groupPermissions, metabaseGroup.getId(), database.getId());
groupPermissions.updatePermissions(metabaseGroup.getId(), globalDatabase.getId());
groupPermissionsRepository.updatePermissionsGraph(groupPermissions, metabaseGroup.getId(), globalDatabase.getId());

CollectionPermissionsService collectionPermissions = new CollectionPermissionsService(collectionPermissionsRepository.getCollectionPermissionsGraph());
collectionPermissions.updatePermissions(metabaseGroup.getId(), metabaseCollection.getId());
collectionPermissionsRepository.updateCollectionPermissions(collectionPermissions, metabaseGroup.getId(), metabaseCollection.getId());
collectionPermissions.updatePermissions(metabaseGroup.getId(), globalCollection.getIdAsInt());
collectionPermissionsRepository.updateCollectionPermissions(collectionPermissions, metabaseGroup.getId(), globalCollection.getIdAsInt());
}

public Database getGlobalDatabase() {
if (globalDatabase == null) {
Organisation currentOrganisation = organisationService.getCurrentOrganisation();
globalDatabase = databaseRepository.getDatabaseByName(new Database(currentOrganisation.getName()));
if (globalDatabase == null) {
throw new RuntimeException("Global database not found.");
}
}
return globalDatabase;
}


public CollectionInfoResponse getGlobalCollection() {
if (globalCollection == null) {
Organisation currentOrganisation = organisationService.getCurrentOrganisation();
globalCollection = databaseRepository.getCollectionByName(new Database(currentOrganisation.getName()));
if (globalCollection == null) {
throw new RuntimeException("Global database not found.");
}
}
return globalCollection;
}
}

private Group findOrCreateGroup(String name) {
List<GroupPermissionResponse> existingGroups = groupPermissionsRepository.getAllGroups();
for (GroupPermissionResponse group : existingGroups) {
if (group.getName().equals(name)) {
return new Group( group.getName(),group.getId());
}
}
return groupPermissionsRepository.save(new Group(name));
}

}

0 comments on commit d336318

Please sign in to comment.