001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.platform.usermanager;
022
023import java.util.List;
024import java.util.Objects;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.NuxeoGroup;
028import org.nuxeo.ecm.core.api.impl.DocumentModelImpl;
029import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel;
030
031/**
032 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
033 */
034public class NuxeoGroupImpl implements NuxeoGroup {
035
036    private static final long serialVersionUID = -69828664399387083L;
037
038    protected DocumentModel model;
039
040    protected GroupConfig config = GroupConfig.DEFAULT;
041
042    public NuxeoGroupImpl(String name) {
043        this(name, name);
044    }
045
046    public NuxeoGroupImpl(String name, String label) {
047        if (name == null) {
048            throw new IllegalArgumentException("group name cannot be null");
049        }
050        name = name.trim();
051        label = label == null ? null : label.trim();
052
053        model = new SimpleDocumentModel();
054        model.setProperty(config.schemaName, config.idField, name);
055        model.setProperty(config.schemaName, config.labelField, label);
056    }
057
058    public NuxeoGroupImpl(DocumentModel model, GroupConfig config) {
059        this.model = model;
060        this.config = config;
061    }
062
063    @SuppressWarnings("unchecked")
064    @Override
065    public List<String> getMemberUsers() {
066        return (List<String>) model.getProperty(config.schemaName, config.membersField);
067    }
068
069    @SuppressWarnings("unchecked")
070    @Override
071    public List<String> getMemberGroups() {
072        return (List<String>) model.getProperty(config.schemaName, config.subGroupsField);
073    }
074
075    @SuppressWarnings("unchecked")
076    @Override
077    public List<String> getParentGroups() {
078        return (List<String>) model.getProperty(config.schemaName, config.parentGroupsField);
079    }
080
081    @Override
082    public void setMemberUsers(List<String> users) {
083        if (users == null) {
084            throw new IllegalArgumentException("member users list cannot be null");
085        }
086        model.setProperty(config.schemaName, config.membersField, users);
087    }
088
089    @Override
090    public void setMemberGroups(List<String> groups) {
091        if (groups == null) {
092            throw new IllegalArgumentException("member groups list cannot be null");
093        }
094        model.setProperty(config.schemaName, config.subGroupsField, groups);
095    }
096
097    @Override
098    public void setParentGroups(List<String> groups) {
099        if (groups == null) {
100            throw new IllegalArgumentException("parent groups list cannot be null");
101        }
102        model.setProperty(config.schemaName, config.parentGroupsField, groups);
103    }
104
105    @Override
106    public String getName() {
107        return (String) model.getProperty(config.schemaName, config.idField);
108    }
109
110    @Override
111    public void setName(String name) {
112        model.setProperty(config.schemaName, config.idField, name);
113    }
114
115    @Override
116    public String getLabel() {
117        String label = (String) model.getProperty(config.schemaName, config.labelField);
118        return label == null ? getName() : label;
119    }
120
121    @Override
122    public void setLabel(String label) {
123        model.setProperty(config.schemaName, config.labelField, label);
124    }
125
126    @Override
127    public DocumentModel getModel() {
128        return model;
129    }
130
131    @Override
132    public boolean equals(Object other) {
133        if (other == this) {
134            return true;
135        }
136        if (other instanceof NuxeoGroupImpl) {
137            String name = getName();
138            String otherName = ((NuxeoGroupImpl) other).getName();
139            return Objects.equals(name, otherName);
140        }
141        return false;
142    }
143
144    @Override
145    public int hashCode() {
146        String name = getName();
147        return name == null ? 0 : name.hashCode();
148    }
149
150    @Override
151    public String toString() {
152        return getName();
153    }
154
155}