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.assetpublisher.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ArrayUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.ListUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.kernel.xml.Document;
34  import com.liferay.portal.kernel.xml.Element;
35  import com.liferay.portal.kernel.xml.SAXReaderUtil;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.PortletPreferencesFactoryUtil;
40  import com.liferay.portlet.tags.model.TagsAsset;
41  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
42  
43  import java.io.IOException;
44  
45  import java.util.HashMap;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Map;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.PortletPreferences;
52  import javax.portlet.PortletRequest;
53  
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.http.HttpSession;
56  
57  /**
58   * <a href="AssetPublisherUtil.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Raymond Augé
61   */
62  public class AssetPublisherUtil {
63  
64      public static final String TYPE_BLOG = "blog";
65  
66      public static final String TYPE_BOOKMARK = "bookmark";
67  
68      public static final String TYPE_CONTENT = "content";
69  
70      public static final String TYPE_DOCUMENT = "document";
71  
72      public static final String TYPE_IMAGE = "image";
73  
74      public static final String TYPE_THREAD = "thread";
75  
76      public static final String TYPE_WIKI = "wiki";
77  
78      public static void addAndStoreSelection(
79              ActionRequest actionRequest, String className, long classPK,
80              int assetOrder)
81          throws Exception {
82  
83          String referringPortletResource =
84              ParamUtil.getString(actionRequest, "referringPortletResource");
85  
86          if (Validator.isNull(referringPortletResource)) {
87              return;
88          }
89  
90          TagsAsset asset = TagsAssetLocalServiceUtil.getAsset(
91              className, classPK);
92  
93          PortletPreferences preferences =
94              PortletPreferencesFactoryUtil.getPortletSetup(
95                  actionRequest, referringPortletResource);
96  
97          addSelection(className, asset.getAssetId(), assetOrder, preferences);
98  
99          preferences.store();
100     }
101 
102     public static void addSelection(
103             ActionRequest actionRequest, PortletPreferences preferences)
104         throws Exception {
105 
106         String assetType = ParamUtil.getString(actionRequest, "assetType");
107         long assetId = ParamUtil.getLong(actionRequest, "assetId");
108         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
109 
110         addSelection(assetType, assetId, assetOrder, preferences);
111     }
112 
113     public static void addSelection(
114             String assetType, long assetId, int assetOrder,
115             PortletPreferences preferences)
116         throws Exception {
117 
118         String[] manualEntries = preferences.getValues(
119             "manual-entries", new String[0]);
120 
121         String assetConfig = _assetConfiguration(assetType, assetId);
122 
123         if (assetOrder > -1) {
124             manualEntries[assetOrder] = assetConfig;
125         }
126         else {
127             manualEntries = ArrayUtil.append(manualEntries, assetConfig);
128         }
129 
130         preferences.setValues("manual-entries", manualEntries);
131     }
132 
133     public static void addRecentFolderId(
134         PortletRequest portletRequest, String className, long classPK) {
135 
136         _getRecentFolderIds(portletRequest).put(className, classPK);
137     }
138 
139     public static long getRecentFolderId(
140         PortletRequest portletRequest, String className) {
141 
142         Long classPK = _getRecentFolderIds(portletRequest).get(className);
143 
144         if (classPK == null) {
145             return 0;
146         }
147         else {
148             return classPK.longValue();
149         }
150     }
151 
152     public static void removeAndStoreSelection(
153             List<Long> assetIds, PortletPreferences preferences)
154         throws Exception {
155 
156         if (assetIds.size() == 0) {
157             return;
158         }
159 
160         String[] manualEntries = preferences.getValues(
161             "manual-entries", new String[0]);
162 
163         List<String> manualEntriesList = ListUtil.fromArray(manualEntries);
164 
165         Iterator<String> itr = manualEntriesList.iterator();
166 
167         while (itr.hasNext()) {
168             String assetEntry = itr.next();
169 
170             Document doc = SAXReaderUtil.read(assetEntry);
171 
172             Element root = doc.getRootElement();
173 
174             long assetId = GetterUtil.getLong(
175                 root.element("asset-id").getText());
176 
177             if (assetIds.contains(assetId)) {
178                 itr.remove();
179             }
180         }
181 
182         preferences.setValues(
183             "manual-entries",
184             manualEntriesList.toArray(new String[manualEntriesList.size()]));
185 
186         preferences.store();
187     }
188 
189     private static String _assetConfiguration(String assetType, long assetId) {
190         String xml = null;
191 
192         try {
193             Document doc = SAXReaderUtil.createDocument(StringPool.UTF8);
194 
195             Element asset = doc.addElement("asset");
196 
197             asset.addElement("asset-type").addText(assetType);
198             asset.addElement("asset-id").addText(String.valueOf(assetId));
199 
200             xml = doc.formattedString(StringPool.BLANK);
201         }
202         catch (IOException ioe) {
203             if (_log.isWarnEnabled()) {
204                 _log.warn(ioe);
205             }
206         }
207 
208         return xml;
209     }
210 
211     private static Map<String, Long> _getRecentFolderIds(
212         PortletRequest portletRequest) {
213 
214         HttpServletRequest request = PortalUtil.getHttpServletRequest(
215             portletRequest);
216         HttpSession session = request.getSession();
217 
218         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
219             WebKeys.THEME_DISPLAY);
220 
221         String key =
222             AssetPublisherUtil.class + "_" + themeDisplay.getScopeGroupId();
223 
224         Map<String, Long> recentFolderIds =
225             (Map<String, Long>)session.getAttribute(key);
226 
227         if (recentFolderIds == null) {
228             recentFolderIds = new HashMap<String, Long>();
229         }
230 
231         session.setAttribute(key, recentFolderIds);
232 
233         return recentFolderIds;
234     }
235 
236     private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
237 
238 }