Exporter for MultiPartArticle

git-svn-id: https://svn.libreccm.org/ccm/trunk@5796 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2019-01-10 18:32:12 +00:00
parent e26b99ca43
commit 0b986f95a1
3 changed files with 251 additions and 61 deletions

View File

@ -25,6 +25,8 @@ import com.arsdigita.runtime.DomainInitEvent;
import com.arsdigita.search.MetadataProviderRegistry; import com.arsdigita.search.MetadataProviderRegistry;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.libreccm.export.ExportManager;
import org.librecms.contenttypes.MpaExporter;
/** /**
* Executes at each system startup and initializes the MultiPartArticle * Executes at each system startup and initializes the MultiPartArticle
@ -50,6 +52,8 @@ public class MultiPartArticleInitializer extends ContentTypeInitializer {
*/ */
public MultiPartArticleInitializer() { public MultiPartArticleInitializer() {
super("ccm-cms-types-mparticle.pdl.mf", MultiPartArticle.BASE_DATA_OBJECT_TYPE); super("ccm-cms-types-mparticle.pdl.mf", MultiPartArticle.BASE_DATA_OBJECT_TYPE);
ExportManager.getInstance().registerExporter(new MpaExporter());
} }

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class MpaExporter extends AbstractContentItemsExporter<MultiPartArticle> {
@Override
public void exportContentItemProperties(
final MultiPartArticle article, final JsonGenerator jsonGenerator)
throws IOException {
final ContentBundle bundle = article.getContentBundle();
final ItemCollection instances = bundle.getInstances();
final Map<Integer, MpaSection> 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<MpaSection> 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<String, Map<Locale, String>> collectLocalizedValues(
final ItemCollection instances) {
final Map<Locale, String> leadPropertyValues = new HashMap<>();
// final Map<Locale, MpaSection> 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<String, Map<Locale, String>> properties = new HashMap<>();
properties.put("lead", leadPropertyValues);
return properties;
}
@Override
public Class<MultiPartArticle> 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<Locale, String> values)
throws IOException {
jsonGenerator.writeObjectFieldStart(name);
for(final Map.Entry<Locale, String> entry : values.entrySet()) {
jsonGenerator.writeStringField(entry.getKey().toString(),
entry.getValue());
}
jsonGenerator.writeEndObject();
}
private class MpaSection {
private Map<Locale, String> title;
private int rank;
private boolean pageBreak;
private Map<Locale, String> text;
public MpaSection() {
title = new HashMap<>();
text = new HashMap<>();
}
public Map<Locale, String> getTitle() {
return Collections.unmodifiableMap(title);
}
public void addTitle(final Locale locale, final String value) {
title.put(locale, value);
}
public void setTitle(final Map<Locale, String> 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<Locale, String> getText() {
return Collections.unmodifiableMap(text);
}
public void addText(final Locale locale, final String value) {
text.put(locale, value);
}
public void setText(final Map<Locale, String> text) {
this.text = new HashMap<>(text);
}
}
}

View File

@ -30,15 +30,12 @@ import com.arsdigita.search.MetadataProvider;
import com.arsdigita.search.MetadataProviderRegistry; import com.arsdigita.search.MetadataProviderRegistry;
import com.arsdigita.util.Assert; import com.arsdigita.util.Assert;
public class DocumentObserver implements com.arsdigita.search.DocumentObserver { public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
private static final Logger s_log = private static final Logger s_log = Logger.getLogger(DocumentObserver.class);
Logger.getLogger(DocumentObserver.class);
/** /**
* Invoked after a searchable object has been * Invoked after a searchable object has been created or updated.
* created or updated.
* *
* @param dobj the updated object * @param dobj the updated object
*/ */
@ -49,12 +46,12 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
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 // retrieve document & if it doesn't exist, create one if required by this DomainObject
BigDecimal id = (BigDecimal) dobj.getOID().get("id");
BigDecimal id = (BigDecimal)dobj.getOID().get("id");
Assert.exists(id, BigDecimal.class); Assert.exists(id, BigDecimal.class);
Document doc = Document.retrieve(id); Document doc = Document.retrieve(id);
if (doc == null) { if (doc == null) {
if ((adapter == null) || (adapter!= null && adapter.isIndexable(dobj))) { if ((adapter == null) || (adapter != null && adapter.isIndexable(
dobj))) {
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("Creating new document"); s_log.debug("Creating new document");
@ -67,12 +64,11 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
if (adapter.isIndexable(dobj)) { if (adapter.isIndexable(dobj)) {
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("Processing object " + dobj.getOID() + s_log.debug("Processing object " + dobj.getOID()
" using new adapters"); + " using new adapters");
s_log.debug( "Locale: " + adapter.getLocale( dobj ) ); s_log.debug("Locale: " + adapter.getLocale(dobj));
} }
doc.setTypeSpecificInfo(adapter.getTypeSpecificInfo(dobj)); doc.setTypeSpecificInfo(adapter.getTypeSpecificInfo(dobj));
doc.setLocale(adapter.getLocale(dobj)); doc.setLocale(adapter.getLocale(dobj));
doc.setTitle(adapter.getTitle(dobj)); doc.setTitle(adapter.getTitle(dobj));
@ -87,7 +83,7 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
ContentProvider[] content = adapter.getContent(dobj, ContentProvider[] content = adapter.getContent(dobj,
ContentType.TEXT); ContentType.TEXT);
StringBuffer buf = new StringBuffer(""); StringBuffer buf = new StringBuffer("");
for (int i = 0 ; i < content.length ; i++) { for (int i = 0; i < content.length; i++) {
Assert.isTrue(content[i].getType().equals(ContentType.TEXT), Assert.isTrue(content[i].getType().equals(ContentType.TEXT),
"content is text"); "content is text");
buf.append(new String(content[i].getBytes())); buf.append(new String(content[i].getBytes()));
@ -103,8 +99,8 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
} }
} else { } else {
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("Processing object " + dobj.getOID() + s_log.debug("Processing object " + dobj.getOID()
" using old adapters"); + " using old adapters");
} }
Registry reg = Registry.getInstance(); Registry reg = Registry.getInstance();
@ -130,15 +126,14 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
} }
/** /**
* Invoked after a searchable object has been * Invoked after a searchable object has been deleted. NB, the only
* deleted. NB, the only guarenteed valid method * guarenteed valid method that can be invoked on the DomainObject is
* that can be invoked on the DomainObject is
* getOID(). * getOID().
* *
* @param dobj the deleted object * @param dobj the deleted object
*/ */
public void onDelete(DomainObject dobj) { public void onDelete(DomainObject dobj) {
BigDecimal id = (BigDecimal)dobj.getOID().get("id"); BigDecimal id = (BigDecimal) dobj.getOID().get("id");
Assert.exists(id, BigDecimal.class); Assert.exists(id, BigDecimal.class);
Document doc = Document.retrieve(id); Document doc = Document.retrieve(id);
if (doc != null) { if (doc != null) {
@ -147,4 +142,5 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
doc.save(); doc.save();
} }
} }
} }