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.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
030import org.nuxeo.ecm.core.io.registry.reflect.Setup;
031
032import com.fasterxml.jackson.databind.JsonNode;
033import com.fasterxml.jackson.databind.ObjectMapper;
034
035/**
036 * @since 8.3
037 */
038@Setup(mode = SINGLETON, priority = REFERENCE)
039public class SavedSearchRequestReader extends EntityJsonReader<SavedSearchRequest> {
040
041    public SavedSearchRequestReader() {
042        super(SavedSearchWriter.ENTITY_TYPE);
043    }
044
045    @Override
046    protected SavedSearchRequest readEntity(JsonNode jn) throws IOException {
047        String id = getStringField(jn, "id");
048        String title = getStringField(jn, "title");
049        String queryParams = getStringField(jn, "queryParams");
050        String query = getStringField(jn, "query");
051        String queryLanguage = getStringField(jn, "queryLanguage");
052        String pageProviderName = getStringField(jn, "pageProviderName");
053        Long pageSize = getLongField(jn, "pageSize");
054        Long currentPageIndex = getLongField(jn, "currentPageIndex");
055        Long maxResults = getLongField(jn, "maxResults");
056        String sortBy = getStringField(jn, "sortBy");
057        String sortOrder = getStringField(jn, "sortOrder");
058        String contentViewData = getStringField(jn, "contentViewData");
059        JsonNode queryParamsNode = jn.has("params") ? jn.get("params") : null;
060
061        Map<String, String> params = new HashMap<>();
062        ObjectMapper mapper = new ObjectMapper();
063        if (queryParamsNode != null) {
064            Iterator<Map.Entry<String, JsonNode>> fields = queryParamsNode.fields();
065            while (fields.hasNext()) {
066                Map.Entry<String, JsonNode> fieldEntry = fields.next();
067                params.put(
068                        fieldEntry.getKey(),
069                        fieldEntry.getValue().isTextual() ? fieldEntry.getValue().textValue()
070                                : mapper.writeValueAsString(fieldEntry.getValue()));
071            }
072        }
073
074        return new SavedSearchRequest(id, title, queryParams, params, query, queryLanguage, pageProviderName, pageSize,
075                currentPageIndex, maxResults, sortBy, sortOrder, contentViewData);
076    }
077}