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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.adapter.impl;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.codehaus.jackson.JsonNode;
026import org.codehaus.jackson.JsonParser;
027import org.codehaus.jackson.map.DeserializationContext;
028import org.codehaus.jackson.map.JsonDeserializer;
029import org.codehaus.jackson.map.ObjectMapper;
030import org.codehaus.jackson.node.ArrayNode;
031import org.codehaus.jackson.node.BooleanNode;
032import org.codehaus.jackson.node.TextNode;
033import org.nuxeo.drive.adapter.FileSystemItem;
034import org.nuxeo.drive.adapter.ScrollFileSystemItemList;
035
036/**
037 * {@link JsonDeserializer} for a {@link ScrollFileSystemItemList}.
038 *
039 * @since 8.3
040 */
041public class ScrollFileSystemItemListDeserializer extends JsonDeserializer<ScrollFileSystemItemList> {
042
043    @Override
044    public ScrollFileSystemItemList deserialize(JsonParser jp, DeserializationContext dc) throws IOException {
045        JsonNode rootNode = jp.readValueAsTree();
046        String scrollId = ((TextNode) rootNode.get("scrollId")).getTextValue();
047        ObjectMapper mapper = new ObjectMapper();
048        ArrayNode fileSystemItemNodes = (ArrayNode) rootNode.get("fileSystemItems");
049        List<FileSystemItem> fileSystemItems = new ArrayList<>(fileSystemItemNodes.size());
050        for (JsonNode fileSystemItemNode : fileSystemItemNodes) {
051            boolean folderish = ((BooleanNode) fileSystemItemNode.get("folder")).getBooleanValue();
052            if (folderish) {
053                fileSystemItems.add(mapper.readValue(fileSystemItemNode, DocumentBackedFolderItem.class));
054            } else {
055                fileSystemItems.add(mapper.readValue(fileSystemItemNode, DocumentBackedFileItem.class));
056            }
057        }
058        return new ScrollFileSystemItemListImpl(scrollId, fileSystemItems);
059    }
060}