/**
 * Copyright (C) 2013
 * by 52 North Initiative for Geospatial Open Source Software GmbH
 *
 * Contact: Andreas Wytzisk
 * 52 North Initiative for Geospatial Open Source Software GmbH
 * Martin-Luther-King-Weg 24
 * 48155 Muenster, Germany
 * info@52north.org
 *
 * This program is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed WITHOUT ANY WARRANTY; even without the implied
 * WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program (see gnu-gpl v2.txt). If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or
 * visit the Free Software Foundation web page, http://www.fsf.org.
 */
package org.n52.sos.encode.json.impl;

import org.n52.sos.coding.json.JSONConstants;
import org.n52.sos.encode.json.AbstractSosResponseEncoder;
import org.n52.sos.ogc.ows.OwsExceptionReport;
import org.n52.sos.response.BatchResponse;
import org.n52.sos.response.BatchResponse.ExceptionOrResponse;
import org.n52.sos.util.BatchConstants;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * TODO JavaDoc
 * 
 * @author Christian Autermann <c.autermann@52north.org>
 * 
 * @since 4.0.0
 */
public class BatchResponseEncoder extends AbstractSosResponseEncoder<BatchResponse> {
    public BatchResponseEncoder() {
        super(BatchResponse.class, BatchConstants.OPERATION_NAME);
    }

    @Override
    protected void encodeResponse(ObjectNode json, BatchResponse response) throws OwsExceptionReport {
        ArrayNode responses = json.putArray(JSONConstants.RESPONSES);
        for (ExceptionOrResponse eor : response) {
            if (eor.isException()) {
                responses.add(encodeObjectToJson(eor.getException()));
            } else {
                responses.add(encodeObjectToJson(eor.getResponse()));
            }

        }
    }
}
