001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     matic
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
013
014import java.io.IOException;
015
016import org.codehaus.jackson.JsonGenerator;
017import org.codehaus.jackson.JsonParser;
018import org.codehaus.jackson.JsonToken;
019import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
020import org.nuxeo.ecm.automation.client.model.Documents;
021import org.nuxeo.ecm.automation.client.model.PaginableDocuments;
022
023/**
024 * @author matic
025 */
026public class DocumentsMarshaller implements JsonMarshaller<Documents> {
027
028    @Override
029    public String getType() {
030        return "documents";
031    }
032
033    @Override
034    public Class<Documents> getJavaType() {
035        return Documents.class;
036    }
037
038    @Override
039    public void write(JsonGenerator jg, Object value) throws IOException {
040        throw new UnsupportedOperationException();
041    }
042
043    protected void readDocumentEntries(JsonParser jp, Documents docs) throws IOException {
044        JsonToken tok = jp.nextToken();
045        while (tok != JsonToken.END_ARRAY) {
046            docs.add(DocumentMarshaller.readDocument(jp));
047            tok = jp.nextToken();
048        }
049    }
050
051    protected Documents readDocuments(JsonParser jp) throws IOException {
052        Documents docs = new Documents();
053        JsonToken tok = jp.nextToken();
054        while (tok != JsonToken.END_ARRAY) {
055            String key = jp.getCurrentName();
056            if ("entries".equals(key)) {
057                readDocumentEntries(jp, docs);
058                return docs;
059            }
060            tok = jp.nextToken();
061        }
062        return docs;
063    }
064
065    protected Documents readPaginableDocuments(JsonParser jp) throws IOException {
066        PaginableDocuments docs = new PaginableDocuments();
067        JsonToken tok = jp.getCurrentToken();
068        while (tok != null && tok != JsonToken.END_OBJECT) {
069            String key = jp.getCurrentName();
070            jp.nextToken();
071            if ("resultsCount".equals(key)) {
072                docs.setResultsCount(jp.getIntValue());
073            } else if ("totalSize".equals(key)) {
074                docs.setResultsCount(jp.getIntValue());
075            } else if ("pageSize".equals(key)) {
076                docs.setPageSize(jp.getIntValue());
077            } else if ("numberOfPages".equals(key)) {
078                docs.setNumberOfPages(jp.getIntValue());
079            } else if ("pageCount".equals(key)) {
080                docs.setNumberOfPages(jp.getIntValue());
081            } else if ("currentPageIndex".equals(key)) {
082                docs.setCurrentPageIndex(jp.getIntValue());
083            } else if ("pageIndex".equals(key)) {
084                docs.setCurrentPageIndex(jp.getIntValue());
085            } else if ("entries".equals(key)) {
086                readDocumentEntries(jp, docs);
087            }
088            tok = jp.nextToken();
089        }
090        if (tok == null) {
091            throw new IllegalArgumentException("Unexpected end of stream.");
092        }
093        return docs;
094    }
095
096    @Override
097    public Documents read(JsonParser jp) throws IOException {
098        jp.nextToken();
099        String key = jp.getCurrentName();
100        if ("isPaginable".equals(key)) {
101            jp.nextToken();
102            boolean isPaginable = jp.getBooleanValue();
103            if (isPaginable) {
104                jp.nextToken();
105                return readPaginableDocuments(jp);
106            }
107            jp.nextToken();
108        }
109        return readDocuments(jp);
110    }
111
112}