Project

General

Profile

1
package eu.dnetlib.data.search.web.api;
2

    
3
import eu.dnetlib.data.search.web.utils.RequestResponseHandler;
4
import org.apache.log4j.Logger;
5

    
6
import javax.servlet.http.HttpServletRequest;
7

    
8
/**
9
 * Created by kiatrop on 28/9/2016.
10
 */
11
public class ResponseFormatter {
12

    
13
    private static final Logger logger = Logger.getLogger(ResponseFormatter.class);
14

    
15
    private static final String error404Message = "{ \"status\" : \"error\", \"code\" : \"404\", \"message\" : \"%s\"}";
16
    private static final String error400Message = "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \"%s\"}";
17
    private static final String error500Message = "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \"%s\", \"description\" : \"%s\" }";
18

    
19
    private static final String metaBasic = "{\"status\": \"success\", \"code\":\"200\" }"; //TODO add self
20
    private static final String countSearch = "{\"status\": \"success\", \"code\":\"200\", \"query\":\"%s\", \"total\":\"%s\"}"; //only count
21
    private static final String metaSearch = "{\"status\": \"success\", \"code\":\"200\", \"query\":\"%s\", \"total\":\"%s\", \"page\":\"%s\", \"size\":\"%s\", \"_links\": %s}";
22

    
23
    private static final String _links = "{\"first\": {\"href\":\"%s\"}, " +
24
            " \"last\": {\"href\":\"%s\"}, " +
25
            " \"previous\": {\"href\":\"%s\"}, " +
26
            "  \"next\": {\"href\":\"%s\"}, " +
27
            "  \"self\": {\"href\":\"%s\"}}";
28

    
29
    private static final String _self = "{\"self\": {\"href\":\"%s\"}}";
30

    
31
    private static final String result = "{%s}";
32

    
33
    private static final String resource = "\"%s\": {%s}";
34

    
35
    private static final String resources = "\"%s\": [{%s}]";
36

    
37
    private static final String response = "{\"meta\": %s, \"results\" : [%s] %s %s}";
38

    
39
    private static final String singleResponse = "{\"meta\": %s, %s }";
40

    
41

    
42
    public static String compose404Message(String message) {
43
        return String.format(error404Message, message);
44
    }
45

    
46
    public static String compose400Message(String message) {
47
        return String.format(error400Message, message);
48
    }
49

    
50
    public static String compose500Message(String message, String description) { return String.format(error500Message, message, description); }
51

    
52
    public static String createCountMeta(String query, int total) {
53
        return String.format(countSearch, query, total);
54
    }
55

    
56
    private static String createMetaSearch(HttpServletRequest request, String query, int total, int currentOffset, int limit) {
57
        return formatMetaSearch(query, total+"", currentOffset+"", limit+"", createPaging(request, total, currentOffset, limit));
58
    }
59

    
60
    private static String formatMetaBasic() {
61
        return String.format(metaBasic);
62
    }
63

    
64
    private static String createMetaBasic() {
65
        return formatMetaBasic();
66
    }
67

    
68
    private static String formatMetaSearch(String query, String total, String currentOffset, String limit, String links) {
69
        return String.format(metaSearch, query, total, limit, currentOffset,links);
70
    }
71

    
72
    public static String createEntityResponse(RequestResponseHandler.Entity entityType, String entity) {
73
        if (entity.startsWith("{") && entity.endsWith("}")) {
74
            String cleanEntity = entity.substring(1, entity.length()-1);
75
            return String.format(singleResponse, createMetaBasic(), cleanEntity);
76
        }
77
        
78
        return String.format(singleResponse, createMetaBasic(), entity);
79
    }
80

    
81
    public static String createEntitiesResponse(HttpServletRequest request, RequestResponseHandler.Entity entityType, String query, String entities, String total, String currentOffset, String limit, boolean refine, String refineResults) {
82
        if (!refine)
83
            return String.format(response, createMetaSearch(request, query, Integer.valueOf(total), Integer.valueOf(limit), Integer.valueOf(currentOffset)), entities, "", "");
84

    
85
        return String.format(response, createMetaSearch(request, query, Integer.valueOf(total), Integer.valueOf(limit), Integer.valueOf(currentOffset)), entities, ",", "\"refineResults\" : { " + refineResults.toString() + "}");
86
    }
87

    
88
    private static String createPaging(HttpServletRequest request, int total, int currentOffset, int limit) {
89
        String url = request.getRequestURL().toString();
90
        logger.debug("limit " + limit);
91

    
92
        if (limit > 0) {
93
            String selfPageUrl = pageUrlCreation(url, currentOffset, limit);
94
            String firstPageUrl = pageUrlCreation(url, 0, 10);
95
            String lastPageUrl = pageUrlCreation(url, calculateLastPage(total, limit), 10);
96
            String previousPageUrl = pageUrlCreation(url, calculatePreviousPage(total, currentOffset, limit), 10);
97
            String nextPageUrl = pageUrlCreation(url, calculateNextPage(total, currentOffset, limit), 10);
98

    
99
            return formatPaging(firstPageUrl, lastPageUrl, previousPageUrl, nextPageUrl, selfPageUrl);
100
        }
101
        return String.format(url);
102
    }
103

    
104
    private static String formatPaging(String first, String last, String previous, String next, String self) {
105
        return String.format(_links, first, last, previous, next, self);
106
    }
107

    
108
    private static int calculateLastPage(int total, int limit) {
109
        if (total <= limit) {
110
            return 0;
111

    
112
        } else {
113
            if (total%limit == 0) {
114
                return total / limit - 1;
115
            }
116
            return total/limit ;
117
        }
118
    }
119

    
120
    private static int calculatePreviousPage(int total, int currentOffset, int limit) {
121
        if (currentOffset-1 <= 0) {
122
            return 0;
123
        }
124

    
125
        return currentOffset-1;
126
    }
127

    
128

    
129
    private static int calculateNextPage(int total, int currentOffset, int limit) {
130
        int lastPage = calculateLastPage(total, limit);
131

    
132
        if (currentOffset + 1 >= lastPage) {
133
            return lastPage;
134
        }
135
            return currentOffset + 1;
136
    }
137

    
138
    private static String pageUrlCreation(String urlPrefix, int offset, int limit) {
139
        return urlPrefix + "?page=" + offset + "&size=" + limit;
140
    }
141

    
142
}
(5-5/8)