|
1 | 1 | package com.afrozaar.wordpress.wpapi.v2; |
2 | 2 |
|
3 | 3 | import static com.afrozaar.wordpress.wpapi.v2.util.FieldExtractor.extractField; |
4 | | -import static com.afrozaar.wordpress.wpapi.v2.util.FieldExtractor.renderableField; |
5 | 4 |
|
6 | 5 | import static java.lang.String.format; |
7 | 6 | import static java.net.URLDecoder.decode; |
|
28 | 27 | import com.afrozaar.wordpress.wpapi.v2.model.Post; |
29 | 28 | import com.afrozaar.wordpress.wpapi.v2.model.PostMeta; |
30 | 29 | import com.afrozaar.wordpress.wpapi.v2.model.PostStatus; |
| 30 | +import com.afrozaar.wordpress.wpapi.v2.model.RenderableField; |
31 | 31 | import com.afrozaar.wordpress.wpapi.v2.model.Taxonomy; |
32 | 32 | import com.afrozaar.wordpress.wpapi.v2.model.Term; |
33 | 33 | import com.afrozaar.wordpress.wpapi.v2.model.User; |
|
59 | 59 | import org.springframework.http.converter.xml.SourceHttpMessageConverter; |
60 | 60 | import org.springframework.util.LinkedMultiValueMap; |
61 | 61 | import org.springframework.util.MultiValueMap; |
| 62 | +import org.springframework.util.ReflectionUtils; |
62 | 63 | import org.springframework.web.client.HttpClientErrorException; |
63 | 64 | import org.springframework.web.client.HttpServerErrorException; |
64 | 65 | import org.springframework.web.client.RestTemplate; |
65 | 66 | import org.springframework.web.util.UriComponents; |
66 | 67 | import org.springframework.web.util.UriComponentsBuilder; |
67 | 68 |
|
| 69 | +import com.fasterxml.jackson.annotation.JsonProperty; |
68 | 70 | import com.fasterxml.jackson.databind.DeserializationFeature; |
69 | 71 | import com.fasterxml.jackson.databind.JsonMappingException; |
70 | 72 | import com.fasterxml.jackson.databind.ObjectMapper; |
71 | 73 |
|
72 | 74 | import org.apache.commons.beanutils.BeanUtils; |
| 75 | +import org.assertj.core.util.VisibleForTesting; |
73 | 76 | import org.slf4j.Logger; |
74 | 77 | import org.slf4j.LoggerFactory; |
75 | 78 |
|
@@ -852,31 +855,54 @@ private String fixQuery(String href) { |
852 | 855 |
|
853 | 856 | } |
854 | 857 |
|
| 858 | + @VisibleForTesting |
855 | 859 | @SuppressWarnings("unchecked") |
856 | | - private Map<String, Object> fieldsFrom(Post post) { |
| 860 | + protected Map<String, Object> fieldsFrom(Post post) { |
857 | 861 | ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); |
858 | 862 |
|
859 | | - BiConsumer<String, Object> biConsumer = (key, value) -> { |
860 | | - if (value != null) { |
861 | | - builder.put(key, value); |
862 | | - } |
863 | | - }; |
864 | | - |
865 | | - biConsumer.accept("date", post.getDate()); |
866 | | - biConsumer.accept("modified_gmt", post.getModified()); |
867 | | - // biConsumer.accept("slug", post.getSlug()); |
868 | | - // biConsumer.accept("status",post.getStatus()); |
869 | | - biConsumer.accept("title", renderableField.apply(Post::getTitle, post).orElse(null)); |
870 | | - biConsumer.accept("content", renderableField.apply(Post::getContent, post).orElse(null)); |
871 | | - biConsumer.accept("author", post.getAuthor()); |
872 | | - biConsumer.accept("excerpt", renderableField.apply(Post::getExcerpt, post).orElse(null)); |
873 | | - biConsumer.accept("comment_status", post.getCommentStatus()); |
874 | | - biConsumer.accept("ping_status", post.getPingStatus()); |
875 | | - biConsumer.accept("format", post.getFormat()); |
876 | | - biConsumer.accept("sticky", post.getSticky()); |
877 | | - biConsumer.accept("featured_media", post.getFeaturedMedia()); |
878 | | - biConsumer.accept("categories", post.getCategoryIds()); |
879 | | - //biConsumer.accept("type", post.getType()); |
| 863 | + BiConsumer<String, Object> biConsumer = (key, value) -> ofNullable(value).ifPresent(v -> builder.put(key, v)); |
| 864 | + |
| 865 | + List<String> processableFields = Arrays.asList( |
| 866 | + "author", |
| 867 | + "categories", |
| 868 | + "comment_status", |
| 869 | + "content", |
| 870 | + "date", |
| 871 | + "featured_media", |
| 872 | + "format", |
| 873 | + "excerpt", |
| 874 | + "modified_gmt", |
| 875 | + "ping_status", |
| 876 | + //"slug", |
| 877 | + //"status", |
| 878 | + "sticky", |
| 879 | + "title" |
| 880 | + //"type" |
| 881 | + ); |
| 882 | + |
| 883 | + // types ignored for now: slug, status, type |
| 884 | + |
| 885 | + Arrays.stream(post.getClass().getDeclaredFields()) |
| 886 | + .filter(field -> field.getAnnotationsByType(JsonProperty.class).length > 0) |
| 887 | + .map(field -> Tuple2.of(field, field.getAnnotationsByType(JsonProperty.class)[0])) |
| 888 | + .filter(fieldTuple -> processableFields.contains(fieldTuple.b.value())) |
| 889 | + .forEach(field -> { |
| 890 | + try { |
| 891 | + ReflectionUtils.makeAccessible(field.a); |
| 892 | + Object theField = field.a.get(post); |
| 893 | + if (nonNull(theField)) { |
| 894 | + final Object value; |
| 895 | + if (theField instanceof RenderableField) { |
| 896 | + value = ((RenderableField) theField).getRendered(); |
| 897 | + } else { |
| 898 | + value = theField; |
| 899 | + } |
| 900 | + biConsumer.accept(field.b.value(), value); |
| 901 | + } |
| 902 | + } catch (IllegalAccessException e) { |
| 903 | + LOG.error("Error populating post fields builder.", e); |
| 904 | + } |
| 905 | + }); |
880 | 906 |
|
881 | 907 | return builder.build(); |
882 | 908 | } |
|
0 commit comments