1
14
15 package com.liferay.portlet.asset.util;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.CharPool;
22 import com.liferay.portal.kernel.util.ListUtil;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.util.StringUtil;
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.util.PortalUtil;
27 import com.liferay.portal.util.WebKeys;
28 import com.liferay.portlet.asset.NoSuchCategoryException;
29 import com.liferay.portlet.asset.NoSuchTagException;
30 import com.liferay.portlet.asset.model.AssetCategory;
31 import com.liferay.portlet.asset.model.AssetCategoryProperty;
32 import com.liferay.portlet.asset.model.AssetTag;
33 import com.liferay.portlet.asset.model.AssetTagProperty;
34 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
35 import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
36 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
37 import com.liferay.portlet.asset.service.AssetTagPropertyLocalServiceUtil;
38
39 import java.util.Collections;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Set;
43
44 import javax.portlet.PortletURL;
45
46 import javax.servlet.http.HttpServletRequest;
47
48
54 public class AssetUtil {
55
56 public static char[] INVALID_CHARACTERS = new char[] {
57 CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
58 CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
59 CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
60 CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
61 CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
62 CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
63 CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
64 CharPool.STAR, CharPool.TILDE
65 };
66
67 public static Set<String> addLayoutTags(
68 HttpServletRequest request, List<AssetTag> tags) {
69
70 Set<String> layoutTags = getLayoutTagNames(request);
71
72 for (AssetTag tag : tags) {
73 layoutTags.add(tag.getName());
74 }
75
76 return layoutTags;
77 }
78
79 public static void addPortletBreadcrumbEntries(
80 long assetCategoryId, HttpServletRequest request,
81 PortletURL portletURL)
82 throws Exception {
83
84 AssetCategory assetCategory = AssetCategoryLocalServiceUtil.getCategory(
85 assetCategoryId);
86
87 List<AssetCategory> ancestorCategories = assetCategory.getAncestors();
88
89 Collections.reverse(ancestorCategories);
90
91 for (AssetCategory ancestorCategory : ancestorCategories) {
92 portletURL.setParameter("categoryId", String.valueOf(
93 ancestorCategory.getCategoryId()));
94
95 PortalUtil.addPortletBreadcrumbEntry(
96 request, ancestorCategory.getName(), portletURL.toString());
97 }
98
99 portletURL.setParameter("categoryId", String.valueOf(assetCategoryId));
100
101 PortalUtil.addPortletBreadcrumbEntry(
102 request, assetCategory.getName(), 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, "name"));
116
117 if (!tags.isEmpty()) {
118 sb.append(StringPool.COMMA);
119 }
120
121 sb.append(ListUtil.toString(categories, "name"));
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 }