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.search.systemprop; 017 018import java.util.Arrays; 019import java.util.Collections; 020import java.util.Map; 021import java.util.stream.Collectors; 022 023import javax.jcr.RepositoryException; 024import javax.jcr.Value; 025 026import org.ametys.cms.contenttype.MetadataType; 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.search.SearchField; 029import org.ametys.cms.search.model.SystemProperty; 030import org.ametys.cms.search.query.Query; 031import org.ametys.cms.search.query.Query.Operator; 032import org.ametys.cms.search.systemprop.AbstractSystemProperty; 033import org.ametys.core.util.LambdaUtils; 034import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 035import org.ametys.plugins.contentio.synchronize.search.field.CollectionSearchField; 036import org.ametys.plugins.contentio.synchronize.search.query.CollectionsQuery; 037import org.ametys.plugins.repository.jcr.JCRAmetysObject; 038 039/** 040 * {@link SystemProperty} which represents the synchronizable contents collections of a content. 041 */ 042public class CollectionsSystemProperty extends AbstractSystemProperty 043{ 044 @Override 045 public MetadataType getType() 046 { 047 return MetadataType.STRING; 048 } 049 050 @Override 051 public boolean isMultiple() 052 { 053 return true; 054 } 055 056 @Override 057 public boolean isSortable() 058 { 059 return false; 060 } 061 062 @Override 063 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 064 { 065 String[] synchronizableContentsCollectionIds = parseStringArray(value); 066 return new CollectionsQuery(operator, synchronizableContentsCollectionIds); 067 } 068 069 @Override 070 public SearchField getSearchField() 071 { 072 return new CollectionSearchField(); 073 } 074 075 @Override 076 public Object getValue(Content content) 077 { 078 if (content instanceof JCRAmetysObject) 079 { 080 try 081 { 082 Value[] values = ((JCRAmetysObject) content).getNode().getProperty(SynchronizableContentsCollection.COLLECTION_ID_PROPERTY).getValues(); 083 return Arrays.stream(values).map(LambdaUtils.wrap(Value::getString)).collect(Collectors.toSet()); 084 } 085 catch (RepositoryException e) 086 { 087 // The property does not exists 088 return Collections.EMPTY_SET; 089 } 090 } 091 return Collections.EMPTY_SET; 092 } 093}