diff --git a/src/main/java/com/github/davidepastore/liferay/converter/ConvertibleJournalArticle.java b/src/main/java/com/github/davidepastore/liferay/converter/ConvertibleJournalArticle.java index a240a66..fef2ff2 100644 --- a/src/main/java/com/github/davidepastore/liferay/converter/ConvertibleJournalArticle.java +++ b/src/main/java/com/github/davidepastore/liferay/converter/ConvertibleJournalArticle.java @@ -27,9 +27,9 @@ * */ 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. @@ -38,7 +38,7 @@ public abstract class ConvertibleJournalArticle { 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. @@ -54,15 +54,15 @@ public void fromJournalArticle(JournalArticle journalArticle, Locale locale) thr 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 + * @throws IllegalAccessException + * @throws IllegalArgumentException + * @throws InstantiationException */ protected void setValueFromElements(Elements elements, Object object, String title) throws IllegalArgumentException, IllegalAccessException, InstantiationException{ List names = getDynamicElementNames(elements); @@ -72,8 +72,12 @@ protected void setValueFromElements(Elements elements, Object object, String tit List listValue = new ArrayList(); int counter = 0; int size = elementsWithName.size(); - boolean isList = size > 1; + boolean isList = isListInObject(name, object) || size > 1; for (Element element : elementsWithName) { + String type = element.attr("type"); + if(type.equals("list")){ + isList = false; + } value = getObjectValue(element, object, title); boolean setValue = true; if(isList){ @@ -86,7 +90,7 @@ protected void setValueFromElements(Elements elements, Object object, String tit setValue = false; } } - + if(setValue){ List linkedFields = getLinkedFields(name, object.getClass()); for (Field linkedField : linkedFields) { @@ -94,13 +98,13 @@ protected void setValueFromElements(Elements elements, Object object, String tit linkedField.set(object, value); } } - + List titleFields = getTitleFields(); for (Field titleField : titleFields) { titleField.setAccessible(true); titleField.set(object, title); } - + List baseFields = getBaseFields(object.getClass()); for (Field baseField : baseFields) { baseField.setAccessible(true); @@ -110,7 +114,7 @@ protected void setValueFromElements(Elements elements, Object object, String tit } } } - + /** * Get the object value of the given {@link Element}. * @param element The {@link Element} that contains the XML structure (dynamic-element or dynamic-content). @@ -128,7 +132,7 @@ protected Object getObjectValue(Element element, Object object, String title) th if(element.tagName().equals("dynamic-content")){ type = element.parent().attr("type"); } - + Elements nestedElements = element.children().select("dynamic-element"); if(!nestedElements.isEmpty()){ //Handle nested type @@ -186,11 +190,34 @@ protected Object getObjectValue(Element element, Object object, String title) th value = stringValue; } else if(type.equals("text_area")){ value = stringValue; + } else if(type.equals("ddm-geolocation")) { + value = stringValue; } - + return value; } - + + /** + * Get the linked {@link Field} with this class. + * @param name The name of the field. + * @param clazz The {@link Class} to use. + * @return Returns the linked {@link Field}. + * + * @author Christian Palombella + */ + protected Field getLinkedField(String name, Class clazz) { + for (Field field : clazz.getDeclaredFields()) { + if (field.isAnnotationPresent(JournalArticleField.class)) { + JournalArticleField annotation = field.getAnnotation(JournalArticleField.class); + String fieldName = annotation.name(); + if(name.equals(fieldName)){ + return field; + } + } + } + return null; + } + /** * Get the {@link List} of linked {@link Field} with this class. * @param name The name of the field. @@ -210,7 +237,7 @@ protected List getLinkedFields(String name, Class clazz){ } return fields; } - + /** * Get the {@link List} of {@link Field} that contains the title. * @return Returns the {@link List} of {@link Field} that contains the title. @@ -227,7 +254,7 @@ protected List getTitleFields(){ } return fields; } - + /** * Get the {@link List} of {@link Field} that should be associated with the base value. * @param clazz The {@link Class} to use. @@ -245,8 +272,8 @@ protected List getBaseFields(Class clazz){ } return fields; } - - + + /** * Get a {@link List} of all the dynamic element. * @param elements The {@link Elements}. @@ -257,10 +284,35 @@ protected List getDynamicElementNames(Elements elements){ for (Element element : elements) { String name = element.attr("name"); if(!names.contains(name)){ - names.add(name); + names.add(name); } } return names; } - + + /** + * Check if the name of {@link Element} is a type of {@link List} in the given object + * @param name of the element + * @param object The object on which set the value. + * @return boolean + * + * @author Christian Palombella + */ + protected boolean isListInObject(String name, Object object) { + + Field field = getLinkedField(name, object.getClass()); + + if(field != null) { + field.setAccessible(true); + Class clazzType = field.getType(); + log.debug("clazzType: " + clazzType); + if(clazzType.getName().equals("java.util.List")){ + log.debug("This seems a list!"); + + return true; + } + } + return false; + } + } diff --git a/src/test/java/com/github/davidepastore/liferay/JournalArticleConverterTest.java b/src/test/java/com/github/davidepastore/liferay/JournalArticleConverterTest.java index b5fbaa3..1ee9f34 100644 --- a/src/test/java/com/github/davidepastore/liferay/JournalArticleConverterTest.java +++ b/src/test/java/com/github/davidepastore/liferay/JournalArticleConverterTest.java @@ -30,10 +30,10 @@ * Unit test for {@link ConvertibleJournalArticle}. */ public class JournalArticleConverterTest extends TestCase { - + @SuppressWarnings("unused") private static Log log = LogFactoryUtil.getLog(JournalArticleConverterTest.class); - + /** * Create the test case * @@ -50,10 +50,10 @@ public JournalArticleConverterTest(String testName) { public static Test suite() { return new TestSuite(JournalArticleConverterTest.class); } - + /** * Test with a simple journal article structure. - * @throws Exception + * @throws Exception */ public void testFromJournalArticleSimple() throws Exception { String content = getFileContent("structure0.xml"); @@ -62,51 +62,54 @@ public void testFromJournalArticleSimple() throws Exception { SimpleTestJournalArticle testJournalArticle = new SimpleTestJournalArticle(); testJournalArticle.fromJournalArticle(article); String text = "English"; - + //String value assertEquals(text, testJournalArticle.getText()); - + //Boolean value assertTrue(testJournalArticle.getMyMagicBoolean().booleanValue()); - + //Date value assertEquals(new Date(1438732800000l), testJournalArticle.getDate()); - + //Decimal value assertEquals(new Double(32.0), testJournalArticle.getDecimal()); - + //Image value String image = "/image/journal/article?img_id=22202&t=1439380567193"; assertEquals(image, testJournalArticle.getImage()); - + //Integer value assertEquals(new Integer(13), testJournalArticle.getInteger()); - + //Link to layout value assertEquals("1@public@20281", testJournalArticle.getLinkToPage()); - + //Number value assertEquals(new BigDecimal(444), testJournalArticle.getNumber()); - + //Radio value assertEquals("value 1", testJournalArticle.getRadio()); - + //List value List list = new ArrayList(); list.add("value 2"); list.add("value 3"); assertEquals(list, testJournalArticle.getSelect()); - + //Text Box value assertEquals("Text box", testJournalArticle.getTextBox()); - + //Text Area value assertEquals("Text area", testJournalArticle.getTextArea()); + + // Geolocation + assertEquals("{\"latitude\":43.89193,\"longitude\":12.51133}", testJournalArticle.getGeolocation()); } /** * Test fromJournalArticle method for catalan. - * @throws Exception + * @throws Exception */ public void testFromJournalArticleForCatalan() throws Exception { String content = getFileContent("structure1.xml"); @@ -120,17 +123,17 @@ public void testFromJournalArticleForCatalan() throws Exception { text.add("Catalan"); text.add("null"); text.add("null"); - + //List of String assertEquals(text, testJournalArticle.getText()); - + //Boolean value assertFalse(testJournalArticle.getMyMagicBoolean().booleanValue()); } - + /** * Test fromJournalArticle method for default language. - * @throws Exception + * @throws Exception */ public void testFromJournalArticleForDefaultLanguage() throws Exception { String content = getFileContent("structure1.xml"); @@ -142,55 +145,55 @@ public void testFromJournalArticleForDefaultLanguage() throws Exception { text.add("English"); text.add("Second text"); text.add("Third text"); - + //List of String assertEquals(text, testJournalArticle.getText()); - + //Boolean value assertTrue(testJournalArticle.getMyMagicBoolean().booleanValue()); - + //Date value assertEquals(new Date(1438732800000l), testJournalArticle.getDate()); - + //Decimal value assertEquals(new Double(32.0), testJournalArticle.getDecimal()); - + //Image value String image = "/image/journal/article?img_id=22202&t=1439380567193"; assertEquals(image, testJournalArticle.getImage()); - + //Documents and media value String documentsAndMedia = "/documents/20281/0/welcome_tools/ab4b08a2-a1ba-4b12-acd7-21b62db8887a?t=1439123008000"; assertEquals(documentsAndMedia, testJournalArticle.getDocumentsAndMedia()); - + //Integer value assertEquals(new Integer(13), testJournalArticle.getInteger()); - + //Link to layout value assertEquals("1@public@20281", testJournalArticle.getLinkToPage()); - + //Number value assertEquals(new BigDecimal(444), testJournalArticle.getNumber()); - + //Radio value assertEquals("value 1", testJournalArticle.getRadio()); - + //List value List list = new ArrayList(); list.add("value 2"); list.add("value 3"); assertEquals(list, testJournalArticle.getSelect()); - + //Text Box value assertEquals("Text box", testJournalArticle.getTextBox()); - + //Text Area value assertEquals("Text area", testJournalArticle.getTextArea()); } - + /** * Test fromJournalArticle method for nested elements. - * @throws Exception + * @throws Exception */ public void testFromJournalArticleForNestedElements() throws Exception { String content = getFileContent("structure2.xml"); @@ -203,10 +206,10 @@ public void testFromJournalArticleForNestedElements() throws Exception { assertTrue(nestedJournalArticle.getMyBoolean().booleanValue()); assertEquals("Nested Text :)", nestedJournalArticle.getMyText()); } - + /** * Test fromJournalArticle method for multi level nested elements. - * @throws Exception + * @throws Exception */ public void testFromJournalArticleForMultiLevelNestedElements() throws Exception { String content = getFileContent("structure4.xml"); @@ -214,7 +217,7 @@ public void testFromJournalArticleForMultiLevelNestedElements() throws Exception article.setContent(content); MultiNestedTestFirstLevelJournalArticle nestedTestJournalArticle = new MultiNestedTestFirstLevelJournalArticle(); nestedTestJournalArticle.fromJournalArticle(article); - + //Mock data MultiNestedTestFirstLevelJournalArticle firstLevelJournalArticle = new MultiNestedTestFirstLevelJournalArticle(); MultiNestedTestSecondLevelJournalArticle secondLevelJournalArticle = new MultiNestedTestSecondLevelJournalArticle(); @@ -224,14 +227,14 @@ public void testFromJournalArticleForMultiLevelNestedElements() throws Exception secondLevelJournalArticle.setNestedLevelDecimal(thirdLevelJournalArticle); secondLevelJournalArticle.setMyBoolean(true); firstLevelJournalArticle.setBaseLevelBoolean(secondLevelJournalArticle); - + //Multi nested value assertEquals(nestedTestJournalArticle, firstLevelJournalArticle); } - + /** * Test fromJournalArticle method for list of nested elements. - * @throws Exception + * @throws Exception */ public void testFromJournalArticleForListOfNestedElements() throws Exception { String content = getFileContent("structure3.xml"); @@ -240,32 +243,32 @@ public void testFromJournalArticleForListOfNestedElements() throws Exception { NestedListTestJournalArticle nestedListTestJournalArticle = new NestedListTestJournalArticle(); nestedListTestJournalArticle.fromJournalArticle(article); List documentsAndMediaList = new ArrayList(); - + //Add first DocumentsAndMediaJournalArticle documentsAndMedia = new DocumentsAndMediaJournalArticle(); documentsAndMedia.setMyBoolean(true); documentsAndMedia.setMyText("Text 1"); documentsAndMedia.setDocumentsAndMedia("/documents/20281/0/welcome_community/d83a745d-8624-44c0-baad-3c7d05e3b2dc?t=1439123090000"); documentsAndMediaList.add(documentsAndMedia); - + //Add second documentsAndMedia = new DocumentsAndMediaJournalArticle(); documentsAndMedia.setMyBoolean(false); documentsAndMedia.setMyText("Text 2"); documentsAndMedia.setDocumentsAndMedia("/documents/20281/0/welcome_community/d83a745d-8624-44c0-baad-3c7d05e3b2dc?t=1439123092344"); documentsAndMediaList.add(documentsAndMedia); - + List myDocumentAndMediaList = nestedListTestJournalArticle.getDocumentsAndMedia(); - + //Nested value assertEquals(documentsAndMediaList, myDocumentAndMediaList); } - + /** * Read the file content. * @param name The file name. * @return Returns the {@link File} instance. - * @throws IOException + * @throws IOException */ private String getFileContent(String name) throws IOException{ ClassLoader classLoader = getClass().getClassLoader(); diff --git a/src/test/java/com/github/davidepastore/liferay/model/SimpleTestJournalArticle.java b/src/test/java/com/github/davidepastore/liferay/model/SimpleTestJournalArticle.java index 4ae3bd2..653a316 100644 --- a/src/test/java/com/github/davidepastore/liferay/model/SimpleTestJournalArticle.java +++ b/src/test/java/com/github/davidepastore/liferay/model/SimpleTestJournalArticle.java @@ -47,10 +47,13 @@ public class SimpleTestJournalArticle extends ConvertibleJournalArticle { @JournalArticleField(name = "textBox") private String textBox; - + @JournalArticleField(name = "textArea") private String textArea; + @JournalArticleField(name = "geolocalizzazione") + private String geolocation; + public String getTitle() { return title; } @@ -163,4 +166,12 @@ public void setTextArea(String textArea) { this.textArea = textArea; } + public String getGeolocation() { + return geolocation; + } + + public void setGeolocation(String geolocation) { + this.geolocation = geolocation; + } + } diff --git a/src/test/resources/structure0.xml b/src/test/resources/structure0.xml index ffe4a3f..4ad0fd4 100644 --- a/src/test/resources/structure0.xml +++ b/src/test/resources/structure0.xml @@ -45,4 +45,7 @@ - \ No newline at end of file + + + +