001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.frontend.workflow;
017:
018: import org.outerj.daisy.repository.query.SortOrder;
019: import org.outerj.daisy.workflow.QueryValueSource;
020: import org.outerj.daisy.frontend.RequestUtil;
021: import org.apache.cocoon.environment.Request;
022:
023: import java.util.Map;
024: import java.net.URLEncoder;
025: import java.io.UnsupportedEncodingException;
026:
027: public class SearchHelper {
028:
029: public static OrderByParams getOrderByParams(Request request)
030: throws Exception {
031: String orderBy = request.getParameter("orderBy");
032: if (orderBy != null && orderBy.length() > 0) {
033: OrderByParams orderByParams = new OrderByParams();
034: orderByParams.orderBy = orderBy;
035: String orderBySourceName = RequestUtil.getStringParameter(
036: request, "orderBySource");
037: if (orderBySourceName.length() == 0)
038: throw new Exception(
039: "Invalid request: parameter 'orderBySource' is missing.");
040: orderByParams.orderBySource = QueryValueSource
041: .fromString(orderBySourceName);
042: String orderByDirectionName = request
043: .getParameter("orderByDirection");
044: if (orderByDirectionName.length() == 0)
045: orderByDirectionName = null;
046: orderByParams.orderByDirection = orderByDirectionName == null ? SortOrder.ASCENDING
047: : SortOrder.fromString(orderByDirectionName);
048: return orderByParams;
049: } else {
050: return null;
051: }
052: }
053:
054: public static OrderByParams getOrderByParams(String orderBy,
055: QueryValueSource orderBySource, SortOrder orderByDirection) {
056: OrderByParams params = new OrderByParams();
057: params.orderBy = orderBy;
058: params.orderBySource = orderBySource;
059: params.orderByDirection = orderByDirection;
060: return params;
061: }
062:
063: public static OffsetParams getOffsetParams(Request request) {
064: String offsetParam = request.getParameter("offset");
065: OffsetParams params = new OffsetParams();
066: if (offsetParam == null) {
067: params.offset = 0;
068: } else {
069: params.offset = Integer.parseInt(offsetParam);
070: }
071: return params;
072: }
073:
074: public static String buildUrl(String base,
075: Map<String, String> params)
076: throws UnsupportedEncodingException {
077: StringBuilder dataUrl = new StringBuilder();
078: dataUrl.append(base).append("?");
079: for (Map.Entry<String, String> param : params.entrySet()) {
080: dataUrl.append(param.getKey());
081: dataUrl.append("=");
082: dataUrl
083: .append(URLEncoder
084: .encode(param.getValue(), "UTF-8"));
085: dataUrl.append("&");
086: }
087: return dataUrl.toString();
088: }
089:
090: public static class OrderByParams {
091: public String orderBy;
092: public QueryValueSource orderBySource;
093: public SortOrder orderByDirection;
094: }
095:
096: public static class OffsetParams {
097: public int offset;
098: public int length = 10;
099: }
100: }
|