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