BibTeX Importer: Kleinere Änderungen an Basisstruktur

git-svn-id: https://svn.libreccm.org/ccm/trunk@2041 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2013-01-15 11:35:35 +00:00
parent 3532cf9a9d
commit f293755302
4 changed files with 163 additions and 17 deletions

View File

@ -19,6 +19,11 @@ public interface BibTeXConverter<T extends Publication, B extends PublicationBun
B createBundle(T publication, boolean pretend);
void processTitle(final BibTeXEntry bibTeXEntry,
final T publication,
final PublicationImportReport importReport,
final boolean pretend);
void processFields(final BibTeXEntry bibTeXEntry,
final T publication,
final ImporterUtil importerUtil,

View File

@ -1,7 +1,16 @@
package com.arsdigita.cms.scipublications.importer.bibtex;
import com.arsdigita.categorization.Category;
import com.arsdigita.cms.Folder;
import com.arsdigita.cms.contenttypes.Publication;
import com.arsdigita.cms.contenttypes.PublicationBundle;
import com.arsdigita.cms.scipublications.importer.report.PublicationImportReport;
import com.arsdigita.cms.scipublications.importer.ris.RisImporter;
import com.arsdigita.cms.scipublications.importer.util.ImporterUtil;
import com.arsdigita.kernel.Kernel;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
@ -17,7 +26,7 @@ import org.jbibtex.BibTeXEntry;
public class BibTeXConverters {
private final static Logger LOGGER = Logger.getLogger(BibTeXConverters.class);
private final Map<String, BibTeXConverter<?, ?>> converters = new HashMap<String, BibTeXConverter<?, ?>>();
private final Map<String, BibTeXConverter<Publication, PublicationBundle>> converters = new HashMap<String, BibTeXConverter<Publication, PublicationBundle>>();
@SuppressWarnings("rawtypes")
private BibTeXConverters() {
@ -35,7 +44,6 @@ public class BibTeXConverters {
private static class Instance {
private static BibTeXConverters INSTANCE = new BibTeXConverters();
}
public static BibTeXConverters getInstance() {
@ -48,9 +56,95 @@ public class BibTeXConverters {
final boolean publishNewItems) {
final PublicationImportReport report = new PublicationImportReport();
BibTeXConverter<Publication, PublicationBundle> 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);
}
}
}

View File

@ -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));
}
}
}

View File

@ -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]