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