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    /** metadata name for the user identity */
039    public static final String METADATA_USER = "user";
040    /** metadata name for the group identity composite */
041    public static final String METADATA_GROUP = "group";
042    /** metadata name for the group directory property */
043    public static final String METADATA_GROUP_DIRECTORY = "groupDirectory";
044    /** metadata name for the group id property */
045    public static final String METADATA_GROUP_ID = "groupId";
046    private static final String METADATA_TYPE = "type";
047    private static final String METADATA_ROLE = "role";
048    
049    /** The type of the member */
050    public enum MemberType
051    {
052        /** The member is a user */
053        USER,
054        /** The member is a group */
055        GROUP
056    }
057    
058    /**
059     * Creates a {@link JCRProjectMember}.
060     * @param node the node backing this {@link AmetysObject}
061     * @param parentPath the parentPath in the Ametys hierarchy
062     * @param factory the ProjectUserFactory which created the AmetysObject
063     */
064    public JCRProjectMember(Node node, String parentPath, JCRProjectMemberFactory factory)
065    {
066        super(node, parentPath, factory);
067    }
068
069    public ModifiableModelLessDataHolder getDataHolder()
070    {
071        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
072        return new DefaultModifiableModelLessDataHolder(_getFactory().getProjectDataTypeExtensionPoint(), repositoryData);
073    }
074
075    /**
076     * Get the type of the member. It can be a user or a group
077     * @return The type of the member
078     */
079    public MemberType getType()
080    {
081        String value = getValue(METADATA_TYPE, MemberType.USER.name());
082        return MemberType.valueOf(value.toUpperCase());
083    }
084    
085    /**
086     * Set the type of the member. It can be a user or a group
087     * @param type The type of the member
088     */
089    public void setType(MemberType type)
090    {
091        this.setValue(METADATA_TYPE, type.name().toLowerCase());
092    }
093
094    /**
095     * Get the user identity of the project member
096     * @return The user identity
097     */
098    public UserIdentity getUser()
099    {
100        return this.getValue(METADATA_USER);
101    }
102    
103    /**
104     * Set the user identity
105     * @param userIdentity The user identity
106     */
107    public void setUser(UserIdentity userIdentity)
108    {
109        this.setValue(METADATA_USER, userIdentity);
110    }
111    
112    /**
113     * Get the group identity of the project member
114     * @return The group identity
115     */
116    public GroupIdentity getGroup()
117    {
118        ModelLessComposite group = this.getComposite(METADATA_GROUP);
119        if (group != null)
120        {
121            return new GroupIdentity(group.getValue(METADATA_GROUP_ID), group.getValue(METADATA_GROUP_DIRECTORY));
122        }
123        else
124        {
125            return null;
126        }
127    }
128    
129    /**
130     * Set the group identity
131     * @param groupIdentity The group identity
132     */
133    public void setGroup(GroupIdentity groupIdentity)
134    {
135        ModifiableModelLessComposite group = getComposite(METADATA_GROUP, true);
136        group.setValue(METADATA_GROUP_DIRECTORY, groupIdentity.getDirectoryId());
137        group.setValue(METADATA_GROUP_ID, groupIdentity.getId());
138    }
139    
140    /**
141     * Get the role of this project member
142     * @return The role
143     */
144    public String getRole()
145    {
146        return this.getValue(METADATA_ROLE);
147    }
148    
149    /**
150     * Set the role of this project member. Can be null
151     * @param role The new role
152     */
153    public void setRole(String role)
154    {
155        this.setValue(METADATA_ROLE, role);
156    }
157    
158}