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;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.rice.krad.datadictionary.parse.BeanTag;
023import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
024import org.kuali.rice.krad.datadictionary.parse.BeanTags;
025import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
026import org.kuali.rice.krad.uif.UifConstants;
027import org.kuali.rice.krad.uif.component.Component;
028import org.kuali.rice.krad.uif.element.Header;
029import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
030import org.kuali.rice.krad.uif.element.BreadcrumbItem;
031import org.kuali.rice.krad.uif.util.LifecycleElement;
032import org.kuali.rice.krad.uif.element.PageBreadcrumbOptions;
033import org.kuali.rice.krad.uif.view.FormView;
034import org.kuali.rice.krad.uif.view.View;
035import org.kuali.rice.krad.web.form.UifFormBase;
036
037/**
038 * A PageGroup represents a page of a View.
039 *
040 * <p>
041 * PageGroups should only be used with a View component.  The contain the main content that will be seen by the
042 * user using the View.  Like all other groups, PageGroup can contain items, headers and footers.  Pages also
043 * have their own BreadcrumbItem.
044 * </p>
045 *
046 * @author Kuali Rice Team (rice.collab@kuali.org)
047 */
048@BeanTags({@BeanTag(name = "page", parent = "Uif-Page"),
049        @BeanTag(name = "documentPage", parent = "Uif-DocumentPage"),
050        @BeanTag(name = "inquiryPage", parent = "Uif-InquiryPage"),
051        @BeanTag(name = "maintenancePage", parent = "Uif-MaintenancePage")})
052public class PageGroupBase extends GroupBase implements PageGroup {
053    private static final long serialVersionUID = 7571981300587270274L;
054
055    private boolean autoFocus = false;
056
057    private PageBreadcrumbOptions breadcrumbOptions;
058    private BreadcrumbItem breadcrumbItem;
059    private boolean stickyFooter;
060    private String formPostUrl;
061
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public void performInitialization(Object model) {
068        super.performInitialization(model);
069
070        //check to see if one of the items is a page, if so throw an exception
071        for (Component item : this.getItems()) {
072            if (item != null && item instanceof PageGroup) {
073                throw new RuntimeException("The page with id='"
074                        + this.getId()
075                        + "' contains a page with id='"
076                        + item.getId()
077                        + "'.  Nesting a page within a page is not allowed since only one "
078                        + "page's content can be shown on the View "
079                        + "at a time.  This may have been caused by possible misuse of the singlePageView flag (when "
080                        + "this flag is true, items set on the View become items of the single page.  Instead use "
081                        + "the page property on the View to set the page being used).");
082            }
083        }
084
085        breadcrumbOptions.setupBreadcrumbs(model);
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public void performFinalize(Object model, LifecycleElement parent) {
093        if (StringUtils.isBlank(this.getWrapperTag())) {
094            this.setWrapperTag(UifConstants.WrapperTags.MAIN);
095        }
096
097        super.performFinalize(model, parent);
098
099        UifFormBase formBase = (UifFormBase) model;
100
101        // If AutoFocus then set the focus_id to FIRST field, unless focus_id is also specified
102        if (isAutoFocus() && StringUtils.isNotBlank(formBase.getFocusId())) {
103            this.addDataAttribute(UifConstants.ActionDataAttributes.FOCUS_ID, formBase.getFocusId());
104        } else if (isAutoFocus()) {
105            this.addDataAttribute(UifConstants.ActionDataAttributes.FOCUS_ID, UifConstants.Order.FIRST.name());
106        }
107
108        // Add jumpToId as a data attribute
109        if (StringUtils.isNotBlank(formBase.getJumpToId())) {
110            this.addDataAttribute(UifConstants.ActionDataAttributes.JUMP_TO_ID, formBase.getJumpToId());
111        }
112
113        // Add jumpToName as a data attribute
114        if (StringUtils.isNotBlank(formBase.getJumpToName())) {
115            this.addDataAttribute(UifConstants.ActionDataAttributes.JUMP_TO_NAME, formBase.getJumpToName());
116        }
117
118        this.addDataAttribute(UifConstants.DataAttributes.ROLE, UifConstants.RoleTypes.PAGE);
119
120        String prefixScript = "";
121        if (this.getOnDocumentReadyScript() != null) {
122            prefixScript = this.getOnDocumentReadyScript();
123        }
124
125        View view = ViewLifecycle.getView();
126        if (view instanceof FormView && ((FormView) view).isValidateClientSide()) {
127            this.setOnDocumentReadyScript(prefixScript + "\nsetupPage(true);");
128        } else {
129            this.setOnDocumentReadyScript(prefixScript + "\nsetupPage(false);");
130        }
131
132        breadcrumbOptions.finalizeBreadcrumbs(model, this, breadcrumbItem);
133    }
134
135    /**
136     * {@inheritDoc}
137     */
138    @Override
139    public List<String> getAdditionalTemplates() {
140        List<String> additionalTemplates = super.getAdditionalTemplates();
141
142        Header viewHeader = ViewLifecycle.getView().getHeader();
143        if (viewHeader != null) {
144            if (additionalTemplates.isEmpty()) {
145                additionalTemplates = new ArrayList<String>();
146            }
147            additionalTemplates.add(viewHeader.getTemplate());
148        }
149
150        return additionalTemplates;
151    }
152
153    /**
154     * {@inheritDoc}
155     */
156    @Override
157    @BeanTagAttribute(name = "autoFocus")
158    public boolean isAutoFocus() {
159        return this.autoFocus;
160    }
161
162    /**
163     * {@inheritDoc}
164     */
165    @Override
166    public void setAutoFocus(boolean autoFocus) {
167        this.autoFocus = autoFocus;
168    }
169
170    /**
171     * {@inheritDoc}
172     */
173    @Override
174    @BeanTagAttribute
175    public PageBreadcrumbOptions getBreadcrumbOptions() {
176        return breadcrumbOptions;
177    }
178
179    /**
180     * {@inheritDoc}
181     */
182    @Override
183    public void setBreadcrumbOptions(PageBreadcrumbOptions breadcrumbOptions) {
184        this.breadcrumbOptions = breadcrumbOptions;
185    }
186
187    /**
188     * {@inheritDoc}
189     */
190    @Override
191    public List<BreadcrumbItem> getHomewardPathBreadcrumbs() {
192        return breadcrumbOptions == null ? null : breadcrumbOptions.getHomewardPathBreadcrumbs();
193    }
194
195    /**
196     * {@inheritDoc}
197     */
198    @Override
199    public List<BreadcrumbItem> getPreViewBreadcrumbs() {
200        return breadcrumbOptions == null ? null : breadcrumbOptions.getPreViewBreadcrumbs();
201    }
202
203    /**
204     * {@inheritDoc}
205     */
206    @Override
207    public List<BreadcrumbItem> getPrePageBreadcrumbs() {
208        return breadcrumbOptions == null ? null : breadcrumbOptions.getPrePageBreadcrumbs();
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public List<BreadcrumbItem> getBreadcrumbOverrides() {
216        return breadcrumbOptions == null ? null : breadcrumbOptions.getBreadcrumbOverrides();
217    }
218
219    /**
220     * {@inheritDoc}
221     */
222    @Override
223    @BeanTagAttribute
224    public BreadcrumbItem getBreadcrumbItem() {
225        return breadcrumbItem;
226    }
227
228    /**
229     * {@inheritDoc}
230     */
231    @Override
232    public void setBreadcrumbItem(BreadcrumbItem breadcrumbItem) {
233        this.breadcrumbItem = breadcrumbItem;
234    }
235
236    /**
237     * {@inheritDoc}
238     */
239    @Override
240    @BeanTagAttribute
241    public boolean isStickyFooter() {
242        return stickyFooter;
243    }
244
245    /**
246     * {@inheritDoc}
247     */
248    @Override
249    public void setStickyFooter(boolean stickyFooter) {
250        this.stickyFooter = stickyFooter;
251
252        if (this.getFooter() != null) {
253            this.getFooter().addDataAttribute(UifConstants.DataAttributes.STICKY_FOOTER, Boolean.toString(
254                    stickyFooter));
255        }
256    }
257
258    /**
259     * {@inheritDoc}
260     */
261    @Override
262    @BeanTagAttribute
263    public String getFormPostUrl() {
264        return formPostUrl;
265    }
266
267    /**
268     * {@inheritDoc}
269     */
270    @Override
271    public void setFormPostUrl(String formPostUrl) {
272        this.formPostUrl = formPostUrl;
273    }
274
275    /**
276     * {@inheritDoc}
277     */
278    @Override
279    public void completeValidation(ValidationTrace tracer) {
280        tracer.addBean(this);
281
282        // Checks that no invalid items are present
283        for (int i = 0; i < getItems().size(); i++) {
284            if (PageGroup.class.isAssignableFrom(getItems().get(i).getClass())
285                    || TabNavigationGroup.class.isAssignableFrom(getItems().get(i).getClass())) {
286                String currentValues[] = {"item(" + i + ").class =" + getItems().get(i).getClass()};
287                tracer.createError("Items in PageGroup cannot be PageGroup or NaviagtionGroup", currentValues);
288            }
289        }
290
291        super.completeValidation(tracer.getCopy());
292    }
293
294}