Skip to content

Commit

Permalink
fix nsf award check to handle the API output
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanetteclark committed Feb 6, 2024
1 parent 2f4dd3f commit 813bb5e
Showing 1 changed file with 43 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

/**
* Looks up award information for given award number
*
* @author leinfelder
*
*/
public class AwardLookupCheck implements Callable<Result> {

private static String apiUrl = "https://www.research.gov/awardapi-service/v1/awards.json"
+ "?printFields="
+ "id,"
Expand All @@ -30,18 +31,18 @@ public class AwardLookupCheck implements Callable<Result> {
+ "fundProgramName,"
+ "primaryProgram"
+ "&id=";

// the award identifier[s]
private Object awards;

public Log log = LogFactory.getLog(this.getClass());

@Override
public Result call() {
Result result = new Result();
List<Output> outputs = new ArrayList<Output>();
URL url = null;

if (this.awards != null) {
result.setStatus(Status.SUCCESS);

Expand All @@ -51,84 +52,73 @@ public Result call() {
} else {
awardList.add(awards.toString());
}

for (Object awardId: awardList) {


for (Object awardId : awardList) {
try {

// fetch the results, extracting just the award number, if other info is in the string.
String idString = awardId.toString().toLowerCase().replace("nsf award", "").trim();

// Pad the award number with leading zeros if necessary
if (awardId instanceof Number) {
idString = String.format("%07d", awardId);
}

url = new URL(apiUrl + idString);
log.debug("URL=" + url.toString());

InputStream jsonStream = url.openStream();
// parse

// parse the JSON array directly
JSONParser parser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
String apiRetStr= parser.parse(jsonStream).toString();
log.debug("String returned from Award API: " + apiRetStr);
JSONObject json = (JSONObject) parser.parse(apiRetStr);
JSONArray jsonAwards = (JSONArray) parser.parse(jsonStream);

if (json == null) {
if (jsonAwards.isEmpty()) {
log.warn("No award information found for " + idString);
continue;
}

log.debug("JSON=" + json.toJSONString());

JSONObject jsonResponse = (JSONObject) json.get("response");
JSONArray jsonAwards = (JSONArray) jsonResponse.get("award");
if (jsonAwards == null || jsonAwards.size() < 1) {
log.warn("No award information found for " + idString);
continue;
}
JSONObject jsonAward = (JSONObject) jsonAwards.get(0);

// find the desired information about the award
String title = (String) jsonAward.get("title");
String agency = (String) jsonAward.get("agency");
String id = (String) jsonAward.get("id");
String fundProgramName = (String) jsonAward.get("fundProgramName");
String primaryProgram = (String) jsonAward.get("primaryProgram");

// add them all as outputs for search and grouping
outputs.add(new Output(title + " (" + agency + " " + id + ")"));
outputs.add(new Output(title));
outputs.add(new Output(agency));
outputs.add(new Output(id));
outputs.add(new Output(fundProgramName));
outputs.add(new Output(primaryProgram));

// look up cross ref info as well
outputs.addAll(this.lookupCrossRef(id));
// Process each award in the JSON array
for (Object obj : jsonAwards) {
JSONObject jsonAward = (JSONObject) obj;

// Extract desired information about the award
String title = (String) jsonAward.get("title");
String agency = (String) jsonAward.get("agency");
String id = (String) jsonAward.get("id");
String fundProgramName = (String) jsonAward.get("fundProgramName");
String primaryProgram = (String) jsonAward.get("primaryProgram");

// Add them all as outputs for search and grouping
outputs.add(new Output(title + " (" + agency + " " + id + ")"));
outputs.add(new Output(title));
outputs.add(new Output(agency));
outputs.add(new Output(id));
outputs.add(new Output(fundProgramName));
outputs.add(new Output(primaryProgram));

// Look up cross-reference info as well
outputs.addAll(this.lookupCrossRef(id));
}
} catch (Exception e) {
log.error("Could not look up award " + awardId + " at URL " + url.toString() + ": ", e);
continue;
}
}

// set them all
result.setOutput(outputs);

} else {
result.setStatus(Status.FAILURE);
result.setOutput(new Output("NA"));
log.warn("No award id given, cannot look-up funding");
}

return result;

}

// TODO: implement this in a way that makes sense
private List<Output> lookupCrossRef(String awardId) {

List<Output> outputs = new ArrayList<Output>();

// find the funder using a work with this awardId
Expand All @@ -137,8 +127,8 @@ private List<Output> lookupCrossRef(String awardId) {

// message.items[0].funder[x].DOI
String funderDOI = null;
String funderId = null; //funderDOI.substring(funderDOI.lastIndexOf("/"));
String funderId = null; // funderDOI.substring(funderDOI.lastIndexOf("/"));

// find the org hierarchy for the funder
String funderUrl = "http://api.crossref.org/funders/" + funderId;

Expand All @@ -152,5 +142,5 @@ public Object getAwards() {
public void setAwards(Object awards) {
this.awards = awards;
}

}

0 comments on commit 813bb5e

Please sign in to comment.