001/*
002 * (C) Copyright 2015 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 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.platform.usermanager.io;
019
020import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
021import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
022import static org.nuxeo.ecm.platform.usermanager.io.NuxeoGroupJsonWriter.ENTITY_TYPE;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.Iterator;
027import java.util.List;
028
029import javax.inject.Inject;
030
031import org.codehaus.jackson.JsonNode;
032import org.nuxeo.ecm.core.api.NuxeoGroup;
033import org.nuxeo.ecm.core.api.impl.NuxeoGroupImpl;
034import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
035import org.nuxeo.ecm.core.io.registry.reflect.Setup;
036import org.nuxeo.ecm.platform.usermanager.UserManager;
037
038/**
039 * Convert Json as {@link NuxeoGroup}.
040 * <p>
041 * Format is (any additional json property is ignored):
042 *
043 * <pre>
044 * {
045 *   "entity-type":"group",
046 *   "groupname": "GROUP_NAME",
047 *   "grouplabel": "GROUP_DISPLAY_NAME",
048 *   "memberUsers": [
049 *     "USERNAME1",
050 *     "USERNAME2",
051 *     ...
052 *   ],
053 *   "memberGroups": [
054 *     "GROUPNAME1",
055 *     "GROUPNAME2",
056 *     ...
057 *   ]
058 * }
059 * </pre>
060 *
061 * </p>
062 *
063 * @since 7.2
064 */
065@Setup(mode = SINGLETON, priority = REFERENCE)
066public class NuxeoGroupJsonReader extends EntityJsonReader<NuxeoGroup> {
067
068    @Inject
069    private UserManager userManager;
070
071    public NuxeoGroupJsonReader() {
072        super(ENTITY_TYPE);
073    }
074
075    @Override
076    protected NuxeoGroup readEntity(JsonNode jn) throws IOException {
077        NuxeoGroup group = null;
078        String id = getStringField(jn, "groupname");
079        if (id != null) {
080            group = userManager.getGroup(id);
081        }
082        if (group == null) {
083            group = new NuxeoGroupImpl(id);
084        }
085        String label = getStringField(jn, "grouplabel");
086        group.setLabel(label);
087        List<String> users = getArrayStringValues(jn.get("memberUsers"));
088        group.setMemberUsers(users);
089        List<String> groups = getArrayStringValues(jn.get("memberGroups"));
090        group.setMemberGroups(groups);
091        return group;
092    }
093
094    private List<String> getArrayStringValues(JsonNode node) {
095        List<String> values = new ArrayList<>();
096        if (node != null && !node.isNull() && node.isArray()) {
097            JsonNode elNode = null;
098            Iterator<JsonNode> it = node.getElements();
099            while (it.hasNext()) {
100                elNode = it.next();
101                if (elNode != null && !elNode.isNull() && elNode.isTextual()) {
102                    values.add(elNode.getTextValue());
103                }
104            }
105        }
106        return values;
107    }
108
109}