Skip to content

Commit

Permalink
Use reflection to extract JSON field names from Post class when popul…
Browse files Browse the repository at this point in the history
…ating map for post creation.
  • Loading branch information
johanmynhardt committed Aug 7, 2017
1 parent ffb0489 commit 01d2329
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 25 deletions.
72 changes: 49 additions & 23 deletions src/main/java/com/afrozaar/wordpress/wpapi/v2/Client.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.afrozaar.wordpress.wpapi.v2;

import static com.afrozaar.wordpress.wpapi.v2.util.FieldExtractor.extractField;
import static com.afrozaar.wordpress.wpapi.v2.util.FieldExtractor.renderableField;

import static java.lang.String.format;
import static java.net.URLDecoder.decode;
Expand All @@ -28,6 +27,7 @@
import com.afrozaar.wordpress.wpapi.v2.model.Post;
import com.afrozaar.wordpress.wpapi.v2.model.PostMeta;
import com.afrozaar.wordpress.wpapi.v2.model.PostStatus;
import com.afrozaar.wordpress.wpapi.v2.model.RenderableField;
import com.afrozaar.wordpress.wpapi.v2.model.Taxonomy;
import com.afrozaar.wordpress.wpapi.v2.model.Term;
import com.afrozaar.wordpress.wpapi.v2.model.User;
Expand Down Expand Up @@ -59,17 +59,20 @@
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.beanutils.BeanUtils;
import org.assertj.core.util.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -852,31 +855,54 @@ private String fixQuery(String href) {

}

@VisibleForTesting
@SuppressWarnings("unchecked")
private Map<String, Object> fieldsFrom(Post post) {
protected Map<String, Object> fieldsFrom(Post post) {
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();

BiConsumer<String, Object> biConsumer = (key, value) -> {
if (value != null) {
builder.put(key, value);
}
};

biConsumer.accept("date", post.getDate());
biConsumer.accept("modified_gmt", post.getModified());
// biConsumer.accept("slug", post.getSlug());
// biConsumer.accept("status",post.getStatus());
biConsumer.accept("title", renderableField.apply(Post::getTitle, post).orElse(null));
biConsumer.accept("content", renderableField.apply(Post::getContent, post).orElse(null));
biConsumer.accept("author", post.getAuthor());
biConsumer.accept("excerpt", renderableField.apply(Post::getExcerpt, post).orElse(null));
biConsumer.accept("comment_status", post.getCommentStatus());
biConsumer.accept("ping_status", post.getPingStatus());
biConsumer.accept("format", post.getFormat());
biConsumer.accept("sticky", post.getSticky());
biConsumer.accept("featured_media", post.getFeaturedMedia());
biConsumer.accept("categories", post.getCategoryIds());
//biConsumer.accept("type", post.getType());
BiConsumer<String, Object> biConsumer = (key, value) -> ofNullable(value).ifPresent(v -> builder.put(key, v));

List<String> processableFields = Arrays.asList(
"author",
"categories",
"comment_status",
"content",
"date",
"featured_media",
"format",
"excerpt",
"modified_gmt",
"ping_status",
//"slug",
//"status",
"sticky",
"title"
//"type"
);

// types ignored for now: slug, status, type

Arrays.stream(post.getClass().getDeclaredFields())
.filter(field -> field.getAnnotationsByType(JsonProperty.class).length > 0)
.map(field -> Tuple2.of(field, field.getAnnotationsByType(JsonProperty.class)[0]))
.filter(fieldTuple -> processableFields.contains(fieldTuple.b.value()))
.forEach(field -> {
try {
ReflectionUtils.makeAccessible(field.a);
Object theField = field.a.get(post);
if (nonNull(theField)) {
final Object value;
if (theField instanceof RenderableField) {
value = ((RenderableField) theField).getRendered();
} else {
value = theField;
}
biConsumer.accept(field.b.value(), value);
}
} catch (IllegalAccessException e) {
LOG.error("Error populating post fields builder.", e);
}
});

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
package com.afrozaar.wordpress.wpapi.v2;

import static org.assertj.core.api.Assertions.assertThat;

import com.afrozaar.wordpress.wpapi.v2.config.ClientConfig;
import com.afrozaar.wordpress.wpapi.v2.config.ClientFactory;
import com.afrozaar.wordpress.wpapi.v2.model.Post;
import com.afrozaar.wordpress.wpapi.v2.model.builder.PostBuilder;
import com.afrozaar.wordpress.wpapi.v2.model.builder.TitleBuilder;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.util.Map;

/**
* @author johan
*/
public class WordpressClientTest {

private static final Logger LOG = LoggerFactory.getLogger(WordpressClientTest.class);
Wordpress wordpress = ClientFactory.fromConfig(ClientConfig.of("http://localhost", "username", "password", false, true));

@Test
public void TestCreateClient() {
Wordpress wordpress = ClientFactory.fromConfig(ClientConfig.of("http://localhost", "username", "password", false, true));

System.out.println("media = " + wordpress.getMedia(10L));
}

@Test
public void PostFieldProcessingTest() {

Post post = PostBuilder.aPost()
.withTitle(TitleBuilder.aTitle().withRendered("foobar").build())
.withModifiedGmt(Instant.now().toString())
.withContent(null)
.withFormat("myformat")
.build();

final Map<String, Object> fieldMap = ((Client) wordpress).fieldsFrom(post);

assertThat(fieldMap).containsOnlyKeys("title", "modified_gmt","format");

LOG.debug("map to post: {}", fieldMap);

}

}

0 comments on commit 01d2329

Please sign in to comment.