From 0b986f95a17b8029f3f2e9e1744d1de82132426e Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 10 Jan 2019 18:32:12 +0000 Subject: [PATCH] Exporter for MultiPartArticle git-svn-id: https://svn.libreccm.org/ccm/trunk@5796 8810af33-2d31-482b-a856-94f89814c4df --- .../MultiPartArticleInitializer.java | 4 + .../librecms/contenttypes/MpaExporter.java | 190 ++++++++++++++++++ .../search/lucene/DocumentObserver.java | 118 ++++++----- 3 files changed, 251 insertions(+), 61 deletions(-) create mode 100644 ccm-cms-types-mparticle/src/org/librecms/contenttypes/MpaExporter.java diff --git a/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticleInitializer.java b/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticleInitializer.java index ab1976cf7..8e2343bcb 100755 --- a/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticleInitializer.java +++ b/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticleInitializer.java @@ -25,6 +25,8 @@ import com.arsdigita.runtime.DomainInitEvent; import com.arsdigita.search.MetadataProviderRegistry; import org.apache.log4j.Logger; +import org.libreccm.export.ExportManager; +import org.librecms.contenttypes.MpaExporter; /** * Executes at each system startup and initializes the MultiPartArticle @@ -50,6 +52,8 @@ public class MultiPartArticleInitializer extends ContentTypeInitializer { */ public MultiPartArticleInitializer() { super("ccm-cms-types-mparticle.pdl.mf", MultiPartArticle.BASE_DATA_OBJECT_TYPE); + + ExportManager.getInstance().registerExporter(new MpaExporter()); } diff --git a/ccm-cms-types-mparticle/src/org/librecms/contenttypes/MpaExporter.java b/ccm-cms-types-mparticle/src/org/librecms/contenttypes/MpaExporter.java new file mode 100644 index 000000000..f7aee4076 --- /dev/null +++ b/ccm-cms-types-mparticle/src/org/librecms/contenttypes/MpaExporter.java @@ -0,0 +1,190 @@ +package org.librecms.contenttypes; + +import com.arsdigita.cms.ContentBundle; +import com.arsdigita.cms.ItemCollection; +import com.arsdigita.cms.contenttypes.ArticleSection; +import com.arsdigita.cms.contenttypes.ArticleSectionCollection; +import com.arsdigita.cms.contenttypes.MultiPartArticle; + +import com.fasterxml.jackson.core.JsonGenerator; +import org.librecms.contentsection.AbstractContentItemsExporter; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + * + * @author Jens Pelzetter + */ +public class MpaExporter extends AbstractContentItemsExporter { + + @Override + public void exportContentItemProperties( + final MultiPartArticle article, final JsonGenerator jsonGenerator) + throws IOException { + + final ContentBundle bundle = article.getContentBundle(); + final ItemCollection instances = bundle.getInstances(); + + final Map mpaSections = new HashMap<>(); + while(instances.next()) { + final ArticleSectionCollection sections = article.getSections(); + while(sections.next()) { + + final ArticleSection section = sections.getArticleSection(); + + final MpaSection mpaSection; + if (mpaSections.containsKey(section.getRank())) { + mpaSection = mpaSections.get(section.getRank()); + } else { + mpaSection = new MpaSection(); + mpaSection.setRank(section.getRank()); + mpaSection.setPageBreak(section.isPageBreak()); + mpaSections.put(section.getRank(), mpaSection); + } + + final String language = section.getLanguage(); + final Locale locale = new Locale(language); + mpaSection.addTitle(locale, section.getTitle()); + mpaSection.addText(locale, section.getText().getText()); + } + } + instances.rewind(); + + final List sectionList = new ArrayList<>( + mpaSections.values()); + sectionList + .sort((section1, section2) -> Integer.compare(section1.getRank(), + section2.getRank())); + + jsonGenerator.writeArrayFieldStart("sections"); + for(final MpaSection mpaSection: sectionList) { + jsonGenerator.writeStartObject(); + exportLocalizedField(jsonGenerator, "title", mpaSection.getTitle()); + jsonGenerator.writeNumberField("rank", mpaSection.getRank()); + jsonGenerator.writeBooleanField("pageBreak", + mpaSection.isPageBreak()); + exportLocalizedField(jsonGenerator, "text", mpaSection.getText()); + jsonGenerator.writeEndObject(); + } + jsonGenerator.writeEndArray(); + } + + @Override + protected Map> collectLocalizedValues( + final ItemCollection instances) { + + final Map leadPropertyValues = new HashMap<>(); +// final Map sections = new HashMap<>(); + + while(instances.next()) { + + final MultiPartArticle article = (MultiPartArticle) instances + .getContentItem(); + final String lang = article.getLanguage(); + final Locale locale = new Locale(lang); + final String lead = article.getSummary(); + + leadPropertyValues.put(locale, lead); + } + + instances.rewind(); + + final Map> properties = new HashMap<>(); + properties.put("lead", leadPropertyValues); + + return properties; + } + + @Override + public Class exportsType() { + + return MultiPartArticle.class; + } + + @Override + public String exportsBaseDataObjectType() { + return MultiPartArticle.BASE_DATA_OBJECT_TYPE; + } + + @Override + public String convertsToType() { + + return "org.librecms.contenttypes.MultiPartArticle"; + } + + private void exportLocalizedField(final JsonGenerator jsonGenerator, + final String name, + final Map values) + throws IOException { + + jsonGenerator.writeObjectFieldStart(name); + + for(final Map.Entry entry : values.entrySet()) { + jsonGenerator.writeStringField(entry.getKey().toString(), + entry.getValue()); + } + + jsonGenerator.writeEndObject(); + } + + private class MpaSection { + + private Map title; + private int rank; + private boolean pageBreak; + private Map text; + + public MpaSection() { + title = new HashMap<>(); + text = new HashMap<>(); + } + + public Map getTitle() { + return Collections.unmodifiableMap(title); + } + + public void addTitle(final Locale locale, final String value) { + title.put(locale, value); + } + + public void setTitle(final Map title) { + this.title = new HashMap<>(title); + } + + public int getRank() { + return rank; + } + + public void setRank(final int rank) { + this.rank = rank; + } + + public boolean isPageBreak() { + return pageBreak; + } + + public void setPageBreak(final boolean pageBreak) { + this.pageBreak = pageBreak; + } + + public Map getText() { + return Collections.unmodifiableMap(text); + } + + public void addText(final Locale locale, final String value) { + text.put(locale, value); + } + + public void setText(final Map text) { + this.text = new HashMap<>(text); + } + + } + +} diff --git a/ccm-core/src/com/arsdigita/search/lucene/DocumentObserver.java b/ccm-core/src/com/arsdigita/search/lucene/DocumentObserver.java index be153aff9..242111b96 100755 --- a/ccm-core/src/com/arsdigita/search/lucene/DocumentObserver.java +++ b/ccm-core/src/com/arsdigita/search/lucene/DocumentObserver.java @@ -30,15 +30,12 @@ import com.arsdigita.search.MetadataProvider; import com.arsdigita.search.MetadataProviderRegistry; import com.arsdigita.util.Assert; - public class DocumentObserver implements com.arsdigita.search.DocumentObserver { - private static final Logger s_log = - Logger.getLogger(DocumentObserver.class); - + private static final Logger s_log = Logger.getLogger(DocumentObserver.class); + /** - * Invoked after a searchable object has been - * created or updated. + * Invoked after a searchable object has been created or updated. * * @param dobj the updated object */ @@ -46,66 +43,65 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver { MetadataProvider adapter = MetadataProviderRegistry .findAdapter(dobj.getObjectType()); - s_log.debug("using adapter " + adapter); + s_log.debug("using adapter " + adapter); - // retrieve document & if it doesn't exist, create one if required by this DomainObject - - BigDecimal id = (BigDecimal)dobj.getOID().get("id"); + // retrieve document & if it doesn't exist, create one if required by this DomainObject + BigDecimal id = (BigDecimal) dobj.getOID().get("id"); Assert.exists(id, BigDecimal.class); Document doc = Document.retrieve(id); if (doc == null) { - if ((adapter == null) || (adapter!= null && adapter.isIndexable(dobj))) { - - if (s_log.isDebugEnabled()) { - s_log.debug("Creating new document"); + if ((adapter == null) || (adapter != null && adapter.isIndexable( + dobj))) { + + if (s_log.isDebugEnabled()) { + s_log.debug("Creating new document"); + } + doc = Document.create(id); + doc.setType(dobj.getObjectType().getQualifiedName()); } - doc = Document.create(id); - doc.setType(dobj.getObjectType().getQualifiedName()); } - } if (adapter != null) { - if (adapter.isIndexable(dobj)) { - - if (s_log.isDebugEnabled()) { - s_log.debug("Processing object " + dobj.getOID() + - " using new adapters"); - s_log.debug( "Locale: " + adapter.getLocale( dobj ) ); - } + if (adapter.isIndexable(dobj)) { + if (s_log.isDebugEnabled()) { + s_log.debug("Processing object " + dobj.getOID() + + " using new adapters"); + s_log.debug("Locale: " + adapter.getLocale(dobj)); + } + + doc.setTypeSpecificInfo(adapter.getTypeSpecificInfo(dobj)); + doc.setLocale(adapter.getLocale(dobj)); + doc.setTitle(adapter.getTitle(dobj)); + doc.setSummary(adapter.getSummary(dobj)); + doc.setCreationDate(adapter.getCreationDate(dobj)); + Party party = adapter.getCreationParty(dobj); + doc.setCreationParty(party == null ? null : party.getID()); + doc.setLastModifiedDate(adapter.getLastModifiedDate(dobj)); + party = adapter.getLastModifiedParty(dobj); + doc.setLastModifiedParty(party == null ? null : party.getID()); + doc.setContentSection(adapter.getContentSection(dobj)); + ContentProvider[] content = adapter.getContent(dobj, + ContentType.TEXT); + StringBuffer buf = new StringBuffer(""); + for (int i = 0; i < content.length; i++) { + Assert.isTrue(content[i].getType().equals(ContentType.TEXT), + "content is text"); + buf.append(new String(content[i].getBytes())); + } + doc.setContent(buf.toString().replace('\0', ' ')); + } else { + // document already exists, but now shouldn't be indexed + if (doc != null) { + + doc.setDeleted(true); + } - doc.setTypeSpecificInfo(adapter.getTypeSpecificInfo(dobj)); - doc.setLocale(adapter.getLocale(dobj)); - doc.setTitle(adapter.getTitle(dobj)); - doc.setSummary(adapter.getSummary(dobj)); - doc.setCreationDate(adapter.getCreationDate(dobj)); - Party party = adapter.getCreationParty(dobj); - doc.setCreationParty(party == null ? null : party.getID()); - doc.setLastModifiedDate(adapter.getLastModifiedDate(dobj)); - party = adapter.getLastModifiedParty(dobj); - doc.setLastModifiedParty(party == null ? null : party.getID()); - doc.setContentSection(adapter.getContentSection(dobj)); - ContentProvider[] content = adapter.getContent(dobj, - ContentType.TEXT); - StringBuffer buf = new StringBuffer(""); - for (int i = 0 ; i < content.length ; i++) { - Assert.isTrue(content[i].getType().equals(ContentType.TEXT), - "content is text"); - buf.append(new String(content[i].getBytes())); } - doc.setContent(buf.toString().replace('\0', ' ')); - } else { - // document already exists, but now shouldn't be indexed - if (doc != null) { - - doc.setDeleted(true); - } - - } } else { if (s_log.isDebugEnabled()) { - s_log.debug("Processing object " + dobj.getOID() + - " using old adapters"); - } + s_log.debug("Processing object " + dobj.getOID() + + " using old adapters"); + } Registry reg = Registry.getInstance(); Adapter ladapter = reg.getAdapter(dobj.getObjectType()); @@ -124,21 +120,20 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver { doc.setContent(ladapter.getContent(dobj)); } if (doc != null) { - doc.setDirty(true); - doc.save(); - } + doc.setDirty(true); + doc.save(); + } } /** - * Invoked after a searchable object has been - * deleted. NB, the only guarenteed valid method - * that can be invoked on the DomainObject is + * Invoked after a searchable object has been deleted. NB, the only + * guarenteed valid method that can be invoked on the DomainObject is * getOID(). * * @param dobj the deleted object */ public void onDelete(DomainObject dobj) { - BigDecimal id = (BigDecimal)dobj.getOID().get("id"); + BigDecimal id = (BigDecimal) dobj.getOID().get("id"); Assert.exists(id, BigDecimal.class); Document doc = Document.retrieve(id); if (doc != null) { @@ -147,4 +142,5 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver { doc.save(); } } + }