001/*
002 *  Copyright 2016 Anyware Services
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
017package org.ametys.plugins.workspaces.members;
018
019import javax.jcr.Node;
020
021import org.ametys.core.group.GroupIdentity;
022import org.ametys.core.user.UserIdentity;
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.plugins.repository.data.ametysobject.ModifiableModelLessDataAwareAmetysObject;
025import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
026import org.ametys.plugins.repository.data.holder.group.ModelLessComposite;
027import org.ametys.plugins.repository.data.holder.group.ModifiableModelLessComposite;
028import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelLessDataHolder;
029import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
030import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
031import org.ametys.plugins.repository.jcr.DefaultAmetysObject;
032
033/**
034 * Class representing of project's member, backed by a JCR node.
035 */
036public class JCRProjectMember extends DefaultAmetysObject<JCRProjectMemberFactory> implements ModifiableModelLessDataAwareAmetysObject
037{
038    private static final String METADATA_USER = "user";
039    private static final String METADATA_GROUP = "group";
040    private static final String METADATA_GROUP_DIRECTORY = "groupDirectory";
041    private static final String METADATA_GROUP_ID = "groupId";
042    private static final String METADATA_TYPE = "type";
043    private static final String METADATA_ROLE = "role";
044    
045    /** The type of the member */
046    public enum MemberType 
047    {
048        /** The member is a user */
049        USER,
050        /** The member is a group */
051        GROUP
052    }
053    
054    /**
055     * Creates a {@link JCRProjectMember}.
056     * @param node the node backing this {@link AmetysObject}
057     * @param parentPath the parentPath in the Ametys hierarchy
058     * @param factory the ProjectUserFactory which created the AmetysObject
059     */
060    public JCRProjectMember(Node node, String parentPath, JCRProjectMemberFactory factory)
061    {
062        super(node, parentPath, factory);
063    }
064
065    public ModifiableModelLessDataHolder getDataHolder()
066    {
067        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
068        return new DefaultModifiableModelLessDataHolder(_getFactory().getProjectDataTypeExtensionPoint(), repositoryData);
069    }
070
071    /**
072     * Get the type of the member. It can be a user or a group
073     * @return The type of the member
074     */
075    public MemberType getType()
076    {
077        String value = getValue(METADATA_TYPE, MemberType.USER.name());
078        return MemberType.valueOf(value.toUpperCase());
079    }
080    
081    /**
082     * Set the type of the member. It can be a user or a group
083     * @param type The type of the member
084     */
085    public void setType(MemberType type)
086    {
087        this.setValue(METADATA_TYPE, type.name().toLowerCase());
088    }
089
090    /**
091     * Get the user identity of the project member
092     * @return The user identity
093     */
094    public UserIdentity getUser()
095    {
096        return this.getValue(METADATA_USER);
097    }
098    
099    /**
100     * Set the user identity
101     * @param userIdentity The user identity
102     */
103    public void setUser(UserIdentity userIdentity)
104    {
105        this.setValue(METADATA_USER, userIdentity);
106    }
107    
108    /**
109     * Get the group identity of the project member
110     * @return The group identity
111     */
112    public GroupIdentity getGroup()
113    {
114        ModelLessComposite group = this.getComposite(METADATA_GROUP);
115        if (group != null)
116        {
117            return new GroupIdentity(group.getValue(METADATA_GROUP_ID), group.getValue(METADATA_GROUP_DIRECTORY));
118        }
119        else
120        {
121            return null;
122        }
123    }
124    
125    /**
126     * Set the group identity
127     * @param groupIdentity The group identity
128     */
129    public void setGroup(GroupIdentity groupIdentity)
130    {
131        ModifiableModelLessComposite group = getComposite(METADATA_GROUP, true);
132        group.setValue(METADATA_GROUP_DIRECTORY, groupIdentity.getDirectoryId());
133        group.setValue(METADATA_GROUP_ID, groupIdentity.getId());
134    }
135    
136    /**
137     * Get the role of this project member
138     * @return The role
139     */
140    public String getRole()
141    {
142        return this.getValue(METADATA_ROLE);
143    }
144    
145    /**
146     * Set the role of this project member. Can be null
147     * @param role The new role
148     */
149    public void setRole(String role)
150    {
151        this.setValue(METADATA_ROLE, role);
152    }
153    
154}