Skip to content

Commit

Permalink
Merge adc89ea into 8328ea2
Browse files Browse the repository at this point in the history
  • Loading branch information
chripalombella committed Jul 9, 2019
2 parents 8328ea2 + adc89ea commit f0365b8
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 46 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ XML structure:
<dynamic-element name="nestedText" index="0" type="text" index-type="keyword">
<dynamic-content language-id="en_US"><![CDATA[Nested Text :)]]></dynamic-content>
</dynamic-element>
<dynamic-content language-id="en_US"><![CDATA[/documents/20281/0/welcome_community/d83a745d-8624-44c0-baad-3c7d05e3b2dc?t=1439123090000]]></dynamic-content>
<dynamic-content language-id="en_US"><![CDATA[{"classPK":"46245","groupId":"20127","title":"Footer.png","type":"document","uuid":"223fac3c-abe2-e24a-1097-31cd1ce030156"}]]></dynamic-content>
</dynamic-element>
</root>
```
Expand Down Expand Up @@ -233,7 +233,7 @@ public class NestedBar extends ConvertibleJournalArticle {
private String myText;

@JournalArticleField(base = true)
private String documentsAndMedia;
private DDMDocumentAndMedia documentsAndMedia;

public Boolean getMyBoolean() {
return myBoolean;
Expand All @@ -251,17 +251,17 @@ public class NestedBar extends ConvertibleJournalArticle {
this.myText = myText;
}

public String getDocumentsAndMedia() {
public DDMDocumentAndMedia getDocumentsAndMedia() {
return documentsAndMedia;
}

public void setDocumentsAndMedia(String documentsAndMedia) {
public void setDocumentsAndMedia(DDMDocumentAndMedia documentsAndMedia) {
this.documentsAndMedia = documentsAndMedia;
}

@Override
public String toString() {
return "[myBoolean = " + myBoolean + ", myText = " + myText + ", documentsAndMedia = " + documentsAndMedia + "]";
return "[myBoolean = " + myBoolean + ", myText = " + myText + "]";
}
}
```
Expand All @@ -274,7 +274,7 @@ JournalArticle journalArticle = JournalArticleLocalServiceUtil.getArticle(id);
NestedFoo nestedFoo = new NestedFoo();
nestedFoo.fromJournalArticle(journalArticle);
NestedBar nestedBar = nestedFoo.getNestedBar();
System.out.println(nestedBar); //[myBoolean = true, myText = Nested Text :), documentsAndMedia = /documents/20281/0/welcome_community/d83a745d-8624-44c0-baad-3c7d05e3b2dc?t=1439123090000]
System.out.println(nestedBar); //[myBoolean = true, myText = Nested Text :)]
```


Expand Down
10 changes: 7 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,25 @@
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.journal.api</artifactId>
<version>2.12.4</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>com.liferay.portal.kernel</artifactId>
<version>2.61.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.dynamic.data.mapping.api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.davidepastore.liferay.converter;

import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
Expand All @@ -14,7 +15,13 @@
import org.jsoup.select.Elements;

import com.github.davidepastore.liferay.annotation.JournalArticleField;
import com.github.davidepastore.liferay.model.DDMDocumentAndMedia;
import com.github.davidepastore.liferay.model.DDMGeolocation;
import com.github.davidepastore.liferay.model.DDMImage;
import com.github.davidepastore.liferay.util.JsonUtil;
import com.github.davidepastore.liferay.util.SimpleLocaleUtil;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.journal.model.JournalArticle;
Expand Down Expand Up @@ -63,8 +70,12 @@ public void fromJournalArticle(JournalArticle journalArticle, Locale locale) thr
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
* @throws FileNotFoundException
* @throws IndexOutOfBoundsException
* @throws JsonIOException
* @throws JsonSyntaxException
*/
protected void setValueFromElements(Elements elements, Object object, String title) throws IllegalArgumentException, IllegalAccessException, InstantiationException{
protected void setValueFromElements(Elements elements, Object object, String title) throws IllegalArgumentException, IllegalAccessException, InstantiationException, JsonSyntaxException, JsonIOException, IndexOutOfBoundsException, FileNotFoundException{
List<String> names = getDynamicElementNames(elements);
Object value = null;
for (String name : names) {
Expand Down Expand Up @@ -123,8 +134,11 @@ protected void setValueFromElements(Elements elements, Object object, String tit
* @return Returns the object with the found value (it also handles nested properties).
* @throws InstantiationException
* @throws IllegalAccessException
* @throws FileNotFoundException
* @throws JsonIOException
* @throws JsonSyntaxException
*/
protected Object getObjectValue(Element element, Object object, String title) throws InstantiationException, IllegalAccessException, NumberFormatException, IndexOutOfBoundsException {
protected Object getObjectValue(Element element, Object object, String title) throws InstantiationException, IllegalAccessException, NumberFormatException, IndexOutOfBoundsException, JsonSyntaxException, JsonIOException, FileNotFoundException {
Object value = null;
String type = element.attr("type");
String stringValue = element.text();
Expand Down Expand Up @@ -162,9 +176,11 @@ protected Object getObjectValue(Element element, Object object, String title) th
} else if(type.equals("ddm-decimal")){
value = new Double(stringValue);
} else if(type.equals("image")){
value = stringValue;
DDMImage ddmImage = JsonUtil.getDDMImage(stringValue);
value = ddmImage;
} else if(type.equals("document_library")){
value = stringValue;
DDMDocumentAndMedia ddmDocumentAndMedia = JsonUtil.getDDMDocumentAndMedia(stringValue);
value = ddmDocumentAndMedia;
} else if(type.equals("ddm-integer")){
value = Integer.parseInt(stringValue);
} else if(type.equals("link_to_layout")){
Expand All @@ -191,7 +207,8 @@ protected Object getObjectValue(Element element, Object object, String title) th
} else if(type.equals("text_area")){
value = stringValue;
} else if(type.equals("ddm-geolocation")) {
value = stringValue;
DDMGeolocation ddmGeolocation = JsonUtil.getDDMGeolocation(stringValue);
value = ddmGeolocation;
}

return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.github.davidepastore.liferay.model;

/**
* @author Christian Palombella
*
*/
public class DDMDocumentAndMedia {

private String classPK;
private String groupId;
private String title;
private String type;
private String uuid;

public DDMDocumentAndMedia() {
super();
}

public String getClassPK() {
return classPK;
}

public void setClassPK(String classPK) {
this.classPK = classPK;
}

public String getGroupId() {
return groupId;
}

public void setGroupId(String groupId) {
this.groupId = groupId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

/**
* Check if the object are equals.
*/
@Override
public boolean equals(Object obj) {
return obj instanceof DDMDocumentAndMedia
&& groupId.equals(((DDMDocumentAndMedia) obj)
.getGroupId())
&& classPK.equals(((DDMDocumentAndMedia) obj)
.getClassPK())
&& uuid.equals(((DDMDocumentAndMedia) obj)
.getUuid())
&& type.equals(((DDMDocumentAndMedia) obj)
.getType())
&& title.equals(((DDMDocumentAndMedia) obj)
.getTitle());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.davidepastore.liferay.model;

/**
*
* @author Christian Palombella
*
*/
public class DDMGeolocation {

private Double latitude;
private Double longitude;

public DDMGeolocation() {
super();
}

public Double getLatitude() {
return latitude;
}

public Double getLongitude() {
return longitude;
}

}
59 changes: 59 additions & 0 deletions src/main/java/com/github/davidepastore/liferay/model/DDMImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.davidepastore.liferay.model;

/**
* @author Christian Palombella
*
*/
public class DDMImage {

private String classPK;
private String groupId;
private String name;
private String alt;
private String title;
private String type;
private String uuid;
private String fileEntryId;
private String resourcePrimKey;

public DDMImage() {
super();
}

public String getClassPK() {
return classPK;
}

public String getGroupId() {
return groupId;
}

public String getTitle() {
return title;
}

public String getType() {
return type;
}

public String getUuid() {
return uuid;
}

public String getName() {
return name;
}

public String getAlt() {
return alt;
}

public String getFileEntryId() {
return fileEntryId;
}

public String getResourcePrimKey() {
return resourcePrimKey;
}

}
66 changes: 66 additions & 0 deletions src/main/java/com/github/davidepastore/liferay/util/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.davidepastore.liferay.util;

import com.github.davidepastore.liferay.model.DDMDocumentAndMedia;
import com.github.davidepastore.liferay.model.DDMGeolocation;
import com.github.davidepastore.liferay.model.DDMImage;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

/**
* @author Christian Palombella
*
*/
public class JsonUtil {

/**
*
* @param ddmGeolocationString
* @return
* @throws JsonSyntaxException
* @throws JsonIOException
* @throws FileNotFoundException
*/
public static DDMGeolocation getDDMGeolocation(String ddmGeolocationString) throws JsonSyntaxException, JsonIOException, JsonParseException {

Gson gson = new GsonBuilder().create();
DDMGeolocation ddmGeolocation = gson.fromJson(ddmGeolocationString, new TypeToken<DDMGeolocation>(){}.getType());

return ddmGeolocation;
}

/**
*
* @param ddmImageString
* @return
* @throws JsonSyntaxException
* @throws JsonIOException
* @throws FileNotFoundException
*/
public static DDMImage getDDMImage(String ddmImageString) throws JsonSyntaxException, JsonIOException, JsonParseException {

Gson gson = new GsonBuilder().create();
DDMImage ddmImage = gson.fromJson(ddmImageString, new TypeToken<DDMImage>(){}.getType());

return ddmImage;
}

/**
*
* @param ddmDocumentAndMediaString
* @return
* @throws JsonSyntaxException
* @throws JsonIOException
* @throws FileNotFoundException
*/
public static DDMDocumentAndMedia getDDMDocumentAndMedia(String ddmDocumentAndMediaString) throws JsonSyntaxException, JsonIOException, JsonParseException {

Gson gson = new GsonBuilder().create();
DDMDocumentAndMedia ddmDocumentAndMedia = gson.fromJson(ddmDocumentAndMediaString, new TypeToken<DDMDocumentAndMedia>(){}.getType());

return ddmDocumentAndMedia;
}
}

0 comments on commit f0365b8

Please sign in to comment.