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