001/*
002 *  Copyright 2023 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.model;
017
018import org.ametys.cms.content.indexing.solr.SolrFieldNames;
019import org.ametys.plugins.repository.model.RepositoryDataContext;
020import org.ametys.runtime.model.type.DataContext;
021
022/**
023 * Object that gives some context for cms data manipulation
024 */
025public class CMSDataContext extends RepositoryDataContext
026{
027    private int _richTextMaxLength;
028    
029    private boolean _indexForFullTextField;
030    private String _fullTextFieldName = SolrFieldNames.SYSTEM_FULL;
031    
032    private boolean _isSearchedValueEscaped;
033    private boolean _isSearchMultilingual;
034    
035    /**
036     * Creates a new instance of a {@link CMSDataContext}
037     */
038    protected CMSDataContext()
039    {
040        // Empty constructor
041    }
042
043    /**
044     * Creates a new instance of a {@link CMSDataContext} from another {@link DataContext}
045     * @param context the data context to copy
046     */
047    protected CMSDataContext(DataContext context)
048    {
049        super(context);
050        if (context instanceof CMSDataContext cmsContext)
051        {
052            withRichTextMaxLength(cmsContext.getRichTextMaxLength());
053            withIndexForFullTextField(cmsContext.indexForFullTextField());
054            withFullTextFieldName(cmsContext.getFullTextFieldName());
055            withSearchedValueEscaped(cmsContext.isSearchedValueEscaped());
056            withMultilingualSearch(cmsContext.isSearchMultilingual());
057        }
058    }
059    
060    /**
061     * Creates a new instance of a {@link CMSDataContext}
062     * @return the created instance
063     */
064    public static CMSDataContext newInstance()
065    {
066        return new CMSDataContext();
067    }
068    
069    /**
070     * Creates a new instance of a {@link CMSDataContext} from another {@link DataContext}.
071     * It can be the same implementation or another one, but it will be casted to the current implementation.
072     * @param context the data context to copy
073     * @return the created instance
074     */
075    public static CMSDataContext newInstance(DataContext context)
076    {
077        return new CMSDataContext(context);
078    }
079    
080    /**
081     * Creates a new instance of a {@link CMSDataContext}, with the current context values
082     * @return the created instance
083     */
084    @Override
085    public CMSDataContext cloneContext()
086    {
087        return newInstance(this);
088    }
089    
090    /**
091     * Retrieves the rich text max length. If the max length is 0, the whole rich text content is kept
092     * @return the rich text max length
093     */
094    public int getRichTextMaxLength()
095    {
096        return _richTextMaxLength;
097    }
098    
099    /**
100     * Sets the rich text max length
101     * Set to 0 to keep the whole rich text content (default) 
102     * @param <T> the type of the retrieved {@link DataContext}
103     * @param richTextMaxLength the rich text max length to set
104     * @return the current {@link CMSDataContext}
105     */
106    @SuppressWarnings("unchecked")
107    public <T extends CMSDataContext> T withRichTextMaxLength(int richTextMaxLength)
108    {
109        _richTextMaxLength = richTextMaxLength;
110        return (T) this;
111    }
112    
113    /**
114     * Determines if the value should be indexed for a fulltext field. The fulltext field is given by this {@link CMSDataContext}
115     * @return <code>true</code> to index value for a fulltext field, <code>false</code> otherwise.
116     */
117    public boolean indexForFullTextField()
118    {
119        return _indexForFullTextField;
120    }
121    
122    /**
123     * Set to <code>true</code> to index the value for a fulltext field. The fulltext field is given by this {@link CMSDataContext}
124     * @param <T> the type of the retrieved {@link DataContext}
125     * @param indexForFullTextField <code>true</code> to index the value for a fulltext field, <code>false</code> otherwise
126     * @return the current {@link CMSDataContext}
127     */
128    @SuppressWarnings("unchecked")
129    public <T extends CMSDataContext> T withIndexForFullTextField(boolean indexForFullTextField)
130    {
131        _indexForFullTextField = indexForFullTextField;
132        return (T) this;
133    }
134    
135    /**
136     * Get the field name of full-text field. Use if this {@link CMSDataContext} is instanciated for full-text field
137     * @return the field name of full-text field. Default to SolrFieldNames.SYSTEM_FULL
138     */
139    public String getFullTextFieldName()
140    {
141        return _fullTextFieldName;
142    }
143
144    /**
145     * Set the field name of full-text field. Use if this {@link CMSDataContext} is instanciated for full-text field
146     * @param <T> the type of the retrieved {@link DataContext}
147     * @param fulltextFieldName the full-text field name
148     * @return the current {@link CMSDataContext}
149     */
150    @SuppressWarnings("unchecked")
151    public <T extends CMSDataContext> T withFullTextFieldName(String fulltextFieldName)
152    {
153        _fullTextFieldName = fulltextFieldName;
154        return (T) this;
155    }
156    
157    /**
158     * Determines if the searched value is escaped
159     * @return <code>true</code> if the searched value is escaped, <code>false</code> otherwise.
160     */
161    public boolean isSearchedValueEscaped()
162    {
163        return _isSearchedValueEscaped;
164    }
165    
166    /**
167     * Set to <code>true</code> if the searched value is escaped (default to <code>false</code>)
168     * @param <T> the type of the retrieved {@link DataContext}
169     * @param isSearchedValueEscaped <code>true</code> if the searched value is escaped, <code>false</code> otherwise
170     * @return the current {@link CMSDataContext}
171     */
172    @SuppressWarnings("unchecked")
173    public <T extends CMSDataContext> T withSearchedValueEscaped(boolean isSearchedValueEscaped)
174    {
175        _isSearchedValueEscaped = isSearchedValueEscaped;
176        return (T) this;
177    }
178    
179    /**
180     * Determines if the search is multilingual
181     * @return <code>true</code> if the search is multilingual, <code>false</code> otherwise.
182     */
183    public boolean isSearchMultilingual()
184    {
185        return _isSearchMultilingual;
186    }
187    
188    /**
189     * Set to <code>true</code> if the search is multilingual (default to <code>false</code>)
190     * @param <T> the type of the retrieved {@link DataContext}
191     * @param isSearchMultilingual <code>true</code> if the search is multilingual, <code>false</code> otherwise
192     * @return the current {@link CMSDataContext}
193     */
194    @SuppressWarnings("unchecked")
195    public <T extends CMSDataContext> T withMultilingualSearch(boolean isSearchMultilingual)
196    {
197        _isSearchMultilingual = isSearchMultilingual;
198        return (T) this;
199    }
200}