001/*
002 * (C) Copyright 2006-2011 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 *     matic
018 */
019package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
020
021import java.io.IOException;
022
023import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
024import org.nuxeo.ecm.automation.client.model.Documents;
025import org.nuxeo.ecm.automation.client.model.PaginableDocuments;
026
027import com.fasterxml.jackson.core.JsonGenerator;
028import com.fasterxml.jackson.core.JsonParser;
029import com.fasterxml.jackson.core.JsonToken;
030
031/**
032 * @author matic
033 */
034public class DocumentsMarshaller implements JsonMarshaller<Documents> {
035
036    @Override
037    public String getType() {
038        return "documents";
039    }
040
041    @Override
042    public Class<Documents> getJavaType() {
043        return Documents.class;
044    }
045
046    @Override
047    public void write(JsonGenerator jg, Object value) throws IOException {
048        throw new UnsupportedOperationException();
049    }
050
051    protected void readDocumentEntries(JsonParser jp, Documents docs) throws IOException {
052        JsonToken tok = jp.nextToken();
053        while (tok != JsonToken.END_ARRAY) {
054            docs.add(DocumentMarshaller.readDocument(jp));
055            tok = jp.nextToken();
056        }
057    }
058
059    protected Documents readDocuments(JsonParser jp) throws IOException {
060        Documents docs = new Documents();
061        JsonToken tok = jp.nextToken();
062        while (tok != JsonToken.END_ARRAY) {
063            String key = jp.getCurrentName();
064            if ("entries".equals(key)) {
065                readDocumentEntries(jp, docs);
066                return docs;
067            }
068            tok = jp.nextToken();
069        }
070        return docs;
071    }
072
073    protected Documents readPaginableDocuments(JsonParser jp) throws IOException {
074        PaginableDocuments docs = new PaginableDocuments();
075        JsonToken tok = jp.getCurrentToken();
076        while (tok != null && tok != JsonToken.END_OBJECT) {
077            String key = jp.getCurrentName();
078            jp.nextToken();
079            if ("resultsCount".equals(key)) {
080                docs.setResultsCount(jp.getIntValue());
081            } else if ("totalSize".equals(key)) {
082                docs.setResultsCount(jp.getIntValue());
083            } else if ("pageSize".equals(key)) {
084                docs.setPageSize(jp.getIntValue());
085            } else if ("numberOfPages".equals(key)) {
086                docs.setNumberOfPages(jp.getIntValue());
087            } else if ("pageCount".equals(key)) {
088                docs.setNumberOfPages(jp.getIntValue());
089            } else if ("currentPageIndex".equals(key)) {
090                docs.setCurrentPageIndex(jp.getIntValue());
091            } else if ("pageIndex".equals(key)) {
092                docs.setCurrentPageIndex(jp.getIntValue());
093            } else if ("entries".equals(key)) {
094                readDocumentEntries(jp, docs);
095            }
096            tok = jp.nextToken();
097        }
098        if (tok == null) {
099            throw new IllegalArgumentException("Unexpected end of stream.");
100        }
101        return docs;
102    }
103
104    @Override
105    public Documents read(JsonParser jp) throws IOException {
106        jp.nextToken();
107        String key = jp.getCurrentName();
108        if ("isPaginable".equals(key)) {
109            jp.nextToken();
110            boolean isPaginable = jp.getBooleanValue();
111            if (isPaginable) {
112                jp.nextToken();
113                return readPaginableDocuments(jp);
114            }
115            jp.nextToken();
116        }
117        return readDocuments(jp);
118    }
119
120}