|
| 1 | +package org.anthillplatform.runtime.services; |
| 2 | + |
| 3 | +import org.anthillplatform.runtime.AnthillRuntime; |
| 4 | +import org.anthillplatform.runtime.requests.JsonRequest; |
| 5 | +import org.anthillplatform.runtime.requests.Request; |
| 6 | +import org.anthillplatform.runtime.util.Utils; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | + |
| 10 | +import java.text.SimpleDateFormat; |
| 11 | +import java.util.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * A service to deliver news and patch notes feed to the users inside the game |
| 15 | + * |
| 16 | + * See https://github.com/anthill-platform/anthill-blog |
| 17 | + */ |
| 18 | +public class BlogService extends Service |
| 19 | +{ |
| 20 | + private JsonRequest currentRequest; |
| 21 | + |
| 22 | + public static final String ID = "blog"; |
| 23 | + public static final String API_VERSION = "0.2"; |
| 24 | + |
| 25 | + /** |
| 26 | + * Please note that you should not create an instance of the service yourself, |
| 27 | + * and use AnthillRuntime.Get(BlogService.ID, BlogService.class) to get existing one instead |
| 28 | + */ |
| 29 | + public BlogService(AnthillRuntime runtime, String location) |
| 30 | + { |
| 31 | + super(runtime, location, ID, API_VERSION); |
| 32 | + } |
| 33 | + |
| 34 | + public static BlogService Get() |
| 35 | + { |
| 36 | + return AnthillRuntime.Get(ID, BlogService.class); |
| 37 | + } |
| 38 | + |
| 39 | + public interface GetBlogEntriesCallback |
| 40 | + { |
| 41 | + void complete(BlogService service, Request request, Request.Result result, BlogEntriesList blogEntries); |
| 42 | + } |
| 43 | + |
| 44 | + public static class BlogEntriesList extends LinkedList<BlogEntry> |
| 45 | + { |
| 46 | + public void read(JSONArray entries) |
| 47 | + { |
| 48 | + clear(); |
| 49 | + |
| 50 | + for (int i = 0, t = entries.length(); i < t; i++) |
| 51 | + { |
| 52 | + BlogEntry event = new BlogEntry(); |
| 53 | + JSONObject data = entries.getJSONObject(i); |
| 54 | + |
| 55 | + if (data != null) |
| 56 | + { |
| 57 | + if (!event.read(data)) |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + this.add(event); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static SimpleDateFormat getTimeFormat() |
| 67 | + { |
| 68 | + return Utils.DATE_FORMAT; |
| 69 | + } |
| 70 | + |
| 71 | + public static class BlogEntry |
| 72 | + { |
| 73 | + public JSONObject data; |
| 74 | + public int id; |
| 75 | + public Date dateCreate; |
| 76 | + public Date dateUpdate; |
| 77 | + |
| 78 | + public BlogEntry() |
| 79 | + { |
| 80 | + } |
| 81 | + |
| 82 | + public boolean read(JSONObject payload) |
| 83 | + { |
| 84 | + id = payload.getInt("id"); |
| 85 | + data = payload.getJSONObject("data"); |
| 86 | + |
| 87 | + try |
| 88 | + { |
| 89 | + SimpleDateFormat format = getTimeFormat(); |
| 90 | + |
| 91 | + dateCreate = format.parse(payload.getString("create_date")); |
| 92 | + dateUpdate = format.parse(payload.getString("update_date")); |
| 93 | + } |
| 94 | + catch (Exception ignored) |
| 95 | + { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + return true; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void getBlogEntries( |
| 104 | + LoginService.AccessToken accessToken, |
| 105 | + String blog, |
| 106 | + final GetBlogEntriesCallback callback) |
| 107 | + { |
| 108 | + if (accessToken == null) |
| 109 | + { |
| 110 | + callback.complete(BlogService.this, null, Request.Result.forbidden, null); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (currentRequest != null) |
| 115 | + { |
| 116 | + callback.complete(BlogService.this, null, Request.Result.pending, null); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + currentRequest = new JsonRequest(getLocation() + "/blog/" + blog, new Request.RequestCallback() |
| 121 | + { |
| 122 | + @Override |
| 123 | + public void complete(Request request, Request.Result result) |
| 124 | + { |
| 125 | + currentRequest = null; |
| 126 | + |
| 127 | + if (result == Request.Result.success) |
| 128 | + { |
| 129 | + JSONObject response = ((JsonRequest) request).getObject(); |
| 130 | + |
| 131 | + JSONArray entries = response.getJSONArray("entries"); |
| 132 | + if (entries != null) |
| 133 | + { |
| 134 | + BlogEntriesList blogEntries = new BlogEntriesList(); |
| 135 | + blogEntries.read(entries); |
| 136 | + callback.complete(BlogService.this, request, result, blogEntries); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + callback.complete(BlogService.this, request, result, null); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + currentRequest.setAPIVersion(getAPIVersion()); |
| 147 | + Request.Fields queryArguments = new Request.Fields(); |
| 148 | + currentRequest.setQueryArguments(queryArguments); |
| 149 | + currentRequest.setToken(accessToken); |
| 150 | + currentRequest.get(); |
| 151 | + } |
| 152 | +} |
0 commit comments