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