001
014
015 package com.liferay.portlet.assetcategoriesnavigation.context;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.KeyValuePair;
021 import com.liferay.portal.kernel.util.KeyValuePairComparator;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.SetUtil;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.theme.ThemeDisplay;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portal.util.WebKeys;
029 import com.liferay.portlet.asset.model.AssetVocabulary;
030 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
031 import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil;
032
033 import java.util.ArrayList;
034 import java.util.Arrays;
035 import java.util.List;
036 import java.util.Set;
037
038 import javax.portlet.PortletPreferences;
039
040 import javax.servlet.http.HttpServletRequest;
041
042
045 public class AssetCategoriesNavigationDisplayContext {
046
047 public AssetCategoriesNavigationDisplayContext(
048 HttpServletRequest request, PortletPreferences portletPreferences) {
049
050 _request = request;
051 _portletPreferences = portletPreferences;
052 }
053
054 public List<AssetVocabulary> getAssetVocabularies() throws PortalException {
055 if (_assetVocabularies != null) {
056 return _assetVocabularies;
057 }
058
059 ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
060 WebKeys.THEME_DISPLAY);
061
062 long[] groupIds = PortalUtil.getCurrentAndAncestorSiteGroupIds(
063 themeDisplay.getScopeGroupId());
064
065 _assetVocabularies = AssetVocabularyServiceUtil.getGroupVocabularies(
066 groupIds);
067
068 return _assetVocabularies;
069 }
070
071 public long[] getAssetVocabularyIds() throws PortalException {
072 if (_assetVocabularyIds != null) {
073 return _assetVocabularyIds;
074 }
075
076 _assetVocabularyIds = getAvailableAssetVocabularyIds();
077
078 if (!isAllAssetVocabularies() &&
079 (_portletPreferences.getValues(
080 "assetVocabularyIds", null) != null)) {
081
082 _assetVocabularyIds = StringUtil.split(
083 _portletPreferences.getValue("assetVocabularyIds", null), 0L);
084 }
085
086 return _assetVocabularyIds;
087 }
088
089 public long[] getAvailableAssetVocabularyIds() throws PortalException {
090 if (_availableAssetVocabularyIds != null) {
091 return _availableAssetVocabularyIds;
092 }
093
094 List<AssetVocabulary> assetVocabularies = getAssetVocabularies();
095
096 _availableAssetVocabularyIds = new long[assetVocabularies.size()];
097
098 for (int i = 0; i < assetVocabularies.size(); i++) {
099 AssetVocabulary assetVocabulary = assetVocabularies.get(i);
100
101 _availableAssetVocabularyIds[i] = assetVocabulary.getVocabularyId();
102 }
103
104 return _availableAssetVocabularyIds;
105 }
106
107 public List<KeyValuePair> getAvailableVocabularyNames()
108 throws PortalException {
109
110 List<KeyValuePair> availableVocabularNames =
111 new ArrayList<KeyValuePair>();
112
113 long[] assetVocabularyIds = getAssetVocabularyIds();
114
115 Arrays.sort(assetVocabularyIds);
116
117 Set<Long> availableAssetVocabularyIdsSet = SetUtil.fromArray(
118 getAvailableAssetVocabularyIds());
119
120 for (long assetVocabularyId : availableAssetVocabularyIdsSet) {
121 if (Arrays.binarySearch(
122 assetVocabularyIds, assetVocabularyId) < 0) {
123
124 AssetVocabulary assetVocabulary =
125 AssetVocabularyLocalServiceUtil.fetchAssetVocabulary(
126 assetVocabularyId);
127
128 assetVocabulary = assetVocabulary.toEscapedModel();
129
130 availableVocabularNames.add(
131 new KeyValuePair(
132 String.valueOf(assetVocabularyId),
133 getTitle(assetVocabulary)));
134 }
135 }
136
137 return ListUtil.sort(
138 availableVocabularNames, new KeyValuePairComparator(false, true));
139 }
140
141 public List<KeyValuePair> getCurrentVocabularyNames()
142 throws PortalException {
143
144 List<KeyValuePair> currentVocabularNames =
145 new ArrayList<KeyValuePair>();
146
147 for (long assetVocabularyId : getAssetVocabularyIds()) {
148 AssetVocabulary assetVocabulary =
149 AssetVocabularyLocalServiceUtil.fetchAssetVocabulary(
150 assetVocabularyId);
151
152 assetVocabulary = assetVocabulary.toEscapedModel();
153
154 currentVocabularNames.add(
155 new KeyValuePair(
156 String.valueOf(assetVocabularyId),
157 getTitle(assetVocabulary)));
158 }
159
160 return currentVocabularNames;
161 }
162
163 public List<AssetVocabulary> getDDMTemplateAssetVocabularies()
164 throws PortalException {
165
166 if (_ddmTemplateAssetVocabularies != null) {
167 return _ddmTemplateAssetVocabularies;
168 }
169
170 _ddmTemplateAssetVocabularies = new ArrayList<AssetVocabulary>();
171
172 if (isAllAssetVocabularies()) {
173 _ddmTemplateAssetVocabularies = getAssetVocabularies();
174
175 return _ddmTemplateAssetVocabularies;
176 }
177
178 for (long assetVocabularyId : getAssetVocabularyIds()) {
179 _ddmTemplateAssetVocabularies.add(
180 AssetVocabularyServiceUtil.fetchVocabulary(assetVocabularyId));
181 }
182
183 return _ddmTemplateAssetVocabularies;
184 }
185
186 public String getDisplayStyle() {
187 if (_displayStyle != null) {
188 return _displayStyle;
189 }
190
191 _displayStyle = _portletPreferences.getValue(
192 "displayStyle", StringPool.BLANK);
193
194 return _displayStyle;
195 }
196
197 public long getDisplayStyleGroupId() {
198 if (_displayStyleGroupId != null) {
199 return _displayStyleGroupId;
200 }
201
202 ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
203 WebKeys.THEME_DISPLAY);
204
205 _displayStyleGroupId = GetterUtil.getLong(
206 _portletPreferences.getValue("displayStyleGroupId", null),
207 themeDisplay.getScopeGroupId());
208
209 return _displayStyleGroupId;
210 }
211
212 public boolean isAllAssetVocabularies() {
213 if (_allAssetVocabularies != null) {
214 return _allAssetVocabularies;
215 }
216
217 _allAssetVocabularies = GetterUtil.getBoolean(
218 _portletPreferences.getValue(
219 "allAssetVocabularies", Boolean.TRUE.toString()));
220
221 return _allAssetVocabularies;
222 }
223
224 protected String getTitle(AssetVocabulary assetVocabulary) {
225 ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
226 WebKeys.THEME_DISPLAY);
227
228 String title = assetVocabulary.getTitle(themeDisplay.getLanguageId());
229
230 if (assetVocabulary.getGroupId() == themeDisplay.getCompanyGroupId()) {
231 title += " (" + LanguageUtil.get(_request, "global") + ")";
232 }
233
234 return title;
235 }
236
237 private Boolean _allAssetVocabularies;
238 private List<AssetVocabulary> _assetVocabularies;
239 private long[] _assetVocabularyIds;
240 private long[] _availableAssetVocabularyIds;
241 private List<AssetVocabulary> _ddmTemplateAssetVocabularies;
242 private String _displayStyle;
243 private Long _displayStyleGroupId;
244 private final PortletPreferences _portletPreferences;
245 private final HttpServletRequest _request;
246
247 }