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