Exporter for MultiPartArticle
git-svn-id: https://svn.libreccm.org/ccm/trunk@5796 8810af33-2d31-482b-a856-94f89814c4dfmaster
parent
e26b99ca43
commit
0b986f95a1
|
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
|
@ -46,65 +43,64 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
|
||||||
MetadataProvider adapter = MetadataProviderRegistry
|
MetadataProvider adapter = MetadataProviderRegistry
|
||||||
.findAdapter(dobj.getObjectType());
|
.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
|
// 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");
|
||||||
|
}
|
||||||
|
doc = Document.create(id);
|
||||||
|
doc.setType(dobj.getObjectType().getQualifiedName());
|
||||||
}
|
}
|
||||||
doc = Document.create(id);
|
|
||||||
doc.setType(dobj.getObjectType().getQualifiedName());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (adapter != null) {
|
if (adapter != null) {
|
||||||
if (adapter.isIndexable(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);
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} 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();
|
||||||
|
|
@ -124,21 +120,20 @@ public class DocumentObserver implements com.arsdigita.search.DocumentObserver {
|
||||||
doc.setContent(ladapter.getContent(dobj));
|
doc.setContent(ladapter.getContent(dobj));
|
||||||
}
|
}
|
||||||
if (doc != null) {
|
if (doc != null) {
|
||||||
doc.setDirty(true);
|
doc.setDirty(true);
|
||||||
doc.save();
|
doc.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue