001
014
015 package com.liferay.portlet.assetpublisher.util;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.ParamUtil;
025 import com.liferay.portal.kernel.util.PrimitiveLongList;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.kernel.xml.Document;
030 import com.liferay.portal.kernel.xml.Element;
031 import com.liferay.portal.kernel.xml.SAXReaderUtil;
032 import com.liferay.portal.model.Group;
033 import com.liferay.portal.model.GroupConstants;
034 import com.liferay.portal.model.Layout;
035 import com.liferay.portal.model.User;
036 import com.liferay.portal.service.GroupLocalServiceUtil;
037 import com.liferay.portal.service.LayoutLocalServiceUtil;
038 import com.liferay.portal.theme.ThemeDisplay;
039 import com.liferay.portal.util.PortalUtil;
040 import com.liferay.portal.util.WebKeys;
041 import com.liferay.portlet.PortletPreferencesFactoryUtil;
042 import com.liferay.portlet.asset.model.AssetCategory;
043 import com.liferay.portlet.asset.model.AssetEntry;
044 import com.liferay.portlet.asset.model.AssetRendererFactory;
045 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
046 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
047 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
048 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
049 import com.liferay.portlet.expando.model.ExpandoBridge;
050
051 import java.io.IOException;
052 import java.io.Serializable;
053
054 import java.util.HashMap;
055 import java.util.Iterator;
056 import java.util.List;
057 import java.util.Map;
058
059 import javax.portlet.PortletPreferences;
060 import javax.portlet.PortletRequest;
061
062 import javax.servlet.http.HttpServletRequest;
063 import javax.servlet.http.HttpSession;
064
065
068 public class AssetPublisherUtil {
069
070 public static void addAndStoreSelection(
071 PortletRequest portletRequest, String className, long classPK,
072 int assetEntryOrder)
073 throws Exception {
074
075 String referringPortletResource = ParamUtil.getString(
076 portletRequest, "referringPortletResource");
077
078 if (Validator.isNull(referringPortletResource)) {
079 return;
080 }
081
082 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
083 WebKeys.THEME_DISPLAY);
084
085 Layout layout = LayoutLocalServiceUtil.getLayout(
086 themeDisplay.getRefererPlid());
087
088 PortletPreferences portletPreferences =
089 PortletPreferencesFactoryUtil.getPortletSetup(
090 themeDisplay.getScopeGroupId(), layout,
091 referringPortletResource, null);
092
093 String selectionStyle = portletPreferences.getValue(
094 "selectionStyle", "dynamic");
095
096 if (selectionStyle.equals("dynamic")) {
097 return;
098 }
099
100 AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
101 className, classPK);
102
103 addSelection(
104 className, assetEntry.getEntryId(), assetEntryOrder,
105 portletPreferences);
106
107 portletPreferences.store();
108 }
109
110 public static void addRecentFolderId(
111 PortletRequest portletRequest, String className, long classPK) {
112
113 _getRecentFolderIds(portletRequest).put(className, classPK);
114 }
115
116 public static void addSelection(
117 PortletRequest portletRequest,
118 PortletPreferences portletPreferences)
119 throws Exception {
120
121 String assetEntryType = ParamUtil.getString(
122 portletRequest, "assetEntryType");
123 long assetEntryId = ParamUtil.getLong(portletRequest, "assetEntryId");
124 int assetEntryOrder = ParamUtil.getInteger(
125 portletRequest, "assetEntryOrder");
126
127 addSelection(
128 assetEntryType, assetEntryId, assetEntryOrder, portletPreferences);
129 }
130
131 public static void addSelection(
132 String assetEntryType, long assetEntryId, int assetEntryOrder,
133 PortletPreferences portletPreferences)
134 throws Exception {
135
136 AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
137 assetEntryId);
138
139 String[] assetEntryXmls = portletPreferences.getValues(
140 "assetEntryXml", new String[0]);
141
142 String assetEntryXml = _getAssetEntryXml(
143 assetEntryType, assetEntry.getClassUuid());
144
145 if (assetEntryOrder > -1) {
146 assetEntryXmls[assetEntryOrder] = assetEntryXml;
147 }
148 else {
149 assetEntryXmls = ArrayUtil.append(assetEntryXmls, assetEntryXml);
150 }
151
152 portletPreferences.setValues("assetEntryXml", assetEntryXmls);
153 }
154
155 public static void addUserAttributes(
156 User user, String[] customUserAttributeNames,
157 AssetEntryQuery assetEntryQuery)
158 throws Exception {
159
160 if ((user == null) || (customUserAttributeNames.length == 0)) {
161 return;
162 }
163
164 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
165 user.getCompanyId());
166
167 long[] allCategoryIds = assetEntryQuery.getAllCategoryIds();
168
169 PrimitiveLongList allCategoryIdsList = new PrimitiveLongList(
170 allCategoryIds.length + customUserAttributeNames.length);
171
172 allCategoryIdsList.addAll(allCategoryIds);
173
174 for (String customUserAttributeName : customUserAttributeNames) {
175 ExpandoBridge userCustomAttributes = user.getExpandoBridge();
176
177 Serializable userCustomFieldValue =
178 userCustomAttributes.getAttribute(customUserAttributeName);
179
180 if (userCustomFieldValue == null) {
181 continue;
182 }
183
184 String userCustomFieldValueString = userCustomFieldValue.toString();
185
186 List<AssetCategory> assetCategories =
187 AssetCategoryLocalServiceUtil.search(
188 companyGroup.getGroupId(), userCustomFieldValueString,
189 new String[0], QueryUtil.ALL_POS, QueryUtil.ALL_POS);
190
191 for (AssetCategory assetCategory : assetCategories) {
192 allCategoryIdsList.add(assetCategory.getCategoryId());
193 }
194 }
195
196 assetEntryQuery.setAllCategoryIds(allCategoryIdsList.getArray());
197 }
198
199 public static AssetEntryQuery getAssetEntryQuery(
200 PortletPreferences portletPreferences, long[] scopeGroupIds)
201 throws Exception {
202
203 AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
204
205 long[] allAssetCategoryIds = new long[0];
206 long[] anyAssetCategoryIds = new long[0];
207 long[] notAllAssetCategoryIds = new long[0];
208 long[] notAnyAssetCategoryIds = new long[0];
209
210 String[] allAssetTagNames = new String[0];
211 String[] anyAssetTagNames = new String[0];
212 String[] notAllAssetTagNames = new String[0];
213 String[] notAnyAssetTagNames = new String[0];
214
215 for (int i = 0; true; i++) {
216 String[] queryValues = portletPreferences.getValues(
217 "queryValues" + i, null);
218
219 if ((queryValues == null) || (queryValues.length == 0)) {
220 break;
221 }
222
223 boolean queryContains = GetterUtil.getBoolean(
224 portletPreferences.getValue(
225 "queryContains" + i, StringPool.BLANK));
226 boolean queryAndOperator = GetterUtil.getBoolean(
227 portletPreferences.getValue(
228 "queryAndOperator" + i, StringPool.BLANK));
229 String queryName = portletPreferences.getValue(
230 "queryName" + i, StringPool.BLANK);
231
232 if (Validator.equals(queryName, "assetCategories")) {
233 long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);
234
235 if (queryContains &&
236 (queryAndOperator || (assetCategoryIds.length == 1))) {
237
238 allAssetCategoryIds = assetCategoryIds;
239 }
240 else if (queryContains && !queryAndOperator) {
241 anyAssetCategoryIds = assetCategoryIds;
242 }
243 else if (!queryContains && queryAndOperator) {
244 notAllAssetCategoryIds = assetCategoryIds;
245 }
246 else {
247 notAnyAssetCategoryIds = assetCategoryIds;
248 }
249 }
250 else {
251 if (queryContains && queryAndOperator) {
252 allAssetTagNames = queryValues;
253 }
254 else if (queryContains && !queryAndOperator) {
255 anyAssetTagNames = queryValues;
256 }
257 else if (!queryContains && queryAndOperator) {
258 notAllAssetTagNames = queryValues;
259 }
260 else {
261 notAnyAssetTagNames = queryValues;
262 }
263 }
264 }
265
266 assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);
267
268 for (String assetTagName : allAssetTagNames) {
269 long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
270 scopeGroupIds, assetTagName);
271
272 assetEntryQuery.addAllTagIdsArray(allAssetTagIds);
273 }
274
275 assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);
276
277 long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
278 scopeGroupIds, anyAssetTagNames);
279
280 assetEntryQuery.setAnyTagIds(anyAssetTagIds);
281
282 assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);
283
284 for (String assetTagName : notAllAssetTagNames) {
285 long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
286 scopeGroupIds, assetTagName);
287
288 assetEntryQuery.addNotAllTagIdsArray(notAllAssetTagIds);
289 }
290
291 assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);
292
293 long[] notAnyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
294 scopeGroupIds, notAnyAssetTagNames);
295
296 assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);
297
298 return assetEntryQuery;
299 }
300
301 public static String[] getAssetTagNames(
302 PortletPreferences portletPreferences, long scopeGroupId)
303 throws Exception {
304
305 String[] allAssetTagNames = new String[0];
306
307 for (int i = 0; true; i++) {
308 String[] queryValues = portletPreferences.getValues(
309 "queryValues" + i, null);
310
311 if ((queryValues == null) || (queryValues.length == 0)) {
312 break;
313 }
314
315 boolean queryContains = GetterUtil.getBoolean(
316 portletPreferences.getValue(
317 "queryContains" + i, StringPool.BLANK));
318 boolean queryAndOperator = GetterUtil.getBoolean(
319 portletPreferences.getValue(
320 "queryAndOperator" + i, StringPool.BLANK));
321 String queryName = portletPreferences.getValue(
322 "queryName" + i, StringPool.BLANK);
323
324 if (!Validator.equals(queryName, "assetCategories") &&
325 queryContains &&
326 (queryAndOperator || (queryValues.length == 1))) {
327
328 allAssetTagNames = queryValues;
329 }
330 }
331
332 return allAssetTagNames;
333 }
334
335 public static String getClassName(
336 AssetRendererFactory assetRendererFactory) {
337
338 Class<?> clazz = assetRendererFactory.getClass();
339
340 String className = clazz.getName();
341
342 int pos = className.lastIndexOf(StringPool.PERIOD);
343
344 return className.substring(pos + 1);
345 }
346
347 public static long[] getClassNameIds(
348 PortletPreferences portletPreferences, long[] availableClassNameIds) {
349
350 boolean anyAssetType = GetterUtil.getBoolean(
351 portletPreferences.getValue(
352 "anyAssetType", Boolean.TRUE.toString()));
353
354 if (anyAssetType) {
355 return availableClassNameIds;
356 }
357
358 long defaultClassNameId = GetterUtil.getLong(
359 portletPreferences.getValue("anyAssetType", null));
360
361 if (defaultClassNameId > 0) {
362 return new long[] {defaultClassNameId};
363 }
364
365 long[] classNameIds = GetterUtil.getLongValues(
366 portletPreferences.getValues("classNameIds", null));
367
368 if (classNameIds != null) {
369 return classNameIds;
370 }
371 else {
372 return availableClassNameIds;
373 }
374 }
375
376 public static Long[] getClassTypeIds(
377 PortletPreferences portletPreferences, String className,
378 Long[] availableClassTypeIds) {
379
380 boolean anyAssetType = GetterUtil.getBoolean(
381 portletPreferences.getValue(
382 "anyClassType" + className, Boolean.TRUE.toString()));
383
384 if (anyAssetType) {
385 return availableClassTypeIds;
386 }
387
388 long defaultClassTypeId = GetterUtil.getLong(
389 portletPreferences.getValue("anyClassType" + className, null));
390
391 if (defaultClassTypeId > 0) {
392 return new Long[] {defaultClassTypeId};
393 }
394
395 Long[] classTypeIds = ArrayUtil.toArray(
396 StringUtil.split(
397 portletPreferences.getValue(
398 "classTypeIds" + className, null), 0L));
399
400 if (classTypeIds != null) {
401 return classTypeIds;
402 }
403 else {
404 return availableClassTypeIds;
405 }
406 }
407
408 public static long[] getGroupIds(
409 PortletPreferences portletPreferences, long scopeGroupId,
410 Layout layout) {
411
412 String defaultScopeId = GetterUtil.getString(
413 portletPreferences.getValue("defaultScope", null));
414
415 if (Validator.isNull(defaultScopeId) ||
416 defaultScopeId.equals(StringPool.FALSE)) {
417
418 String[] scopeIds = portletPreferences.getValues(
419 "scopeIds",
420 new String[] {"group" + StringPool.UNDERLINE + scopeGroupId});
421
422 long[] groupIds = new long[scopeIds.length];
423
424 for (int i = 0; i < scopeIds.length; i++) {
425 try {
426 groupIds[i] = _getGroupId(
427 scopeIds[i], scopeGroupId, layout.isPrivateLayout());
428 }
429 catch (Exception e) {
430 continue;
431 }
432 }
433
434 return groupIds;
435 }
436
437 if (defaultScopeId.equals(StringPool.TRUE)) {
438 return new long[] {scopeGroupId};
439 }
440
441 try {
442 long groupId = _getGroupId(
443 defaultScopeId, scopeGroupId, layout.isPrivateLayout());
444
445 return new long[] {groupId};
446 }
447 catch (Exception e) {
448 return new long[0];
449 }
450 }
451
452 public static long getRecentFolderId(
453 PortletRequest portletRequest, String className) {
454
455 Long classPK = _getRecentFolderIds(portletRequest).get(className);
456
457 if (classPK == null) {
458 return 0;
459 }
460 else {
461 return classPK.longValue();
462 }
463 }
464
465 public static void removeAndStoreSelection(
466 List<String> assetEntryUuids, PortletPreferences portletPreferences)
467 throws Exception {
468
469 if (assetEntryUuids.size() == 0) {
470 return;
471 }
472
473 String[] assetEntryXmls = portletPreferences.getValues(
474 "assetEntryXml", new String[0]);
475
476 List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls);
477
478 Iterator<String> itr = assetEntryXmlsList.iterator();
479
480 while (itr.hasNext()) {
481 String assetEntryXml = itr.next();
482
483 Document document = SAXReaderUtil.read(assetEntryXml);
484
485 Element rootElement = document.getRootElement();
486
487 String assetEntryUuid = rootElement.elementText("asset-entry-uuid");
488
489 if (assetEntryUuids.contains(assetEntryUuid)) {
490 itr.remove();
491 }
492 }
493
494 portletPreferences.setValues(
495 "assetEntryXml",
496 assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()]));
497
498 portletPreferences.store();
499 }
500
501 public static void removeRecentFolderId(
502 PortletRequest portletRequest, String className, long classPK) {
503
504 if (getRecentFolderId(portletRequest, className) == classPK) {
505 _getRecentFolderIds(portletRequest).remove(className);
506 }
507 }
508
509 private static String _getAssetEntryXml(
510 String assetEntryType, String assetEntryUuid) {
511
512 String xml = null;
513
514 try {
515 Document document = SAXReaderUtil.createDocument(StringPool.UTF8);
516
517 Element assetEntryElement = document.addElement("asset-entry");
518
519 Element assetEntryTypeElement = assetEntryElement.addElement(
520 "asset-entry-type");
521
522 assetEntryTypeElement.addText(assetEntryType);
523
524 Element assetEntryUuidElement = assetEntryElement.addElement(
525 "asset-entry-uuid");
526
527 assetEntryUuidElement.addText(assetEntryUuid);
528
529 xml = document.formattedString(StringPool.BLANK);
530 }
531 catch (IOException ioe) {
532 if (_log.isWarnEnabled()) {
533 _log.warn(ioe);
534 }
535 }
536
537 return xml;
538 }
539
540 private static long _getGroupId(
541 String scopeId, long scopeGroupId, boolean privateLayout)
542 throws Exception {
543
544 String[] scopeIdParts = StringUtil.split(scopeId, CharPool.UNDERLINE);
545
546 if (scopeIdParts[0].equals("Layout")) {
547 long scopeIdLayoutId = GetterUtil.getLong(scopeIdParts[1]);
548
549 Layout scopeIdLayout = LayoutLocalServiceUtil.getLayout(
550 scopeGroupId, privateLayout, scopeIdLayoutId);
551
552 Group scopeIdGroup = scopeIdLayout.getScopeGroup();
553
554 return scopeIdGroup.getGroupId();
555 }
556
557 if (scopeIdParts[1].equals(GroupConstants.DEFAULT)) {
558 return scopeGroupId;
559 }
560
561 return GetterUtil.getLong(scopeIdParts[1]);
562 }
563
564 private static Map<String, Long> _getRecentFolderIds(
565 PortletRequest portletRequest) {
566
567 HttpServletRequest request = PortalUtil.getHttpServletRequest(
568 portletRequest);
569 HttpSession session = request.getSession();
570
571 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
572 WebKeys.THEME_DISPLAY);
573
574 String key =
575 AssetPublisherUtil.class + StringPool.UNDERLINE +
576 themeDisplay.getScopeGroupId();
577
578 Map<String, Long> recentFolderIds =
579 (Map<String, Long>)session.getAttribute(key);
580
581 if (recentFolderIds == null) {
582 recentFolderIds = new HashMap<String, Long>();
583 }
584
585 session.setAttribute(key, recentFolderIds);
586
587 return recentFolderIds;
588 }
589
590 private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
591
592 }