001/* 002 * Copyright 2016 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.ArrayList; 019import java.util.Collection; 020import java.util.List; 021import java.util.Set; 022import java.util.stream.Collectors; 023 024import javax.jcr.Node; 025import javax.jcr.RepositoryException; 026import javax.jcr.Value; 027 028import org.apache.avalon.framework.service.ServiceException; 029import org.apache.avalon.framework.service.ServiceManager; 030import org.apache.avalon.framework.service.Serviceable; 031 032import org.ametys.cms.content.external.ExternalizableMetadataProvider; 033import org.ametys.cms.repository.Content; 034import org.ametys.cms.repository.DefaultContent; 035import org.ametys.plugins.repository.AmetysRepositoryException; 036import org.ametys.runtime.plugin.component.AbstractLogEnabled; 037 038/** 039 * {@link ExternalizableMetadataProvider} based on a {@link SynchronizableContentsCollection} 040 */ 041public class SynchronizableContentsCollectionMetadataProvider extends AbstractLogEnabled implements ExternalizableMetadataProvider, Serviceable 042{ 043 /** The DAO for synchronizable contents collections */ 044 protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE); 050 } 051 052 @Override 053 public Set<String> getExternalOnlyMetadata(Content content) 054 { 055 List<SynchronizableContentsCollection> collections = _getCollectionIds(content).stream() 056 .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection) 057 .collect(Collectors.toList()); 058 059 // Return the intersection of the no-synchronized metadata of each collection 060 Set<String> externalMetadata = collections.get(0).getExternalOnlyFields(); 061 for (int i = 1; i < collections.size(); i++) 062 { 063 externalMetadata.retainAll(collections.get(i).getExternalOnlyFields()); 064 } 065 066 return externalMetadata; 067 } 068 069 @Override 070 public Set<String> getExternalAndLocalMetadata(Content content) 071 { 072 return _getCollectionIds(content).stream() 073 .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection) 074 .map(col -> col.getLocalAndExternalFields()) 075 .flatMap(Collection::stream) 076 .collect(Collectors.toSet()); 077 } 078 079 private List<String> _getCollectionIds(Content content) throws AmetysRepositoryException 080 { 081 List<String> collectionIds = new ArrayList<>(); 082 083 if (content instanceof DefaultContent) 084 { 085 try 086 { 087 Node node = ((DefaultContent) content).getNode(); 088 if (node.hasProperty(SynchronizableContentsCollection.COLLECTION_ID_PROPERTY)) 089 { 090 Value[] values = node.getProperty(SynchronizableContentsCollection.COLLECTION_ID_PROPERTY).getValues(); 091 for (Value value : values) 092 { 093 collectionIds.add(value.getString()); 094 } 095 } 096 } 097 catch (RepositoryException e) 098 { 099 getLogger().error("Failed to get linked synchronizable collections for content " + content.getId(), e); 100 throw new AmetysRepositoryException("Failed to get linked synchronizable collections for content " + content.getId(), e); 101 } 102 } 103 104 return collectionIds; 105 } 106}