001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Gabriel Barata <gbarata@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.search.core;
020
021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
023
024import java.io.IOException;
025import java.util.HashMap;
026import java.util.Iterator;
027import java.util.Map;
028
029import org.codehaus.jackson.JsonNode;
030import org.codehaus.jackson.map.ObjectMapper;
031import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
032import org.nuxeo.ecm.core.io.registry.reflect.Setup;
033
034/**
035 * @since 8.3
036 */
037@Setup(mode = SINGLETON, priority = REFERENCE)
038public class SavedSearchRequestReader extends EntityJsonReader<SavedSearchRequest> {
039
040    public SavedSearchRequestReader() {
041        super(SavedSearchWriter.ENTITY_TYPE);
042    }
043
044    @Override
045    protected SavedSearchRequest readEntity(JsonNode jn) throws IOException {
046        String id = getStringField(jn, "id");
047        String title = getStringField(jn, "title");
048        String queryParams = getStringField(jn, "queryParams");
049        String query = getStringField(jn, "query");
050        String queryLanguage = getStringField(jn, "queryLanguage");
051        String pageProviderName = getStringField(jn, "pageProviderName");
052        Long pageSize = getLongField(jn, "pageSize");
053        Long currentPageIndex = getLongField(jn, "currentPageIndex");
054        Long maxResults = getLongField(jn, "maxResults");
055        String sortBy = getStringField(jn, "sortBy");
056        String sortOrder = getStringField(jn, "sortOrder");
057        String contentViewData = getStringField(jn, "contentViewData");
058        JsonNode queryParamsNode = jn.has("params") ? jn.get("params") : null;
059
060        Map<String, String> params = new HashMap<>();
061        ObjectMapper mapper = new ObjectMapper();
062        if (queryParamsNode != null) {
063            Iterator<Map.Entry<String, JsonNode>> fields = queryParamsNode.getFields();
064            while (fields.hasNext()) {
065                Map.Entry<String, JsonNode> fieldEntry = fields.next();
066                params.put(
067                        fieldEntry.getKey(),
068                        fieldEntry.getValue().isTextual() ? fieldEntry.getValue().getTextValue()
069                                : mapper.writeValueAsString(fieldEntry.getValue()));
070            }
071        }
072
073        return new SavedSearchRequest(id, title, queryParams, params, query, queryLanguage, pageProviderName, pageSize,
074                currentPageIndex, maxResults, sortBy, sortOrder, contentViewData);
075    }
076
077    protected Long getLongField(JsonNode jn, String elName) {
078        JsonNode elNode = jn.get(elName);
079        if (elNode != null && !elNode.isNull()) {
080            if (elNode.isNumber()) {
081                return elNode.getLongValue();
082            } else if (elNode.isTextual()) {
083                return Long.valueOf(elNode.getTextValue());
084            } else {
085                return null;
086            }
087        } else {
088            return null;
089        }
090    }
091}