Skip to content

Commit

Permalink
Finalização da funcionalidade Importação de arquivos Bibtex
Browse files Browse the repository at this point in the history
  • Loading branch information
diogovss authored and rafaelbrandao committed Apr 25, 2013
1 parent 589fec2 commit 95bbfa3
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 36 deletions.
1 change: 1 addition & 0 deletions grails-app/conf/BuildConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ grails.project.dependency.resolution = {
compile('lib:itextpdf:5.4.0')
compile('lib:itext-pdfa:5.4.0')
compile('lib:itext-xtra:5.4.0')
compile('lib:jbibtex:1.0.3')
// runtime 'mysql:mysql-connector-java:5.1.16'
}

Expand Down
5 changes: 5 additions & 0 deletions grails-app/conf/UrlMappings.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class UrlMappings {
}
}

"/bibtexFileController/upload" {
controller = "bibtexFile"
action = "upload"
}

"/"(controller: "Auth", action: "index")
"500"(view:'/error')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package rgms.publication

import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
*
* @author Diogo Vinícius
*
*/
class BibtexFileController {

def index() {}

def upload () {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest )request
CommonsMultipartFile cmf = (CommonsMultipartFile)multiRequest.getFile("file")
byte[] bytes = cmf.bytes
File file = new File("web-app//uploads//temp.bibtex")
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))
bos.write(bytes)
bos.close()

System.out.println("********** " + file.getBytes().size());

BibtexFile bibtexFile = transform(file)

System.out.println("********** " + bibtexFile.getPublications().size())
for (Publication publication : bibtexFile.getPublications()) {
System.out.println("************* " + publication);
System.out.println("************************** School : " + publication.getSchool());
System.out.println("************************** Address: " + publication.getAddress());
System.out.println("************************** Title : " + publication.getTitle());
System.out.println("************************** Date : " + publication.getPublicationDate());
if (publication.save(failOnError: true)) {
System.out.println("salvando o objeto");
}
}
redirect(action: "home")

}

def home () {

}

def BibtexFile transform(file) {
return new BibtexFile(file)
}

}
54 changes: 54 additions & 0 deletions grails-app/domain/rgms/publication/BibtexFile.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package rgms.publication

import java.util.Collection;

import java.io.BufferedReader
import java.io.File;
import java.io.FileInputStream
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader
import java.io.Reader;
import java.util.ArrayList
import java.util.List

import org.jbibtex.BibTeXEntry;
import org.jbibtex.Value;
import org.jbibtex.BibTeXDatabase;
import org.jbibtex.BibTeXEntry;
import org.jbibtex.BibTeXParser;
import org.jbibtex.BibTeXString;
import org.jbibtex.Key;
import org.jbibtex.ParseException;

import rgms.member.Member

/**
*
* @author Diogo Vinícius
*
*/
class BibtexFile {

List<Publication> publications = new ArrayList<Publication>()
Member member

public BibtexFile(file) {
publications = BibtexParse.generatePublications(file)
}

public List<Publication> getPublications() {
return this.publications
}

public List getPublications(Class clazz) {
List publicationsFiltered = new ArrayList()
for (Publication publication : this.getPublications()) {
if (publication.getClass().getName().equals(clazz.getName())) {
publicationsFiltered.add(publication)
}
}
return publicationsFiltered
}

}
116 changes: 116 additions & 0 deletions grails-app/domain/rgms/publication/BibtexParse.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package rgms.publication

import org.jbibtex.BibTeXDatabase
import org.jbibtex.BibTeXEntry;
import org.jbibtex.BibTeXParser
import org.jbibtex.BibTeXString
import org.jbibtex.Key
import org.jbibtex.ParseException
import org.jbibtex.Value

/**
*
* @author Diogo Vinícius
*
*/
class BibtexParse {

public static List<Publication> generatePublications(File file) {
List<Publication> publications = new ArrayList<Publication>()
BibTeXDatabase bibtexDatabase = parseBibTeX(file)
Collection<BibTeXEntry> entries = bibtexDatabase.getEntries().values();

for(BibTeXEntry entry : entries){
//TODO settar todos os atributos de acordo com a classe a ser instanciada
//Para pegar os valores do objeto 'entry' basta seguir o modelo da linha abaixo
//String value = entry.getField(BibTeXEntry.KEY_TITLE).toUserString();
if (entry.getType().equals(BibTeXEntry.TYPE_ARTICLE)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_BOOK)) {
publications.add(new BookChapter())
}
else if (entry.getType().equals(BibTeXEntry.TYPE_BOOKLET)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_CONFERENCE)) {
publications.add(new Conferencia())
}
else if (entry.getType().equals(BibTeXEntry.TYPE_INBOOK)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_INCOLLECTION)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_INPROCEEDINGS)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_MANUAL)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_MASTERSTHESIS)) {
Dissertacao dissertacao = new Dissertacao()
dissertacao.setTitle(entry.getField(BibTeXEntry.KEY_TITLE).toUserString())
dissertacao.setSchool(entry.getField(BibTeXEntry.KEY_SCHOOL).toUserString())
dissertacao.setAddress(entry.getField(BibTeXEntry.KEY_ADDRESS).toUserString())
dissertacao.setPublicationDate(new Date())//TODO transformar o date para setar no objeto
dissertacao.setFile("file")//TODO tirar a obrigatoriedade. futuramente processar a url para importar
publications.add(dissertacao)
}
else if (entry.getType().equals(BibTeXEntry.TYPE_MISC)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_PHDTHESIS)) {
Tese tese = new Tese()
tese.setTitle(entry.getField(BibTeXEntry.KEY_TITLE).toUserString())
tese.setSchool(entry.getField(BibTeXEntry.KEY_SCHOOL).toUserString())
tese.setAddress(entry.getField(BibTeXEntry.KEY_ADDRESS).toUserString())
tese.setPublicationDate(new Date())//TODO transformar o date para setar no objeto
tese.setFile("file")//TODO settar corretamente este atributo
publications.add(tese)
}
else if (entry.getType().equals(BibTeXEntry.TYPE_PROCEEDINGS)) {

}
else if (entry.getType().equals(BibTeXEntry.TYPE_TECHREPORT)) {
publications.add(new TechnicalReport())
}
else if (entry.getType().equals(BibTeXEntry.TYPE_UNPUBLISHED)) {

}

}

return publications

}

private static BibTeXDatabase parseBibTeX(File file) throws IOException, ParseException {
Reader reader = new FileReader(file);

try {
BibTeXParser parser = new BibTeXParser(){

@Override
public void checkStringResolution(Key key, BibTeXString string){

if(string == null){
System.err.println("Unresolved string: \"" + key.getValue() + "\"");
}
}

@Override
public void checkCrossReferenceResolution(Key key, BibTeXEntry entry){

if(entry == null){
System.err.println("Unresolved cross-reference: \"" + key.getValue() + "\"");
}
}
};

return parser.parse(reader);
} finally {
reader.close();
}
}
}
17 changes: 17 additions & 0 deletions grails-app/views/bibtexFile/home.gsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Insert title here</title>
</head>
<body>
<div class="body">
<br />
<g:form controller="BibtexFileController" method="post" action="upload" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</g:form>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions grails-app/views/initial.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<li><g:link controller="TechnicalReport" action="list">Technical Report</g:link></li>
<li><g:link controller="Tese" action="list">Tese</g:link></li>
<li><g:link controller="Orientation" action="list">Orientation</g:link></li>
<li><g:link controller="BibtexFile" action="home">Import Bibtex File</g:link></li>
</ol>
</div>
<!-- </div>-->
Expand Down
4 changes: 2 additions & 2 deletions grails-app/views/tese/list.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<th><g:message code="tese.researchLine.label" default="Research Line" /></th>

<g:sortableColumn property="bibTex" title="${message(code: 'tese.bibTex.label', default: 'Bib Tex')}" />
<%-- g:sortableColumn property="bibTex" title="${message(code: 'tese.bibTex.label', default: 'Bib Tex')}" / --%>

<g:sortableColumn property="school" title="${message(code: 'tese.school.label', default: 'School')}" />

Expand All @@ -50,7 +50,7 @@

<td>${fieldValue(bean: teseInstance, field: "researchLine")}</td>

<td>${fieldValue(bean: teseInstance, field: "bibTex")}</td>
<%-- td>${fieldValue(bean: teseInstance, field: "bibTex")}</td --%>

<td>${fieldValue(bean: teseInstance, field: "school")}</td>

Expand Down
Binary file added lib/jbibtex-1.0.3.jar
Binary file not shown.
8 changes: 4 additions & 4 deletions test/cucumber/BibtexImport.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Feature: BibtexImport
Scenario: bibtex file with several publication types
Given I am on Import Bibtex File Menu
When I click "Choose file"
And selected a bibtex file with one Book Chapter and two Technical Report and I click "Import"
Then is created one Book Chapter publication
And is created two Technical Report publications
And one Book Chapter is stored and two Technical Report is stored
And selected a bibtex file with one Dissertacao and two Tese and I click "Import"
Then is created one Dissertacao publication
And is created two Tese publications
And one Dissertacao is stored and two Tese is stored
51 changes: 21 additions & 30 deletions test/cucumber/steps/BibtexImportSteps.groovy
Original file line number Diff line number Diff line change
@@ -1,68 +1,59 @@
import junit.framework.Test;
import rgms.publication.*;
import cucumber.runtime.PendingException;
import rgms.publication.BibtexFile;
import static cucumber.api.groovy.EN.*

//You can implement missing steps with the snippets below:
Given(~'^I am on Import Bibtex File Menu$') { ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
}

When(~'^I click "([^"]*)"$') { String arg1 ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
}

When(~'^selected a bibtex file and I click "([^"]*)"$') { String arg1 ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
}

Then(~'^is created all corresponding publications$') { ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
BibtexFileController bibtexFileController = new BibtexFileController()
BibtexFile bibtexFile = bibtexFileController.transform(new File("test//cucumber//steps//sample.bibtex"))
assert bibtexFile.getPublications().size() == 3
}

Then(~'^all of then are stored$') { ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
}

When(~'^selected a bibtex file unformatted and I click "([^"]*)"$') { String arg1 ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
}

//@Test(expected=RuntimeException.class)
Then(~'^the system output the message error "([^"]*)"$') { String arg1 ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
BibtexFileController bibtexFileController = new BibtexFileController()
BibtexFile bibtexFile = bibtexFileController.transform(new File("test//cucumber//steps//sample.bibtex"))
}

Then(~'^none publication is stored$') { ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
}

When(~'^selected a bibtex file with one Book Chapter and two Technical Report and I click "([^"]*)"$') { String arg1 ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
When(~'^selected a bibtex file with one Dissertacao and two Tese and I click "([^"]*)"$') { String arg1 ->
}

Then(~'^is created one Book Chapter publication$') { ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
Then(~'^is created one Dissertacao publication$') { ->
BibtexFileController bibtexFileController = new BibtexFileController()
BibtexFile bibtexFile = bibtexFileController.transform(new File("test//cucumber//steps//sample.bibtex"))
assert bibtexFile.getPublications(Conferencia.class).isEmpty()
assert bibtexFile.getPublications(Dissertacao.class).size() == 1
}

Then(~'^is created two Technical Report publications$') { ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
Then(~'^is created two Tese publications$') { ->
BibtexFileController bibtexFileController = new BibtexFileController()
BibtexFile bibtexFile = bibtexFileController.transform(new File("test//cucumber//steps//sample.bibtex"))
assert bibtexFile.getPublications(TechnicalReport.class).isEmpty()
assert bibtexFile.getPublications(Tese.class).size() == 2
}

Then(~'^one Book Chapter is stored and two Technical Report is stored$') { ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
Then(~'^one Dissertacao is stored and two Tese is stored$') { ->
}

When(~'^I click on "([^"]*)" option at Technical Report list$') { String arg1 ->
// Express the Regexp above with the code you wish you had
throw new PendingException()
}
Loading

0 comments on commit 95bbfa3

Please sign in to comment.