001    /**
002     * Copyright (c) 2000-2012 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.util.CharPool;
025    import com.liferay.portal.kernel.util.Constants;
026    import com.liferay.portal.kernel.util.ListUtil;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.theme.PortletDisplay;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
036    import com.liferay.portlet.asset.NoSuchCategoryException;
037    import com.liferay.portlet.asset.NoSuchTagException;
038    import com.liferay.portlet.asset.model.AssetCategory;
039    import com.liferay.portlet.asset.model.AssetCategoryProperty;
040    import com.liferay.portlet.asset.model.AssetRendererFactory;
041    import com.liferay.portlet.asset.model.AssetTag;
042    import com.liferay.portlet.asset.model.AssetTagProperty;
043    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
044    import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
045    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
046    import com.liferay.portlet.asset.service.AssetTagPropertyLocalServiceUtil;
047    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
048    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
049    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
050    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
051    import com.liferay.portlet.journal.model.JournalArticle;
052    
053    import java.util.Collections;
054    import java.util.HashMap;
055    import java.util.HashSet;
056    import java.util.List;
057    import java.util.Map;
058    import java.util.Set;
059    
060    import javax.portlet.PortletMode;
061    import javax.portlet.PortletURL;
062    
063    import javax.servlet.http.HttpServletRequest;
064    
065    /**
066     * @author Brian Wing Shun Chan
067     * @author Jorge Ferrer
068     */
069    public class AssetUtil {
070    
071            public static final String CLASSNAME_SEPARATOR = "_CLASSNAME_";
072    
073            public static final char[] INVALID_CHARACTERS = new char[] {
074                    CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
075                    CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
076                    CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
077                    CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
078                    CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
079                    CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
080                    CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
081                    CharPool.STAR, CharPool.TILDE
082            };
083    
084            public static Set<String> addLayoutTags(
085                    HttpServletRequest request, List<AssetTag> tags) {
086    
087                    Set<String> layoutTags = getLayoutTagNames(request);
088    
089                    for (AssetTag tag : tags) {
090                            layoutTags.add(tag.getName());
091                    }
092    
093                    return layoutTags;
094            }
095    
096            public static void addPortletBreadcrumbEntries(
097                            long assetCategoryId, HttpServletRequest request,
098                            PortletURL portletURL)
099                    throws Exception {
100    
101                    AssetCategory assetCategory = AssetCategoryLocalServiceUtil.getCategory(
102                            assetCategoryId);
103    
104                    List<AssetCategory> ancestorCategories = assetCategory.getAncestors();
105    
106                    Collections.reverse(ancestorCategories);
107    
108                    for (AssetCategory ancestorCategory : ancestorCategories) {
109                            portletURL.setParameter(
110                                    "categoryId", String.valueOf(ancestorCategory.getCategoryId()));
111    
112                            PortalUtil.addPortletBreadcrumbEntry(
113                                    request, ancestorCategory.getTitleCurrentValue(),
114                                    portletURL.toString());
115                    }
116    
117                    portletURL.setParameter("categoryId", String.valueOf(assetCategoryId));
118    
119                    PortalUtil.addPortletBreadcrumbEntry(
120                            request, assetCategory.getTitleCurrentValue(),
121                            portletURL.toString());
122            }
123    
124            public static PortletURL getAddPortletURL(
125                            LiferayPortletRequest liferayPortletRequest,
126                            LiferayPortletResponse liferayPortletResponse, String className,
127                            long classTypeId, long[] allAssetCategoryIds,
128                            String[] allAssetTagNames, String redirect)
129                    throws Exception {
130    
131                    ThemeDisplay themeDisplay =
132                            (ThemeDisplay)liferayPortletRequest.getAttribute(
133                                    WebKeys.THEME_DISPLAY);
134    
135                    AssetRendererFactory assetRendererFactory =
136                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
137                                    className);
138    
139                    if (assetRendererFactory == null) {
140                            return null;
141                    }
142    
143                    liferayPortletRequest.setAttribute(
144                            WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID, classTypeId);
145    
146                    PortletURL addPortletURL = assetRendererFactory.getURLAdd(
147                            liferayPortletRequest, liferayPortletResponse);
148    
149                    if (addPortletURL == null) {
150                            return null;
151                    }
152    
153                    if (redirect != null) {
154                            addPortletURL.setParameter("redirect", redirect);
155                    }
156    
157                    String referringPortletResource = ParamUtil.getString(
158                            liferayPortletRequest, "portletResource");
159    
160                    if (Validator.isNotNull(referringPortletResource)) {
161                            addPortletURL.setParameter(
162                                    "referringPortletResource", referringPortletResource);
163                    }
164                    else {
165                            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
166    
167                            addPortletURL.setParameter(
168                                    "referringPortletResource", portletDisplay.getId());
169    
170                            if (allAssetCategoryIds != null) {
171                                    Map<Long, String> assetVocabularyAssetCategoryIds =
172                                            new HashMap<Long, String>();
173    
174                                    for (long assetCategoryId : allAssetCategoryIds) {
175                                            AssetCategory assetCategory =
176                                                    AssetCategoryLocalServiceUtil.getAssetCategory(
177                                                            assetCategoryId);
178    
179                                            long assetVocabularyId = assetCategory.getVocabularyId();
180    
181                                            if (assetVocabularyAssetCategoryIds.containsKey(
182                                                            assetVocabularyId)) {
183    
184                                                    String assetCategoryIds =
185                                                            assetVocabularyAssetCategoryIds.get(
186                                                                    assetVocabularyId);
187    
188                                                    assetVocabularyAssetCategoryIds.put(
189                                                            assetVocabularyId,
190                                                            assetCategoryIds + StringPool.COMMA +
191                                                                    assetCategoryId);
192                                            }
193                                            else {
194                                                    assetVocabularyAssetCategoryIds.put(
195                                                            assetVocabularyId, String.valueOf(assetCategoryId));
196                                            }
197                                    }
198    
199                                    for (Map.Entry<Long, String> entry :
200                                                    assetVocabularyAssetCategoryIds.entrySet()) {
201    
202                                            long assetVocabularyId = entry.getKey();
203                                            String assetCategoryIds = entry.getValue();
204    
205                                            addPortletURL.setParameter(
206                                                    "assetCategoryIds_" + assetVocabularyId,
207                                                    assetCategoryIds);
208                                    }
209                            }
210    
211                            if (allAssetTagNames != null) {
212                                    addPortletURL.setParameter(
213                                            "assetTagNames", StringUtil.merge(allAssetTagNames));
214                            }
215                    }
216    
217                    if (classTypeId > 0) {
218                            addPortletURL.setParameter(
219                                    "classTypeId", String.valueOf(classTypeId));
220    
221                            if (className.equals(DLFileEntry.class.getName())) {
222                                    addPortletURL.setParameter(Constants.CMD, Constants.ADD);
223                                    addPortletURL.setParameter(
224                                            "folderId",
225                                            String.valueOf(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID));
226                                    addPortletURL.setParameter(
227                                            "fileEntryTypeId", String.valueOf(classTypeId));
228                            }
229    
230                            if (className.equals(JournalArticle.class.getName())) {
231                                    DDMStructure ddmStructure =
232                                            DDMStructureLocalServiceUtil.getStructure(classTypeId);
233    
234                                    addPortletURL.setParameter(
235                                            "structureId", ddmStructure.getStructureKey());
236                            }
237                    }
238    
239                    addPortletURL.setPortletMode(PortletMode.VIEW);
240                    addPortletURL.setWindowState(LiferayWindowState.POP_UP);
241    
242                    return addPortletURL;
243            }
244    
245            public static String getAssetKeywords(String className, long classPK)
246                    throws SystemException {
247    
248                    List<AssetTag> tags = AssetTagLocalServiceUtil.getTags(
249                            className, classPK);
250                    List<AssetCategory> categories =
251                            AssetCategoryLocalServiceUtil.getCategories(className, classPK);
252    
253                    StringBuffer sb = new StringBuffer();
254    
255                    sb.append(ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
256    
257                    if (!tags.isEmpty()) {
258                            sb.append(StringPool.COMMA);
259                    }
260    
261                    sb.append(ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
262    
263                    return sb.toString();
264            }
265    
266            public static Set<String> getLayoutTagNames(HttpServletRequest request) {
267                    Set<String> tagNames = (Set<String>)request.getAttribute(
268                            WebKeys.ASSET_LAYOUT_TAG_NAMES);
269    
270                    if (tagNames == null) {
271                            tagNames = new HashSet<String>();
272    
273                            request.setAttribute(WebKeys.ASSET_LAYOUT_TAG_NAMES, tagNames);
274                    }
275    
276                    return tagNames;
277            }
278    
279            public static boolean isValidWord(String word) {
280                    if (Validator.isNull(word)) {
281                            return false;
282                    }
283                    else {
284                            char[] wordCharArray = word.toCharArray();
285    
286                            for (char c : wordCharArray) {
287                                    for (char invalidChar : INVALID_CHARACTERS) {
288                                            if (c == invalidChar) {
289                                                    if (_log.isDebugEnabled()) {
290                                                            _log.debug(
291                                                                    "Word " + word + " is not valid because " + c +
292                                                                            " is not allowed");
293                                                    }
294    
295                                                    return false;
296                                            }
297                                    }
298                            }
299                    }
300    
301                    return true;
302            }
303    
304            public static String substituteCategoryPropertyVariables(
305                            long groupId, long categoryId, String s)
306                    throws PortalException, SystemException {
307    
308                    String result = s;
309    
310                    AssetCategory category = null;
311    
312                    if (categoryId > 0) {
313                            try {
314                                    category = AssetCategoryLocalServiceUtil.getCategory(
315                                            categoryId);
316                            }
317                            catch (NoSuchCategoryException nsce) {
318                            }
319                    }
320    
321                    if (category != null) {
322                            List<AssetCategoryProperty> categoryProperties =
323                                    AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
324                                            categoryId);
325    
326                            for (AssetCategoryProperty categoryProperty : categoryProperties) {
327                                    result = StringUtil.replace(
328                                            result, "[$" + categoryProperty.getKey() + "$]",
329                                            categoryProperty.getValue());
330                            }
331                    }
332    
333                    return StringUtil.stripBetween(result, "[$", "$]");
334            }
335    
336            public static String substituteTagPropertyVariables(
337                            long groupId, String tagName, String s)
338                    throws PortalException, SystemException {
339    
340                    String result = s;
341    
342                    AssetTag tag = null;
343    
344                    if (tagName != null) {
345                            try {
346                                    tag = AssetTagLocalServiceUtil.getTag(groupId, tagName);
347                            }
348                            catch (NoSuchTagException nste) {
349                            }
350                    }
351    
352                    if (tag != null) {
353                            List<AssetTagProperty> tagProperties =
354                                    AssetTagPropertyLocalServiceUtil.getTagProperties(
355                                            tag.getTagId());
356    
357                            for (AssetTagProperty tagProperty : tagProperties) {
358                                    result = StringUtil.replace(
359                                            result, "[$" + tagProperty.getKey() + "$]",
360                                            tagProperty.getValue());
361                            }
362                    }
363    
364                    return StringUtil.stripBetween(result, "[$", "$]");
365            }
366    
367            public static String toWord(String text) {
368                    if (Validator.isNull(text)) {
369                            return text;
370                    }
371                    else {
372                            char[] textCharArray = text.toCharArray();
373    
374                            for (int i = 0; i < textCharArray.length; i++) {
375                                    char c = textCharArray[i];
376    
377                                    for (char invalidChar : INVALID_CHARACTERS) {
378                                            if (c == invalidChar) {
379                                                    textCharArray[i] = CharPool.SPACE;
380    
381                                                    break;
382                                            }
383                                    }
384                            }
385    
386                            return new String(textCharArray);
387                    }
388            }
389    
390            private static Log _log = LogFactoryUtil.getLog(AssetUtil.class);
391    
392    }