001    /**
002     * Copyright (c) 2000-2013 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.portlet.journal.search;
016    
017    import com.liferay.portal.kernel.dao.search.DisplayTerms;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.WebKeys;
024    
025    import javax.portlet.PortletRequest;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class StructureDisplayTerms extends DisplayTerms {
031    
032            public static final String DESCRIPTION = "description";
033    
034            public static final String GROUP_IDS = "groupIds";
035    
036            public static final String NAME = "name";
037    
038            public static final String STRUCTURE_ID = "searchStructureId";
039    
040            public StructureDisplayTerms(PortletRequest portletRequest) {
041                    super(portletRequest);
042    
043                    description = ParamUtil.getString(portletRequest, DESCRIPTION);
044                    groupIds = setGroupIds(portletRequest);
045                    name = ParamUtil.getString(portletRequest, NAME);
046                    structureId = ParamUtil.getString(portletRequest, STRUCTURE_ID);
047            }
048    
049            public String getDescription() {
050                    return description;
051            }
052    
053            public long getGroupId() {
054                    if (groupIds.length == 1) {
055                            return groupIds[0];
056                    }
057                    else {
058                            return 0;
059                    }
060            }
061    
062            public long[] getGroupIds() {
063                    return groupIds;
064            }
065    
066            public String getGroupIds(PortletRequest portletRequest) {
067                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
068                            WebKeys.THEME_DISPLAY);
069    
070                    String strutsAction = ParamUtil.getString(
071                            portletRequest, "struts_action");
072    
073                    StringBundler sb = new StringBundler();
074    
075                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
076    
077                    if (groupId > 0) {
078                            sb.append(groupId);
079                    }
080                    else {
081                            sb.append(themeDisplay.getScopeGroupId());
082                    }
083    
084                    if (strutsAction.equalsIgnoreCase("/journal/select_structure")) {
085                            sb.append(StringPool.COMMA);
086                            sb.append(themeDisplay.getCompanyGroupId());
087                    }
088    
089                    return sb.toString();
090            }
091    
092            public String getName() {
093                    return name;
094            }
095    
096            public String getStructureId() {
097                    return structureId;
098            }
099    
100            protected long[] setGroupIds(PortletRequest portletRequest) {
101                    String groupIdsString = ParamUtil.getString(
102                            portletRequest, GROUP_IDS, getGroupIds(portletRequest));
103    
104                    return StringUtil.split(groupIdsString, 0L);
105            }
106    
107            protected String description;
108            protected long[] groupIds;
109            protected String name;
110            protected String structureId;
111    
112    }