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 */
016package org.ametys.plugins.workspaces.members;
017
018import java.util.Collection;
019import java.util.List;
020
021import javax.jcr.Node;
022
023import org.ametys.cms.repository.DefaultIndexableAmetysObjectFactory;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.repository.RepositoryConstants;
027import org.ametys.plugins.repository.jcr.SimpleModelAwareAmetysObject;
028import org.ametys.plugins.repository.model.CompositeDefinition;
029import org.ametys.plugins.workspaces.data.type.ModelItemTypeExtensionPoint;
030import org.ametys.runtime.model.DefaultElementDefinition;
031import org.ametys.runtime.model.Model;
032import org.ametys.runtime.model.type.ModelItemTypeConstants;
033
034/**
035 * {@link AmetysObjectFactory} for handling {@link JCRProjectMember}s.
036 */
037public class JCRProjectMemberFactory extends DefaultIndexableAmetysObjectFactory
038{
039    /** Node type for project member */
040    public static final String MEMBER_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":project-member";
041    
042    private Model _projectMemberModel;
043    
044    @Override
045    public JCRProjectMember getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
046    {
047        return new JCRProjectMember(node, parentPath, this);
048    }
049    
050    @Override
051    public Collection< ? extends Model> getModel(SimpleModelAwareAmetysObject ametysObject)
052    {
053        if (_projectMemberModel == null)
054        {
055            _projectMemberModel = createProjectMemberModel();
056        }
057
058        return List.of(_projectMemberModel);
059    }
060    
061    /**
062     * Creates the project member model
063     * @return the created project member model
064     */
065    protected Model createProjectMemberModel()
066    {
067        try
068        {
069            String role = ModelItemTypeExtensionPoint.ROLE_PROJECT;
070            return Model.of(
071                    "project.member.model.id", 
072                    "calendar.event.model.family.id",
073                    DefaultElementDefinition.of(JCRProjectMember.ATTRIBUTE_USER, false, ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
074                    DefaultElementDefinition.of(JCRProjectMember.ATTRIBUTE_TYPE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
075                    DefaultElementDefinition.of(JCRProjectMember.ATTRIBUTE_ROLE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
076                    CompositeDefinition.of(JCRProjectMember.ATTRIBUTE_GROUP, role,
077                            DefaultElementDefinition.of(JCRProjectMember.ATTRIBUTE_GROUP_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
078                            DefaultElementDefinition.of(JCRProjectMember.ATTRIBUTE_GROUP_DIRECTORY, false, ModelItemTypeConstants.STRING_TYPE_ID, role))
079                    );
080        }
081        catch (Exception e) 
082        {
083            getLogger().error("An error occurred getting the calendar resource model", e);
084            throw new RuntimeException("An error occurred getting the calendar resource model", e);
085        }
086    }
087}