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.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    
048    import java.util.ArrayList;
049    import java.util.Collections;
050    import java.util.HashMap;
051    import java.util.List;
052    import java.util.Map;
053    
054    import javax.portlet.PortletMode;
055    import javax.portlet.PortletRequest;
056    import javax.portlet.PortletURL;
057    
058    import javax.servlet.http.HttpServletRequest;
059    import javax.servlet.jsp.PageContext;
060    
061    /**
062     * @author Jos?? Manuel Navarro
063     */
064    public class InputAssetLinksDisplayContext {
065    
066            public InputAssetLinksDisplayContext(PageContext pageContext) {
067                    _pageContext = pageContext;
068    
069                    _request = (HttpServletRequest)pageContext.getRequest();
070    
071                    _assetEntryId = GetterUtil.getLong(
072                            (String)_request.getAttribute(
073                                    "liferay-ui:input-asset-links:assetEntryId"));
074                    _portletRequest = (PortletRequest)_request.getAttribute(
075                            JavaConstants.JAVAX_PORTLET_REQUEST);
076                    _themeDisplay = (ThemeDisplay)_request.getAttribute(
077                            WebKeys.THEME_DISPLAY);
078            }
079    
080            public AssetEntry getAssetLinkEntry(AssetLink assetLink)
081                    throws PortalException {
082    
083                    if ((_assetEntryId > 0) || (assetLink.getEntryId1() == _assetEntryId)) {
084                            return AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId2());
085                    }
086    
087                    return AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId1());
088            }
089    
090            public List<AssetLink> getAssetLinks() throws PortalException {
091                    if (_assetLinks == null) {
092                            _assetLinks = _createAssetLinks();
093                    }
094    
095                    return _assetLinks;
096            }
097    
098            public int getAssetLinksCount() throws PortalException {
099                    List<AssetLink> assetLinks = getAssetLinks();
100    
101                    return assetLinks.size();
102            }
103    
104            public List<AssetRendererFactory<?>> getAssetRendererFactories() {
105                    List<AssetRendererFactory<?>> assetRendererFactories =
106                            AssetRendererFactoryRegistryUtil.getAssetRendererFactories(
107                                    _themeDisplay.getCompanyId());
108    
109                    assetRendererFactories = ListUtil.filter(
110                            assetRendererFactories,
111                            new PredicateFilter<AssetRendererFactory<?>>() {
112    
113                                    @Override
114                                    public boolean filter(
115                                            AssetRendererFactory<?> assetRendererFactory) {
116    
117                                            if (assetRendererFactory.isLinkable() &&
118                                                    assetRendererFactory.isSelectable()) {
119    
120                                                    return true;
121                                            }
122    
123                                            return false;
124                                    }
125    
126                            });
127    
128                    return ListUtil.sort(
129                            assetRendererFactories,
130                            new AssetRendererFactoryTypeNameComparator(
131                                    _themeDisplay.getLocale()));
132            }
133    
134            public String getAssetType(AssetEntry entry) {
135                    AssetRendererFactory<?> assetRendererFactory =
136                            entry.getAssetRendererFactory();
137    
138                    return assetRendererFactory.getTypeName(_themeDisplay.getLocale());
139            }
140    
141            public String getEventName() {
142                    if (_eventName != null) {
143                            return _eventName;
144                    }
145    
146                    _eventName = _randomNamespace + "selectAsset";
147    
148                    return _eventName;
149            }
150    
151            public String getGroupDescriptiveName(AssetEntry assetEntry)
152                    throws PortalException {
153    
154                    Group group = GroupLocalServiceUtil.getGroup(assetEntry.getGroupId());
155    
156                    return group.getDescriptiveName(_themeDisplay.getLocale());
157            }
158    
159            public String getRandomNamespace() {
160                    if (_randomNamespace != null) {
161                            return _randomNamespace;
162                    }
163    
164                    _randomNamespace = PortalUtil.generateRandomKey(
165                            _request, "taglib_ui_input_asset_links_page") +
166                            StringPool.UNDERLINE;
167    
168                    return _randomNamespace;
169            }
170    
171            public List<Map<String, Object>> getSelectorEntries() throws Exception {
172                    List<Map<String, Object>> selectorEntries = new ArrayList<>();
173    
174                    for (AssetRendererFactory<?> assetRendererFactory :
175                                    getAssetRendererFactories()) {
176    
177                            if (assetRendererFactory.isSupportsClassTypes()) {
178                                    selectorEntries.addAll(
179                                            _getSelectorEntries(assetRendererFactory));
180                            }
181                            else {
182                                    Map<String, Object> selectorEntry = new HashMap<>();
183    
184                                    selectorEntry.put(
185                                            "data", _geSelectorEntryData(assetRendererFactory));
186                                    selectorEntry.put(
187                                            "iconCssClass",
188                                            _getSelectorEntryIconCssClass(assetRendererFactory));
189                                    selectorEntry.put(
190                                            "id", _getSelectorEntryId(assetRendererFactory));
191                                    selectorEntry.put(
192                                            "message", _getSelectorEntryMessage(assetRendererFactory));
193                                    selectorEntry.put(
194                                            "src", _getSelectorEntrySrc(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                    String typeName = assetRendererFactory.getTypeName(
271                            _themeDisplay.getLocale());
272    
273                    HttpServletRequest request =
274                            (HttpServletRequest)_pageContext.getRequest();
275    
276                    selectorEntryData.put(
277                            "title", LanguageUtil.format(request, "select-x", typeName, false));
278    
279                    selectorEntryData.put("type", assetRendererFactory.getClassName());
280    
281                    return selectorEntryData;
282            }
283    
284            private long _getAssetBrowserGroupId(
285                    AssetRendererFactory<?> assetRendererFactory) {
286    
287                    Group scopeGroup = _themeDisplay.getScopeGroup();
288    
289                    long groupId = scopeGroup.getGroupId();
290    
291                    if (_isStagedLocally()) {
292                            boolean stagedReferencePortlet = scopeGroup.isStagedPortlet(
293                                    assetRendererFactory.getPortletId());
294    
295                            if (_isStagedReferrerPortlet() && !stagedReferencePortlet) {
296                                    groupId = scopeGroup.getLiveGroupId();
297                            }
298                    }
299    
300                    return groupId;
301            }
302    
303            private PortletURL _getAssetBrowserPortletURL(
304                            AssetRendererFactory<?> assetRendererFactory)
305                    throws Exception {
306    
307                    PortletURL portletURL = PortletProviderUtil.getPortletURL(
308                            _request, assetRendererFactory.getClassName(),
309                            PortletProvider.Action.BROWSE);
310    
311                    long groupId = _getAssetBrowserGroupId(assetRendererFactory);
312    
313                    portletURL.setParameter("groupId", String.valueOf(groupId));
314                    portletURL.setParameter(
315                            "selectedGroupIds",
316                            StringUtil.merge(
317                                    PortalUtil.getSharedContentSiteGroupIds(
318                                            _themeDisplay.getCompanyId(), groupId,
319                                            _themeDisplay.getUserId())));
320    
321                    if (_assetEntryId > 0) {
322                            portletURL.setParameter(
323                                    "refererAssetEntryId", String.valueOf(_assetEntryId));
324                    }
325    
326                    portletURL.setParameter(
327                            "typeSelection", assetRendererFactory.getClassName());
328                    portletURL.setParameter("eventName", getEventName());
329                    portletURL.setPortletMode(PortletMode.VIEW);
330                    portletURL.setWindowState(LiferayWindowState.POP_UP);
331    
332                    return portletURL;
333            }
334    
335            private List<Map<String, Object>> _getSelectorEntries(
336                            AssetRendererFactory<?> assetRendererFactory)
337                    throws Exception {
338    
339                    long groupId = _getAssetBrowserGroupId(assetRendererFactory);
340    
341                    ClassTypeReader classTypeReader =
342                            assetRendererFactory.getClassTypeReader();
343    
344                    List<ClassType> classTypes = classTypeReader.getAvailableClassTypes(
345                            PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId),
346                            _themeDisplay.getLocale());
347    
348                    if (classTypes.isEmpty()) {
349                            return Collections.emptyList();
350                    }
351    
352                    List<Map<String, Object>> selectorEntries = new ArrayList<>();
353    
354                    for (ClassType classType : classTypes) {
355                            Map<String, Object> selectorEntry = new HashMap<>();
356    
357                            selectorEntry.put(
358                                    "data",
359                                    _getSelectorEntryData(assetRendererFactory, classType));
360                            selectorEntry.put(
361                                    "iconCssClass",
362                                    _getSelectorEntryIconCssClass(assetRendererFactory));
363                            selectorEntry.put(
364                                    "id",
365                                    _getSelectorEntryId(assetRendererFactory, classType));
366                            selectorEntry.put("message", _getSelectorEntryMessage(classType));
367                            selectorEntry.put(
368                                    "src", _getSelectorEntrySrc(assetRendererFactory));
369    
370                            selectorEntries.add(selectorEntry);
371                    }
372    
373                    return selectorEntries;
374            }
375    
376            private Map<String, Object> _getSelectorEntryData(
377                            AssetRendererFactory<?> assetRendererFactory, ClassType classType)
378                    throws Exception {
379    
380                    Map<String, Object> selectorEntryData = new HashMap<>();
381    
382                    PortletURL portletURL = _getAssetBrowserPortletURL(
383                            assetRendererFactory);
384    
385                    portletURL.setParameter(
386                            "subtypeSelectionId", String.valueOf(classType.getClassTypeId()));
387    
388                    selectorEntryData.put("href", portletURL.toString());
389    
390                    HttpServletRequest request =
391                            (HttpServletRequest)_pageContext.getRequest();
392    
393                    selectorEntryData.put(
394                            "title", LanguageUtil.format(
395                                    request, "select-x", classType.getName(), false));
396                    selectorEntryData.put("type", classType.getName());
397    
398                    return selectorEntryData;
399            }
400    
401            private String _getSelectorEntryIconCssClass(
402                            AssetRendererFactory<?> assetRendererFactory)
403                    throws Exception {
404    
405                    return assetRendererFactory.getIconCssClass();
406            }
407    
408            private String _getSelectorEntryId(
409                    AssetRendererFactory<?> assetRendererFactory) {
410    
411                    return FriendlyURLNormalizerUtil.normalize(
412                            assetRendererFactory.getTypeName(_themeDisplay.getLocale()));
413            }
414    
415            private String _getSelectorEntryId(
416                    AssetRendererFactory<?> assetRendererFactory, ClassType classType) {
417    
418                    String selectorEntryId = String.valueOf(
419                            _getAssetBrowserGroupId(assetRendererFactory));
420    
421                    selectorEntryId += FriendlyURLNormalizerUtil.normalize(
422                            classType.getName());
423    
424                    return selectorEntryId;
425            }
426    
427            private String _getSelectorEntryMessage(
428                    AssetRendererFactory<?> assetRendererFactory) {
429    
430                    return assetRendererFactory.getTypeName(_themeDisplay.getLocale());
431            }
432    
433            private String _getSelectorEntryMessage(ClassType classType) {
434                    return classType.getName();
435            }
436    
437            private String _getSelectorEntrySrc(
438                    AssetRendererFactory<?> assetRendererFactory) {
439    
440                    return assetRendererFactory.getIconPath(_portletRequest);
441            }
442    
443            private boolean _isStagedLocally() {
444                    if (_stagedLocally != null) {
445                            return _stagedLocally;
446                    }
447    
448                    Group scopeGroup = _themeDisplay.getScopeGroup();
449    
450                    if (scopeGroup.isStaged() && !scopeGroup.isStagedRemotely()) {
451                            _stagedLocally = true;
452                    }
453                    else {
454                            _stagedLocally = false;
455                    }
456    
457                    return _stagedLocally;
458            }
459    
460            private boolean _isStagedReferrerPortlet() {
461                    if (_stagedReferrerPortlet != null) {
462                            return _stagedReferrerPortlet;
463                    }
464    
465                    if (_isStagedLocally()) {
466                            String className = (String)_request.getAttribute(
467                                    "liferay-ui:input-asset-links:className");
468    
469                            AssetRendererFactory<?> assetRendererFactory =
470                                    AssetRendererFactoryRegistryUtil.
471                                            getAssetRendererFactoryByClassName(className);
472    
473                            Group scopeGroup = _themeDisplay.getScopeGroup();
474    
475                            _stagedReferrerPortlet = scopeGroup.isStagedPortlet(
476                                    assetRendererFactory.getPortletId());
477                    }
478                    else {
479                            _stagedReferrerPortlet = false;
480                    }
481    
482                    return _stagedReferrerPortlet;
483            }
484    
485            private final long _assetEntryId;
486            private List<AssetLink> _assetLinks;
487            private String _eventName;
488            private final PageContext _pageContext;
489            private final PortletRequest _portletRequest;
490            private String _randomNamespace;
491            private final HttpServletRequest _request;
492            private Boolean _stagedLocally;
493            private Boolean _stagedReferrerPortlet;
494            private final ThemeDisplay _themeDisplay;
495    
496    }