001/*
002 *  Copyright 2014 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.calendars.jcr;
017
018import javax.jcr.Node;
019
020import org.ametys.plugins.repository.AmetysObjectFactory;
021import org.ametys.plugins.repository.AmetysRepositoryException;
022import org.ametys.plugins.repository.RepositoryConstants;
023import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
024import org.ametys.plugins.repository.jcr.DefaultAmetysObjectFactory;
025import org.ametys.runtime.model.DefaultElementDefinition;
026import org.ametys.runtime.model.Model;
027import org.ametys.runtime.model.type.ModelItemTypeConstants;
028
029/**
030 * {@link AmetysObjectFactory} for handling {@link JCRCalendarEvent}s.
031 */
032public class JCRCalendarResourceFactory extends DefaultAmetysObjectFactory
033{
034    
035    /** JCR nodetype for calendar resources */
036    public static final String CALENDAR_RESOURCE_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":calendar-resource";
037    
038    /** The calendar resource model */
039    private Model _calendarResource;
040    
041    @Override
042    public JCRCalendarResource getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
043    {
044        return new JCRCalendarResource(node, parentPath, this);
045    }
046    
047    /**
048     * Get the calendar resource model
049     * @return the calendar resource model
050     */
051    public Model getCalendarResourceModel()
052    {
053        if (_calendarResource == null)
054        {
055            try
056            {
057                String role = ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC;
058                _calendarResource = Model.of(
059                        "calendar.resource.model.id", 
060                        "calendar.resource.model.family.id",
061                        DefaultElementDefinition.of(JCRCalendarResource.ATTRIBUTE_CALENDAR_RESOURCE_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
062                        DefaultElementDefinition.of(JCRCalendarResource.ATTRIBUTE_TITLE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
063                        DefaultElementDefinition.of(JCRCalendarResource.ATTRIBUTE_ICON, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
064                        DefaultElementDefinition.of(JCRCalendarResource.ATTRIBUTE_INSTRUCTIONS, false, ModelItemTypeConstants.STRING_TYPE_ID, role)
065                        );
066            }
067            catch (Exception e) 
068            {
069                getLogger().error("An error occurred getting the calendar resource model", e);
070                throw new RuntimeException("An error occurred getting the calendar resource model", e);
071            }
072        }
073        return _calendarResource;
074    }
075}