Skip to content

Commit

Permalink
Merge 8627bce into a470364
Browse files Browse the repository at this point in the history
  • Loading branch information
chripalombella committed May 27, 2019
2 parents a470364 + 8627bce commit 2ba505c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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<String> names = getDynamicElementNames(elements);
Expand All @@ -72,8 +72,12 @@ protected void setValueFromElements(Elements elements, Object object, String tit
List<Object> listValue = new ArrayList<Object>();
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){
Expand All @@ -86,21 +90,21 @@ protected void setValueFromElements(Elements elements, Object object, String tit
setValue = false;
}
}

if(setValue){
List<Field> linkedFields = getLinkedFields(name, object.getClass());
for (Field linkedField : linkedFields) {
linkedField.setAccessible(true);
linkedField.set(object, value);
}
}

List<Field> titleFields = getTitleFields();
for (Field titleField : titleFields) {
titleField.setAccessible(true);
titleField.set(object, title);
}

List<Field> baseFields = getBaseFields(object.getClass());
for (Field baseField : baseFields) {
baseField.setAccessible(true);
Expand All @@ -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).
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -210,7 +237,7 @@ protected List<Field> 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.
Expand All @@ -227,7 +254,7 @@ protected List<Field> 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.
Expand All @@ -245,8 +272,8 @@ protected List<Field> getBaseFields(Class<?> clazz){
}
return fields;
}


/**
* Get a {@link List} of all the dynamic element.
* @param elements The {@link Elements}.
Expand All @@ -257,10 +284,35 @@ protected List<String> 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;
}

}

0 comments on commit 2ba505c

Please sign in to comment.