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