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.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.JavaConstants;
024    import com.liferay.portal.kernel.util.ListUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.PredicateFilter;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.kernel.util.WebKeys;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.service.GroupLocalServiceUtil;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portlet.PortletURLFactoryUtil;
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 =
173                            new ArrayList<Map<String, Object>>();
174    
175                    for (AssetRendererFactory assetRendererFactory :
176                                    getAssetRendererFactories()) {
177    
178                            if (assetRendererFactory.isSupportsClassTypes()) {
179                                    selectorEntries.addAll(
180                                            _getSelectorEntries(assetRendererFactory));
181                            }
182                            else {
183                                    Map<String, Object> selectorEntry =
184                                            new HashMap<String, Object>();
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                                    selectorEntry.put(
196                                            "src", _getSelectorEntrySrc(assetRendererFactory));
197    
198                                    selectorEntries.add(selectorEntry);
199                            }
200                    }
201    
202                    return selectorEntries;
203            }
204    
205            private List<AssetLink> _createAssetLinks() throws PortalException {
206                    List<AssetLink> assetLinks = new ArrayList<AssetLink>();
207    
208                    String assetLinksSearchContainerPrimaryKeys = ParamUtil.getString(
209                            _request, "assetLinksSearchContainerPrimaryKeys");
210    
211                    if (Validator.isNull(assetLinksSearchContainerPrimaryKeys) &&
212                            SessionErrors.isEmpty(_portletRequest) && (_assetEntryId > 0)) {
213    
214                            List<AssetLink> directAssetLinks =
215                                    AssetLinkLocalServiceUtil.getDirectLinks(_assetEntryId);
216    
217                            for (AssetLink assetLink : directAssetLinks) {
218                                    AssetEntry assetLinkEntry = getAssetLinkEntry(assetLink);
219    
220                                    AssetRendererFactory assetRendererFactory =
221                                            AssetRendererFactoryRegistryUtil.
222                                                    getAssetRendererFactoryByClassName(
223                                                            assetLinkEntry.getClassName());
224    
225                                    if (assetRendererFactory.isActive(
226                                                    _themeDisplay.getCompanyId())) {
227    
228                                            assetLinks.add(assetLink);
229                                    }
230                            }
231                    }
232                    else {
233                            String[] assetEntriesPrimaryKeys = StringUtil.split(
234                                    assetLinksSearchContainerPrimaryKeys);
235    
236                            for (String assetEntryPrimaryKey : assetEntriesPrimaryKeys) {
237                                    long assetEntryPrimaryKeyLong = GetterUtil.getLong(
238                                            assetEntryPrimaryKey);
239    
240                                    AssetEntry assetEntry = AssetEntryServiceUtil.getEntry(
241                                            assetEntryPrimaryKeyLong);
242    
243                                    AssetLink assetLink = AssetLinkLocalServiceUtil.createAssetLink(
244                                            0);
245    
246                                    if (_assetEntryId > 0) {
247                                            assetLink.setEntryId1(_assetEntryId);
248                                    }
249                                    else {
250                                            assetLink.setEntryId1(0);
251                                    }
252    
253                                    assetLink.setEntryId2(assetEntry.getEntryId());
254    
255                                    assetLinks.add(assetLink);
256                            }
257                    }
258    
259                    return assetLinks;
260            }
261    
262            private Map<String, Object> _geSelectorEntryData(
263                            AssetRendererFactory assetRendererFactory)
264                    throws Exception {
265    
266                    Map<String, Object> selectorEntryData = new HashMap<String, Object>();
267    
268                    selectorEntryData.put(
269                            "href",
270                            _getAssetBrowserPortletURL(assetRendererFactory).toString());
271    
272                    String typeName = assetRendererFactory.getTypeName(
273                            _themeDisplay.getLocale());
274    
275                    HttpServletRequest request =
276                            (HttpServletRequest)_pageContext.getRequest();
277    
278                    selectorEntryData.put(
279                            "title", LanguageUtil.format(request, "select-x", typeName, false));
280    
281                    selectorEntryData.put("type", assetRendererFactory.getClassName());
282    
283                    return selectorEntryData;
284            }
285    
286            private long _getAssetBrowserGroupId(
287                    AssetRendererFactory assetRendererFactory) {
288    
289                    Group scopeGroup = _themeDisplay.getScopeGroup();
290    
291                    long groupId = scopeGroup.getGroupId();
292    
293                    if (_isStagedLocally()) {
294                            boolean stagedReferencePortlet = scopeGroup.isStagedPortlet(
295                                    assetRendererFactory.getPortletId());
296    
297                            if (_isStagedReferrerPortlet() && !stagedReferencePortlet) {
298                                    groupId = scopeGroup.getLiveGroupId();
299                            }
300                    }
301    
302                    return groupId;
303            }
304    
305            private PortletURL _getAssetBrowserPortletURL(
306                            AssetRendererFactory assetRendererFactory)
307                    throws Exception {
308    
309                    long controlPanelPlid = PortalUtil.getControlPanelPlid(
310                            _themeDisplay.getCompanyId());
311    
312                    PortletURL portletURL = PortletURLFactoryUtil.create(
313                            _request, PortletKeys.ASSET_BROWSER, controlPanelPlid,
314                            PortletRequest.RENDER_PHASE);
315    
316                    portletURL.setParameter("struts_action", "/asset_browser/view");
317    
318                    long groupId = _getAssetBrowserGroupId(assetRendererFactory);
319    
320                    portletURL.setParameter("groupId", String.valueOf(groupId));
321                    portletURL.setParameter(
322                            "selectedGroupIds",
323                            StringUtil.merge(
324                                    PortalUtil.getSharedContentSiteGroupIds(
325                                            _themeDisplay.getCompanyId(), groupId,
326                                            _themeDisplay.getUserId())));
327    
328                    if (_assetEntryId > 0) {
329                            portletURL.setParameter(
330                                    "refererAssetEntryId", String.valueOf(_assetEntryId));
331                    }
332    
333                    portletURL.setParameter(
334                            "typeSelection", assetRendererFactory.getClassName());
335                    portletURL.setParameter("eventName", getEventName());
336                    portletURL.setPortletMode(PortletMode.VIEW);
337                    portletURL.setWindowState(LiferayWindowState.POP_UP);
338    
339                    return portletURL;
340            }
341    
342            private List<Map<String, Object>> _getSelectorEntries(
343                            AssetRendererFactory assetRendererFactory)
344                    throws Exception {
345    
346                    long groupId = _getAssetBrowserGroupId(assetRendererFactory);
347    
348                    ClassTypeReader classTypeReader =
349                            assetRendererFactory.getClassTypeReader();
350    
351                    List<ClassType> classTypes =
352                            classTypeReader.getAvailableClassTypes(
353                                    PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId),
354                                    _themeDisplay.getLocale());
355    
356                    if (classTypes.isEmpty()) {
357                            return Collections.emptyList();
358                    }
359    
360                    List<Map<String, Object>> selectorEntries =
361                            new ArrayList<Map<String, Object>>();
362    
363                    for (ClassType classType : classTypes) {
364                            Map<String, Object> selectorEntry = new HashMap<String, Object>();
365    
366                            selectorEntry.put(
367                                    "data",
368                                    _getSelectorEntryData(assetRendererFactory, classType));
369                            selectorEntry.put(
370                                    "iconCssClass",
371                                    _getSelectorEntryIconCssClass(assetRendererFactory));
372                            selectorEntry.put(
373                                    "id",
374                                    _getSelectorEntryId(assetRendererFactory, classType));
375                            selectorEntry.put("message", _getSelectorEntryMessage(classType));
376                            selectorEntry.put(
377                                    "src", _getSelectorEntrySrc(assetRendererFactory));
378    
379                            selectorEntries.add(selectorEntry);
380                    }
381    
382                    return selectorEntries;
383            }
384    
385            private Map<String, Object> _getSelectorEntryData(
386                            AssetRendererFactory assetRendererFactory, ClassType classType)
387                    throws Exception {
388    
389                    Map<String, Object> selectorEntryData = new HashMap<String, Object>();
390    
391                    PortletURL portletURL = _getAssetBrowserPortletURL(
392                            assetRendererFactory);
393    
394                    portletURL.setParameter(
395                            "subtypeSelectionId", String.valueOf(classType.getClassTypeId()));
396    
397                    selectorEntryData.put("href", portletURL.toString());
398    
399                    HttpServletRequest request =
400                            (HttpServletRequest)_pageContext.getRequest();
401    
402                    selectorEntryData.put(
403                            "title", LanguageUtil.format(
404                                    request, "select-x", classType.getName(), false));
405                    selectorEntryData.put("type", classType.getName());
406    
407                    return selectorEntryData;
408            }
409    
410            private String _getSelectorEntryIconCssClass(
411                            AssetRendererFactory assetRendererFactory)
412                    throws Exception {
413    
414                    return assetRendererFactory.getIconCssClass();
415            }
416    
417            private String _getSelectorEntryId(
418                    AssetRendererFactory assetRendererFactory) {
419    
420                    return FriendlyURLNormalizerUtil.normalize(
421                            assetRendererFactory.getTypeName(_themeDisplay.getLocale()));
422            }
423    
424            private String _getSelectorEntryId(
425                    AssetRendererFactory assetRendererFactory, ClassType classType) {
426    
427                    String selectorEntryId = String.valueOf(
428                            _getAssetBrowserGroupId(assetRendererFactory));
429    
430                    selectorEntryId += FriendlyURLNormalizerUtil.normalize(
431                            classType.getName());
432    
433                    return selectorEntryId;
434            }
435    
436            private String _getSelectorEntryMessage(
437                    AssetRendererFactory assetRendererFactory) {
438    
439                    return assetRendererFactory.getTypeName(_themeDisplay.getLocale());
440            }
441    
442            private String _getSelectorEntryMessage(ClassType classType) {
443                    return classType.getName();
444            }
445    
446            private String _getSelectorEntrySrc(
447                    AssetRendererFactory assetRendererFactory) {
448    
449                    return assetRendererFactory.getIconPath(_portletRequest);
450            }
451    
452            private boolean _isStagedLocally() {
453                    if (_stagedLocally != null) {
454                            return _stagedLocally;
455                    }
456    
457                    Group scopeGroup = _themeDisplay.getScopeGroup();
458    
459                    if (scopeGroup.isStaged() && !scopeGroup.isStagedRemotely()) {
460                            _stagedLocally = true;
461                    }
462                    else {
463                            _stagedLocally = false;
464                    }
465    
466                    return _stagedLocally;
467            }
468    
469            private boolean _isStagedReferrerPortlet() {
470                    if (_stagedReferrerPortlet != null) {
471                            return _stagedReferrerPortlet;
472                    }
473    
474                    if (_isStagedLocally()) {
475                            String className = (String)_request.getAttribute(
476                                    "liferay-ui:input-asset-links:className");
477    
478                            AssetRendererFactory assetRendererFactory =
479                                    AssetRendererFactoryRegistryUtil.
480                                            getAssetRendererFactoryByClassName(className);
481    
482                            Group scopeGroup = _themeDisplay.getScopeGroup();
483    
484                            _stagedReferrerPortlet = scopeGroup.isStagedPortlet(
485                                    assetRendererFactory.getPortletId());
486                    }
487                    else {
488                            _stagedReferrerPortlet = false;
489                    }
490    
491                    return _stagedReferrerPortlet;
492            }
493    
494            private long _assetEntryId;
495            private List<AssetLink> _assetLinks;
496            private String _eventName;
497            private PageContext _pageContext;
498            private PortletRequest _portletRequest;
499            private String _randomNamespace;
500            private HttpServletRequest _request;
501            private Boolean _stagedLocally;
502            private Boolean _stagedReferrerPortlet;
503            private ThemeDisplay _themeDisplay;
504    
505    }