001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krad.uif.container.collections;
017
018import org.kuali.rice.krad.uif.UifConstants;
019import org.kuali.rice.krad.uif.component.Component;
020import org.kuali.rice.krad.uif.container.CollectionGroup;
021import org.kuali.rice.krad.uif.container.DialogGroup;
022import org.kuali.rice.krad.uif.field.Field;
023import org.kuali.rice.krad.uif.field.FieldGroup;
024import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
025import org.kuali.rice.krad.uif.view.ViewModel;
026
027import java.io.Serializable;
028import java.util.Collections;
029import java.util.List;
030
031/**
032 * Holds components and configuration for a line during the build process.
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 * @see org.kuali.rice.krad.uif.container.CollectionGroupBuilder
036 * @see org.kuali.rice.krad.uif.container.CollectionGroupLineBuilder
037 */
038public class LineBuilderContext implements Serializable {
039    private static final long serialVersionUID = -2025777471407211781L;
040
041    private int lineIndex;
042    private Object currentLine;
043    private String bindingPath;
044    private boolean bindToForm;
045
046    private ViewModel model;
047    private CollectionGroup collectionGroup;
048
049    private List<? extends Component> lineActions;
050    private List<Field> lineFields;
051    private List<FieldGroup> subCollectionFields;
052    private List<DialogGroup> lineDialogs;
053
054    /**
055     * Empty constructor.
056     */
057    public LineBuilderContext() {
058
059    }
060
061    /**
062     * Constructor.
063     *
064     * @param lineIndex index of line
065     * @param currentLine object containing the line data
066     * @param bindingPath path to the line in the model
067     * @param bindToForm indicates if the line fields bind to the form (not the default object path)
068     * @param model object containing the views data
069     * @param collectionGroup collection group instance the line is being built for
070     * @param lineActions list of components for the lines action column
071     */
072    public LineBuilderContext(int lineIndex, Object currentLine, String bindingPath, boolean bindToForm, ViewModel model,
073            CollectionGroup collectionGroup, List<? extends Component> lineActions) {
074        this.lineIndex = lineIndex;
075        this.currentLine = currentLine;
076        this.bindingPath = bindingPath;
077        this.bindToForm = bindToForm;
078        this.model = model;
079        this.collectionGroup = collectionGroup;
080        this.lineActions = lineActions;
081        this.lineDialogs = Collections.emptyList();
082    }
083
084    /**
085     * Constructor.
086     *
087     * @param lineIndex index of line
088     * @param currentLine object containing the line data
089     * @param bindingPath path to the line in the model
090     * @param bindToForm indicates if the line fields bind to the form (not the default object path)
091     * @param model object containing the views data
092     * @param collectionGroup collection group instance the line is being built for
093     * @param lineActions list of components for the lines action column
094     * @param lineDialogs list of dialogs configured on the line
095     */
096    public LineBuilderContext(int lineIndex, Object currentLine, String bindingPath, boolean bindToForm, ViewModel model,
097            CollectionGroup collectionGroup, List<? extends Component> lineActions, List<DialogGroup> lineDialogs) {
098        this.lineIndex = lineIndex;
099        this.currentLine = currentLine;
100        this.bindingPath = bindingPath;
101        this.bindToForm = bindToForm;
102        this.model = model;
103        this.collectionGroup = collectionGroup;
104        this.lineActions = lineActions;
105        this.lineDialogs = lineDialogs;
106    }
107
108    /**
109     * Suffix to use for adjusting the ids on components within the line.
110     *
111     * @return String id suffix
112     */
113    public String getIdSuffix() {
114        String idSuffix;
115
116        if (isAddLine()) {
117            idSuffix = UifConstants.IdSuffixes.ADD_LINE;
118        } else {
119            idSuffix = UifConstants.IdSuffixes.LINE + Integer.toString(lineIndex);
120        }
121
122        return idSuffix;
123    }
124
125    /**
126     * Indicates whether the line is the add line, or an existing collection line.
127     *
128     * @return boolean true if the line is the add line, false if not
129     */
130    public boolean isAddLine() {
131        return this.lineIndex == -1;
132    }
133
134    /**
135     * Returns the {@link org.kuali.rice.krad.uif.layout.CollectionLayoutManager} configured on the collection
136     * group.
137     *
138     * @return collection layout manager instance
139     */
140    public CollectionLayoutManager getLayoutManager() {
141        if (this.collectionGroup != null) {
142            return (CollectionLayoutManager) this.collectionGroup.getLayoutManager();
143        }
144
145        return null;
146    }
147
148    /**
149     * Index for the line within the collection, or -1 for the add line.
150     *
151     * @return line index
152     */
153    public int getLineIndex() {
154        return lineIndex;
155    }
156
157    /**
158     * @see LineBuilderContext#getLineIndex()
159     */
160    public void setLineIndex(int lineIndex) {
161        this.lineIndex = lineIndex;
162    }
163
164    /**
165     * Object containing the line's data.
166     *
167     * @return object instance
168     */
169    public Object getCurrentLine() {
170        return currentLine;
171    }
172
173    /**
174     * @see LineBuilderContext#getCurrentLine()
175     */
176    public void setCurrentLine(Object currentLine) {
177        this.currentLine = currentLine;
178    }
179
180    /**
181     * Path to the line in the full model.
182     *
183     * @return binding path
184     */
185    public String getBindingPath() {
186        return bindingPath;
187    }
188
189    /**
190     * @see LineBuilderContext#getBindingPath()
191     */
192    public void setBindingPath(String bindingPath) {
193        this.bindingPath = bindingPath;
194    }
195
196    /**
197     * Indicates if the line fields bind to the form (not the default object path).
198     *
199     * @return boolean true if line fields bindi to the form, false if not
200     */
201    public boolean isBindToForm() {
202        return bindToForm;
203    }
204
205    /**
206     * @see LineBuilderContext#isBindToForm()
207     */
208    public void setBindToForm(boolean bindToForm) {
209        this.bindToForm = bindToForm;
210    }
211
212    /**
213     * Object containing the view's data.
214     *
215     * @return model instance
216     */
217    public ViewModel getModel() {
218        return model;
219    }
220
221    /**
222     * @see LineBuilderContext#getModel()
223     */
224    public void setModel(ViewModel model) {
225        this.model = model;
226    }
227
228    /**
229     * Collection group the line is being built for.
230     *
231     * @return collection group instance
232     */
233    public CollectionGroup getCollectionGroup() {
234        return collectionGroup;
235    }
236
237    /**
238     * @see LineBuilderContext#getCollectionGroup()
239     */
240    public void setCollectionGroup(CollectionGroup collectionGroup) {
241        this.collectionGroup = collectionGroup;
242    }
243
244    /**
245     * List of components to render in the lines action column.
246     *
247     * @return list of component instances
248     */
249    public List<? extends Component> getLineActions() {
250        return lineActions;
251    }
252
253    /**
254     * @see LineBuilderContext#getLineActions()
255     */
256    public void setLineActions(List<? extends Component> lineActions) {
257        this.lineActions = lineActions;
258    }
259
260    /**
261     * List of field instances that make up the lines columns.
262     *
263     * @return list of field instances.
264     */
265    public List<Field> getLineFields() {
266        return lineFields;
267    }
268
269    /**
270     * @see LineBuilderContext#getLineFields()
271     */
272    public void setLineFields(List<Field> lineFields) {
273        this.lineFields = lineFields;
274    }
275
276    /**
277     * List of field groups that wrap the sub-collections for the line.
278     *
279     * @return list of field groups instances
280     */
281    public List<FieldGroup> getSubCollectionFields() {
282        return subCollectionFields;
283    }
284
285    /**
286     * @see LineBuilderContext#getSubCollectionFields()
287     */
288    public void setSubCollectionFields(List<FieldGroup> subCollectionFields) {
289        this.subCollectionFields = subCollectionFields;
290    }
291
292    /**
293     * List of dialog groups that make up the lines dialogs.
294     *
295     * @return list of field instances.
296     */
297    public List<DialogGroup> getLineDialogs() {
298        return lineDialogs;
299    }
300
301    /**
302     * @see org.kuali.rice.krad.uif.container.collections.LineBuilderContext#getDialogGroups()
303     */
304    public void setLineDialogs(List<DialogGroup> dialogGroups) {
305        this.lineDialogs = dialogGroups;
306    }
307}