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