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.data.holder.ModifiableIndexableDataHolder; 023import org.ametys.cms.data.holder.impl.DefaultModifiableModelAwareDataHolder; 024import org.ametys.cms.repository.ModelAwareJCRAmetysObject; 025import org.ametys.plugins.repository.AmetysObject; 026import org.ametys.plugins.repository.AmetysRepositoryException; 027import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData; 028import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData; 029import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint; 030import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObjectFactory; 031import org.ametys.plugins.repository.jcr.SimpleAmetysObject; 032import org.ametys.plugins.repository.model.RepeaterDefinition; 033import org.ametys.runtime.model.DefaultElementDefinition; 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 /** Valid values data name */ 052 public static final String AUTOSAVE_VALUES = "values"; 053 /** Invalid values data name */ 054 public static final String AUTOSAVE_INVALID_VALUES = "invalid"; 055 /** Repeaters data name */ 056 public static final String AUTOSAVE_REPEATERS = "repeaters"; 057 058 /** Attribute name data name for values (valid and invalid) data */ 059 public static final String ATTRIBUTE_NAME = "name"; 060 /** Attribute value data name for values (valid and invalid) data */ 061 public static final String ATTRIBUTE_VALUE = "value"; 062 063 /** Repeater name data name */ 064 public static final String REPEATER_NAME = "name"; 065 /** Repeater count data name */ 066 public static final String REPEATER_COUNT = "count"; 067 /** Repeater prefix data name */ 068 public static final String REPEATER_PREFIX = "prefix"; 069 070 private ModifiableIndexableDataHolder _dataHolder; 071 072 /** 073 * Creates a DefaultTraversableAmetysObject. 074 * @param node the node backing this {@link AmetysObject}. 075 * @param parentPath the parent path in the Ametys hierarchy. 076 * @param factory the {@link DefaultTraversableAmetysObjectFactory} which creates the AmetysObject. 077 * @throws AmetysRepositoryException if an error occurs. 078 */ 079 public ContentBackupAmetysObject(Node node, String parentPath, ContentBackupAmetysObjectFactory factory) throws AmetysRepositoryException 080 { 081 super(node, parentPath, factory); 082 083 // Initialize Model and data holder 084 ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode()); 085 _dataHolder = new DefaultModifiableModelAwareDataHolder(repositoryData, _createContentBackupModel()); 086 } 087 088 /** 089 * Creates the content backup model 090 * @return the created model 091 * @throws AmetysRepositoryException if an error occurs. 092 */ 093 protected Model _createContentBackupModel() throws AmetysRepositoryException 094 { 095 try 096 { 097 String role = ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC; 098 return Model.of(ContentBackupClientInteraction.class.getName(), ContentBackupClientInteraction.class.getName(), 099 DefaultElementDefinition.of(AUTOSAVE_CONTENT_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, role), 100 DefaultElementDefinition.of(AUTOSAVE_CREATOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role), 101 DefaultElementDefinition.of(AUTOSAVE_TEMP_DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role), 102 RepeaterDefinition.of(AUTOSAVE_REPEATERS, role, 103 DefaultElementDefinition.of(REPEATER_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role), 104 DefaultElementDefinition.of(REPEATER_PREFIX, false, ModelItemTypeConstants.STRING_TYPE_ID, role), 105 DefaultElementDefinition.of(REPEATER_COUNT, false, ModelItemTypeConstants.STRING_TYPE_ID, role)), 106 RepeaterDefinition.of(AUTOSAVE_VALUES, role, 107 DefaultElementDefinition.of(ATTRIBUTE_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role), 108 DefaultElementDefinition.of(ATTRIBUTE_VALUE, false, ModelItemTypeConstants.STRING_TYPE_ID, role)), 109 RepeaterDefinition.of(AUTOSAVE_INVALID_VALUES, role, 110 DefaultElementDefinition.of(ATTRIBUTE_NAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role), 111 DefaultElementDefinition.of(ATTRIBUTE_VALUE, false, ModelItemTypeConstants.STRING_TYPE_ID, role))); 112 } 113 catch (UnknownTypeException | BadItemTypeException | ServiceException e) 114 { 115 throw new AmetysRepositoryException("An error occurred while create the content backup model", e); 116 } 117 } 118 119 public ModifiableIndexableDataHolder getDataHolder() 120 { 121 return _dataHolder; 122 } 123}