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