001
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
052 public class AssetUtil {
053
054 public static final 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(
091 "categoryId", String.valueOf(ancestorCategory.getCategoryId()));
092
093 PortalUtil.addPortletBreadcrumbEntry(
094 request, ancestorCategory.getTitleCurrentValue(),
095 portletURL.toString());
096 }
097
098 portletURL.setParameter("categoryId", String.valueOf(assetCategoryId));
099
100 PortalUtil.addPortletBreadcrumbEntry(
101 request, assetCategory.getTitleCurrentValue(),
102 portletURL.toString());
103 }
104
105 public static String getAssetKeywords(String className, long classPK)
106 throws SystemException {
107
108 List<AssetTag> tags = AssetTagLocalServiceUtil.getTags(
109 className, classPK);
110 List<AssetCategory> categories =
111 AssetCategoryLocalServiceUtil.getCategories(className, classPK);
112
113 StringBuffer sb = new StringBuffer();
114
115 sb.append(ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
116
117 if (!tags.isEmpty()) {
118 sb.append(StringPool.COMMA);
119 }
120
121 sb.append(ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
122
123 return sb.toString();
124 }
125
126 public static Set<String> getLayoutTagNames(HttpServletRequest request) {
127 Set<String> tagNames = (Set<String>)request.getAttribute(
128 WebKeys.ASSET_LAYOUT_TAG_NAMES);
129
130 if (tagNames == null) {
131 tagNames = new HashSet<String>();
132
133 request.setAttribute(WebKeys.ASSET_LAYOUT_TAG_NAMES, tagNames);
134 }
135
136 return tagNames;
137 }
138
139 public static boolean isValidWord(String word) {
140 if (Validator.isNull(word)) {
141 return false;
142 }
143 else {
144 char[] wordCharArray = word.toCharArray();
145
146 for (char c : wordCharArray) {
147 for (char invalidChar : INVALID_CHARACTERS) {
148 if (c == invalidChar) {
149 if (_log.isDebugEnabled()) {
150 _log.debug(
151 "Word " + word + " is not valid because " + c +
152 " is not allowed");
153 }
154
155 return false;
156 }
157 }
158 }
159 }
160
161 return true;
162 }
163
164 public static String substituteCategoryPropertyVariables(
165 long groupId, long categoryId, String s)
166 throws PortalException, SystemException {
167
168 String result = s;
169
170 AssetCategory category = null;
171
172 if (categoryId > 0) {
173 try {
174 category = AssetCategoryLocalServiceUtil.getCategory(
175 categoryId);
176 }
177 catch (NoSuchCategoryException nsce) {
178 }
179 }
180
181 if (category != null) {
182 List<AssetCategoryProperty> categoryProperties =
183 AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
184 categoryId);
185
186 for (AssetCategoryProperty categoryProperty : categoryProperties) {
187 result = StringUtil.replace(
188 result, "[$" + categoryProperty.getKey() + "$]",
189 categoryProperty.getValue());
190 }
191 }
192
193 return StringUtil.stripBetween(result, "[$", "$]");
194 }
195
196 public static String substituteTagPropertyVariables(
197 long groupId, String tagName, String s)
198 throws PortalException, SystemException {
199
200 String result = s;
201
202 AssetTag tag = null;
203
204 if (tagName != null) {
205 try {
206 tag = AssetTagLocalServiceUtil.getTag(groupId, tagName);
207 }
208 catch (NoSuchTagException nste) {
209 }
210 }
211
212 if (tag != null) {
213 List<AssetTagProperty> tagProperties =
214 AssetTagPropertyLocalServiceUtil.getTagProperties(
215 tag.getTagId());
216
217 for (AssetTagProperty tagProperty : tagProperties) {
218 result = StringUtil.replace(
219 result, "[$" + tagProperty.getKey() + "$]",
220 tagProperty.getValue());
221 }
222 }
223
224 return StringUtil.stripBetween(result, "[$", "$]");
225 }
226
227 public static String toWord(String text) {
228 if (Validator.isNull(text)) {
229 return text;
230 }
231 else {
232 char[] textCharArray = text.toCharArray();
233
234 for (int i = 0; i < textCharArray.length; i++) {
235 char c = textCharArray[i];
236
237 for (char invalidChar : INVALID_CHARACTERS) {
238 if (c == invalidChar) {
239 textCharArray[i] = CharPool.SPACE;
240
241 break;
242 }
243 }
244 }
245
246 return new String(textCharArray);
247 }
248 }
249
250 private static Log _log = LogFactoryUtil.getLog(AssetUtil.class);
251
252 }