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