1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class TagsUtil {
62  
63      public static final String[] ASSET_TYPE_CLASS_NAMES = {
64          BlogsEntry.class.getName(), BookmarksEntry.class.getName(),
65          DLFileEntry.class.getName(), IGImage.class.getName(),
66          JournalArticle.class.getName(), MBMessage.class.getName(),
67          WikiPage.class.getName()
68      };
69  
70      public static final String[] ASSET_TYPE_PORTLET_IDS = {
71          PortletKeys.BLOGS, PortletKeys.BOOKMARKS, PortletKeys.DOCUMENT_LIBRARY,
72          PortletKeys.IMAGE_GALLERY, PortletKeys.JOURNAL,
73          PortletKeys.MESSAGE_BOARDS, PortletKeys.WIKI
74      };
75  
76      public static char[] INVALID_CHARACTERS = new char[] {
77          CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
78          CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
79          CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
80          CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
81          CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
82          CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
83          CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
84          CharPool.STAR, CharPool.TILDE
85      };
86  
87      public static Set<String> addLayoutTagsEntries(
88          HttpServletRequest request, List<TagsEntry> entries) {
89  
90          Set<String> layoutTagsEntries = getLayoutTagsEntries(request);
91  
92          for (TagsEntry entry : entries) {
93              layoutTagsEntries.add(entry.getName());
94          }
95  
96          return layoutTagsEntries;
97      }
98  
99      public static Set<String> getLayoutTagsEntries(HttpServletRequest request) {
100         Set<String> entries = (Set<String>)request.getAttribute(
101             WebKeys.TAGS_LAYOUT_ENTRIES);
102 
103         if (entries == null) {
104             entries = new HashSet<String>();
105 
106             request.setAttribute(WebKeys.TAGS_LAYOUT_ENTRIES, entries);
107         }
108 
109         return entries;
110     }
111 
112     public static String[] getTagsCategories(PortletRequest portletRequest) {
113         return StringUtil.split(
114             ParamUtil.getString(portletRequest, "tagsCategories"));
115     }
116 
117     public static String[] getTagsEntries(PortletRequest portletRequest) {
118         return StringUtil.split(
119             ParamUtil.getString(portletRequest, "tagsEntries"));
120     }
121 
122     public static boolean isValidWord(String word) {
123         if (Validator.isNull(word)) {
124             return false;
125         }
126         else {
127             char[] wordCharArray = word.toCharArray();
128 
129             for (char c : wordCharArray) {
130                 for (char invalidChar : INVALID_CHARACTERS) {
131                     if (c == invalidChar) {
132                         if (_log.isDebugEnabled()) {
133                             _log.debug(
134                                 "Word " + word + " is not valid because " + c +
135                                     " is not allowed");
136                         }
137 
138                         return false;
139                     }
140                 }
141             }
142         }
143 
144         return true;
145     }
146 
147     public static String substitutePropertyVariables(
148             long groupId, String entryName, String s)
149         throws PortalException, SystemException {
150 
151         String result = s;
152 
153         TagsEntry entry = null;
154 
155         if (entryName != null) {
156             try {
157                 entry = TagsEntryLocalServiceUtil.getEntry(groupId, entryName);
158             }
159             catch (NoSuchEntryException nsee) {
160             }
161         }
162 
163         if (entry != null) {
164             List<TagsProperty> properties =
165                 TagsPropertyLocalServiceUtil.getProperties(entry.getEntryId());
166 
167             for (TagsProperty property : properties) {
168                 result = StringUtil.replace(
169                     result, "[$" + property.getKey() + "$]",
170                     property.getValue());
171             }
172         }
173 
174         return StringUtil.stripBetween(result, "[$", "$]");
175     }
176 
177     public static String toWord(String text) {
178         if (Validator.isNull(text)) {
179             return text;
180         }
181         else {
182             char[] textCharArray = text.toCharArray();
183 
184             for (int i = 0; i < textCharArray.length; i++) {
185                 char c = textCharArray[i];
186 
187                 for (char invalidChar : INVALID_CHARACTERS) {
188                     if (c == invalidChar) {
189                         textCharArray[i] = CharPool.SPACE;
190 
191                         break;
192                     }
193                 }
194             }
195 
196             return new String(textCharArray);
197         }
198     }
199 
200     private static Log _log = LogFactoryUtil.getLog(TagsUtil.class);
201 
202 }