001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.servlet.taglib.ui.FormNavigatorCategoryUtil;
018    import com.liferay.portal.kernel.servlet.taglib.ui.FormNavigatorEntryUtil;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.taglib.util.IncludeTag;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * @author Julio Camarero
029     * @author Sergio Gonz??lez
030     */
031    public class FormNavigatorTag extends IncludeTag {
032    
033            @Override
034            public int doStartTag() {
035                    return EVAL_BODY_INCLUDE;
036            }
037    
038            public void setBackURL(String backURL) {
039                    _backURL = backURL;
040            }
041    
042            /**
043             * @deprecated As of 7.0.0
044             */
045            @Deprecated
046            public void setCategoryNames(String[] categoryNames) {
047                    _categoryNames = categoryNames;
048            }
049    
050            /**
051             * @deprecated As of 7.0.0
052             */
053            @Deprecated
054            public void setCategorySections(String[][] categorySections) {
055                    _categorySections = categorySections;
056            }
057    
058            public void setDisplayStyle(String displayStyle) {
059                    _displayStyle = displayStyle;
060            }
061    
062            public void setFormModelBean(Object formModelBean) {
063                    _formModelBean = formModelBean;
064            }
065    
066            public void setFormName(String formName) {
067                    _formName = formName;
068            }
069    
070            public void setHtmlBottom(String htmlBottom) {
071                    _htmlBottom = htmlBottom;
072            }
073    
074            public void setHtmlTop(String htmlTop) {
075                    _htmlTop = htmlTop;
076            }
077    
078            public void setId(String id) {
079                    _id = id;
080            }
081    
082            /**
083             * @deprecated As of 7.0.0
084             */
085            @Deprecated
086            public void setJspPath(String jspPath) {
087                    _jspPath = jspPath;
088            }
089    
090            public void setMarkupView(String markupView) {
091                    _markupView = markupView;
092            }
093    
094            public void setShowButtons(boolean showButtons) {
095                    _showButtons = showButtons;
096            }
097    
098            @Override
099            protected void cleanUp() {
100                    _backURL = null;
101                    _categoryNames = null;
102                    _categorySections = null;
103                    _displayStyle = "form";
104                    _formModelBean = null;
105                    _formName = "fm";
106                    _htmlBottom = null;
107                    _htmlTop = null;
108                    _id = null;
109                    _jspPath = null;
110                    _markupView = null;
111                    _showButtons = true;
112            }
113    
114            protected String[] getCategoryKeys() {
115                    if (_categoryNames != null) {
116                            return _categoryNames;
117                    }
118    
119                    return FormNavigatorCategoryUtil.getKeys(_id);
120            }
121    
122            protected String[] getCategoryLabels() {
123                    if (_categoryNames != null) {
124                            return _categoryNames;
125                    }
126    
127                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
128                            WebKeys.THEME_DISPLAY);
129    
130                    return FormNavigatorCategoryUtil.getLabels(
131                            _id, themeDisplay.getLocale());
132            }
133    
134            protected String[][] getCategorySectionKeys() {
135                    if (_categorySections != null) {
136                            return _categorySections;
137                    }
138    
139                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
140                            WebKeys.THEME_DISPLAY);
141    
142                    String[] categoryKeys = getCategoryKeys();
143    
144                    String[][] categorySectionKeys = new String[0][];
145    
146                    for (int i = 0; i < categoryKeys.length; i++) {
147                            String categoryKey = categoryKeys[i];
148    
149                            categorySectionKeys = ArrayUtil.append(
150                                    categorySectionKeys,
151                                    FormNavigatorEntryUtil.getKeys(
152                                            _id, categoryKey, themeDisplay.getUser(), _formModelBean));
153                    }
154    
155                    return categorySectionKeys;
156            }
157    
158            protected String[][] getCategorySectionLabels() {
159                    if (_categorySections != null) {
160                            return _categorySections;
161                    }
162    
163                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
164                            WebKeys.THEME_DISPLAY);
165    
166                    String[] categoryKeys = getCategoryKeys();
167    
168                    String[][] categorySectionLabels = new String[0][];
169    
170                    for (int i = 0; i < categoryKeys.length; i++) {
171                            String categoryKey = categoryKeys[i];
172    
173                            categorySectionLabels = ArrayUtil.append(
174                                    categorySectionLabels,
175                                    FormNavigatorEntryUtil.getLabels(
176                                            _id, categoryKey, themeDisplay.getUser(), _formModelBean,
177                                            themeDisplay.getLocale()));
178                    }
179    
180                    return categorySectionLabels;
181            }
182    
183            protected String[] getDeprecatedCategorySections() {
184                    if (_categorySections == null) {
185                            return new String[0];
186                    }
187    
188                    String[] deprecatedCategorySections = new String[0];
189    
190                    for (String[] categorySection : _categorySections) {
191                            deprecatedCategorySections = ArrayUtil.append(
192                                    deprecatedCategorySections, categorySection);
193                    }
194    
195                    return deprecatedCategorySections;
196            }
197    
198            @Override
199            protected String getPage() {
200                    if (Validator.isNotNull(_markupView)) {
201                            return
202                                    "/html/taglib/ui/form_navigator/" + _markupView + "/page.jsp";
203                    }
204    
205                    return "/html/taglib/ui/form_navigator/page.jsp";
206            }
207    
208            @Override
209            protected void setAttributes(HttpServletRequest request) {
210                    request.setAttribute("liferay-ui:form-navigator:backURL", _backURL);
211                    request.setAttribute(
212                            "liferay-ui:form-navigator:categoryKeys", getCategoryKeys());
213                    request.setAttribute(
214                            "liferay-ui:form-navigator:categoryLabels", getCategoryLabels());
215                    request.setAttribute(
216                            "liferay-ui:form-navigator:categorySectionKeys",
217                            getCategorySectionKeys());
218                    request.setAttribute(
219                            "liferay-ui:form-navigator:categorySectionLabels",
220                            getCategorySectionLabels());
221                    request.setAttribute(
222                            "liferay-ui:form-navigator:deprecatedCategorySections",
223                            getDeprecatedCategorySections());
224                    request.setAttribute(
225                            "liferay-ui:form-navigator:displayStyle", _displayStyle);
226                    request.setAttribute(
227                            "liferay-ui:form-navigator:formModelBean", _formModelBean);
228                    request.setAttribute("liferay-ui:form-navigator:formName", _formName);
229                    request.setAttribute("liferay-ui:form-navigator:id", _id);
230                    request.setAttribute(
231                            "liferay-ui:form-navigator:htmlBottom", _htmlBottom);
232                    request.setAttribute("liferay-ui:form-navigator:htmlTop", _htmlTop);
233                    request.setAttribute("liferay-ui:form-navigator:jspPath", _jspPath);
234                    request.setAttribute(
235                            "liferay-ui:form-navigator:showButtons",
236                            String.valueOf(_showButtons));
237            }
238    
239            private String _backURL;
240            private String[] _categoryNames;
241            private String[][] _categorySections;
242            private String _displayStyle = "form";
243            private Object _formModelBean;
244            private String _formName = "fm";
245            private String _htmlBottom;
246            private String _htmlTop;
247            private String _id;
248            private String _jspPath;
249            private String _markupView;
250            private boolean _showButtons = true;
251    
252    }