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