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