001/*
002 *  Copyright 2019 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.cms.content.autosave;
017
018import java.util.Collection;
019import java.util.List;
020
021import javax.jcr.Node;
022
023import org.apache.avalon.framework.service.ServiceException;
024
025import org.ametys.cms.repository.SimpleIndexableAmetysObjectFactory;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
028import org.ametys.plugins.repository.jcr.SimpleAmetysObjectFactory;
029import org.ametys.plugins.repository.jcr.SimpleModelAwareAmetysObject;
030import org.ametys.plugins.repository.model.RepeaterDefinition;
031import org.ametys.runtime.model.DefaultElementDefinition;
032import org.ametys.runtime.model.Model;
033import org.ametys.runtime.model.exception.BadItemTypeException;
034import org.ametys.runtime.model.exception.UnknownTypeException;
035import org.ametys.runtime.model.type.ModelItemTypeConstants;
036
037/**
038 * Default implementation of a {@link SimpleAmetysObjectFactory},
039 * handling {@link ContentBackupAmetysObject}.
040 */
041public class ContentBackupAmetysObjectFactory extends SimpleIndexableAmetysObjectFactory
042{
043    private Model _contentBackupModel;
044    
045    @Override
046    public ContentBackupAmetysObject getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
047    {
048        return new ContentBackupAmetysObject(node, parentPath, this);
049    }
050    
051    @Override
052    public Collection<? extends Model> getModel(SimpleModelAwareAmetysObject ametysObject)
053    {
054        if (_contentBackupModel == null)
055        {
056            _contentBackupModel = createContentBackupModel();
057        }
058        
059        return List.of(_contentBackupModel);
060    }
061        
062    /**
063     * Creates the content backup model
064     * @return the created model
065     * @throws AmetysRepositoryException if an error occurs.
066     */
067    protected Model createContentBackupModel() throws AmetysRepositoryException
068    {
069        try
070        {
071            String role = ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC;
072            return Model.of(ContentBackupClientInteraction.class.getName(), ContentBackupClientInteraction.class.getName(),
073                    DefaultElementDefinition.of(ContentBackupAmetysObject.AUTOSAVE_CONTENT_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
074                    DefaultElementDefinition.of(ContentBackupAmetysObject.AUTOSAVE_CREATOR, false, ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
075                    DefaultElementDefinition.of(ContentBackupAmetysObject.AUTOSAVE_TEMP_DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
076                    RepeaterDefinition.of(ContentBackupAmetysObject.AUTOSAVE_REPEATERS, role,
077                            DefaultElementDefinition.of(ContentBackupAmetysObject.REPEATER_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
078                            DefaultElementDefinition.of(ContentBackupAmetysObject.REPEATER_PREFIX, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
079                            DefaultElementDefinition.of(ContentBackupAmetysObject.REPEATER_COUNT, false, ModelItemTypeConstants.STRING_TYPE_ID, role)),
080                    RepeaterDefinition.of(ContentBackupAmetysObject.AUTOSAVE_VALUES, role,
081                            DefaultElementDefinition.of(ContentBackupAmetysObject.ATTRIBUTE_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
082                            DefaultElementDefinition.of(ContentBackupAmetysObject.ATTRIBUTE_VALUE, false, ModelItemTypeConstants.STRING_TYPE_ID, role)),
083                    RepeaterDefinition.of(ContentBackupAmetysObject.AUTOSAVE_INVALID_VALUES, role,
084                            DefaultElementDefinition.of(ContentBackupAmetysObject.ATTRIBUTE_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
085                            DefaultElementDefinition.of(ContentBackupAmetysObject.ATTRIBUTE_VALUE, false, ModelItemTypeConstants.STRING_TYPE_ID, role)));
086        }
087        catch (UnknownTypeException | BadItemTypeException | ServiceException e)
088        {
089            throw new AmetysRepositoryException("An error occurred while create the content backup model", e);
090        }
091    }
092}