001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.jaxrs.io.usermanager;
018
019import java.io.IOException;
020
021import javax.ws.rs.Produces;
022import javax.ws.rs.ext.Provider;
023
024import org.codehaus.jackson.JsonGenerationException;
025import org.codehaus.jackson.JsonGenerator;
026import org.nuxeo.ecm.automation.jaxrs.io.EntityWriter;
027import org.nuxeo.ecm.core.api.NuxeoGroup;
028import org.nuxeo.ecm.platform.usermanager.io.NuxeoGroupJsonWriter;
029import org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate;
030
031/**
032 * @since 5.7.3
033 * @deprecated since 7.10 The Nuxeo JSON marshalling was migrated to nuxeo-core-io. This class is replaced by
034 *             {@link NuxeoGroupJsonWriter} which is registered by default and available to marshal {@link NuxeoGroup}
035 *             from the Nuxeo Rest API thanks to the JAX-RS marshaller {@link JsonCoreIODelegate}
036 */
037@Deprecated
038@Provider
039@Produces({ "application/json+nxentity", "application/json" })
040public class NuxeoGroupWriter extends EntityWriter<NuxeoGroup> {
041
042    /**
043     *
044     */
045    public static final String ENTITY_TYPE = "group";
046
047    /**
048     * @param createGenerator
049     * @param group
050     * @return
051     * @throws IOException
052     * @throws JsonGenerationException
053     */
054    @Override
055    public void writeEntityBody(JsonGenerator jg, NuxeoGroup group) throws JsonGenerationException,
056            IOException {
057        jg.writeStringField("groupname", group.getName());
058
059        jg.writeStringField("grouplabel", group.getLabel());
060
061        jg.writeArrayFieldStart("memberUsers");
062        for (String user : group.getMemberUsers()) {
063            jg.writeString(user);
064        }
065        jg.writeEndArray();
066
067        jg.writeArrayFieldStart("memberGroups");
068        for (String user : group.getMemberGroups()) {
069            jg.writeString(user);
070        }
071        jg.writeEndArray();
072
073    }
074
075    @Override
076    protected String getEntityType() {
077        return ENTITY_TYPE;
078    }
079
080}