001
014
015 package com.liferay.portlet.asset.util;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
022 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
023 import com.liferay.portal.kernel.portlet.LiferayWindowState;
024 import com.liferay.portal.kernel.search.Document;
025 import com.liferay.portal.kernel.search.Field;
026 import com.liferay.portal.kernel.search.Hits;
027 import com.liferay.portal.kernel.search.Indexer;
028 import com.liferay.portal.kernel.search.SearchContext;
029 import com.liferay.portal.kernel.search.SearchContextFactory;
030 import com.liferay.portal.kernel.search.Sort;
031 import com.liferay.portal.kernel.util.ArrayUtil;
032 import com.liferay.portal.kernel.util.CharPool;
033 import com.liferay.portal.kernel.util.Constants;
034 import com.liferay.portal.kernel.util.GetterUtil;
035 import com.liferay.portal.kernel.util.ListUtil;
036 import com.liferay.portal.kernel.util.LocaleUtil;
037 import com.liferay.portal.kernel.util.ParamUtil;
038 import com.liferay.portal.kernel.util.StringPool;
039 import com.liferay.portal.kernel.util.StringUtil;
040 import com.liferay.portal.kernel.util.Validator;
041 import com.liferay.portal.model.Layout;
042 import com.liferay.portal.model.Portlet;
043 import com.liferay.portal.security.permission.comparator.ModelResourceComparator;
044 import com.liferay.portal.service.PortletLocalServiceUtil;
045 import com.liferay.portal.theme.PortletDisplay;
046 import com.liferay.portal.theme.ThemeDisplay;
047 import com.liferay.portal.util.PortalUtil;
048 import com.liferay.portal.util.WebKeys;
049 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
050 import com.liferay.portlet.asset.NoSuchTagException;
051 import com.liferay.portlet.asset.model.AssetCategory;
052 import com.liferay.portlet.asset.model.AssetCategoryProperty;
053 import com.liferay.portlet.asset.model.AssetEntry;
054 import com.liferay.portlet.asset.model.AssetRendererFactory;
055 import com.liferay.portlet.asset.model.AssetTag;
056 import com.liferay.portlet.asset.model.AssetTagProperty;
057 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
058 import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
059 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
060 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
061 import com.liferay.portlet.asset.service.AssetTagPropertyLocalServiceUtil;
062 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
063 import com.liferay.portlet.assetpublisher.util.AssetSearcher;
064 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
065 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
066 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
067 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
068 import com.liferay.portlet.dynamicdatamapping.util.DDMIndexerImpl;
069 import com.liferay.portlet.journal.model.JournalArticle;
070
071 import java.io.Serializable;
072
073 import java.util.ArrayList;
074 import java.util.Collections;
075 import java.util.HashMap;
076 import java.util.HashSet;
077 import java.util.List;
078 import java.util.Locale;
079 import java.util.Map;
080 import java.util.Set;
081 import java.util.TreeMap;
082
083 import javax.portlet.PortletMode;
084 import javax.portlet.PortletRequest;
085 import javax.portlet.PortletURL;
086
087 import javax.servlet.http.HttpServletRequest;
088
089
093 public class AssetUtil {
094
095 public static final String CLASSNAME_SEPARATOR = "_CLASSNAME_";
096
097 public static final char[] INVALID_CHARACTERS = new char[] {
098 CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
099 CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
100 CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
101 CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
102 CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
103 CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.PRIME,
104 CharPool.QUESTION, CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON,
105 CharPool.SLASH, CharPool.STAR, CharPool.TILDE
106 };
107
108 public static Set<String> addLayoutTags(
109 HttpServletRequest request, List<AssetTag> tags) {
110
111 Set<String> layoutTags = getLayoutTagNames(request);
112
113 for (AssetTag tag : tags) {
114 layoutTags.add(tag.getName());
115 }
116
117 return layoutTags;
118 }
119
120 public static void addPortletBreadcrumbEntries(
121 long assetCategoryId, HttpServletRequest request,
122 PortletURL portletURL)
123 throws Exception {
124
125 AssetCategory assetCategory = AssetCategoryLocalServiceUtil.getCategory(
126 assetCategoryId);
127
128 List<AssetCategory> ancestorCategories = assetCategory.getAncestors();
129
130 Collections.reverse(ancestorCategories);
131
132 for (AssetCategory ancestorCategory : ancestorCategories) {
133 portletURL.setParameter(
134 "categoryId", String.valueOf(ancestorCategory.getCategoryId()));
135
136 PortalUtil.addPortletBreadcrumbEntry(
137 request, ancestorCategory.getTitleCurrentValue(),
138 portletURL.toString());
139 }
140
141 portletURL.setParameter("categoryId", String.valueOf(assetCategoryId));
142
143 PortalUtil.addPortletBreadcrumbEntry(
144 request, assetCategory.getTitleCurrentValue(),
145 portletURL.toString());
146 }
147
148 public static PortletURL getAddPortletURL(
149 LiferayPortletRequest liferayPortletRequest,
150 LiferayPortletResponse liferayPortletResponse, String className,
151 long classTypeId, long[] allAssetCategoryIds,
152 String[] allAssetTagNames, String redirect)
153 throws Exception {
154
155 ThemeDisplay themeDisplay =
156 (ThemeDisplay)liferayPortletRequest.getAttribute(
157 WebKeys.THEME_DISPLAY);
158
159 AssetRendererFactory assetRendererFactory =
160 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
161 className);
162
163 if (assetRendererFactory == null) {
164 return null;
165 }
166
167 liferayPortletRequest.setAttribute(
168 WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID, classTypeId);
169
170 PortletURL addPortletURL = assetRendererFactory.getURLAdd(
171 liferayPortletRequest, liferayPortletResponse);
172
173 if (addPortletURL == null) {
174 return null;
175 }
176
177 if (redirect != null) {
178 addPortletURL.setParameter("redirect", redirect);
179 }
180
181 String referringPortletResource = ParamUtil.getString(
182 liferayPortletRequest, "portletResource");
183
184 if (Validator.isNotNull(referringPortletResource)) {
185 addPortletURL.setParameter(
186 "referringPortletResource", referringPortletResource);
187 }
188 else {
189 PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
190
191 addPortletURL.setParameter(
192 "referringPortletResource", portletDisplay.getId());
193
194 if (allAssetCategoryIds != null) {
195 Map<Long, String> assetVocabularyAssetCategoryIds =
196 new HashMap<Long, String>();
197
198 for (long assetCategoryId : allAssetCategoryIds) {
199 AssetCategory assetCategory =
200 AssetCategoryLocalServiceUtil.fetchAssetCategory(
201 assetCategoryId);
202
203 if (assetCategory == null) {
204 continue;
205 }
206
207 long assetVocabularyId = assetCategory.getVocabularyId();
208
209 if (assetVocabularyAssetCategoryIds.containsKey(
210 assetVocabularyId)) {
211
212 String assetCategoryIds =
213 assetVocabularyAssetCategoryIds.get(
214 assetVocabularyId);
215
216 assetVocabularyAssetCategoryIds.put(
217 assetVocabularyId,
218 assetCategoryIds + StringPool.COMMA +
219 assetCategoryId);
220 }
221 else {
222 assetVocabularyAssetCategoryIds.put(
223 assetVocabularyId, String.valueOf(assetCategoryId));
224 }
225 }
226
227 for (Map.Entry<Long, String> entry :
228 assetVocabularyAssetCategoryIds.entrySet()) {
229
230 long assetVocabularyId = entry.getKey();
231 String assetCategoryIds = entry.getValue();
232
233 addPortletURL.setParameter(
234 "assetCategoryIds_" + assetVocabularyId,
235 assetCategoryIds);
236 }
237 }
238
239 if (allAssetTagNames != null) {
240 addPortletURL.setParameter(
241 "assetTagNames", StringUtil.merge(allAssetTagNames));
242 }
243 }
244
245 if (classTypeId > 0) {
246 addPortletURL.setParameter(
247 "classTypeId", String.valueOf(classTypeId));
248
249 if (className.equals(DLFileEntry.class.getName())) {
250 addPortletURL.setParameter(Constants.CMD, Constants.ADD);
251 addPortletURL.setParameter(
252 "folderId",
253 String.valueOf(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID));
254 addPortletURL.setParameter(
255 "fileEntryTypeId", String.valueOf(classTypeId));
256 }
257
258 if (className.equals(JournalArticle.class.getName())) {
259 DDMStructure ddmStructure =
260 DDMStructureLocalServiceUtil.getStructure(classTypeId);
261
262 addPortletURL.setParameter(
263 "structureId", ddmStructure.getStructureKey());
264 }
265 }
266
267 addPortletURL.setPortletMode(PortletMode.VIEW);
268 addPortletURL.setWindowState(LiferayWindowState.POP_UP);
269
270 return addPortletURL;
271 }
272
273 public static Map<String, PortletURL> getAddPortletURLs(
274 LiferayPortletRequest liferayPortletRequest,
275 LiferayPortletResponse liferayPortletResponse, long[] classNameIds,
276 long[] classTypeIds, long[] allAssetCategoryIds,
277 String[] allAssetTagNames, String redirect)
278 throws Exception {
279
280 ThemeDisplay themeDisplay =
281 (ThemeDisplay)liferayPortletRequest.getAttribute(
282 WebKeys.THEME_DISPLAY);
283
284 Map<String, PortletURL> addPortletURLs =
285 new TreeMap<String, PortletURL>(
286 new ModelResourceComparator(themeDisplay.getLocale()));
287
288 if (Validator.isNull(redirect)) {
289 PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
290
291 PortletURL redirectURL =
292 liferayPortletResponse.createLiferayPortletURL(
293 themeDisplay.getPlid(), portletDisplay.getId(),
294 PortletRequest.RENDER_PHASE, false);
295
296 redirectURL.setParameter(
297 "struts_action", "/asset_publisher/add_asset_redirect");
298 redirectURL.setParameter("redirect", themeDisplay.getURLCurrent());
299 redirectURL.setWindowState(LiferayWindowState.POP_UP);
300
301 redirect = redirectURL.toString();
302 }
303
304 for (long classNameId : classNameIds) {
305 String className = PortalUtil.getClassName(classNameId);
306
307 AssetRendererFactory assetRendererFactory =
308 AssetRendererFactoryRegistryUtil.
309 getAssetRendererFactoryByClassName(className);
310
311 Portlet portlet = PortletLocalServiceUtil.getPortletById(
312 themeDisplay.getCompanyId(),
313 assetRendererFactory.getPortletId());
314
315 if (!portlet.isActive()) {
316 continue;
317 }
318
319 Map<Long, String> classTypes = assetRendererFactory.getClassTypes(
320 new long[] {
321 themeDisplay.getCompanyGroupId(),
322 themeDisplay.getScopeGroupId()
323 },
324 themeDisplay.getLocale());
325
326 if ((classTypeIds.length == 0) || classTypes.isEmpty()) {
327 PortletURL addPortletURL = getAddPortletURL(
328 liferayPortletRequest, liferayPortletResponse, className, 0,
329 allAssetCategoryIds, allAssetTagNames, redirect);
330
331 if (addPortletURL != null) {
332 addPortletURLs.put(className, addPortletURL);
333 }
334 }
335
336 for (long classTypeId : classTypes.keySet()) {
337 if (ArrayUtil.contains(classTypeIds, classTypeId) ||
338 (classTypeIds.length == 0)) {
339
340 PortletURL addPortletURL = getAddPortletURL(
341 liferayPortletRequest, liferayPortletResponse,
342 className, classTypeId, allAssetCategoryIds,
343 allAssetTagNames, redirect);
344
345 if (addPortletURL != null) {
346 String mesage =
347 className + CLASSNAME_SEPARATOR +
348 classTypes.get(classTypeId);
349
350 addPortletURLs.put(mesage, addPortletURL);
351 }
352 }
353 }
354 }
355
356 return addPortletURLs;
357 }
358
359 public static List<AssetEntry> getAssetEntries(Hits hits) {
360 List<AssetEntry> assetEntries = new ArrayList<AssetEntry>();
361
362 for (Document document : hits.getDocs()) {
363 String className = GetterUtil.getString(
364 document.get(Field.ENTRY_CLASS_NAME));
365 long classPK = GetterUtil.getLong(
366 document.get(Field.ENTRY_CLASS_PK));
367
368 try {
369 AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
370 className, classPK);
371
372 assetEntries.add(assetEntry);
373 }
374 catch (Exception e) {
375 }
376 }
377
378 return assetEntries;
379 }
380
381 public static String getAssetKeywords(String className, long classPK)
382 throws SystemException {
383
384 List<AssetTag> tags = AssetTagLocalServiceUtil.getTags(
385 className, classPK);
386 List<AssetCategory> categories =
387 AssetCategoryLocalServiceUtil.getCategories(className, classPK);
388
389 StringBuffer sb = new StringBuffer();
390
391 sb.append(ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
392
393 if (!tags.isEmpty()) {
394 sb.append(StringPool.COMMA);
395 }
396
397 sb.append(ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
398
399 return sb.toString();
400 }
401
402 public static Set<String> getLayoutTagNames(HttpServletRequest request) {
403 Set<String> tagNames = (Set<String>)request.getAttribute(
404 WebKeys.ASSET_LAYOUT_TAG_NAMES);
405
406 if (tagNames == null) {
407 tagNames = new HashSet<String>();
408
409 request.setAttribute(WebKeys.ASSET_LAYOUT_TAG_NAMES, tagNames);
410 }
411
412 return tagNames;
413 }
414
415 public static boolean hasSubtype(
416 String subtypeClassName, Map<String, PortletURL> addPortletURLs) {
417
418 for (Map.Entry<String, PortletURL> entry : addPortletURLs.entrySet()) {
419 String className = entry.getKey();
420
421 if (className.startsWith(subtypeClassName) &&
422 !className.equals(subtypeClassName)) {
423
424 return true;
425 }
426 }
427
428 return false;
429 }
430
431 public static boolean isValidWord(String word) {
432 if (Validator.isNull(word)) {
433 return false;
434 }
435 else {
436 char[] wordCharArray = word.toCharArray();
437
438 for (char c : wordCharArray) {
439 for (char invalidChar : INVALID_CHARACTERS) {
440 if (c == invalidChar) {
441 if (_log.isDebugEnabled()) {
442 _log.debug(
443 "Word " + word + " is not valid because " + c +
444 " is not allowed");
445 }
446
447 return false;
448 }
449 }
450 }
451 }
452
453 return true;
454 }
455
456 public static Hits search(
457 HttpServletRequest request, AssetEntryQuery assetEntryQuery,
458 int start, int end)
459 throws Exception {
460
461 SearchContext searchContext = SearchContextFactory.getInstance(request);
462
463 return search(searchContext, assetEntryQuery, start, end);
464 }
465
466 public static Hits search(
467 SearchContext searchContext, AssetEntryQuery assetEntryQuery,
468 int start, int end)
469 throws Exception {
470
471 Indexer searcher = AssetSearcher.getInstance();
472
473 AssetSearcher assetSearcher = (AssetSearcher)searcher;
474
475 assetSearcher.setAssetEntryQuery(assetEntryQuery);
476
477 Layout layout = assetEntryQuery.getLayout();
478
479 if (layout != null) {
480 searchContext.setAttribute(Field.LAYOUT_UUID, layout.getUuid());
481 }
482
483 String ddmStructureFieldName = (String)assetEntryQuery.getAttribute(
484 "ddmStructureFieldName");
485 Serializable ddmStructureFieldValue = assetEntryQuery.getAttribute(
486 "ddmStructureFieldValue");
487
488 if (Validator.isNotNull(ddmStructureFieldName) &&
489 Validator.isNotNull(ddmStructureFieldValue)) {
490
491 searchContext.setAttribute(
492 "ddmStructureFieldName", ddmStructureFieldName);
493 searchContext.setAttribute(
494 "ddmStructureFieldValue", ddmStructureFieldValue);
495 }
496
497 String paginationType = GetterUtil.getString(
498 assetEntryQuery.getPaginationType(), "more");
499
500 if (!paginationType.equals("none") &&
501 !paginationType.equals("simple")) {
502
503 searchContext.setAttribute("paginationType", paginationType);
504 }
505
506 searchContext.setClassTypeIds(assetEntryQuery.getClassTypeIds());
507 searchContext.setEnd(end);
508 searchContext.setGroupIds(assetEntryQuery.getGroupIds());
509
510 if (Validator.isNotNull(assetEntryQuery.getKeywords())) {
511 searchContext.setLike(true);
512 }
513
514 searchContext.setSorts(
515 getSorts(assetEntryQuery, searchContext.getLocale()));
516 searchContext.setStart(start);
517
518 return assetSearcher.search(searchContext);
519 }
520
521 public static String substituteCategoryPropertyVariables(
522 long groupId, long categoryId, String s)
523 throws SystemException {
524
525 String result = s;
526
527 AssetCategory category = null;
528
529 if (categoryId > 0) {
530 category = AssetCategoryLocalServiceUtil.fetchCategory(categoryId);
531 }
532
533 if (category != null) {
534 List<AssetCategoryProperty> categoryProperties =
535 AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
536 categoryId);
537
538 for (AssetCategoryProperty categoryProperty : categoryProperties) {
539 result = StringUtil.replace(
540 result, "[$" + categoryProperty.getKey() + "$]",
541 categoryProperty.getValue());
542 }
543 }
544
545 return StringUtil.stripBetween(result, "[$", "$]");
546 }
547
548 public static String substituteTagPropertyVariables(
549 long groupId, String tagName, String s)
550 throws PortalException, SystemException {
551
552 String result = s;
553
554 AssetTag tag = null;
555
556 if (tagName != null) {
557 try {
558 tag = AssetTagLocalServiceUtil.getTag(groupId, tagName);
559 }
560 catch (NoSuchTagException nste) {
561 }
562 }
563
564 if (tag != null) {
565 List<AssetTagProperty> tagProperties =
566 AssetTagPropertyLocalServiceUtil.getTagProperties(
567 tag.getTagId());
568
569 for (AssetTagProperty tagProperty : tagProperties) {
570 result = StringUtil.replace(
571 result, "[$" + tagProperty.getKey() + "$]",
572 tagProperty.getValue());
573 }
574 }
575
576 return StringUtil.stripBetween(result, "[$", "$]");
577 }
578
579 public static String toWord(String text) {
580 if (Validator.isNull(text)) {
581 return text;
582 }
583 else {
584 char[] textCharArray = text.toCharArray();
585
586 for (int i = 0; i < textCharArray.length; i++) {
587 char c = textCharArray[i];
588
589 for (char invalidChar : INVALID_CHARACTERS) {
590 if (c == invalidChar) {
591 textCharArray[i] = CharPool.SPACE;
592
593 break;
594 }
595 }
596 }
597
598 return new String(textCharArray);
599 }
600 }
601
602 protected static Sort getSort(
603 String orderByType, String sortField, Locale locale)
604 throws Exception {
605
606 if (Validator.isNull(orderByType)) {
607 orderByType = "asc";
608 }
609
610 int sortType = getSortType(sortField);
611
612 if (sortField.startsWith(
613 DDMIndexerImpl.DDM_FIELD_NAMESPACE +
614 StringPool.FORWARD_SLASH)) {
615
616 String[] sortFields = sortField.split(StringPool.FORWARD_SLASH);
617
618 long ddmStructureId = GetterUtil.getLong(sortFields[1]);
619 String fieldName = sortFields[2];
620
621 DDMStructure ddmStructure =
622 DDMStructureLocalServiceUtil.getStructure(ddmStructureId);
623
624 String fieldType = ddmStructure.getFieldType(fieldName);
625
626 sortType = getSortType(fieldType);
627
628 sortField = sortField.concat(StringPool.UNDERLINE).concat(
629 LocaleUtil.toLanguageId(locale));
630 }
631 else if (sortField.equals("modifiedDate")) {
632 sortField = Field.MODIFIED_DATE;
633 }
634 else if (sortField.equals("title")) {
635 sortField = "localized_title_".concat(
636 LocaleUtil.toLanguageId(locale));
637 }
638
639 return new Sort(
640 sortField, sortType, !orderByType.equalsIgnoreCase("asc"));
641 }
642
643 protected static Sort[] getSorts(
644 AssetEntryQuery assetEntryQuery, Locale locale)
645 throws Exception {
646
647 Sort sort1 = getSort(
648 assetEntryQuery.getOrderByType1(), assetEntryQuery.getOrderByCol1(),
649 locale);
650 Sort sort2 = getSort(
651 assetEntryQuery.getOrderByType2(), assetEntryQuery.getOrderByCol2(),
652 locale);
653
654 return new Sort[] {sort1, sort2};
655 }
656
657 protected static int getSortType(String sortField) {
658 int sortType = Sort.STRING_TYPE;
659
660 if (sortField.equals(Field.CREATE_DATE) ||
661 sortField.equals(Field.EXPIRATION_DATE) ||
662 sortField.equals(Field.PUBLISH_DATE) ||
663 sortField.equals("ddm-date") ||
664 sortField.equals("modifiedDate")) {
665
666 sortType = Sort.LONG_TYPE;
667 }
668 else if (sortField.equals(Field.PRIORITY) ||
669 sortField.equals(Field.RATINGS) ||
670 sortField.equals("ddm-decimal") ||
671 sortField.equals("ddm-number")) {
672
673 sortType = Sort.DOUBLE_TYPE;
674 }
675 else if (sortField.equals(Field.VIEW_COUNT) ||
676 sortField.equals("ddm-integer")) {
677
678 sortType = Sort.INT_TYPE;
679 }
680
681 return sortType;
682 }
683
684 private static Log _log = LogFactoryUtil.getLog(AssetUtil.class);
685
686 }