001/*
002 *  Copyright 2020 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.runtime.model;
017
018import java.util.Optional;
019
020import org.apache.commons.lang3.StringUtils;
021
022/**
023 * Object that gives some context for definitions manipulation
024 */
025public final class DefinitionContext
026{
027    private Optional<Object> _object = Optional.empty();
028    private boolean _isEdition;
029    private Optional<String> _itemTagName = Optional.empty();
030    private Optional<ModelItem> _modelItem = Optional.empty();
031    private Optional<View> _view = Optional.empty();
032    private Optional<String> _conditionPrefix = Optional.empty();
033
034    private DefinitionContext()
035    {
036        // Empty private constructor
037    }
038    
039    /**
040     * Creates a new instance of a {@link DefinitionContext} from another {@link DefinitionContext}
041     * @param context the definition context to copy
042     */
043    private DefinitionContext(DefinitionContext context)
044    {
045        withEdition(context.isEdition());
046        withObject(context.getObject().orElse(null));
047        withItemTagName(context.getItemTagName().orElse(null));
048        
049        withModelItem(context.getModelItem().orElse(null));
050        withView(context.getView().orElse(null));
051        withConditionPrefix(context.getConditionPrefix().orElse(null));
052    }
053    
054    /**
055     * Creates a new instance of a {@link DefinitionContext}
056     * @return the created instance
057     */
058    public static DefinitionContext newInstance()
059    {
060        return new DefinitionContext();
061    }
062    
063    /**
064     * Creates a new instance of a {@link DefinitionContext} from another {@link DefinitionContext}.
065     * @param context the definition context to copy
066     * @return the created instance
067     */
068    public static DefinitionContext newInstance(DefinitionContext context)
069    {
070        return new DefinitionContext(context);
071    }
072    
073    /**
074     * Creates a new instance of a {@link DefinitionContext}, with the current context values
075     * @return the created instance
076     */
077    public DefinitionContext cloneContext()
078    {
079        return newInstance(this);
080    }
081    
082    /**
083     * Retrieves the context object for the definition
084     * @return the context object
085     */
086    public Optional<Object> getObject()
087    {
088        return _object;
089    }
090    
091    /**
092     * Set the context object
093     * @param object the context object to set
094     * @return the current {@link DefinitionContext}
095     */
096    public DefinitionContext withObject(Object object)
097    {
098        _object = Optional.ofNullable(object);
099        return this;
100    }
101    
102    /**
103     * Checks if the context is in edition
104     * @return <code>true</code> if the context is in edition, <code>false</code> otherwise
105     */
106    public boolean isEdition()
107    {
108        return _isEdition;
109    }
110    
111    /**
112     * Set to <code>true</code> if the context is in edition (default to <code>false</code>)
113     * @param isEdition <code>true</code> if the context is in edition, <code>false</code> otherwise
114     * @return the current {@link DefinitionContext}
115     */
116    public DefinitionContext withEdition(boolean isEdition)
117    {
118        _isEdition = isEdition;
119        return this;
120    }
121    
122    /**
123     * Retrieves the tag name to use while generating SAX events
124     * @return the tag name to use while generating SAX events
125     */
126    public Optional<String> getItemTagName()
127    {
128        return _itemTagName;
129    }
130    
131    /**
132     * Set the tag name to use while generating SAX events
133     * @param itemTagName the tag name to set
134     * @return the current {@link DefinitionContext}
135     */
136    public DefinitionContext withItemTagName(String itemTagName)
137    {
138        _itemTagName = Optional.ofNullable(itemTagName)
139                               .filter(StringUtils::isNotEmpty);
140        return this;
141    }
142    
143    /**
144     * Retrieves the model item
145     * @return the model item
146     */
147    public Optional<ModelItem> getModelItem()
148    {
149        return _modelItem;
150    }
151    
152    /**
153     * Sets the model item
154     * @param modelItem the model item to set
155     * @return the current {@link DefinitionContext}
156     */
157    public DefinitionContext withModelItem(ModelItem modelItem)
158    {
159        _modelItem = Optional.ofNullable(modelItem);
160        return this;
161    }
162    
163    /**
164     * Retrieves the view
165     * @return the view
166     */
167    public Optional<View> getView()
168    {
169        return _view;
170    }
171    
172    /**
173     * Set the view
174     * @param view the view to set
175     * @return the current {@link DefinitionContext}
176     */
177    public DefinitionContext withView(View view)
178    {
179        _view = Optional.ofNullable(view);
180        return this;
181    }
182    
183    /**
184     * Get the disable condition prefix
185     * @return the disable condition prefix
186     */
187    public Optional<String> getConditionPrefix()
188    {
189        return _conditionPrefix;
190    }
191    
192    /**
193     * Set the disable condition prefix
194     * @param conditionPrefix the disable condition prefix to set
195     * @return the current {@link DefinitionContext}
196     */
197    public DefinitionContext withConditionPrefix(String conditionPrefix)
198    {
199        _conditionPrefix = Optional.ofNullable(conditionPrefix);
200        return this;
201    }
202}