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

Feature/1013 add bibtex support #3020

Merged
merged 5 commits into from
Mar 24, 2016
Merged
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
1 change: 1 addition & 0 deletions src/main/java/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ dataset.cite.standards.tip=If you use these data, please add this citation to yo
dataset.cite.downloadBtn=Download Citation
dataset.cite.downloadBtn.xml=EndNote XML
dataset.cite.downloadBtn.ris=RIS Format
dataset.cite.downloadBtn.bib=BibTeX Format
dataset.create.authenticatedUsersOnly=Only authenticated users can create datasets.
dataset.deaccession.reason=Deaccession Reason
dataset.beAccessedAt=The dataset can now be accessed at:
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Bundle_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ dataset.cite.whyCite=\u4e3a\u4ec0\u4e48\u8981\u5f15\u7528\uff1f
dataset.cite.downloadBtn=\u4e0b\u8f7d\u5f15\u7528
dataset.cite.downloadBtn.xml=EndNote XML
dataset.cite.downloadBtn.ris=RIS\u683c\u5f0f
dataset.cite.downloadBtn.bib=BibTeX\u683c\u5f0f
dataset.deaccession.reason=Deaccession\u539f\u56e0\uff1a
dataset.beAccessedAt=\u8be5\u6570\u636e\u96c6\u73b0\u5728\u53ef\u4ee5\u5728{0}\u8bbf\u95ee
dataset.noTemplate.label=\u6ca1\u6709\u6a21\u677f - \u6e05\u6670\u7684\u9ed8\u8ba4\u503c
Expand Down
39 changes: 38 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3310,7 +3310,44 @@ public void downloadCitationRIS(FileMetadata fileMetadata) {

}
}


public void downloadDatasetCitationBibtex() {

downloadCitationBibtex(null);

}

public void downloadDatafileCitationBibtex(FileMetadata fileMetadata) {
downloadCitationBibtex(fileMetadata);
}

public void downloadCitationBibtex(FileMetadata fileMetadata) {

String bibFormatDowload = datasetService.createCitationBibtex(workingVersion, fileMetadata);
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
response.setContentType("application/download");

String fileNameString = "";
if (fileMetadata == null || fileMetadata.getLabel() == null) {
// Dataset-level citation:
fileNameString = "attachment;filename=" + getFileNameDOI() + ".bib";
} else {
// Datafile-level citation:
fileNameString = "attachment;filename=" + getFileNameDOI() + "-" + fileMetadata.getLabel().replaceAll("\\.tab$", ".bib");
}
response.setHeader("Content-Disposition", fileNameString);

try {
ServletOutputStream out = response.getOutputStream();
out.write(bibFormatDowload.getBytes());
out.flush();
ctx.responseComplete();
} catch (Exception e) {

}
}

public String getDatasetPublishCustomText(){
String datasetPublishCustomText = settingsService.getValueForKey(SettingsServiceBean.Key.DatasetPublishPopupCustomText);
if( datasetPublishCustomText!= null && !datasetPublishCustomText.isEmpty()){
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/DatasetServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public List<Dataset> findAll() {

/**
* For docs, see the equivalent method on the DataverseServiceBean.
* @see DataverseServiceBean#findAllOrSubset(long, long)
* @see DataverseServiceBean#findAllOrSubset(long, long, boolean)
*/
public List<Dataset> findAllOrSubset(long numPartitions, long partitionId, boolean skipIndexed) {
if (numPartitions < 1) {
Expand Down Expand Up @@ -265,6 +265,36 @@ public String createCitationRIS(DatasetVersion version, FileMetadata fileMetadat
return retString;
}

public String createCitationBibtex(DatasetVersion version) {
return createCitationBibtex(version, null);
}

public String createCitationBibtex(DatasetVersion version, FileMetadata fileMetadata) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmckinney I mentioned a while ago that it would be great to have unit tests for this createCitationBibtex method but it hasn't been until today at http://irclog.iq.harvard.edu/dataverse/2016-03-22#i_33154 that I got around to explaining my suggestion. As we discussed, it boils down to using DatasetVersionDTO and FileMetadataDTO objects rather than the non-DTO "entity" equivalents you're using now. To see some DTO objects in action see https://github.com/IQSS/dataverse/blob/v4.3/src/main/java/edu/harvard/iq/dataverse/export/ddi/DdiExportUtil.java and the unit tests that exercise them at https://github.com/IQSS/dataverse/blob/v4.3/src/test/java/edu/harvard/iq/dataverse/export/ddi/DdiExportUtilTest.java

The advantage of using these DTO objects is that you can run the unit tests without having PostgreSQL running.

@scolapasta just asked @kcondon to take a look at this pull request (#3020) so you may want to ping @scolapasta on if he'd like you to add unit tests.

String publisher = version.getRootDataverseNameforCitation();
List<DatasetAuthor> authorList = version.getDatasetAuthors();
List<String> authorDisplayList = new ArrayList<>();

for (DatasetAuthor author : authorList) {
authorDisplayList.add(author.getName().getDisplayValue());
}

String retString = "@data{";
retString += version.getDataset().getIdentifier() + "_" + version.getVersionYear() + "," + "\r\n";
retString += "author = {";
retString += String.join("; ", authorDisplayList);
retString += "}," + "\r\n";
retString += "publisher = {" + publisher + "}," + "\r\n";
retString += "title = {" + version.getTitle() + "}," + "\r\n";
retString += "year = {" + version.getVersionYear() + "}," + "\r\n";
retString += "doi = {" + version.getDataset().getAuthority() +
version.getDataset().getDoiSeparator() +
version.getDataset().getIdentifier() + "}," + "\r\n";
retString += "url = {" + version.getDataset().getPersistentURL() + "}" + "\r\n";
retString += "}";

return retString;
}

private XMLOutputFactory xmlOutputFactory = null;

public String createCitationXML(DatasetVersion datasetVersion, FileMetadata fileMetadata) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Access.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public BundleDownloadInstance datafileBundle(@PathParam("fileId") Long fileId, @

downloadInstance.setFileCitationEndNote(datasetService.createCitationXML(datasetVersion, fileMetadata));
downloadInstance.setFileCitationRIS(datasetService.createCitationRIS(datasetVersion, fileMetadata));

downloadInstance.setFileCitationBibtex(datasetService.createCitationBibtex(datasetVersion, fileMetadata));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmckinney does this mean that the bibtex citation will be available via API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pdurbin I believe this means that it will be included in the zip file produced by datafile/bundle/{fileId}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. What if I want to get the citation in bibtex format via API? Might be nice. And you could write an integration test around it.


ByteArrayOutputStream outStream = null;
outStream = new ByteArrayOutputStream();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package edu.harvard.iq.dataverse.api;

import java.util.List;

/**
*
* @author Leonid Andreev
Expand All @@ -19,6 +17,7 @@ public class BundleDownloadInstance {
private String fileCitationEndNote = "";
private String fileCitationRIS = "";
private String fileDDIXML = "";
private String fileCitationBibtex = "";

public BundleDownloadInstance() {

Expand Down Expand Up @@ -59,4 +58,13 @@ public String getFileDDIXML() {
public void setFileDDIXML(String fileDDIXML) {
this.fileDDIXML = fileDDIXML;
}

public String getFileCitationBibtex() {
return fileCitationBibtex;
}

public void setFileCitationBibtex(String fileCitationBibtex) {
this.fileCitationBibtex = fileCitationBibtex;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ public void writeTo(BundleDownloadInstance di, Class<?> clazz, Type type, Annota
zout.closeEntry();
}

if (di.getFileCitationBibtex() != null) {
e = new ZipEntry(fileName.replaceAll("\\.tab$","citation-bib.bib"));

zout.putNextEntry(e);
zout.write(di.getFileCitationBibtex().getBytes());
zout.closeEntry();
}

zout.close();
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/dataset.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@
#{bundle['dataset.cite.downloadBtn.ris']}
</a>
</li>
<li>
<a jsf:id="bibLink" jsf:actionListener="#{DatasetPage.downloadDatasetCitationBibtex()}">
#{bundle['dataset.cite.downloadBtn.bib']}
</a>
</li>
</ul>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions src/main/webapp/filesFragment.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,12 @@
<h:outputText value="#{bundle['dataset.cite.downloadBtn.xml']}" />
</h:commandLink>
</li>
<li>
<h:commandLink id="bibLinkFile" action="#{DatasetPage.downloadDatafileCitationBibtex(fileMetadata)}">
<h:outputText value="#{bundle['dataset.cite.downloadBtn.bib']}" />
</h:commandLink>
</li>

</ul>
</li>
</ul>
Expand Down