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.contentio.synchronize; 017 018import java.util.Arrays; 019import java.util.Collection; 020import java.util.Map; 021import java.util.Optional; 022import java.util.Set; 023import java.util.stream.Collectors; 024 025import org.apache.avalon.framework.component.Component; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029 030import org.ametys.cms.repository.Content; 031import org.ametys.plugins.repository.data.external.ExternalizableDataProvider; 032import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 033import org.ametys.runtime.model.ModelItem; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035 036/** 037 * {@link ExternalizableDataProvider} based on {@link SynchronizableContentsCollection} 038 */ 039public class SynchronizableContentsCollectionDataProvider extends AbstractLogEnabled implements ExternalizableDataProvider, Component, Serviceable 040{ 041 /** Constant for storing the scc identifier into the externalizable data context */ 042 public static final String SCC_ID_CONTEXT_KEY = "sccId"; 043 044 /** The DAO for synchronizable contents collections */ 045 protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO; 046 /** SCC helper */ 047 protected SynchronizableContentsCollectionHelper _sccHelper; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE); 053 _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE); 054 } 055 056 public Set<String> getExternalizableDataPaths(ModelAwareDataHolder dataHolder) 057 { 058 if (dataHolder instanceof Content) 059 { 060 Content content = (Content) dataHolder; 061 return _sccHelper.getSynchronizableCollectionIds(content) 062 .stream() 063 .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection) 064 .filter(collection -> collection != null) 065 .map(collection -> _getExternalizableDataPaths(dataHolder, collection)) 066 .flatMap(Collection::stream) 067 .collect(Collectors.toSet()); 068 } 069 else 070 { 071 return Set.of(); 072 } 073 } 074 075 public boolean isDataExternalizable(ModelAwareDataHolder dataHolder, ModelItem modelItem, Map<String, Object> context) 076 { 077 Set<String> externalizableDataPaths = Optional.of(context) 078 .map(ctx -> ctx.get(SCC_ID_CONTEXT_KEY)) 079 .filter(String.class::isInstance) 080 .map(String.class::cast) 081 .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection) 082 .map(col -> _getExternalizableDataPaths(dataHolder, col)) 083 .orElseGet(() -> getExternalizableDataPaths(dataHolder)); 084 085 return externalizableDataPaths.contains(modelItem.getPath()); 086 } 087 088 private Set<String> _getExternalizableDataPaths(ModelAwareDataHolder dataHolder, SynchronizableContentsCollection collection) 089 { 090 return dataHolder instanceof Content ? collection.getLocalAndExternalFields(buildParametersMap((Content) dataHolder)) : Set.of(); 091 } 092 093 private Map<String, Object> buildParametersMap(Content content) 094 { 095 return Map.of("contentTypes", Arrays.asList(content.getTypes())); 096 } 097 098}