aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/result/Templating.java
blob: a8b1eedf52819fbbe29e3ebb28ee61a542bdf9cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;

import java.util.Map;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.prelude.templates.SearchRendererAdaptor;
import com.yahoo.prelude.templates.TemplateSet;
import com.yahoo.prelude.templates.UserTemplate;
import com.yahoo.processing.rendering.Renderer;
import com.yahoo.search.Result;
import com.yahoo.search.query.Presentation;

/**
 * Helper methods and data store for result attributes geared towards result
 * rendering and presentation.
 *
 * @author Steinar Knutsen
 * @deprecated do not use
 */
@Deprecated // OK (But wait for deprecated handlers in vespaclient-container-plugin to be removed)
// TODO: Remove on Vespa 7
public class Templating {

    private final Result result;
    private Renderer<Result> renderer;

    public Templating(Result result) {
        super();
        this.result = result;
    }

    /**
     * Returns The first hit presented in the result as an index into the global
     * list of all hits generated by the user query.
     */
    public int getFirstHitNo() {
        return result.getQuery().getOffset() + 1;
    }

    /**
     * Returns the first hit of the next result page, 0 if there aren't any more
     * hits available
     */
    public long getNextFirstHitNo() {
        if (result.getQuery().getHits() > result.getConcreteHitCount()) {
            return 0;
        }

        return Math.min(getLastHitNo() + 1, result.getTotalHitCount());
    }

    /**
     * Returns the first hit of the next result page, 0 if there aren't any more
     * hits available
     */
    public long getNextLastHitNo() {
        if (result.getQuery().getHits() > result.getConcreteHitCount()) {
            return 0;
        }

        return Math.min(getLastHitNo() + result.getConcreteHitCount(), result.getTotalHitCount());
    }

    /**
     * Returns the number of the last result of the current hit page.
     */
    public int getLastHitNo() {
        return getFirstHitNo() + result.getConcreteHitCount() - 1;
    }

    /**
     * The first hit presented on the previous result page as an index into the
     * global list of all hits generated by the user query
     */
    public int getPrevFirstHitNo() {
        return Math.max(getFirstHitNo() - result.getQuery().getHits(), 1);
    }

    /**
     * The last hit presented on the previous result page as an index into the
     * global list of all hits generated by the user query
     */
    public int getPrevLastHitNo() {
        return Math.max(getFirstHitNo() - 1, 0);
    }

    /**
     * An URL that may be used to obtain the next result page.
     */
    public String getNextResultURL() {
        HttpRequest request = result.getQuery().getHttpRequest();
        StringBuilder nextURL = new StringBuilder();

        nextURL.append(getPath(request)).append("?");
        parametersExceptOffset(request, nextURL);

        int offset = getLastHitNo();

        nextURL.append("&").append("offset=").append(Integer.toString(offset));
        return nextURL.toString();
    }

    /**
     * An URL that may be used to obtain the previous result page.
     */
    public String getPreviousResultURL() {
        HttpRequest request = result.getQuery().getHttpRequest();
        StringBuilder prevURL = new StringBuilder();

        prevURL.append(getPath(request)).append("?");
        parametersExceptOffset(request, prevURL);
        int offset = getPrevFirstHitNo() - 1;
        prevURL.append("&").append("offset=").append(Integer.toString(offset));
        return prevURL.toString();
    }

    public String getCurrentResultURL() {
        HttpRequest request = result.getQuery().getHttpRequest();
        StringBuilder thisURL = new StringBuilder();

        thisURL.append(getPath(request)).append("?");
        parameters(request, thisURL);
        return thisURL.toString();
    }

    private String getPath(HttpRequest request) {
        String path = request.getUri().getPath();
        if (path == null) {
            path = "";
        }
        return path;
    }

    private void parametersExceptOffset(HttpRequest request, StringBuilder nextURL) {
        int startLength = nextURL.length();
        for (Map.Entry<String, String> property : request.propertyMap().entrySet()) {
            if (property.getKey().equals("offset")) continue;

            if (nextURL.length() > startLength)
                nextURL.append("&");
            nextURL.append(property.getKey()).append("=").append(property.getValue());
        }
    }

    private void parameters(HttpRequest request, StringBuilder nextURL) {
        int startLength = nextURL.length();
        for (Map.Entry<String, String> property : request.propertyMap().entrySet()) {
            if (nextURL.length() > startLength)
                nextURL.append("&");
            nextURL.append(property.getKey()).append("=").append(property.getValue());
        }
    }

    /**
     * Returns the templates which will render the result. This is never null.
     * If default rendering is used, it is a TemplateSet containing no
     * templates.
     *
     * @deprecated use a renderer instead
     */
    @SuppressWarnings("rawtypes")
    // TODO: Remove on Vespa 7
    @Deprecated // OK
    public UserTemplate getTemplates() {
        if (renderer == null) {
            return TemplateSet.getDefault();
        } else if (renderer instanceof SearchRendererAdaptor) {
            return ((SearchRendererAdaptor) renderer).getAdaptee();
        } else {
            throw new RuntimeException(
                    "Please use getTemplate() instead of getTemplates() when using the new template api.");
        }
    }

    /**
     * Sets the template set which should render this result set
     *
     * @param templates
     *                the templates which should render this result, or null to
     *                use the default xml rendering
     */
    @SuppressWarnings("deprecation")
    public void setTemplates(@SuppressWarnings("rawtypes") UserTemplate templates) {
        if (templates == null) {
            setTemplates(TemplateSet.getDefault());
        } else {
            setRenderer(new SearchRendererAdaptor(templates));
        }
    }

    /**
     * @deprecated since 5.1.21, use {@link Presentation#getRenderer()}
     */
    public Renderer<Result> getRenderer() {
        return renderer;
    }

    /**
     * @deprecated since 5.1.21, use {@link Presentation#setRenderer(com.yahoo.component.ComponentSpecification)}
     */
    public void setRenderer(Renderer<Result> renderer) {
        this.renderer = renderer;
    }

    /**
     * For internal use only.
     */
    public boolean usesDefaultTemplate() {
        return renderer == null ||
                (renderer instanceof SearchRendererAdaptor &&
                ((SearchRendererAdaptor) renderer).getAdaptee().isDefaultTemplateSet());
    }

}