-
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.
fix: resolved merge conflict and refactored DBRepository
- Loading branch information
Showing
29 changed files
with
734 additions
and
62 deletions.
There are no files selected for viewing
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
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
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 |
|---|---|---|
| @@ -1,10 +1,24 @@ | ||
| package edu.group5.app; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| import edu.group5.app.control.OrgAPIWrapper; | ||
| import tools.jackson.core.type.TypeReference; | ||
| import tools.jackson.databind.ObjectMapper; | ||
|
|
||
| /** | ||
| * Hello world! | ||
| */ | ||
| public class App { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello World!"); | ||
| } | ||
| public static void main(String[] args) throws InterruptedException { | ||
| OrgAPIWrapper orgWrap = new OrgAPIWrapper("https://app.innsamlingskontrollen.no/api/public/v1/all"); | ||
| System.out.println(); | ||
| System.out.println(); | ||
| orgWrap.importData(); | ||
| Object[] imports = orgWrap.getData(); | ||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
| HashMap<String, Object> map = objectMapper.convertValue(imports[0], new TypeReference<HashMap<String, Object>>() { | ||
| }); | ||
| System.out.println(map.get("org_number")); | ||
| } | ||
| } |
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,54 @@ | ||
| package edu.group5.app.control; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
|
|
||
| import tools.jackson.core.exc.StreamReadException; | ||
| import tools.jackson.databind.ObjectMapper; | ||
|
|
||
| public class OrgAPIWrapper extends Wrapper { | ||
| private Object[] data; | ||
| private HttpClient client; | ||
| private HttpRequest request; | ||
|
|
||
| public OrgAPIWrapper(String urlString) { | ||
| if (urlString == null) { | ||
| throw new IllegalArgumentException("url can't be null"); | ||
| } else if (urlString.isBlank()) { | ||
| throw new IllegalArgumentException("url can't be blank"); | ||
| } | ||
| try { | ||
| URI uri = URI.create(urlString); | ||
| this.client = HttpClient.newHttpClient(); | ||
| this.request = HttpRequest.newBuilder() | ||
| .uri(uri) | ||
| .GET() | ||
| .build(); | ||
| } catch (IllegalArgumentException IAe) { | ||
| throw new IllegalArgumentException("url has to be valid"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public boolean importData() throws InterruptedException { | ||
| try { | ||
| HttpResponse<String> response = this.client.send( | ||
| this.request, HttpResponse.BodyHandlers.ofString()); | ||
| this.data = new ObjectMapper().readValue(response.body(), Object[].class); | ||
| return true; | ||
| } catch (IOException IOe) { | ||
| return false; | ||
| } catch (StreamReadException e) { | ||
| throw new StreamReadException("The URL leads to a website that can't be parsed"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Object[] getData() { | ||
| return this.data; | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| package edu.group5.app.control; | ||
|
|
||
| public class Wrapper { | ||
|
|
||
| abstract class Wrapper { | ||
|
|
||
| protected Wrapper() { | ||
| } | ||
|
|
||
| public abstract boolean importData() throws InterruptedException; | ||
|
|
||
| public abstract Object getData(); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,26 @@ | ||
| package edu.group5.app.model; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Represents a repository. | ||
| */ | ||
| public abstract class Repository { | ||
| /** | ||
| * The underlying data structure that holds the repository's content. | ||
| */ | ||
| protected Object content; | ||
|
|
||
| public abstract class Repository<K, V> { | ||
| protected final Map<K, V> content; | ||
| /** | ||
| * Constructs a new Repository with the specified content. | ||
| * | ||
| * @param content the underlying data structure used to store entities | ||
| */ | ||
| protected Repository(Object content) { | ||
| public Repository(Map<K, V> content) { | ||
| this.content = content; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the content of the repo | ||
| * @return content of the repo | ||
| */ | ||
| public abstract Object getContent(); | ||
| public Map<K, V> getContent() { | ||
| return content; | ||
| } | ||
| } |
60 changes: 58 additions & 2 deletions
60
src/main/java/edu/group5/app/model/organization/Organization.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 |
|---|---|---|
| @@ -1,5 +1,61 @@ | ||
| package edu.group5.app.model.organization; | ||
|
|
||
| public class Organization { | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Represents an organization. | ||
| * | ||
| * <p> | ||
| * An organization is identified by an organization number, a name, | ||
| * trust status, website URL, pre-approval status, and a textual description. | ||
| * | ||
| * <p> | ||
| * Instances are validated on creation: | ||
| * <ul> | ||
| * <li>orgNumber must be non-negative</li> | ||
| * <li>name and websiteURL must not be null or blank</li> | ||
| * <li>description must not be null</li> | ||
| * </ul> | ||
| */ | ||
| public record Organization( | ||
| int orgNumber, | ||
| String name, | ||
| boolean trusted, | ||
| String websiteURL, | ||
| boolean isPreApproved, | ||
| String description) { | ||
| /** | ||
| * Creates a new organization. | ||
| * | ||
| * @param orgNumber the organization number; must be non-negative | ||
| * @param name the organization name; must not be null or blank | ||
| * @param trusted whether the organization is trusted | ||
| * @param websiteURL the organization's website URL; must not be null or | ||
| * blank | ||
| * @param isPreApproved whether the organization is pre-approved | ||
| * @param description a textual description of the organization; must not be | ||
| * null | ||
| * @throws NullPointerException if name, websiteURL or description is null | ||
| * @throws IllegalArgumentException if orgNumber is negative, or if name or | ||
| * websiteURL is blank | ||
| */ | ||
| public Organization(int orgNumber, String name, boolean trusted, String websiteURL, boolean isPreApproved, | ||
| String description) { | ||
| if (orgNumber < 0) { | ||
| throw new IllegalArgumentException("orgNumber cannot be negative"); | ||
| } | ||
| this.orgNumber = orgNumber; | ||
| this.name = Objects.requireNonNull(name, "name cannot be null"); | ||
| this.trusted = trusted; | ||
| this.websiteURL = Objects.requireNonNull(websiteURL, "websiteURL cannot be null"); | ||
| this.isPreApproved = isPreApproved; | ||
| this.description = Objects.requireNonNull(description, "description cannot be null"); | ||
|
|
||
| if (name.isBlank()) { | ||
| throw new IllegalArgumentException("name cannot be blank"); | ||
| } | ||
| if (websiteURL.isBlank()) { | ||
| throw new IllegalArgumentException("websiteURL cannot be blank"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.