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.jcr.DefaultAmetysObject;
025import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
026
027/**
028 * Class representing of project's member, backed by a JCR node.
029 */
030public class JCRProjectMember extends DefaultAmetysObject<JCRProjectMemberFactory>
031{
032    private static final String METADATA_USER = "user";
033    private static final String METADATA_GROUP = "group";
034    private static final String METADATA_GROUP_DIRECTORY = "groupDirectory";
035    private static final String METADATA_GROUP_ID = "groupId";
036    private static final String METADATA_TYPE = "type";
037    private static final String METADATA_ROLE = "role";
038    
039    /** The type of the member */
040    public enum MemberType 
041    {
042        /** The member is a user */
043        USER("user"),
044        /** The member is a group */
045        GROUP("group");
046        
047        private String _value;
048        
049        private MemberType(String value)
050        {
051            _value = value;
052        }
053        
054        @Override
055        public String toString()
056        {
057            return _value;
058        }
059    }
060    
061    /**
062     * Creates a {@link JCRProjectMember}.
063     * @param node the node backing this {@link AmetysObject}
064     * @param parentPath the parentPath in the Ametys hierarchy
065     * @param factory the ProjectUserFactory which created the AmetysObject
066     */
067    public JCRProjectMember(Node node, String parentPath, JCRProjectMemberFactory factory)
068    {
069        super(node, parentPath, factory);
070    }
071    
072    /**
073     * Get the type of the member. It can be a user or a group
074     * @return The type of the member
075     */
076    public String getType()
077    {
078        return this.getMetadataHolder().getString(METADATA_TYPE, MemberType.USER.toString());
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.getMetadataHolder().setMetadata(METADATA_TYPE, type.toString());
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.getMetadataHolder().getUser(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.getMetadataHolder().setMetadata(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        if (this.getMetadataHolder().hasMetadata(METADATA_GROUP))
115        {
116            ModifiableCompositeMetadata groupMetadata = this.getMetadataHolder().getCompositeMetadata(METADATA_GROUP);
117            return new GroupIdentity(groupMetadata.getString(METADATA_GROUP_ID), groupMetadata.getString(METADATA_GROUP_DIRECTORY));
118        }
119        return null;
120    }
121    
122    /**
123     * Set the group identity
124     * @param groupIdentity The group identity
125     */
126    public void setGroup(GroupIdentity groupIdentity)
127    {
128        ModifiableCompositeMetadata groupMetadata = this.getMetadataHolder().getCompositeMetadata(METADATA_GROUP, true);
129        groupMetadata.setMetadata(METADATA_GROUP_DIRECTORY, groupIdentity.getDirectoryId());
130        groupMetadata.setMetadata(METADATA_GROUP_ID, groupIdentity.getId());
131    }
132    
133    /**
134     * Get the role of this project member
135     * @return The role
136     */
137    public String getRole()
138    {
139        return this.getMetadataHolder().getString(METADATA_ROLE, null);
140    }
141    
142    /**
143     * Set the role of this project member. Can be null
144     * @param role The new role
145     */
146    public void setRole(String role)
147    {
148        this.getMetadataHolder().setMetadata(METADATA_ROLE, role);
149    }
150    
151}