001/* 002 * Copyright 2020 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.repository.model; 017 018import java.util.HashSet; 019import java.util.Set; 020 021import org.ametys.runtime.model.ModelHelper; 022import org.ametys.runtime.model.type.DataContext; 023 024/** 025 * Object that gives some context for repository data manipulation 026 */ 027public class RepositoryDataContext extends DataContext 028{ 029 private Set<String> _externalizableData = new HashSet<>(); 030 031 /** 032 * Creates a new instance of a {@link RepositoryDataContext} 033 */ 034 protected RepositoryDataContext() 035 { 036 // Empty constructor 037 } 038 039 /** 040 * Creates a new instance of a {@link RepositoryDataContext} from another {@link DataContext} 041 * @param context the data context to copy 042 */ 043 protected RepositoryDataContext(DataContext context) 044 { 045 super(context); 046 if (context instanceof RepositoryDataContext repositoryContext) 047 { 048 withExternalizableData(repositoryContext.getExternalizableData()); 049 } 050 } 051 052 /** 053 * Creates a new instance of a {@link RepositoryDataContext} 054 * @return the created instance 055 */ 056 public static RepositoryDataContext newInstance() 057 { 058 return new RepositoryDataContext(); 059 } 060 061 /** 062 * Creates a new instance of a {@link RepositoryDataContext} from another {@link DataContext}. 063 * It can be the same implementation or another one, but it will be casted to the current implementation. 064 * @param context the data context to copy 065 * @return the created instance 066 */ 067 public static RepositoryDataContext newInstance(DataContext context) 068 { 069 return new RepositoryDataContext(context); 070 } 071 072 /** 073 * Creates a new instance of a {@link RepositoryDataContext}, with the current context values 074 * @return the created instance 075 */ 076 @Override 077 public RepositoryDataContext cloneContext() 078 { 079 return newInstance(this); 080 } 081 082 /** 083 * Retrieves the externalizable data 084 * @return the externalizable data 085 */ 086 public Set<String> getExternalizableData() 087 { 088 return _externalizableData; 089 } 090 091 /** 092 * Check if the current data is externalizable 093 * @return <code>true</code> if the data is externalizable, <code>false</code> otherwise 094 */ 095 public boolean isDataExternalizable() 096 { 097 String dataPath = getDataPath(); 098 String definitionPath = ModelHelper.getDefinitionPathFromDataPath(dataPath); 099 return _externalizableData.contains(definitionPath); 100 } 101 102 /** 103 * Sets the externalizable data 104 * @param externalizableData the externalizable data to set 105 * @return the current {@link RepositoryDataContext} 106 */ 107 @SuppressWarnings("unchecked") 108 public <T extends RepositoryDataContext> T withExternalizableData(Set<String> externalizableData) 109 { 110 _externalizableData = externalizableData; 111 return (T) this; 112 } 113}