
com.github.davidepastore.liferay.converter.ConvertibleJournalArticle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liferay-journal-article-converter
Show all versions of liferay-journal-article-converter
Liferay Journal Article converter
package com.github.davidepastore.liferay.converter;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import com.github.davidepastore.liferay.annotation.JournalArticleField;
import com.github.davidepastore.liferay.util.SimpleLocaleUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.journal.model.JournalArticle;
/**
* The Convertible Journal Article class that contains the methods to convert a {@link JournalArticle}
* to an {@link Object}. You should use {@link JournalArticleField} annotations to
* be sure that your data is correctly mapped.
* @author Davide Pastore
*
*/
public abstract class ConvertibleJournalArticle {
private static Log log = LogFactoryUtil.getLog(ConvertibleJournalArticle.class);
/**
* Create the object from the given {@link JournalArticle} instance.
* @param journalArticle The {@link JournalArticle} instance.
* @throws Exception
*/
public void fromJournalArticle(JournalArticle journalArticle) throws Exception{
fromJournalArticle(journalArticle, SimpleLocaleUtil.buildLocale(journalArticle.getDefaultLanguageId()));
}
/**
* Create the object from the given {@link JournalArticle} and {@link Locale}.
* @param journalArticle The {@link JournalArticle} instance.
* @param locale The {@link Locale} to use to read from the {@link JournalArticle}.
* @throws Exception
*/
public void fromJournalArticle(JournalArticle journalArticle, Locale locale) throws Exception{
log.debug("Not localized content: " + journalArticle.getContent());
log.debug("Localized content: " + journalArticle.getContentByLocale(locale.getLanguage()));
String content = journalArticle.getContentByLocale(locale.getLanguage());
org.jsoup.nodes.Document document = Jsoup.parse(content, "", Parser.xmlParser());
Elements elements = document.select("root > dynamic-element");
setValueFromElements(elements, this, journalArticle.getTitle(locale));
log.debug("Object: " + this);
}
/**
* Set value from the {@link Elements} instance.
* @param elements The {@link Elements} on which search the value.
* @param object The object on which set the fields.
* @param title The title.
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
*/
protected void setValueFromElements(Elements elements, Object object, String title) throws IllegalArgumentException, IllegalAccessException, InstantiationException{
List names = getDynamicElementNames(elements);
Object value = null;
for (String name : names) {
Elements elementsWithName = elements.select("[name=" + name + "]");
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy