Skip to content

Commit

Permalink
use fint meta modell to get resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander14121 committed May 30, 2024
1 parent 1539cdd commit 1608125
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
Expand All @@ -20,8 +17,8 @@ public ResourceController(ResourceService resourceService) {
this.resourceService = resourceService;
}

@PostMapping
public ResponseEntity<List<String>> getResources(@RequestBody Map<String, String> body) throws JSONException {
return ResponseEntity.ok(resourceService.getResources(body.get("components")));
@GetMapping
public ResponseEntity<Map<String,List<String>>> getResources(){
return ResponseEntity.ok(resourceService.getResources());
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
package no.fintlabs.linkwalker.task.resources;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
import java.util.*;

@Service
public class ResourceService {

@Value("${fint.baseUrl}")
private String baseUrl;
@Value("${fint.metaModel}")
private String url;

public List<String> getResources(String domainAndComponent) throws JSONException {
List<String> urls = new ArrayList<>();
RestTemplate restTemplate = new RestTemplate();
String url = baseUrl + domainAndComponent;
private final RestTemplate restTemplate = new RestTemplate();

public Map<String, List<String>> getResources() {
ObjectMapper objectMapper = new ObjectMapper();

String response = restTemplate.getForObject(url, String.class);

JSONObject jsonObject = new JSONObject(response);
try {
JsonNode rootNode = objectMapper.readTree(response);

for (Iterator it = jsonObject.keys(); it.hasNext(); ) {
String key = (String) it.next();
JSONObject innerObject = jsonObject.getJSONObject(key);
urls.add(innerObject.getString("collectionUrl"));
}
return findResources(urls);
}
Map<String, List<String>> resultMap = new HashMap<>();

JsonNode entriesNode = rootNode.path("_embedded").path("_entries");

public List<String> findResources(List<String> urls){
List<String> resousces = new ArrayList<>();
urls.forEach(s -> {
resousces.add(s.substring(s.lastIndexOf("/")));
});
return resousces;
Iterator<JsonNode> elements = entriesNode.elements();
while (elements.hasNext()) {
JsonNode entry = elements.next();

String id = entry.path("id").path("identifikatorverdi").asText();

String[] parts = id.split("\\.");
String key = parts[2] + " " + parts[3];
String value = parts[parts.length - 1];

resultMap.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}
return resultMap;
} catch (IOException e) {
e.printStackTrace();
}
return Collections.emptyMap();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
base-path: /link-walker

fint:
baseUrl: "https://play-with-fint.felleskomponent.no/"
metaModel: "https://beta.felleskomponent.no/fint/metamodell/klasse"
application-id: link-walker
kafka:
topic:
Expand Down

0 comments on commit 1608125

Please sign in to comment.