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;
028
029/**
030 * @since 5.7.3
031 */
032@Provider
033@Produces({ "application/json+nxentity", "application/json" })
034public class NuxeoGroupWriter extends EntityWriter<NuxeoGroup> {
035
036    /**
037     *
038     */
039    public static final String ENTITY_TYPE = "group";
040
041    /**
042     * @param createGenerator
043     * @param group
044     * @return
045     * @throws IOException
046     * @throws JsonGenerationException
047     */
048    @Override
049    public void writeEntityBody(JsonGenerator jg, NuxeoGroup group) throws JsonGenerationException,
050            IOException {
051        jg.writeStringField("groupname", group.getName());
052
053        jg.writeStringField("grouplabel", group.getLabel());
054
055        jg.writeArrayFieldStart("memberUsers");
056        for (String user : group.getMemberUsers()) {
057            jg.writeString(user);
058        }
059        jg.writeEndArray();
060
061        jg.writeArrayFieldStart("memberGroups");
062        for (String user : group.getMemberGroups()) {
063            jg.writeString(user);
064        }
065        jg.writeEndArray();
066
067    }
068
069    @Override
070    protected String getEntityType() {
071        return ENTITY_TYPE;
072    }
073
074}