Revision 44663
Added by Katerina Iatropoulou over 7 years ago
APIResponseFormatter.java | ||
---|---|---|
38 | 38 |
return String.format(XMLResponseFormat.error500Message, message, description); |
39 | 39 |
} |
40 | 40 |
|
41 |
public static String createCountMeta(HttpServletRequest request, String query, int total, String type) { |
|
42 |
String selfPageUrl = pageUrlCreation(request); |
|
43 |
|
|
44 |
if (type.equals(MediaType.APPLICATION_JSON)) { |
|
45 |
return String.format(JSONResponseFormat.countSearch, StringEscapeUtils.escapeJson(query), formatSelfPaging(type, selfPageUrl)); |
|
46 |
} |
|
47 |
|
|
48 |
return String.format(XMLResponseFormat.countSearch, StringEscapeUtils.escapeXml10(query), formatSelfPaging(type, selfPageUrl)); |
|
49 |
} |
|
50 |
|
|
41 | 51 |
public static String createEntityResponse(HttpServletRequest request, RequestResponseHandler.Entity entityType, String entity, String type) { |
42 | 52 |
if (type.equals(MediaType.APPLICATION_JSON)) { |
43 | 53 |
String cleanEntity = null; |
... | ... | |
63 | 73 |
return String.format(JSONResponseFormat.response, createMeta(request, type, query, filters, searchResult.getTotal(), searchResult.getPage(), searchResult.getSize()), formatSearchResults(type, searchResult.getSearchResults()), ""); |
64 | 74 |
} |
65 | 75 |
return String.format(XMLResponseFormat.response, createMeta(request, type, query, filters, searchResult.getTotal(), searchResult.getPage(), searchResult.getSize()), formatSearchResults(type, searchResult.getSearchResults()), ""); |
66 |
|
|
67 | 76 |
} |
68 | 77 |
|
69 | 78 |
private static String formatRefineResults(String type, List<String> refineResults) { |
... | ... | |
114 | 123 |
return stringBuilder.toString(); |
115 | 124 |
} |
116 | 125 |
|
117 |
/* |
|
118 |
public static String createEntitiesResponse(HttpServletRequest request, RequestResponseHandler.Entity entityType, |
|
119 |
String query, String entities, List<String> filters, int total, int currentOffset, int limit, boolean refine, List<String> refineResults, String type) { |
|
120 |
if (refine) { |
|
121 |
if (type.equals(MediaType.APPLICATION_JSON)) { |
|
122 |
return String.format(JSONResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit), entities, ", refine", new Gson().toJson(refineResults)); |
|
123 |
} |
|
124 |
return String.format(XMLResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit), entities, "", ""); |
|
125 |
} |
|
126 |
|
|
127 |
if (type.equals(MediaType.APPLICATION_JSON)) { |
|
128 |
return String.format(JSONResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit), entities, "", ""); |
|
129 |
} |
|
130 |
|
|
131 |
return String.format(XMLResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit)); |
|
132 |
|
|
133 |
}*/ |
|
134 |
|
|
135 | 126 |
public static String createMetaBasic(HttpServletRequest request, String type) { |
136 | 127 |
String selfPageUrl = pageUrlCreation(request); |
137 | 128 |
if (type.equals(MediaType.APPLICATION_JSON)) { |
... | ... | |
235 | 226 |
return 0; |
236 | 227 |
} |
237 | 228 |
|
238 |
return currentOffset-1; |
|
229 |
int lastPage = calculateLastPage(total, limit); |
|
230 |
return (currentOffset-1 < lastPage) ? (currentOffset-1) : lastPage; |
|
239 | 231 |
} |
240 | 232 |
|
241 | 233 |
private static int calculateNextPage(int total, int currentOffset, int limit) { |
242 | 234 |
int lastPage = calculateLastPage(total, limit); |
243 |
|
|
244 |
if (currentOffset + 1 >= lastPage) { |
|
245 |
return lastPage; |
|
246 |
} |
|
247 |
return currentOffset + 1; |
|
235 |
return (currentOffset + 1 >= lastPage) ? lastPage:(currentOffset+1); |
|
248 | 236 |
} |
249 | 237 |
|
250 | 238 |
private static String pageUrlCreation(String urlPrefix, int offset, int limit) { |
Also available in: Unified diff
more code cleaning