Skip to content

Commit

Permalink
Merge branch 'bulk_add'
Browse files Browse the repository at this point in the history
  • Loading branch information
nmacedo committed Jun 1, 2017
2 parents 936d4d8 + 5942957 commit 10723eb
Show file tree
Hide file tree
Showing 52 changed files with 1,471 additions and 460 deletions.
77 changes: 59 additions & 18 deletions src/main/java/pt/ptcris/ORCIDClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
/*
* Copyright (c) 2016, 2017 PTCRIS - FCT|FCCN and others.
* Licensed under MIT License
* http://ptcris.pt
*
* This copyright and license information (including a link to the full license)
* shall be included in its entirety in all copies or substantial portion of
* the software.
*/
package pt.ptcris;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
import org.um.dsi.gavea.orcid.model.activities.Works;
import org.um.dsi.gavea.orcid.model.work.Work;
import org.um.dsi.gavea.orcid.model.work.WorkSummary;

/**
* Interface that encapsulates the communication with the ORCID client for a
Expand All @@ -25,48 +38,67 @@ public interface ORCIDClient {

/**
* Retrieves a complete work from the ORCID profile (as opposed to only its
* summary).
* summary). Exceptions are embedded in the {@link PTCRISyncResult}.
*
* @param putcode
* the put-code of the work
* @param summary
* the summary of the work to be retrieved
* @return the complete work
* @throws OrcidClientException
* if the communication with ORCID fails
*/
public Work getWork(BigInteger putcode) throws OrcidClientException;
public PTCRISyncResult getWork(WorkSummary summary);

/**
* Adds a new work to the ORCID profile.
* Retrieves a list of complete work from the ORCID profile (as opposed to
* only their summaries). Exceptions are embedded in the
* {@link PTCRISyncResult}. This should generate a single API call
* internally.
*
* @param summaries
* the summaries of the works to be retrieved
* @return the complete works
*/
public Map<BigInteger, PTCRISyncResult> getWorks(List<WorkSummary> summaries);

/**
* Adds a new work to the ORCID profile. Exceptions are embedded in the
* {@link PTCRISyncResult}.
*
* @param work
* the work to be added to the ORCID profile
* @return the put-code assigned by ORCID to the newly created work
* @throws OrcidClientException
* if the communication with ORCID fails
*/
public BigInteger addWork(Work work) throws OrcidClientException;
public PTCRISyncResult addWork(Work work);

/**
* Deletes a work from the ORCID profile.
* Adds a list of new works to the ORCID profile. Exceptions are embedded in
* the {@link PTCRISyncResult}. This should generate a single API call
* internally.
*
* @param works
* the works to be added to the ORCID profile
* @return the put-codes assigned by ORCID to each of the newly created
* works
*/
public List<PTCRISyncResult> addWorks(List<Work> works);

/**
* Deletes a work from the ORCID profile. Exceptions are embedded in
* the {@link PTCRISyncResult}.
*
* @param putcode
* the put-code of the work to be deleted
* @throws OrcidClientException
* if the communication with ORCID fails
*/
public void deleteWork(BigInteger putcode) throws OrcidClientException;
public PTCRISyncResult deleteWork(BigInteger putcode);

/**
* Updates a work in the ORCID profile.
* Updates a work in the ORCID profile. Exceptions are embedded in
* the {@link PTCRISyncResult}.
*
* @param putcode
* the put-code of the work to be updated
* @param work
* the new state of the work
* @throws OrcidClientException
* if the communication with ORCID fails
*/
public void updateWork(BigInteger putcode, Work work) throws OrcidClientException;
public PTCRISyncResult updateWork(BigInteger putcode, Work work);

/**
* Retrieves every activity summary from the ORCID profile.
Expand All @@ -77,6 +109,15 @@ public interface ORCIDClient {
*/
public ActivitiesSummary getActivitiesSummary() throws OrcidClientException;

/**
* Retrieves every work summary from the ORCID profile.
*
* @return the works summary of the ORCID profile
* @throws OrcidClientException
* if the communication with ORCID fails
*/
public Works getWorksSummary() throws OrcidClientException;

/**
* The number of worker threads that will be used to communicate with the
* ORCID API.
Expand Down
149 changes: 141 additions & 8 deletions src/main/java/pt/ptcris/ORCIDClientImpl.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
/*
* Copyright (c) 2016, 2017 PTCRIS - FCT|FCCN and others.
* Licensed under MIT License
* http://ptcris.pt
*
* This copyright and license information (including a link to the full license)
* shall be included in its entirety in all copies or substantial portion of
* the software.
*/
package pt.ptcris;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
import org.um.dsi.gavea.orcid.client.OrcidOAuthClient;
import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
import org.um.dsi.gavea.orcid.model.activities.Works;
import org.um.dsi.gavea.orcid.model.bulk.Bulk;
import org.um.dsi.gavea.orcid.model.work.Work;
import org.um.dsi.gavea.orcid.model.work.WorkSummary;
import org.um.dsi.gavea.orcid.model.error.Error;

import pt.ptcris.utils.ORCIDHelper;

/**
* An implementation of the ORCID client interface built over the
Expand Down Expand Up @@ -135,32 +155,122 @@ public String getClientId() {
* {@inheritDoc}
*/
@Override
public Work getWork(BigInteger putcode) throws OrcidClientException {
return orcidClient.readWork(orcidToken, putcode.toString());
public PTCRISyncResult getWork(WorkSummary putcode) {
PTCRISyncResult res;
try {
Work work = orcidClient.readWork(orcidToken, putcode.getPutCode().toString());
finalizeGet(work, putcode);
res = PTCRISyncResult.got(putcode.getPutCode(), work);
} catch (OrcidClientException e) {
res = PTCRISyncResult.fail(e);
}
return res;
}

/**
* {@inheritDoc}
*/
@Override
public Map<BigInteger,PTCRISyncResult> getWorks(List<WorkSummary> summaries) {
List<String> pcs = new ArrayList<String>();
for (WorkSummary i : summaries)
pcs.add(i.getPutCode().toString());
Map<BigInteger,PTCRISyncResult> res = new HashMap<BigInteger,PTCRISyncResult>();
try {
List<Serializable> bulk = orcidClient.readWorks(orcidToken, pcs).getWorkOrError();
for (int i = 0; i < summaries.size(); i++) {
Serializable w = bulk.get(i);
if (w instanceof Work) {
finalizeGet((Work) w, summaries.get(i));
res.put(summaries.get(i).getPutCode(),PTCRISyncResult.got(summaries.get(i).getPutCode(),(Work) w));
}
else {
Error err = (Error) w;
OrcidClientException e = new OrcidClientException(err.getResponseCode(),
err.getUserMessage(),
err.getErrorCode(),
err.getDeveloperMessage());
res.put(summaries.get(i).getPutCode(),PTCRISyncResult.fail(e));
}
}
} catch (OrcidClientException e1) {
for (int i = 0; i < summaries.size(); i++)
res.put(summaries.get(i).getPutCode(),PTCRISyncResult.fail(e1));
}
return res;
}

/**
* {@inheritDoc}
*/
@Override
public PTCRISyncResult addWork(Work work) {
PTCRISyncResult res;
try {
BigInteger putcode = new BigInteger(orcidClient.addWork(orcidToken, work));
res = PTCRISyncResult.ok(putcode);
} catch (OrcidClientException e) {
return PTCRISyncResult.fail(e);
}
return res;
}

/**
* {@inheritDoc}
*/
@Override
public BigInteger addWork(Work work) throws OrcidClientException {
return new BigInteger(orcidClient.addWork(orcidToken, work));
public List<PTCRISyncResult> addWorks(List<Work> works) {
Bulk bulk = new Bulk();
List<PTCRISyncResult> res = new ArrayList<PTCRISyncResult>();
for (Work work : works)
bulk.getWorkOrError().add(work);
try {
Bulk res_bulk = orcidClient.addWorks(orcidToken, bulk);
for (Serializable r : res_bulk.getWorkOrError()) {
if (r instanceof Work)
res.add(PTCRISyncResult.ok(((Work) r).getPutCode()));
else {
Error err = (Error) r;
OrcidClientException e = new OrcidClientException(err.getResponseCode(),
err.getUserMessage(),
err.getErrorCode(),
err.getDeveloperMessage());
res.add(PTCRISyncResult.fail(e));
}
}
} catch (OrcidClientException e) {
for (int i=0;i<works.size();i++)
res.add(PTCRISyncResult.fail(e));
}
return res;
}

/**
* {@inheritDoc}
*/
@Override
public void deleteWork(BigInteger putcode) throws OrcidClientException {
orcidClient.deleteWork(orcidToken, putcode.toString());
public PTCRISyncResult deleteWork(BigInteger putcode) {
try {
orcidClient.deleteWork(orcidToken, putcode.toString());
return PTCRISyncResult.OK_DEL_RESULT;
} catch (OrcidClientException e) {
return PTCRISyncResult.fail(e);
}
}

/**
* {@inheritDoc}
*/
@Override
public void updateWork(BigInteger putcode, Work work) throws OrcidClientException {
orcidClient.updateWork(orcidToken, putcode.toString(), work);
public PTCRISyncResult updateWork(BigInteger putcode, Work work) {
PTCRISyncResult res;
try {
orcidClient.updateWork(orcidToken, putcode.toString(), work);
res = PTCRISyncResult.OK_UPD_RESULT;
} catch (OrcidClientException e) {
return PTCRISyncResult.fail(e);
}
return res;
}

/**
Expand All @@ -171,12 +281,35 @@ public ActivitiesSummary getActivitiesSummary() throws OrcidClientException {
return orcidClient.readActivitiesSummary(orcidToken);
}

/**
* {@inheritDoc}
*/
@Override
public Works getWorksSummary() throws OrcidClientException {
return orcidClient.readWorksSummary(orcidToken);
}

/**
* {@inheritDoc}
*/
@Override
public int threads() {
return threads;
}

/**
* Finalizes a get by updating the meta-data.
*
* @see #getFullWork(WorkSummary)
*
* @param fullWork
* the newly retrieved work
* @param sumWork
* the original summary
*/
private static void finalizeGet(Work fullWork, WorkSummary sumWork) {
fullWork.setExternalIds(ORCIDHelper.getNonNullExternalIds(sumWork));
ORCIDHelper.cleanWorkLocalKey(fullWork);
}

}
9 changes: 9 additions & 0 deletions src/main/java/pt/ptcris/PTCRISGrouper.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (c) 2016, 2017 PTCRIS - FCT|FCCN and others.
* Licensed under MIT License
* http://ptcris.pt
*
* This copyright and license information (including a link to the full license)
* shall be included in its entirety in all copies or substantial portion of
* the software.
*/
package pt.ptcris;

import java.util.Collection;
Expand Down
Loading

0 comments on commit 10723eb

Please sign in to comment.