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 javax.jcr.Node;
019
020import org.apache.avalon.framework.service.ServiceException;
021
022import org.ametys.cms.model.ModelAwareBasicTypesExtensionPoint;
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.plugins.repository.AmetysRepositoryException;
025import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
026import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelAwareDataHolder;
027import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
028import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
029import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObjectFactory;
030import org.ametys.plugins.repository.jcr.ModelAwareJCRAmetysObject;
031import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
032import org.ametys.plugins.repository.model.RepeaterDefinition;
033import org.ametys.runtime.model.ElementDefinition;
034import org.ametys.runtime.model.Model;
035import org.ametys.runtime.model.exception.BadItemTypeException;
036import org.ametys.runtime.model.exception.UnknownTypeException;
037import org.ametys.runtime.model.type.ModelItemTypeConstants;
038
039/**
040 * Implementation of a {@link ModelAwareJCRAmetysObject}, for content backup.<br>
041 * This implementation heavily relies on its {@link ContentBackupAmetysObjectFactory} counterpart.
042 */
043public class ContentBackupAmetysObject extends SimpleAmetysObject<ContentBackupAmetysObjectFactory> implements ModelAwareJCRAmetysObject
044{
045    /** Content identifier data name */
046    public static final String AUTOSAVE_CONTENT_ID = "contentId";
047    /** Creator data name */
048    public static final String AUTOSAVE_CREATOR = "creator";
049    /** Temporary date data name */
050    public static final String AUTOSAVE_TEMP_DATE = "tempContentDate";
051    /** Comments data name */
052    public static final String AUTOSAVE_COMMENTS = "jsonComments";
053    /** Valid values data name */
054    public static final String AUTOSAVE_VALUES = "values";
055    /** Invalid values data name */
056    public static final String AUTOSAVE_INVALID_VALUES = "invalid";
057    /** Repeaters data name */
058    public static final String AUTOSAVE_REPEATERS = "repeaters";
059    
060    /** Attribute name data name for values (valid and invalid) data */
061    public static final String ATTRIBUTE_NAME = "name";
062    /** Attribute value data name for values (valid and invalid) data */
063    public static final String ATTRIBUTE_VALUE = "value";
064    
065    /** Repeater name data name */
066    public static final String REPEATER_NAME = "name";
067    /** Repeater count data name */
068    public static final String REPEATER_COUNT = "count";
069    /** Repeater prefix data name */
070    public static final String REPEATER_PREFIX = "prefix";
071    
072    private ModifiableModelAwareDataHolder _dataHolder;
073
074    /**
075     * Creates a DefaultTraversableAmetysObject.
076     * @param node the node backing this {@link AmetysObject}.
077     * @param parentPath the parent path in the Ametys hierarchy.
078     * @param factory the {@link DefaultTraversableAmetysObjectFactory} which creates the AmetysObject.
079     * @throws AmetysRepositoryException if an error occurs.
080     */
081    public ContentBackupAmetysObject(Node node, String parentPath, ContentBackupAmetysObjectFactory factory) throws AmetysRepositoryException
082    {
083        super(node, parentPath, factory);
084
085        // Initialize Model and data holder
086        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
087        _dataHolder = new DefaultModifiableModelAwareDataHolder(repositoryData, _createContentBackupModel());
088    }
089    
090    /**
091     * Creates the content backup model
092     * @return the created model
093     * @throws AmetysRepositoryException if an error occurs.
094     */
095    protected Model _createContentBackupModel() throws AmetysRepositoryException
096    {
097        try
098        {
099            String role = ModelAwareBasicTypesExtensionPoint.ROLE;
100            return Model.of(ContentBackupClientInteraction.class.getName(), ContentBackupClientInteraction.class.getName(),
101                    ElementDefinition.of(AUTOSAVE_CONTENT_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
102                    ElementDefinition.of(AUTOSAVE_CREATOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
103                    ElementDefinition.of(AUTOSAVE_TEMP_DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
104                    ElementDefinition.of(AUTOSAVE_COMMENTS, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
105                    RepeaterDefinition.of(AUTOSAVE_REPEATERS, role,
106                            ElementDefinition.of(REPEATER_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
107                            ElementDefinition.of(REPEATER_PREFIX, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
108                            ElementDefinition.of(REPEATER_COUNT, false, ModelItemTypeConstants.STRING_TYPE_ID, role)),
109                    RepeaterDefinition.of(AUTOSAVE_VALUES, role,
110                            ElementDefinition.of(ATTRIBUTE_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
111                            ElementDefinition.of(ATTRIBUTE_VALUE, false, ModelItemTypeConstants.STRING_TYPE_ID, role)),
112                    RepeaterDefinition.of(AUTOSAVE_INVALID_VALUES, role,
113                            ElementDefinition.of(ATTRIBUTE_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
114                            ElementDefinition.of(ATTRIBUTE_VALUE, false, ModelItemTypeConstants.STRING_TYPE_ID, role)));
115        }
116        catch (UnknownTypeException | BadItemTypeException | ServiceException e)
117        {
118            throw new AmetysRepositoryException("An error occurred while create the content backup model", e);
119        }
120    }
121
122    public ModifiableModelAwareDataHolder getDataHolder()
123    {
124        return _dataHolder;
125    }
126}