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.servlet.taglib.ui.BreadcrumbEntry;
022    import com.liferay.portal.kernel.util.CharPool;
023    import com.liferay.portal.kernel.util.ListUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.asset.NoSuchCategoryException;
030    import com.liferay.portlet.asset.NoSuchTagException;
031    import com.liferay.portlet.asset.model.AssetCategory;
032    import com.liferay.portlet.asset.model.AssetCategoryProperty;
033    import com.liferay.portlet.asset.model.AssetTag;
034    import com.liferay.portlet.asset.model.AssetTagProperty;
035    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
036    import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
037    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
038    import com.liferay.portlet.asset.service.AssetTagPropertyLocalServiceUtil;
039    
040    import java.util.Collections;
041    import java.util.HashSet;
042    import java.util.List;
043    import java.util.Set;
044    
045    import javax.portlet.PortletURL;
046    
047    import javax.servlet.http.HttpServletRequest;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     * @author Jorge Ferrer
052     */
053    public class AssetUtil {
054    
055            public static final char[] INVALID_CHARACTERS = new char[] {
056                    CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
057                    CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
058                    CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
059                    CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
060                    CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
061                    CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
062                    CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
063                    CharPool.STAR, CharPool.TILDE
064            };
065    
066            public static Set<String> addLayoutTags(
067                    HttpServletRequest request, List<AssetTag> tags) {
068    
069                    Set<String> layoutTags = getLayoutTagNames(request);
070    
071                    for (AssetTag tag : tags) {
072                            layoutTags.add(tag.getName());
073                    }
074    
075                    return layoutTags;
076            }
077    
078            public static void addPortletBreadcrumbEntries(
079                            long assetCategoryId, HttpServletRequest request,
080                            PortletURL portletURL)
081                    throws Exception {
082    
083                    AssetCategory assetCategory = AssetCategoryLocalServiceUtil.getCategory(
084                            assetCategoryId);
085    
086                    List<AssetCategory> ancestorCategories = assetCategory.getAncestors();
087    
088                    Collections.reverse(ancestorCategories);
089    
090                    for (AssetCategory ancestorCategory : ancestorCategories) {
091                            portletURL.setParameter("categoryId", String.valueOf(
092                                    ancestorCategory.getCategoryId()));
093    
094                            addPortletBreadcrumbEntry(
095                                    request, ancestorCategory.getTitleCurrentValue(),
096                                    portletURL.toString());
097                    }
098    
099                    portletURL.setParameter("categoryId", String.valueOf(assetCategoryId));
100    
101                    addPortletBreadcrumbEntry(
102                            request, assetCategory.getTitleCurrentValue(),
103                            portletURL.toString());
104            }
105    
106            public static void addPortletBreadcrumbEntry(
107                            HttpServletRequest request, String title, String url)
108                    throws Exception {
109    
110                    List<BreadcrumbEntry> breadcrumbEntries =
111                            (List<BreadcrumbEntry>)request.getAttribute(
112                                    WebKeys.PORTLET_BREADCRUMBS);
113    
114                    if (breadcrumbEntries != null) {
115                            for (BreadcrumbEntry breadcrumbEntry : breadcrumbEntries) {
116                                    if (title.equals(breadcrumbEntry.getTitle())) {
117                                            return;
118                                    }
119                            }
120                    }
121    
122                    PortalUtil.addPortletBreadcrumbEntry(request, title, url);
123            }
124    
125            public static String getAssetKeywords(String className, long classPK)
126                    throws SystemException {
127    
128                    List<AssetTag> tags = AssetTagLocalServiceUtil.getTags(
129                            className, classPK);
130                    List<AssetCategory> categories =
131                            AssetCategoryLocalServiceUtil.getCategories(className, classPK);
132    
133                    StringBuffer sb = new StringBuffer();
134    
135                    sb.append(ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
136    
137                    if (!tags.isEmpty()) {
138                            sb.append(StringPool.COMMA);
139                    }
140    
141                    sb.append(ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
142    
143                    return sb.toString();
144            }
145    
146            public static Set<String> getLayoutTagNames(HttpServletRequest request) {
147                    Set<String> tagNames = (Set<String>)request.getAttribute(
148                            WebKeys.ASSET_LAYOUT_TAG_NAMES);
149    
150                    if (tagNames == null) {
151                            tagNames = new HashSet<String>();
152    
153                            request.setAttribute(WebKeys.ASSET_LAYOUT_TAG_NAMES, tagNames);
154                    }
155    
156                    return tagNames;
157            }
158    
159            public static boolean isValidWord(String word) {
160                    if (Validator.isNull(word)) {
161                            return false;
162                    }
163                    else {
164                            char[] wordCharArray = word.toCharArray();
165    
166                            for (char c : wordCharArray) {
167                                    for (char invalidChar : INVALID_CHARACTERS) {
168                                            if (c == invalidChar) {
169                                                    if (_log.isDebugEnabled()) {
170                                                            _log.debug(
171                                                                    "Word " + word + " is not valid because " + c +
172                                                                            " is not allowed");
173                                                    }
174    
175                                                    return false;
176                                            }
177                                    }
178                            }
179                    }
180    
181                    return true;
182            }
183    
184            public static String substituteCategoryPropertyVariables(
185                            long groupId, long categoryId, String s)
186                    throws PortalException, SystemException {
187    
188                    String result = s;
189    
190                    AssetCategory category = null;
191    
192                    if (categoryId > 0) {
193                            try {
194                                    category = AssetCategoryLocalServiceUtil.getCategory(
195                                            categoryId);
196                            }
197                            catch (NoSuchCategoryException nsce) {
198                            }
199                    }
200    
201                    if (category != null) {
202                            List<AssetCategoryProperty> categoryProperties =
203                                    AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
204                                            categoryId);
205    
206                            for (AssetCategoryProperty categoryProperty : categoryProperties) {
207                                    result = StringUtil.replace(
208                                            result, "[$" + categoryProperty.getKey() + "$]",
209                                            categoryProperty.getValue());
210                            }
211                    }
212    
213                    return StringUtil.stripBetween(result, "[$", "$]");
214            }
215    
216            public static String substituteTagPropertyVariables(
217                            long groupId, String tagName, String s)
218                    throws PortalException, SystemException {
219    
220                    String result = s;
221    
222                    AssetTag tag = null;
223    
224                    if (tagName != null) {
225                            try {
226                                    tag = AssetTagLocalServiceUtil.getTag(groupId, tagName);
227                            }
228                            catch (NoSuchTagException nste) {
229                            }
230                    }
231    
232                    if (tag != null) {
233                            List<AssetTagProperty> tagProperties =
234                                    AssetTagPropertyLocalServiceUtil.getTagProperties(
235                                            tag.getTagId());
236    
237                            for (AssetTagProperty tagProperty : tagProperties) {
238                                    result = StringUtil.replace(
239                                            result, "[$" + tagProperty.getKey() + "$]",
240                                            tagProperty.getValue());
241                            }
242                    }
243    
244                    return StringUtil.stripBetween(result, "[$", "$]");
245            }
246    
247            public static String toWord(String text) {
248                    if (Validator.isNull(text)) {
249                            return text;
250                    }
251                    else {
252                            char[] textCharArray = text.toCharArray();
253    
254                            for (int i = 0; i < textCharArray.length; i++) {
255                                    char c = textCharArray[i];
256    
257                                    for (char invalidChar : INVALID_CHARACTERS) {
258                                            if (c == invalidChar) {
259                                                    textCharArray[i] = CharPool.SPACE;
260    
261                                                    break;
262                                            }
263                                    }
264                            }
265    
266                            return new String(textCharArray);
267                    }
268            }
269    
270            private static Log _log = LogFactoryUtil.getLog(AssetUtil.class);
271    
272    }