Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix results model convert result to a list #1

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Version 2.0.0
- Update pom.xml
- fix results model convert result to a list
- fix example result
- remove old model
- new model based on jaxb extraction
- new junit tests
- client response model
- XSD model inclusion and support
- helper classes to help with using the model
- maven dependencies for testing and jaxb
- adding usage examples to README.md
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,27 @@ This repository contains the data model used in the OpenAIRE Funders API.

Use this JAR as a dependency in projects for marshalling and unmarshalling OpenAIRE Funders API.

## Usage examples

InputStream is = getClass().getClassLoader().getResourceAsStream("example.xml");

Response response = OpenAIREHandler.unmarshal(is);
is.close();

//We can have multiple results, let's pick the first one
Result result = response.getResults().getResult().get(0);

assertNotNull(result);
assertNotNull(result.getMetadata().getEntity().getProject());

ProjectHelper projectHelper = new ProjectHelper(
result.getMetadata().getEntity().getProject().getCodeOrTitleOrAcronym());


// get the first item (according to the model can have multiple)
System.out.println ("Project Code: " + projectHelper.getCodes().stream().findFirst().get());

// get all titles in a project
for (String title : projectHelper.getTitles()) {
System.out.println("Title: " + title);
}
75 changes: 56 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.openaire</groupId>
<artifactId>funders-model</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<name>openairefundersjaxbmodel</name>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.openaire</groupId>
<artifactId>funders-model</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.25.0-GA</version>
</dependency>

<!-- junit 5, unit test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>

</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<name>openairefundersjaxbmodel</name>
</project>
61 changes: 61 additions & 0 deletions src/main/java/eu/openaire/jaxb/helper/FundingHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package eu.openaire.jaxb.helper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBElement;

import eu.openaire.oaf.model.base.FundingType;

public class FundingHelper {
private Map<String, Collection<FundingType>> fundingTypeProperties;

public FundingHelper (List<JAXBElement<FundingType>> fundingProperties) {
this.fundingTypeProperties = new HashMap<String, Collection<FundingType>>();

for (JAXBElement<?> element : fundingProperties) {
String nodeName = element.getName().getLocalPart();
String nodeType = element.getDeclaredType().getSimpleName();
Object castValue = element.getDeclaredType().cast(element.getValue());

switch (nodeType) {
case "FundingType":
Collection<FundingType> fundingTypeCollection = this.fundingTypeProperties.getOrDefault(nodeName,
new ArrayList<>());
fundingTypeCollection.add((FundingType) castValue);
this.fundingTypeProperties.put(nodeName, fundingTypeCollection);
break;
}
}

}

public Collection<FundingType> getFirstAvailableFunding() {
if (this.fundingTypeProperties.containsKey("funding_level_2")) {
return getFundingTypeValue("funding_level_2");
}
if (this.fundingTypeProperties.containsKey("funding_level_1")) {
return getFundingTypeValue("funding_level_1");
}
return getFundingTypeValue("funding_level_0");
}

public Collection<FundingType> getFundingLevel2() {
return getFundingTypeValue("funding_level_2");
}

public Collection<FundingType> getFundingLevel1() {
return getFundingTypeValue("funding_level_1");
}

public Collection<FundingType> getFundingLevel0() {
return getFundingTypeValue("funding_level_0");
}

private Collection<FundingType> getFundingTypeValue(String fieldName) {
return this.fundingTypeProperties.getOrDefault(fieldName,new ArrayList<>());
}
}
73 changes: 73 additions & 0 deletions src/main/java/eu/openaire/jaxb/helper/OpenAIREHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package eu.openaire.jaxb.helper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import eu.openaire.jaxb.model.Response;

/**
* This Class handles the Marshaling from provided sources like URL or
* InputStream
*
* @author dpie
* @author pgraca
*
*/
public class OpenAIREHandler {

// Export: Marshalling
public static void marshal(Response response, File selectedFile) throws IOException, JAXBException {
JAXBContext context;
BufferedWriter writer = null;
writer = new BufferedWriter(new FileWriter(selectedFile));
context = JAXBContext.newInstance(Response.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(response, writer);
writer.close();
}

// Import: Unmarshalling
public static Response unmarshal(URL importFile) throws JAXBException {
Response response = null;
JAXBContext context;

context = JAXBContext.newInstance(Response.class);
Unmarshaller um = context.createUnmarshaller();
response = (Response) um.unmarshal(importFile);

return response;
}

/**
* Unmarshal and returns a Response from an InputStream
*
* @param iStream
* @return
* @throws JAXBException
*/
public static Response unmarshal(InputStream iStream) throws JAXBException {
Response response = null;
JAXBContext context;

context = JAXBContext.newInstance(Response.class);
Unmarshaller um = context.createUnmarshaller();
response = (Response) um.unmarshal(iStream);

return response;
}
}
Loading