001/*
002 * (C) Copyright 2006-2007 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 *     Thierry Delprat
018 * *
019 */
020
021package org.nuxeo.ecm.platform.computedgroups;
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel;
028import org.nuxeo.ecm.platform.usermanager.GroupConfig;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Computed group implementation class. Delegates part of the implementation logic to the {@link ComputedGroupsService}
033 * that is pluggable.
034 *
035 * @author Thierry Delprat
036 */
037public class NuxeoComputedGroup implements ComputedGroup {
038
039    private static final long serialVersionUID = 1L;
040
041    protected List<String> members;
042
043    protected List<String> subGroups;
044
045    protected List<String> parents;
046
047    protected DocumentModel model;
048
049    protected GroupConfig config = GroupConfig.DEFAULT;
050
051    public NuxeoComputedGroup(String name, GroupConfig config) {
052        this(name, null, config);
053    }
054
055    public NuxeoComputedGroup(String name, String label, GroupConfig config) {
056        this.config = config;
057        model = new SimpleDocumentModel();
058        model.setProperty(config.schemaName, config.idField, name);
059        model.setProperty(config.schemaName, config.labelField, label);
060    }
061
062    /**
063     * @deprecated since 9.3. Use {@link #NuxeoComputedGroup(String, GroupConfig)}.
064     */
065    @Deprecated
066    public NuxeoComputedGroup(String name) {
067        this(name, GroupConfig.DEFAULT);
068    }
069
070    /**
071     * @deprecated since 9.3. Use {@link #NuxeoComputedGroup(String, String, GroupConfig)}.
072     */
073    @Deprecated
074    public NuxeoComputedGroup(String name, String label) {
075        this(name, label, GroupConfig.DEFAULT);
076    }
077
078    @SuppressWarnings("unchecked")
079    @Override
080    public List<String> getMemberUsers() {
081        if (members == null) {
082            ComputedGroupsService cgs = Framework.getService(ComputedGroupsService.class);
083            if (cgs != null) {
084                members = cgs.getComputedGroupMembers(getName());
085            }
086            if (members == null) {
087                members = new ArrayList<>();
088            }
089            model.setProperty(config.schemaName, config.membersField, members);
090        }
091        return members;
092    }
093
094    @Override
095    public String getName() {
096        return (String) model.getProperty(config.schemaName, config.idField);
097    }
098
099    @Override
100    public String getLabel() {
101        String label = (String) model.getProperty(config.schemaName, config.labelField);
102        return label == null ? getName() : label;
103    }
104
105    @SuppressWarnings("unchecked")
106    @Override
107    public List<String> getParentGroups() {
108        if (parents == null) {
109            ComputedGroupsService cgs = Framework.getService(ComputedGroupsService.class);
110            if (cgs != null) {
111                parents = cgs.getComputedGroupParent(getName());
112            }
113            if (parents == null) {
114                parents = new ArrayList<>();
115            }
116            model.setProperty(config.schemaName, config.parentGroupsField, parents);
117        }
118        return parents;
119    }
120
121    @SuppressWarnings("unchecked")
122    @Override
123    public List<String> getMemberGroups() {
124        if (subGroups == null) {
125            ComputedGroupsService cgs = Framework.getService(ComputedGroupsService.class);
126            if (cgs != null) {
127                subGroups = cgs.getComputedGroupSubGroups(getName());
128            }
129            if (subGroups == null) {
130                subGroups = new ArrayList<>();
131            }
132            model.setProperty(config.schemaName, config.subGroupsField, subGroups);
133        }
134        return subGroups;
135    }
136
137    @Override
138    public void setMemberGroups(List<String> groups) {
139        throw new UnsupportedOperationException("Computed groups are read only");
140    }
141
142    @Override
143    public void setMemberUsers(List<String> users) {
144        throw new UnsupportedOperationException("Computed groups are read only");
145    }
146
147    @Override
148    public void setName(String name) {
149        throw new UnsupportedOperationException("Computed groups are read only");
150    }
151
152    @Override
153    public void setLabel(String label) {
154        throw new UnsupportedOperationException("Computed groups are read only");
155    }
156
157    @Override
158    public void setParentGroups(List<String> groups) {
159        throw new UnsupportedOperationException("Computed groups are read only");
160    }
161
162    @Override
163    public DocumentModel getModel() {
164        return model;
165    }
166}