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
033    private DefinitionContext()
034    {
035        // Empty private constructor
036    }
037    
038    /**
039     * Creates a new instance of a {@link DefinitionContext} from another {@link DefinitionContext}
040     * @param context the definition context to copy
041     */
042    private DefinitionContext(DefinitionContext context)
043    {
044        withEdition(context.isEdition());
045        withObject(context.getObject().orElse(null));
046        withItemTagName(context.getItemTagName().orElse(null));
047        
048        withModelItem(context.getModelItem().orElse(null));
049        withView(context.getView().orElse(null));
050    }
051    
052    /**
053     * Creates a new instance of a {@link DefinitionContext}
054     * @return the created instance
055     */
056    public static DefinitionContext newInstance()
057    {
058        return new DefinitionContext();
059    }
060    
061    /**
062     * Creates a new instance of a {@link DefinitionContext} from another {@link DefinitionContext}.
063     * @param context the definition context to copy
064     * @return the created instance
065     */
066    public static DefinitionContext newInstance(DefinitionContext context)
067    {
068        return new DefinitionContext(context);
069    }
070    
071    /**
072     * Creates a new instance of a {@link DefinitionContext}, with the current context values
073     * @return the created instance
074     */
075    public DefinitionContext cloneContext()
076    {
077        return newInstance(this);
078    }
079    
080    /**
081     * Retrieves the context object for the definition
082     * @return the context object
083     */
084    public Optional<Object> getObject()
085    {
086        return _object;
087    }
088    
089    /**
090     * Set the context object
091     * @param object the context object to set
092     * @return the current {@link DefinitionContext}
093     */
094    public DefinitionContext withObject(Object object)
095    {
096        _object = Optional.ofNullable(object);
097        return this;
098    }
099    
100    /**
101     * Checks if the context is in edition
102     * @return <code>true</code> if the context is in edition, <code>false</code> otherwise
103     */
104    public boolean isEdition()
105    {
106        return _isEdition;
107    }
108    
109    /**
110     * Set to <code>true</code> if the context is in edition (default to <code>false</code>) 
111     * @param isEdition <code>true</code> if the context is in edition, <code>false</code> otherwise
112     * @return the current {@link DefinitionContext}
113     */
114    public DefinitionContext withEdition(boolean isEdition)
115    {
116        _isEdition = isEdition;
117        return this;
118    }
119    
120    /**
121     * Retrieves the tag name to use while generating SAX events
122     * @return the tag name to use while generating SAX events
123     */
124    public Optional<String> getItemTagName()
125    {
126        return _itemTagName;
127    }
128    
129    /**
130     * Set the tag name to use while generating SAX events
131     * @param itemTagName the tag name to set
132     * @return the current {@link DefinitionContext}
133     */
134    public DefinitionContext withItemTagName(String itemTagName)
135    {
136        _itemTagName = Optional.ofNullable(itemTagName)
137                               .filter(StringUtils::isNotEmpty);
138        return this;
139    }
140    
141    /**
142     * Retrieves the model item
143     * @return the model item
144     */
145    public Optional<ModelItem> getModelItem()
146    {
147        return _modelItem;
148    }
149    
150    /**
151     * Sets the model item
152     * @param modelItem the model item to set
153     * @return the current {@link DefinitionContext}
154     */
155    public DefinitionContext withModelItem(ModelItem modelItem)
156    {
157        _modelItem = Optional.ofNullable(modelItem);
158        return this;
159    }
160    
161    /**
162     * Retrieves the view 
163     * @return the view
164     */
165    public Optional<View> getView()
166    {
167        return _view;
168    }
169    
170    /**
171     * Set the view
172     * @param view the view to set
173     * @return the current {@link DefinitionContext}
174     */
175    public DefinitionContext withView(View view)
176    {
177        _view = Optional.ofNullable(view);
178        return this;
179    }
180}