diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXConverter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXConverter.java index 8af79236e..a394cb4eb 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXConverter.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXConverter.java @@ -17,6 +17,8 @@ public interface BibTeXConverter> converters = new HashMap>(); + private final Map> converters = + new HashMap>(); @SuppressWarnings("rawtypes") private BibTeXConverters() { @@ -54,8 +65,8 @@ public class BibTeXConverters { final ImporterUtil importerUtil, final boolean pretend, final boolean publishNewItems) { - final PublicationImportReport report = new PublicationImportReport(); - + final PublicationImportReport report = new PublicationImportReport(); + BibTeXConverter converter = converters.get(bibTeXEntry. getType().getValue().toLowerCase()); @@ -68,6 +79,10 @@ public class BibTeXConverters { return report; } + if(isPublicationAlreadyInDatabase(bibTeXEntry, converter.getTypeName(), report)) { + return report; + } + try { converter = converter.getClass().newInstance(); } catch (InstantiationException ex) { @@ -97,7 +112,7 @@ public class BibTeXConverters { if (!pretend) { publication.setLanguage(Kernel.getConfig().getLanguagesIndependentCode()); } - final PublicationBundle bundle = converter.createBundle(publication, pretend); + final PublicationBundle bundle = converter.createBundle(publication, pretend); converter.processFields(bibTeXEntry, publication, importerUtil, report, pretend); @@ -146,4 +161,50 @@ public class BibTeXConverters { defaultCat.addChild(bundle); } } + + private boolean isPublicationAlreadyInDatabase(final BibTeXEntry bibTeXEntry, + final String type, + final PublicationImportReport importReport) { + final String title; + final String year; + + try { + final BibTeXUtil bibTeXUtil = new BibTeXUtil(null); + + title = bibTeXUtil.toPlainString(bibTeXEntry.getField(BibTeXEntry.KEY_TITLE)); + year = bibTeXUtil.toPlainString(bibTeXEntry.getField(BibTeXEntry.KEY_YEAR)); + } catch (IOException ex) { + return false; + } catch (ParseException ex) { + return false; + } + + int yearOfPublication; + try { + yearOfPublication = Integer.parseInt(year); + } catch (NumberFormatException ex) { + yearOfPublication = 0; + } + + final Session session = SessionManager.getSession(); + final DataCollection collection = session.retrieve(Publication.BASE_DATA_OBJECT_TYPE); + final FilterFactory filterFactory = collection.getFilterFactory(); + final Filter titleFilter = filterFactory.equals("title", title); + final Filter yearFilter = filterFactory.equals("yearOfPublication", yearOfPublication); + collection.addFilter(titleFilter); + collection.addFilter(yearFilter); + + final boolean result = !collection.isEmpty(); + collection.close(); + + if (result) { + importReport.setTitle(title); + importReport.setType(type); + importReport.addField(new FieldImportReport(Publication.YEAR_OF_PUBLICATION, year)); + importReport.setAlreadyInDatabase(result); + } + + return result; + } + } diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/converters/ArticleConverter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/converters/ArticleConverter.java index 8c15d94c4..b244cadcd 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/converters/ArticleConverter.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/converters/ArticleConverter.java @@ -24,6 +24,10 @@ public class ArticleConverter implements BibTeXConverter values = dataset.getValues().get(RisField.PY); + if ((values == null) || values.isEmpty()) { + return "0"; + } else { + return values.get(0); + } + } protected abstract void processFields(final RisDataset dataset, final T publication, @@ -38,9 +57,13 @@ public abstract class AbstractRisConverter colVolTitle = dataset.getValues().get(cvTitleField); - final List colVolYear = dataset.getValues().get(cvYearField); - final List colVolPlace = dataset.getValues().get(cvPlaceField); - final List colVolPublisher = dataset.getValues().get(cvPublisherField); - final List colVolEdition = dataset.getValues().get(cvEditionField); + final List colVolTitleValue = dataset.getValues().get(cvTitleField); + final List colVolYearValue = dataset.getValues().get(cvYearField); + final List colVolPlaceValue = dataset.getValues().get(cvPlaceField); + final List colVolPublisherValue = dataset.getValues().get(cvPublisherField); + final List colVolEditionValue = dataset.getValues().get(cvEditionField); final List colVolEditors = dataset.getValues().get(cvEditorsField); final List colVolEditorData = new ArrayList(); @@ -53,14 +53,37 @@ public class RisColVolUtil { } } - if ((colVolTitle != null) && !colVolTitle.isEmpty()) { + if ((colVolTitleValue != null) && !colVolTitleValue.isEmpty()) { + final String colVolTitle = colVolTitleValue.get(0); + final String colVolYear = colVolYearValue.get(0); + final String colVolPlace; + final String colVolPublisher; + final String colVolEdition; + + if ((colVolPlaceValue == null) || colVolPlaceValue.isEmpty()) { + colVolPlace = ""; + } else { + colVolPlace = colVolPlaceValue.get(0); + } + if ((colVolPublisherValue == null) || colVolPublisherValue.isEmpty()) { + colVolPublisher = ""; + } else { + colVolPublisher = colVolPublisherValue.get(0); + } + if ((colVolEditionValue == null) || colVolEditionValue.isEmpty()) { + colVolEdition = ""; + } else { + colVolEdition = colVolEditionValue.get(0); + } + + final CollectedVolumeImportReport colVolReport = importerUtil.processCollectedVolume(article, - colVolTitle.get(0), - colVolYear.get(0), + colVolTitle, + colVolYear, colVolEditorData, - colVolPublisher.get(0), - colVolPlace.get(0), - colVolEdition.get(0), + colVolPublisher, + colVolPlace, + colVolEdition, pretend); report.setCollectedVolume(colVolReport); } diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ris/converters/utils/RisFieldUtil.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ris/converters/utils/RisFieldUtil.java index 23b61d1c3..de180ba8f 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ris/converters/utils/RisFieldUtil.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ris/converters/utils/RisFieldUtil.java @@ -24,11 +24,9 @@ public class RisFieldUtil { } @SuppressWarnings("PMD.ConfusingTernary") - public void processTitle(final RisDataset dataset, - final Publication publication, - final PublicationImportReport report) { + public String getTitle(final RisDataset dataset) { final String title; - + if ((dataset.getValues().get(RisField.TI) != null) && !dataset.getValues().get(RisField.TI).isEmpty()) { title = dataset.getValues().get(RisField.TI).get(0); } else if ((dataset.getValues().get(RisField.BT) != null) && !dataset.getValues().get(RisField.BT).isEmpty()) { @@ -36,6 +34,15 @@ public class RisFieldUtil { } else { title = "Unknown"; } + + return title; + } + + + public void processTitle(final RisDataset dataset, + final Publication publication, + final PublicationImportReport report) { + final String title = getTitle(dataset); if (!pretend) { publication.setTitle(title); diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/util/ImporterUtil.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/util/ImporterUtil.java index 4c2eb104d..ae6e70c03 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/util/ImporterUtil.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/util/ImporterUtil.java @@ -215,7 +215,13 @@ public class ImporterUtil { final Session session = SessionManager.getSession(); final DataCollection collection = session.retrieve(CollectedVolume.BASE_DATA_OBJECT_TYPE); collection.addEqualsFilter("title", title); - collection.addEqualsFilter("yearOfPublication", year); + int yearOfPublication; + try { + yearOfPublication = Integer.parseInt(year); + } catch(NumberFormatException ex) { + yearOfPublication = 0; + } + collection.addEqualsFilter("yearOfPublication", yearOfPublication); report.setCollectedVolumeTitle(title); if (collection.isEmpty()) { if (!pretend) {