1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tags.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.CharPool;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.blogs.model.BlogsEntry;
36  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
37  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
38  import com.liferay.portlet.imagegallery.model.IGImage;
39  import com.liferay.portlet.journal.model.JournalArticle;
40  import com.liferay.portlet.messageboards.model.MBMessage;
41  import com.liferay.portlet.tags.NoSuchEntryException;
42  import com.liferay.portlet.tags.model.TagsEntry;
43  import com.liferay.portlet.tags.model.TagsProperty;
44  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
45  import com.liferay.portlet.tags.service.TagsPropertyLocalServiceUtil;
46  import com.liferay.portlet.wiki.model.WikiPage;
47  
48  import java.util.HashSet;
49  import java.util.List;
50  import java.util.Set;
51  
52  import javax.portlet.PortletRequest;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  /**
57   * <a href="TagsUtil.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class TagsUtil {
63  
64      public static final String[] ASSET_TYPE_CLASS_NAMES = {
65          BlogsEntry.class.getName(), BookmarksEntry.class.getName(),
66          DLFileEntry.class.getName(), IGImage.class.getName(),
67          JournalArticle.class.getName(), MBMessage.class.getName(),
68          WikiPage.class.getName()
69      };
70  
71      public static final String[] ASSET_TYPE_PORTLET_IDS = {
72          PortletKeys.BLOGS, PortletKeys.BOOKMARKS, PortletKeys.DOCUMENT_LIBRARY,
73          PortletKeys.IMAGE_GALLERY, PortletKeys.JOURNAL,
74          PortletKeys.MESSAGE_BOARDS, PortletKeys.WIKI
75      };
76  
77      public static char[] INVALID_CHARACTERS = new char[] {
78          CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
79          CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
80          CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
81          CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
82          CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
83          CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
84          CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
85          CharPool.STAR, CharPool.TILDE
86      };
87  
88      public static Set<String> addLayoutTagsEntries(
89          HttpServletRequest request, List<TagsEntry> entries) {
90  
91          Set<String> layoutTagsEntries = getLayoutTagsEntries(request);
92  
93          for (TagsEntry entry : entries) {
94              layoutTagsEntries.add(entry.getName());
95          }
96  
97          return layoutTagsEntries;
98      }
99  
100     public static Set<String> getLayoutTagsEntries(HttpServletRequest request) {
101         Set<String> entries = (Set<String>)request.getAttribute(
102             WebKeys.TAGS_LAYOUT_ENTRIES);
103 
104         if (entries == null) {
105             entries = new HashSet<String>();
106 
107             request.setAttribute(WebKeys.TAGS_LAYOUT_ENTRIES, entries);
108         }
109 
110         return entries;
111     }
112 
113     public static String[] getTagsCategories(PortletRequest portletRequest) {
114         return StringUtil.split(
115             ParamUtil.getString(portletRequest, "tagsCategories"));
116     }
117 
118     public static String[] getTagsEntries(PortletRequest portletRequest) {
119         return StringUtil.split(
120             ParamUtil.getString(portletRequest, "tagsEntries"));
121     }
122 
123     public static boolean isValidWord(String word) {
124         if (Validator.isNull(word)) {
125             return false;
126         }
127         else {
128             char[] wordCharArray = word.toCharArray();
129 
130             for (char c : wordCharArray) {
131                 for (char invalidChar : INVALID_CHARACTERS) {
132                     if (c == invalidChar) {
133                         if (_log.isDebugEnabled()) {
134                             _log.debug(
135                                 "Word " + word + " is not valid because " + c +
136                                     " is not allowed");
137                         }
138 
139                         return false;
140                     }
141                 }
142             }
143         }
144 
145         return true;
146     }
147 
148     public static String substitutePropertyVariables(
149             long groupId, String entryName, String s)
150         throws PortalException, SystemException {
151 
152         String result = s;
153 
154         TagsEntry entry = null;
155 
156         if (entryName != null) {
157             try {
158                 entry = TagsEntryLocalServiceUtil.getEntry(groupId, entryName);
159             }
160             catch (NoSuchEntryException nsee) {
161             }
162         }
163 
164         if (entry != null) {
165             List<TagsProperty> properties =
166                 TagsPropertyLocalServiceUtil.getProperties(entry.getEntryId());
167 
168             for (TagsProperty property : properties) {
169                 result = StringUtil.replace(
170                     result, "[$" + property.getKey() + "$]",
171                     property.getValue());
172             }
173         }
174 
175         return StringUtil.stripBetween(result, "[$", "$]");
176     }
177 
178     public static String toWord(String text) {
179         if (Validator.isNull(text)) {
180             return text;
181         }
182         else {
183             char[] textCharArray = text.toCharArray();
184 
185             for (int i = 0; i < textCharArray.length; i++) {
186                 char c = textCharArray[i];
187 
188                 for (char invalidChar : INVALID_CHARACTERS) {
189                     if (c == invalidChar) {
190                         textCharArray[i] = CharPool.SPACE;
191 
192                         break;
193                     }
194                 }
195             }
196 
197             return new String(textCharArray);
198         }
199     }
200 
201     private static Log _log = LogFactoryUtil.getLog(TagsUtil.class);
202 
203 }