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