001/*
002 *  Copyright 2022 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.data.ametysobject;
017
018import java.util.List;
019import java.util.Optional;
020
021import org.apache.commons.lang3.StringUtils;
022import org.apache.solr.common.SolrInputDocument;
023
024import org.ametys.cms.data.holder.IndexableDataHolder;
025import org.ametys.cms.data.holder.group.IndexableComposite;
026import org.ametys.cms.data.holder.group.IndexableRepeater;
027import org.ametys.cms.data.holder.impl.IndexableDataHolderHelper;
028import org.ametys.cms.search.model.SystemProperty;
029import org.ametys.cms.search.model.SystemPropertyExtensionPoint;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.runtime.model.exception.BadDataPathCardinalityException;
032import org.ametys.runtime.model.exception.BadItemTypeException;
033import org.ametys.runtime.model.exception.UndefinedItemPathException;
034import org.ametys.runtime.model.type.DataContext;
035
036/**
037 * Model aware {@link AmetysObject} that can handle indexable data.
038 */
039public interface ModelAwareDataAwareAmetysObject extends org.ametys.plugins.repository.data.ametysobject.ModelAwareDataAwareAmetysObject, IndexableDataHolder
040{
041    @Override
042    public IndexableDataHolder getDataHolder();
043    
044    /**
045     * Retrieves the system properties extension point of this {@link AmetysObject}, or an empty {@link Optional} if this object does not support system properties 
046     * @return the system properties extension point of this {@link AmetysObject}
047     */
048    public default Optional<SystemPropertyExtensionPoint> getSystemPropertyExtensionPoint()
049    {
050        return Optional.empty();
051    }
052    
053    /**
054     * Indexes all data and properties of this {@link AmetysObject}
055     * @param document the solr document representing this {@link AmetysObject}
056     * @return additional solr documents that may have been created (ex: repeater entries)
057     * @throws BadItemTypeException if the saxed value's type does not matches the stored data
058     */
059    public default List<SolrInputDocument> indexData(SolrInputDocument document) throws BadItemTypeException
060    {
061        return indexData(document, DataContext.newInstance());
062    }
063    
064    /**
065     * Indexes all data and properties of this {@link AmetysObject}
066     * @param document the solr document representing this {@link AmetysObject}
067     * @param context The context of the data to index
068     * @return additional solr documents that may have been created (ex: repeater entries)
069     * @throws BadItemTypeException if the saxed value's type does not matches the stored data
070     */
071    @SuppressWarnings("unchecked")
072    public default List<SolrInputDocument> indexData(SolrInputDocument document, DataContext context) throws BadItemTypeException
073    {
074        DataContext newContext = context.cloneContext()
075                                        .withObjectId(getId());
076        
077        // Index system properties
078        if (getSystemPropertyExtensionPoint().isPresent())
079        {
080            SystemPropertyExtensionPoint systemPropertyExtensionPoint = getSystemPropertyExtensionPoint().get();
081            for (String systemPropertyId : systemPropertyExtensionPoint.getExtensionsIds())
082            {
083                SystemProperty systemProperty = systemPropertyExtensionPoint.getExtension(systemPropertyId);
084                DataContext systemPropertyContext = newContext.cloneContext()
085                                                           .addSegmentToDataPath(systemPropertyId)
086                                                           .withModelItem(systemProperty);
087                systemProperty.indexValue(document, this, systemPropertyContext);
088            }
089        }
090        
091        // Index data
092        return IndexableDataHolderHelper.indexData(this, document, document, StringUtils.EMPTY, newContext);
093    }
094    
095    @Override
096    public default IndexableComposite getComposite(String compositePath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
097    {
098        return getDataHolder().getComposite(compositePath);
099    }
100
101    @Override
102    public default IndexableComposite getLocalComposite(String compositePath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
103    {
104        return getDataHolder().getLocalComposite(compositePath);
105    }
106
107    @Override
108    public default IndexableComposite getExternalComposite(String compositePath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
109    {
110        return getDataHolder().getExternalComposite(compositePath);
111    }
112
113    @Override
114    public default IndexableRepeater getRepeater(String repeaterPath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
115    {
116        return getDataHolder().getRepeater(repeaterPath);
117    }
118
119    @Override
120    public default IndexableRepeater getLocalRepeater(String repeaterPath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
121    {
122        return getDataHolder().getLocalRepeater(repeaterPath);
123    }
124    
125    @Override
126    public default IndexableRepeater getExternalRepeater(String repeaterPath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
127    {
128        return getDataHolder().getExternalRepeater(repeaterPath);
129    }
130    
131    @Override
132    public default Optional<? extends IndexableDataHolder> getParentDataHolder()
133    {
134        return getDataHolder().getParentDataHolder();
135    }
136    
137    @Override
138    public default IndexableDataHolder getRootDataHolder()
139    {
140        return getDataHolder().getRootDataHolder();
141    }
142}