001
014
015 package com.liferay.taglib.ui.display.context;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.portlet.LiferayWindowState;
020 import com.liferay.portal.kernel.portlet.PortletProvider;
021 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
022 import com.liferay.portal.kernel.servlet.SessionErrors;
023 import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.JavaConstants;
026 import com.liferay.portal.kernel.util.ListUtil;
027 import com.liferay.portal.kernel.util.ParamUtil;
028 import com.liferay.portal.kernel.util.PredicateFilter;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.util.WebKeys;
033 import com.liferay.portal.model.Group;
034 import com.liferay.portal.service.GroupLocalServiceUtil;
035 import com.liferay.portal.theme.ThemeDisplay;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
038 import com.liferay.portlet.asset.model.AssetEntry;
039 import com.liferay.portlet.asset.model.AssetLink;
040 import com.liferay.portlet.asset.model.AssetRendererFactory;
041 import com.liferay.portlet.asset.model.ClassType;
042 import com.liferay.portlet.asset.model.ClassTypeReader;
043 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
044 import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
045 import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
046 import com.liferay.portlet.asset.util.comparator.AssetRendererFactoryTypeNameComparator;
047 import com.liferay.taglib.util.TagResourceBundleUtil;
048
049 import java.util.ArrayList;
050 import java.util.Collections;
051 import java.util.HashMap;
052 import java.util.List;
053 import java.util.Map;
054 import java.util.ResourceBundle;
055
056 import javax.portlet.PortletMode;
057 import javax.portlet.PortletRequest;
058 import javax.portlet.PortletURL;
059
060 import javax.servlet.http.HttpServletRequest;
061 import javax.servlet.jsp.PageContext;
062
063
066 public class InputAssetLinksDisplayContext {
067
068 public InputAssetLinksDisplayContext(PageContext pageContext) {
069 _pageContext = pageContext;
070
071 _request = (HttpServletRequest)pageContext.getRequest();
072
073 _assetEntryId = GetterUtil.getLong(
074 (String)_request.getAttribute(
075 "liferay-ui:input-asset-links:assetEntryId"));
076 _portletRequest = (PortletRequest)_request.getAttribute(
077 JavaConstants.JAVAX_PORTLET_REQUEST);
078 _themeDisplay = (ThemeDisplay)_request.getAttribute(
079 WebKeys.THEME_DISPLAY);
080 }
081
082 public AssetEntry getAssetLinkEntry(AssetLink assetLink)
083 throws PortalException {
084
085 if ((_assetEntryId > 0) || (assetLink.getEntryId1() == _assetEntryId)) {
086 return AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId2());
087 }
088
089 return AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId1());
090 }
091
092 public List<AssetLink> getAssetLinks() throws PortalException {
093 if (_assetLinks == null) {
094 _assetLinks = _createAssetLinks();
095 }
096
097 return _assetLinks;
098 }
099
100 public int getAssetLinksCount() throws PortalException {
101 List<AssetLink> assetLinks = getAssetLinks();
102
103 return assetLinks.size();
104 }
105
106 public List<AssetRendererFactory<?>> getAssetRendererFactories() {
107 List<AssetRendererFactory<?>> assetRendererFactories =
108 AssetRendererFactoryRegistryUtil.getAssetRendererFactories(
109 _themeDisplay.getCompanyId());
110
111 assetRendererFactories = ListUtil.filter(
112 assetRendererFactories,
113 new PredicateFilter<AssetRendererFactory<?>>() {
114
115 @Override
116 public boolean filter(
117 AssetRendererFactory<?> assetRendererFactory) {
118
119 if (assetRendererFactory.isLinkable() &&
120 assetRendererFactory.isSelectable()) {
121
122 return true;
123 }
124
125 return false;
126 }
127
128 });
129
130 return ListUtil.sort(
131 assetRendererFactories,
132 new AssetRendererFactoryTypeNameComparator(
133 _themeDisplay.getLocale()));
134 }
135
136 public String getAssetType(AssetEntry entry) {
137 AssetRendererFactory<?> assetRendererFactory =
138 entry.getAssetRendererFactory();
139
140 return assetRendererFactory.getTypeName(_themeDisplay.getLocale());
141 }
142
143 public String getEventName() {
144 if (_eventName != null) {
145 return _eventName;
146 }
147
148 _eventName = _randomNamespace + "selectAsset";
149
150 return _eventName;
151 }
152
153 public String getGroupDescriptiveName(AssetEntry assetEntry)
154 throws PortalException {
155
156 Group group = GroupLocalServiceUtil.getGroup(assetEntry.getGroupId());
157
158 return group.getDescriptiveName(_themeDisplay.getLocale());
159 }
160
161 public String getRandomNamespace() {
162 if (_randomNamespace != null) {
163 return _randomNamespace;
164 }
165
166 _randomNamespace = PortalUtil.generateRandomKey(
167 _request, "taglib_ui_input_asset_links_page") +
168 StringPool.UNDERLINE;
169
170 return _randomNamespace;
171 }
172
173 public List<Map<String, Object>> getSelectorEntries() throws Exception {
174 List<Map<String, Object>> selectorEntries = new ArrayList<>();
175
176 for (AssetRendererFactory<?> assetRendererFactory :
177 getAssetRendererFactories()) {
178
179 if (assetRendererFactory.isSupportsClassTypes()) {
180 selectorEntries.addAll(
181 _getSelectorEntries(assetRendererFactory));
182 }
183 else {
184 Map<String, Object> selectorEntry = new HashMap<>();
185
186 selectorEntry.put(
187 "data", _geSelectorEntryData(assetRendererFactory));
188 selectorEntry.put(
189 "iconCssClass",
190 _getSelectorEntryIconCssClass(assetRendererFactory));
191 selectorEntry.put(
192 "id", _getSelectorEntryId(assetRendererFactory));
193 selectorEntry.put(
194 "message", _getSelectorEntryMessage(assetRendererFactory));
195
196 selectorEntries.add(selectorEntry);
197 }
198 }
199
200 return selectorEntries;
201 }
202
203 private List<AssetLink> _createAssetLinks() throws PortalException {
204 List<AssetLink> assetLinks = new ArrayList<>();
205
206 String assetLinksSearchContainerPrimaryKeys = ParamUtil.getString(
207 _request, "assetLinksSearchContainerPrimaryKeys");
208
209 if (Validator.isNull(assetLinksSearchContainerPrimaryKeys) &&
210 SessionErrors.isEmpty(_portletRequest) && (_assetEntryId > 0)) {
211
212 List<AssetLink> directAssetLinks =
213 AssetLinkLocalServiceUtil.getDirectLinks(_assetEntryId);
214
215 for (AssetLink assetLink : directAssetLinks) {
216 AssetEntry assetLinkEntry = getAssetLinkEntry(assetLink);
217
218 AssetRendererFactory<?> assetRendererFactory =
219 AssetRendererFactoryRegistryUtil.
220 getAssetRendererFactoryByClassName(
221 assetLinkEntry.getClassName());
222
223 if (assetRendererFactory.isActive(
224 _themeDisplay.getCompanyId())) {
225
226 assetLinks.add(assetLink);
227 }
228 }
229 }
230 else {
231 String[] assetEntriesPrimaryKeys = StringUtil.split(
232 assetLinksSearchContainerPrimaryKeys);
233
234 for (String assetEntryPrimaryKey : assetEntriesPrimaryKeys) {
235 long assetEntryPrimaryKeyLong = GetterUtil.getLong(
236 assetEntryPrimaryKey);
237
238 AssetEntry assetEntry = AssetEntryServiceUtil.getEntry(
239 assetEntryPrimaryKeyLong);
240
241 AssetLink assetLink = AssetLinkLocalServiceUtil.createAssetLink(
242 0);
243
244 if (_assetEntryId > 0) {
245 assetLink.setEntryId1(_assetEntryId);
246 }
247 else {
248 assetLink.setEntryId1(0);
249 }
250
251 assetLink.setEntryId2(assetEntry.getEntryId());
252
253 assetLinks.add(assetLink);
254 }
255 }
256
257 return assetLinks;
258 }
259
260 private Map<String, Object> _geSelectorEntryData(
261 AssetRendererFactory<?> assetRendererFactory)
262 throws Exception {
263
264 Map<String, Object> selectorEntryData = new HashMap<>();
265
266 selectorEntryData.put(
267 "href",
268 _getAssetBrowserPortletURL(assetRendererFactory).toString());
269
270 ResourceBundle resourceBundle = TagResourceBundleUtil.getResourceBundle(
271 _pageContext);
272
273 String typeName = assetRendererFactory.getTypeName(
274 _themeDisplay.getLocale());
275
276 selectorEntryData.put(
277 "title",
278 LanguageUtil.format(resourceBundle, "select-x", typeName, false));
279
280 selectorEntryData.put("type", assetRendererFactory.getClassName());
281
282 return selectorEntryData;
283 }
284
285 private long _getAssetBrowserGroupId(
286 AssetRendererFactory<?> assetRendererFactory) {
287
288 Group scopeGroup = _themeDisplay.getScopeGroup();
289
290 long groupId = scopeGroup.getGroupId();
291
292 if (_isStagedLocally() && scopeGroup.isStagingGroup()) {
293 boolean stagedReferencePortlet = scopeGroup.isStagedPortlet(
294 assetRendererFactory.getPortletId());
295
296 if (_isStagedReferrerPortlet() && !stagedReferencePortlet) {
297 groupId = scopeGroup.getLiveGroupId();
298 }
299 }
300
301 return groupId;
302 }
303
304 private PortletURL _getAssetBrowserPortletURL(
305 AssetRendererFactory<?> assetRendererFactory)
306 throws Exception {
307
308 PortletURL portletURL = PortletProviderUtil.getPortletURL(
309 _request, assetRendererFactory.getClassName(),
310 PortletProvider.Action.BROWSE);
311
312 long groupId = _getAssetBrowserGroupId(assetRendererFactory);
313
314 portletURL.setParameter("groupId", String.valueOf(groupId));
315 portletURL.setParameter(
316 "selectedGroupIds",
317 StringUtil.merge(
318 PortalUtil.getSharedContentSiteGroupIds(
319 _themeDisplay.getCompanyId(), groupId,
320 _themeDisplay.getUserId())));
321
322 if (_assetEntryId > 0) {
323 portletURL.setParameter(
324 "refererAssetEntryId", String.valueOf(_assetEntryId));
325 }
326
327 portletURL.setParameter(
328 "typeSelection", assetRendererFactory.getClassName());
329 portletURL.setParameter("eventName", getEventName());
330 portletURL.setPortletMode(PortletMode.VIEW);
331 portletURL.setWindowState(LiferayWindowState.POP_UP);
332
333 return portletURL;
334 }
335
336 private List<Map<String, Object>> _getSelectorEntries(
337 AssetRendererFactory<?> assetRendererFactory)
338 throws Exception {
339
340 long groupId = _getAssetBrowserGroupId(assetRendererFactory);
341
342 ClassTypeReader classTypeReader =
343 assetRendererFactory.getClassTypeReader();
344
345 List<ClassType> classTypes = classTypeReader.getAvailableClassTypes(
346 PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId),
347 _themeDisplay.getLocale());
348
349 if (classTypes.isEmpty()) {
350 return Collections.emptyList();
351 }
352
353 List<Map<String, Object>> selectorEntries = new ArrayList<>();
354
355 for (ClassType classType : classTypes) {
356 Map<String, Object> selectorEntry = new HashMap<>();
357
358 selectorEntry.put(
359 "data",
360 _getSelectorEntryData(assetRendererFactory, classType));
361 selectorEntry.put(
362 "iconCssClass",
363 _getSelectorEntryIconCssClass(assetRendererFactory));
364 selectorEntry.put(
365 "id",
366 _getSelectorEntryId(assetRendererFactory, classType));
367 selectorEntry.put("message", _getSelectorEntryMessage(classType));
368
369 selectorEntries.add(selectorEntry);
370 }
371
372 return selectorEntries;
373 }
374
375 private Map<String, Object> _getSelectorEntryData(
376 AssetRendererFactory<?> assetRendererFactory, ClassType classType)
377 throws Exception {
378
379 Map<String, Object> selectorEntryData = new HashMap<>();
380
381 PortletURL portletURL = _getAssetBrowserPortletURL(
382 assetRendererFactory);
383
384 portletURL.setParameter(
385 "subtypeSelectionId", String.valueOf(classType.getClassTypeId()));
386
387 selectorEntryData.put("href", portletURL.toString());
388
389 ResourceBundle resourceBundle = TagResourceBundleUtil.getResourceBundle(
390 _pageContext);
391
392 selectorEntryData.put(
393 "title",
394 LanguageUtil.format(
395 resourceBundle, "select-x", classType.getName(), false));
396
397 selectorEntryData.put("type", classType.getName());
398
399 return selectorEntryData;
400 }
401
402 private String _getSelectorEntryIconCssClass(
403 AssetRendererFactory<?> assetRendererFactory)
404 throws Exception {
405
406 return assetRendererFactory.getIconCssClass();
407 }
408
409 private String _getSelectorEntryId(
410 AssetRendererFactory<?> assetRendererFactory) {
411
412 return FriendlyURLNormalizerUtil.normalize(
413 assetRendererFactory.getTypeName(_themeDisplay.getLocale()));
414 }
415
416 private String _getSelectorEntryId(
417 AssetRendererFactory<?> assetRendererFactory, ClassType classType) {
418
419 String selectorEntryId = String.valueOf(
420 _getAssetBrowserGroupId(assetRendererFactory));
421
422 selectorEntryId += FriendlyURLNormalizerUtil.normalize(
423 classType.getName());
424
425 return selectorEntryId;
426 }
427
428 private String _getSelectorEntryMessage(
429 AssetRendererFactory<?> assetRendererFactory) {
430
431 return assetRendererFactory.getTypeName(_themeDisplay.getLocale());
432 }
433
434 private String _getSelectorEntryMessage(ClassType classType) {
435 return classType.getName();
436 }
437
438 private boolean _isStagedLocally() {
439 if (_stagedLocally != null) {
440 return _stagedLocally;
441 }
442
443 Group scopeGroup = _themeDisplay.getScopeGroup();
444
445 if (scopeGroup.isStaged() && !scopeGroup.isStagedRemotely()) {
446 _stagedLocally = true;
447 }
448 else {
449 _stagedLocally = false;
450 }
451
452 return _stagedLocally;
453 }
454
455 private boolean _isStagedReferrerPortlet() {
456 if (_stagedReferrerPortlet != null) {
457 return _stagedReferrerPortlet;
458 }
459
460 if (_isStagedLocally()) {
461 String className = (String)_request.getAttribute(
462 "liferay-ui:input-asset-links:className");
463
464 AssetRendererFactory<?> assetRendererFactory =
465 AssetRendererFactoryRegistryUtil.
466 getAssetRendererFactoryByClassName(className);
467
468 Group scopeGroup = _themeDisplay.getScopeGroup();
469
470 _stagedReferrerPortlet = scopeGroup.isStagedPortlet(
471 assetRendererFactory.getPortletId());
472 }
473 else {
474 _stagedReferrerPortlet = false;
475 }
476
477 return _stagedReferrerPortlet;
478 }
479
480 private final long _assetEntryId;
481 private List<AssetLink> _assetLinks;
482 private String _eventName;
483 private final PageContext _pageContext;
484 private final PortletRequest _portletRequest;
485 private String _randomNamespace;
486 private final HttpServletRequest _request;
487 private Boolean _stagedLocally;
488 private Boolean _stagedReferrerPortlet;
489 private final ThemeDisplay _themeDisplay;
490
491 }