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