001/*
002 * Copyright (c) 2006-2012 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 *     Nuxeo
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
013
014import java.io.IOException;
015import java.io.Serializable;
016import java.util.Map;
017
018import org.codehaus.jackson.JsonGenerator;
019import org.codehaus.jackson.JsonParser;
020import org.codehaus.jackson.JsonToken;
021import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
022import org.nuxeo.ecm.automation.client.model.RecordSet;
023
024/**
025 * Manage JSON Decoding of RecordSet object returned by QueryAndFetch
026 *
027 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
028 * @since 5.7
029 */
030public class RecordSetMarshaller implements JsonMarshaller<RecordSet> {
031
032    @Override
033    public String getType() {
034        return "recordSet";
035    }
036
037    @Override
038    public Class<RecordSet> getJavaType() {
039        return RecordSet.class;
040    }
041
042    @Override
043    public RecordSet read(JsonParser jp) throws IOException {
044        jp.nextToken();
045        String key = jp.getCurrentName();
046        if ("isPaginable".equals(key)) {
047            jp.nextToken();
048            boolean isPaginable = jp.getBooleanValue();
049            if (isPaginable) {
050                jp.nextToken();
051                return readPaginableRecordSet(jp);
052            }
053        }
054        return readRecord(jp);
055    }
056
057    protected RecordSet readPaginableRecordSet(JsonParser jp) throws IOException {
058        RecordSet record = new RecordSet();
059        JsonToken tok = jp.getCurrentToken();
060        while (tok != null && tok != JsonToken.END_OBJECT) {
061            String key = jp.getCurrentName();
062            jp.nextToken();
063            if ("pageSize".equals(key)) {
064                record.setPageSize(jp.getIntValue());
065            } else if ("numberOfPages".equals(key)) {
066                record.setNumberOfPages(jp.getIntValue());
067            } else if ("currentPageIndex".equals(key)) {
068                record.setCurrentPageIndex(jp.getIntValue());
069            } else if ("entries".equals(key)) {
070                readRecordEntries(jp, record);
071            }
072            tok = jp.nextToken();
073        }
074        if (tok == null) {
075            throw new IllegalArgumentException("Unexpected end of stream.");
076        }
077        return record;
078    }
079
080    protected RecordSet readRecord(JsonParser jp) throws IOException {
081        RecordSet record = new RecordSet();
082        JsonToken tok = jp.nextToken();
083        while (tok != JsonToken.END_ARRAY) {
084            String key = jp.getCurrentName();
085            if ("entries".equals(key)) {
086                readRecordEntries(jp, record);
087                return record;
088            }
089            tok = jp.nextToken();
090        }
091        return record;
092    }
093
094    protected void readRecordEntries(JsonParser jp, RecordSet record) throws IOException {
095        JsonToken tok = jp.nextToken();
096        while (tok != JsonToken.END_ARRAY) {
097            @SuppressWarnings("unchecked")
098            Map<String, Serializable> entry = jp.readValueAs(Map.class);
099            record.add(entry);
100            tok = jp.nextToken();
101        }
102    }
103
104    @Override
105    public void write(JsonGenerator jg, Object value) throws IOException {
106    }
107
108}