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.runtime.api.Framework;
027
028/**
029 * Computed group implementation class. Delegates part of the implementation logic to the {@link ComputedGroupsService}
030 * that is pluggable.
031 *
032 * @author Thierry Delprat
033 */
034public class NuxeoComputedGroup implements ComputedGroup {
035
036    private static final long serialVersionUID = 1L;
037
038    protected String name;
039
040    protected List<String> parents;
041
042    protected List<String> subGroups;
043
044    protected List<String> members;
045
046    private String label;
047
048    public NuxeoComputedGroup(String name) {
049        this(name, null);
050    }
051
052    public NuxeoComputedGroup(String name, String label) {
053        this.name = name;
054        this.label = label;
055    }
056
057    @Override
058    public List<String> getMemberUsers() {
059        if (members == null) {
060            ComputedGroupsService cgs = Framework.getLocalService(ComputedGroupsService.class);
061            if (cgs != null) {
062                members = cgs.getComputedGroupMembers(name);
063            }
064            if (members == null) {
065                members = new ArrayList<String>();
066            }
067        }
068        return members;
069    }
070
071    @Override
072    public String getName() {
073        return name;
074    }
075
076    @Override
077    public String getLabel() {
078        if (label != null) {
079            return label;
080        }
081        return getName();
082    }
083
084    @Override
085    public List<String> getParentGroups() {
086        if (parents == null) {
087            ComputedGroupsService cgs = Framework.getLocalService(ComputedGroupsService.class);
088            if (cgs != null) {
089                parents = cgs.getComputedGroupParent(name);
090            }
091            if (parents == null) {
092                parents = new ArrayList<String>();
093            }
094        }
095        return parents;
096    }
097
098    @Override
099    public List<String> getMemberGroups() {
100        if (subGroups == null) {
101            ComputedGroupsService cgs = Framework.getLocalService(ComputedGroupsService.class);
102            if (cgs != null) {
103                subGroups = cgs.getComputedGroupSubGroups(name);
104            }
105            if (subGroups == null) {
106                subGroups = new ArrayList<String>();
107            }
108        }
109        return subGroups;
110    }
111
112    @Override
113    public void setMemberGroups(List<String> groups) {
114        throw new UnsupportedOperationException("Computed groups are read only");
115    }
116
117    @Override
118    public void setMemberUsers(List<String> users) {
119        throw new UnsupportedOperationException("Computed groups are read only");
120    }
121
122    @Override
123    public void setName(String name) {
124        throw new UnsupportedOperationException("Computed groups are read only");
125    }
126
127    @Override
128    public void setLabel(String label) {
129        throw new UnsupportedOperationException("Computed groups are read only");
130    }
131
132    @Override
133    public void setParentGroups(List<String> groups) {
134        throw new UnsupportedOperationException("Computed groups are read only");
135    }
136
137}