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.cms.repository.DefaultIndexableAmetysObject;
022import org.ametys.core.group.GroupIdentity;
023import org.ametys.core.user.UserIdentity;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.data.holder.group.ModelAwareComposite;
026import org.ametys.plugins.repository.data.holder.group.ModifiableModelAwareComposite;
027
028/**
029 * Class representing of project's member, backed by a JCR node.
030 */
031public class JCRProjectMember extends DefaultIndexableAmetysObject<JCRProjectMemberFactory>
032{
033    /** attribute name for the user identity */
034    public static final String ATTRIBUTE_USER = "user";
035    /** attribute name for the group identity composite */
036    public static final String ATTRIBUTE_GROUP = "group";
037    /** attribute name for the group directory property */
038    public static final String ATTRIBUTE_GROUP_DIRECTORY = "groupDirectory";
039    /** attribute name for the group id property */
040    public static final String ATTRIBUTE_GROUP_ID = "groupId";
041    /** attribute name for the member type */
042    public static final String ATTRIBUTE_TYPE = "type";
043    /** attribute name for the member role */
044    public static final String ATTRIBUTE_ROLE = "role";
045    
046    /** The type of the member */
047    public enum MemberType
048    {
049        /** The member is a user */
050        USER,
051        /** The member is a group */
052        GROUP
053    }
054    
055    /**
056     * Creates a {@link JCRProjectMember}.
057     * @param node the node backing this {@link AmetysObject}
058     * @param parentPath the parentPath in the Ametys hierarchy
059     * @param factory the ProjectUserFactory which created the AmetysObject
060     */
061    public JCRProjectMember(Node node, String parentPath, JCRProjectMemberFactory factory)
062    {
063        super(node, parentPath, factory);
064    }
065
066    /**
067     * Get the type of the member. It can be a user or a group
068     * @return The type of the member
069     */
070    public MemberType getType()
071    {
072        String value = getValueOrDefault(ATTRIBUTE_TYPE, MemberType.USER.name());
073        return MemberType.valueOf(value.toUpperCase());
074    }
075    
076    /**
077     * Set the type of the member. It can be a user or a group
078     * @param type The type of the member
079     */
080    public void setType(MemberType type)
081    {
082        this.setValue(ATTRIBUTE_TYPE, type.name().toLowerCase());
083    }
084
085    /**
086     * Get the user identity of the project member
087     * @return The user identity
088     */
089    public UserIdentity getUser()
090    {
091        return this.getValue(ATTRIBUTE_USER);
092    }
093    
094    /**
095     * Set the user identity
096     * @param userIdentity The user identity
097     */
098    public void setUser(UserIdentity userIdentity)
099    {
100        this.setValue(ATTRIBUTE_USER, userIdentity);
101    }
102    
103    /**
104     * Get the group identity of the project member
105     * @return The group identity
106     */
107    public GroupIdentity getGroup()
108    {
109        ModelAwareComposite group = this.getComposite(ATTRIBUTE_GROUP);
110        if (group != null)
111        {
112            return new GroupIdentity(group.getValue(ATTRIBUTE_GROUP_ID), group.getValue(ATTRIBUTE_GROUP_DIRECTORY));
113        }
114        else
115        {
116            return null;
117        }
118    }
119    
120    /**
121     * Set the group identity
122     * @param groupIdentity The group identity
123     */
124    public void setGroup(GroupIdentity groupIdentity)
125    {
126        ModifiableModelAwareComposite group = getComposite(ATTRIBUTE_GROUP, true);
127        group.setValue(ATTRIBUTE_GROUP_DIRECTORY, groupIdentity.getDirectoryId());
128        group.setValue(ATTRIBUTE_GROUP_ID, groupIdentity.getId());
129    }
130    
131    /**
132     * Get the role of this project member
133     * @return The role
134     */
135    public String getRole()
136    {
137        return this.getValue(ATTRIBUTE_ROLE);
138    }
139    
140    /**
141     * Set the role of this project member. Can be null
142     * @param role The new role
143     */
144    public void setRole(String role)
145    {
146        this.setValue(ATTRIBUTE_ROLE, role);
147    }
148    
149}