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.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.taglib.util.IncludeTag;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Jorge Ferrer
031     */
032    public class AssetTagsSelectorTag extends IncludeTag {
033    
034            public void setAutoFocus(boolean autoFocus) {
035                    _autoFocus = autoFocus;
036            }
037    
038            public void setClassName(String className) {
039                    _className = className;
040            }
041    
042            public void setClassPK(long classPK) {
043                    _classPK = classPK;
044            }
045    
046            public void setContentCallback(String contentCallback) {
047                    _contentCallback = contentCallback;
048            }
049    
050            public void setCurTags(String curTags) {
051                    _curTags = curTags;
052            }
053    
054            public void setGroupIds(long[] groupIds) {
055                    _groupIds = groupIds;
056            }
057    
058            public void setHiddenInput(String hiddenInput) {
059                    _hiddenInput = hiddenInput;
060            }
061    
062            public void setId(String id) {
063                    _id = id;
064            }
065    
066            @Override
067            protected void cleanUp() {
068                    _autoFocus = false;
069                    _className = null;
070                    _classPK = 0;
071                    _contentCallback = null;
072                    _curTags = null;
073                    _groupIds = null;
074                    _hiddenInput = "assetTagNames";
075                    _id = null;
076            }
077    
078            @Override
079            protected String getPage() {
080                    return _PAGE;
081            }
082    
083            @Override
084            protected void setAttributes(HttpServletRequest request) {
085                    String id = _id;
086    
087                    if (Validator.isNull(id)) {
088                            id = PortalUtil.generateRandomKey(
089                                    request,
090                                    "taglib_ui_asset_tags_selector_page") + StringPool.UNDERLINE;
091                    }
092    
093                    request.setAttribute(
094                            "liferay-ui:asset-tags-selector:autoFocus",
095                            String.valueOf(_autoFocus));
096                    request.setAttribute(
097                            "liferay-ui:asset-tags-selector:className", _className);
098                    request.setAttribute(
099                            "liferay-ui:asset-tags-selector:classPK", String.valueOf(_classPK));
100                    request.setAttribute(
101                            "liferay-ui:asset-tags-selector:contentCallback",
102                            String.valueOf(_contentCallback));
103                    request.setAttribute(
104                            "liferay-ui:asset-tags-selector:curTags", _curTags);
105    
106                    if (_groupIds == null) {
107                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
108                                    WebKeys.THEME_DISPLAY);
109    
110                            long[] groupIds = null;
111    
112                            Group group = themeDisplay.getScopeGroup();
113    
114                            if (group.isLayout()) {
115                                    groupIds = new long[] {group.getParentGroupId()};
116                            }
117                            else {
118                                    groupIds = new long[] {group.getGroupId()};
119                            }
120    
121                            if (group.getParentGroupId() != themeDisplay.getCompanyGroupId()) {
122                                    groupIds = ArrayUtil.append(
123                                            groupIds, themeDisplay.getCompanyGroupId());
124                            }
125    
126                            _groupIds = groupIds;
127                    }
128    
129                    request.setAttribute(
130                            "liferay-ui:asset-tags-selector:groupIds", _groupIds);
131    
132                    request.setAttribute(
133                            "liferay-ui:asset-tags-selector:hiddenInput", _hiddenInput);
134                    request.setAttribute("liferay-ui:asset-tags-selector:id", id);
135            }
136    
137            private static final String _PAGE =
138                    "/html/taglib/ui/asset_tags_selector/page.jsp";
139    
140            private boolean _autoFocus;
141            private String _className;
142            private long _classPK;
143            private String _contentCallback;
144            private String _curTags;
145            private long[] _groupIds;
146            private String _hiddenInput = "assetTagNames";
147            private String _id;
148    
149    }