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.plugins.repository.model.RepositoryDataContext;
019import org.ametys.runtime.model.type.DataContext;
020
021/**
022 * Object that gives some context for cms data manipulation
023 */
024public class CMSDataContext extends RepositoryDataContext
025{
026    private int _richTextMaxLength;
027    
028    /**
029     * Creates a new instance of a {@link CMSDataContext}
030     */
031    protected CMSDataContext()
032    {
033        // Empty constructor
034    }
035
036    /**
037     * Creates a new instance of a {@link CMSDataContext} from another {@link DataContext}
038     * @param context the data context to copy
039     */
040    protected CMSDataContext(DataContext context)
041    {
042        super(context);
043        if (context instanceof CMSDataContext cmsContext)
044        {
045            withRichTextMaxLength(cmsContext.getRichTextMaxLength());
046        }
047    }
048    
049    /**
050     * Creates a new instance of a {@link CMSDataContext}
051     * @return the created instance
052     */
053    public static CMSDataContext newInstance()
054    {
055        return new CMSDataContext();
056    }
057    
058    /**
059     * Creates a new instance of a {@link CMSDataContext} from another {@link DataContext}.
060     * It can be the same implementation or another one, but it will be casted to the current implementation.
061     * @param context the data context to copy
062     * @return the created instance
063     */
064    public static CMSDataContext newInstance(DataContext context)
065    {
066        return new CMSDataContext(context);
067    }
068    
069    /**
070     * Creates a new instance of a {@link CMSDataContext}, with the current context values
071     * @return the created instance
072     */
073    @Override
074    public CMSDataContext cloneContext()
075    {
076        return newInstance(this);
077    }
078    
079    /**
080     * Retrieves the rich text max length. If the max length is 0, the whole rich text content is kept
081     * @return the rich text max length
082     */
083    public int getRichTextMaxLength()
084    {
085        return _richTextMaxLength;
086    }
087    
088    /**
089     * Sets the rich text max length
090     * Set to 0 to keep the whole rich text content (default) 
091     * @param <T> the type of the retrieved {@link DataContext}
092     * @param richTextMaxLength the rich text max length to set
093     * @return the current {@link CMSDataContext}
094     */
095    @SuppressWarnings("unchecked")
096    public <T extends CMSDataContext> T withRichTextMaxLength(int richTextMaxLength)
097    {
098        _richTextMaxLength = richTextMaxLength;
099        return (T) this;
100    }
101}