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 e2cfed1f8..8af79236e 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 @@ -19,6 +19,11 @@ public interface BibTeXConverter * @version $Id$ */ public class BibTeXConverters { - + private final static Logger LOGGER = Logger.getLogger(BibTeXConverters.class); - private final Map> converters = new HashMap>(); - + private final Map> converters = new HashMap>(); + @SuppressWarnings("rawtypes") private BibTeXConverters() { LOGGER.debug("Loading BibTeX converters..."); final ServiceLoader converterServices = ServiceLoader.load(BibTeXConverter.class); - - for(BibTeXConverter converter : converterServices) { + + for (BibTeXConverter converter : converterServices) { LOGGER.debug(String.format("Found converter for BibTeX type '%s'.", converter.getBibTeXType())); - + converters.put(converter.getBibTeXType(), converter); } - LOGGER.debug(String.format("Found %d BibTeX converters.", converters.size())); + LOGGER.debug(String.format("Found %d BibTeX converters.", converters.size())); } - + private static class Instance { - + private static BibTeXConverters INSTANCE = new BibTeXConverters(); - } - + public static BibTeXConverters getInstance() { return Instance.INSTANCE; } - + public PublicationImportReport convert(final BibTeXEntry bibTeXEntry, final ImporterUtil importerUtil, final boolean pretend, final boolean publishNewItems) { final PublicationImportReport report = new PublicationImportReport(); - - - + + BibTeXConverter converter = converters.get(bibTeXEntry. + getType().getValue()); + + if (converter == null) { + report.addMessage(String.format("No converter for BibTeX type '%s' available. Publication '%s' has not" + + "been imported.", + bibTeXEntry.getType().getValue(), + bibTeXEntry.getKey().getValue())); + + return report; + } + + try { + converter = converter.getClass().newInstance(); + } catch (InstantiationException ex) { + final StringWriter writer = new StringWriter(); + writer.append(String.format("Failed to create instance of converter for BibTeX type '%s'. Publication" + + " '%s' was not imported.", + bibTeXEntry.getType().getValue(), + bibTeXEntry.getKey().getValue())); + ex.printStackTrace(new PrintWriter(writer)); + report.addMessage(writer.toString()); + + return report; + } catch (IllegalAccessException ex) { + final StringWriter writer = new StringWriter(); + writer.append(String.format("Failed to create instance of converter for BibTeX type '%s'. Publication" + + " '%s' was not imported.", + bibTeXEntry.getType().getValue(), + bibTeXEntry.getKey().getValue())); + ex.printStackTrace(new PrintWriter(writer)); + report.addMessage(writer.toString()); + + return report; + } + + final Publication publication = converter.createPublication(pretend); + converter.processTitle(bibTeXEntry, publication, report, pretend); + if (!pretend) { + publication.setLanguage(Kernel.getConfig().getLanguagesIndependentCode()); + } + final PublicationBundle bundle = converter.createBundle(publication, pretend); + report.setType(publication.BASE_DATA_OBJECT_TYPE); + + converter.processFields(bibTeXEntry, publication, importerUtil, report, pretend); + + if (!pretend) { + publication.save(); + + publication.setLanguage(Kernel.getConfig().getLanguagesIndependentCode()); + + publication.save(); + + assignFolder(publication, bundle); + assignCategories(bundle); + + bundle.save(); + publication.save(); + } + + if (publishNewItems) { + importerUtil.publishItem(publication); + } + + report.setSuccessful(true); + return report; } - + + /** + * Overwrite this method to put a publication of specific type into a special folder. + * + * @return + */ + protected Integer getFolderId() { + return Publication.getConfig().getDefaultPublicationsFolder(); + } + + protected void assignFolder(final Publication publication, final PublicationBundle bundle) { + final Folder folder = new Folder(new BigDecimal(getFolderId())); + bundle.setParent(folder); + bundle.setContentSection(folder.getContentSection()); + publication.setContentSection(folder.getContentSection()); + } + + protected void assignCategories(final PublicationBundle bundle) { + final Category defaultCat = RisImporter.getConfig().getDefaultCategory(); + if (defaultCat != null) { + defaultCat.addChild(bundle); + } + } } diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXImporterConfig.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXImporterConfig.java new file mode 100644 index 000000000..1b2164aac --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXImporterConfig.java @@ -0,0 +1,43 @@ +package com.arsdigita.cms.scipublications.importer.bibtex; + +import com.arsdigita.categorization.Category; +import com.arsdigita.runtime.AbstractConfig; +import com.arsdigita.util.parameter.IntegerParameter; +import com.arsdigita.util.parameter.Parameter; +import java.math.BigDecimal; + +/** + * + * @author Jens Pelzetter + * @version $Id$ + */ +public class BibTeXImporterConfig extends AbstractConfig { + + private Parameter defaultCategoryId; + + public BibTeXImporterConfig() { + super(); + + defaultCategoryId = new IntegerParameter("com.arsdigita.cms.scipublications.importer.bibtex.default_category_id", + Parameter.REQUIRED, + 0); + + register(defaultCategoryId); + + loadInfo(); + } + + public Integer getDefaultCategoryId() { + return (Integer) get(defaultCategoryId); + } + + public Category getDefaultCategory() { + final Integer categoryId = getDefaultCategoryId(); + + if (categoryId == 0) { + return null; + } else { + return new Category(new BigDecimal(categoryId)); + } + } +} \ No newline at end of file diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXImporterConfig_parameter.properties.properties b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXImporterConfig_parameter.properties.properties new file mode 100644 index 000000000..e93724a69 --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/bibtex/BibTeXImporterConfig_parameter.properties.properties @@ -0,0 +1,4 @@ +com.arsdigita.cms.scipublications.importer.bibtex.default_category_id.title = Default category for imported publications +com.arsdigita.cms.scipublications.importer.bibtex.default_category_id.purpose = All publications which are created by the BibTeX importer will be put into this category. +com.arsdigita.cms.scipublications.importer.bibtex.default_category_id.example = 12345 +com.arsdigita.cms.scipublications.importer.bibtex.default_category_id.format = [Integer]