forked from avniproject/avni-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avniproject#733 | Some refactoring changes,and fixed the permissions …
…group error which was caused due to wrong code being pushed in my last commit
- Loading branch information
1 parent
98b4545
commit fd0215c
Showing
22 changed files
with
493 additions
and
209 deletions.
There are no files selected for viewing
44 changes: 0 additions & 44 deletions
44
avni-server-api/src/main/java/org/avni/server/dao/metabase/BaseMetabaseRepository.java
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
...erver-api/src/main/java/org/avni/server/dao/metabase/CollectionPermissionsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.avni.server.dao.metabase; | ||
|
||
import org.avni.server.domain.metabase.CollectionPermissions; | ||
import org.avni.server.domain.metabase.CollectionPermissionsGraphResponse; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Repository | ||
public class CollectionPermissionsRepository extends MetabaseConnector { | ||
|
||
public CollectionPermissionsRepository(RestTemplateBuilder restTemplateBuilder) { | ||
super(restTemplateBuilder); | ||
} | ||
|
||
public CollectionPermissionsGraphResponse getCollectionPermissionsGraph() { | ||
String url = metabaseApiUrl + "/collection/graph"; | ||
return getForObject(url, CollectionPermissionsGraphResponse.class); | ||
} | ||
|
||
public void updateCollectionPermissions(CollectionPermissions collectionPermissions, int groupId, int collectionId) { | ||
collectionPermissions.updatePermissionsGraph(groupId, collectionId); | ||
String url = metabaseApiUrl + "/collection/graph"; | ||
sendPutRequest(url, convertToMap(collectionPermissions.getPermissionsGraph())); | ||
} | ||
|
||
private Map<String, Object> convertToMap(CollectionPermissionsGraphResponse collectionPermissionsGraphResponse) { | ||
Map<String, Object> map = new HashMap<>(); | ||
map.put("revision", collectionPermissionsGraphResponse.getRevision()); | ||
map.put("groups", collectionPermissionsGraphResponse.getGroups()); | ||
return map; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
avni-server-api/src/main/java/org/avni/server/dao/metabase/MetabaseConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.avni.server.dao.metabase; | ||
|
||
import org.avni.server.domain.metabase.PermissionsGroup; | ||
import org.avni.server.domain.metabase.PermissionsGroupBody; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
@Repository | ||
public class MetabaseConnector { | ||
protected final RestTemplate restTemplate; | ||
|
||
@Value("${metabase.api.url}") | ||
protected String metabaseApiUrl; | ||
|
||
@Value("${metabase.api.key}") | ||
private String apiKey; | ||
|
||
public MetabaseConnector(RestTemplateBuilder restTemplateBuilder) { | ||
this.restTemplate = restTemplateBuilder.build(); | ||
} | ||
|
||
protected HttpHeaders getHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.set("x-api-key", apiKey); | ||
return headers; | ||
} | ||
|
||
protected <T> HttpEntity<T> createHttpEntity(T body) { | ||
HttpHeaders headers = getHeaders(); | ||
return new HttpEntity<>(body, headers); | ||
} | ||
|
||
protected void sendPutRequest(String url, Map<String, ?> requestBody) { | ||
HttpEntity<Map<String, ?>> entity = createHttpEntity(requestBody); | ||
restTemplate.exchange(url, HttpMethod.PUT, entity, Map.class); | ||
} | ||
|
||
protected <T> T postForObject(String url, Object request, Class<T> responseType) { | ||
HttpEntity<Object> entity = createHttpEntity(request); | ||
return restTemplate.postForObject(url, entity, responseType); | ||
} | ||
|
||
protected <T> T getForObject(String url, Class<T> responseType) { | ||
HttpHeaders headers = getHeaders(); | ||
HttpEntity<Void> entity = new HttpEntity<>(headers); | ||
return restTemplate.exchange(url, HttpMethod.GET, entity, responseType).getBody(); | ||
} | ||
|
||
protected HttpEntity<Map<String, Object>> createJsonEntity(PermissionsGroupBody body) { | ||
HttpHeaders headers = getHeaders(); | ||
return new HttpEntity<>(body.getBody(), headers); | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
avni-server-api/src/main/java/org/avni/server/dao/metabase/MetabaseRepository.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
avni-server-api/src/main/java/org/avni/server/dao/metabase/PermissionsGroupRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.avni.server.dao.metabase; | ||
|
||
import org.avni.server.domain.metabase.PermissionsGroupService; | ||
import org.avni.server.domain.metabase.PermissionsGraphResponse; | ||
import org.avni.server.domain.metabase.PermissionsGroup; | ||
import org.avni.server.domain.metabase.PermissionsGroupBody; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Map; | ||
|
||
@Repository | ||
public class PermissionsGroupRepository extends MetabaseConnector { | ||
|
||
public PermissionsGroupRepository(RestTemplateBuilder restTemplateBuilder) { | ||
super(restTemplateBuilder); | ||
} | ||
|
||
public PermissionsGroup save(PermissionsGroup permissionsGroup) { | ||
String url = metabaseApiUrl + "/permissions/group"; | ||
PermissionsGroupBody body = new PermissionsGroupBody(permissionsGroup.getName()); | ||
HttpEntity<Map<String, Object>> entity = createJsonEntity(body); | ||
|
||
PermissionsGroup response = restTemplate.postForObject(url, entity, PermissionsGroup.class); | ||
return response; | ||
} | ||
|
||
public PermissionsGraphResponse getPermissionsGraph() { | ||
String url = metabaseApiUrl + "/permissions/graph"; | ||
return getForObject(url, PermissionsGraphResponse.class); | ||
} | ||
|
||
public void updatePermissionsGraph(PermissionsGroupService permissions, int groupId, int databaseId) { | ||
String url = metabaseApiUrl + "/permissions/graph"; | ||
Map<String, Object> requestBody = permissions.getUpdatedPermissionsGraph(); | ||
sendPutRequest(url, requestBody); | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
avni-server-api/src/main/java/org/avni/server/dao/metabase/PermissionsRepository.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.