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.taggedcontent.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 void addAndStoreSelection(
65              ActionRequest actionRequest, String className, long classPK,
66              int assetOrder)
67          throws Exception {
68  
69          String referringPortletResource =
70              ParamUtil.getString(actionRequest, "referringPortletResource");
71  
72          if (Validator.isNull(referringPortletResource)) {
73              return;
74          }
75  
76          TagsAsset asset = TagsAssetLocalServiceUtil.getAsset(
77              className, classPK);
78  
79          PortletPreferences prefs =
80              PortletPreferencesFactoryUtil.getPortletSetup(
81                  actionRequest, referringPortletResource);
82  
83          addSelection(className, asset.getAssetId(), assetOrder, prefs);
84  
85          prefs.store();
86      }
87  
88      public static void addSelection(
89              ActionRequest actionRequest, PortletPreferences prefs)
90          throws Exception {
91  
92          String assetType = ParamUtil.getString(actionRequest, "assetType");
93          long assetId = ParamUtil.getLong(actionRequest, "assetId");
94          int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
95  
96          addSelection(assetType, assetId, assetOrder, prefs);
97      }
98  
99      public static void addSelection(
100             String assetType, long assetId, int assetOrder,
101             PortletPreferences prefs)
102         throws Exception {
103 
104         String[] manualEntries = prefs.getValues(
105             "manual-entries", new String[0]);
106 
107         String assetConfig = _assetConfiguration(assetType, assetId);
108 
109         if (assetOrder > -1) {
110             manualEntries[assetOrder] = assetConfig;
111         }
112         else {
113             manualEntries = ArrayUtil.append(manualEntries, assetConfig);
114         }
115 
116         prefs.setValues("manual-entries", manualEntries);
117     }
118 
119     public static void addRecentFolderId(
120         PortletRequest portletRequest, String className, long classPK) {
121 
122         _getRecentFolderIds(portletRequest).put(className, classPK);
123     }
124 
125     public static long getRecentFolderId(
126         PortletRequest portletRequest, String className) {
127 
128         Long classPK = _getRecentFolderIds(portletRequest).get(className);
129 
130         if (classPK == null) {
131             return 0;
132         }
133         else {
134             return classPK.longValue();
135         }
136     }
137 
138     public static void removeAndStoreSelection(
139         List<Long> assetIds, PortletPreferences prefs) throws Exception {
140 
141         if (assetIds.size() == 0) {
142             return;
143         }
144 
145         String[] manualEntries = prefs.getValues(
146             "manual-entries", new String[0]);
147 
148         List<String> manualEntriesList = ListUtil.fromArray(manualEntries);
149 
150         Iterator<String> itr = manualEntriesList.iterator();
151 
152         while (itr.hasNext()) {
153             String assetEntry = itr.next();
154 
155             Document doc = SAXReaderUtil.read(assetEntry);
156 
157             Element root = doc.getRootElement();
158 
159             long assetId = GetterUtil.getLong(
160                 root.element("asset-id").getText());
161 
162             if (assetIds.contains(assetId)) {
163                 itr.remove();
164             }
165         }
166 
167         prefs.setValues(
168             "manual-entries",
169             manualEntriesList.toArray(new String[manualEntriesList.size()]));
170 
171         prefs.store();
172     }
173 
174     private static String _assetConfiguration(String assetType, long assetId) {
175         String xml = null;
176 
177         try {
178             Document doc = SAXReaderUtil.createDocument(StringPool.UTF8);
179 
180             Element asset = doc.addElement("asset");
181 
182             asset.addElement("asset-type").addText(assetType);
183             asset.addElement("asset-id").addText(String.valueOf(assetId));
184 
185             xml = doc.formattedString(StringPool.BLANK);
186         }
187         catch (IOException ioe) {
188             if (_log.isWarnEnabled()) {
189                 _log.warn(ioe);
190             }
191         }
192 
193         return xml;
194     }
195 
196     private static Map<String, Long> _getRecentFolderIds(
197         PortletRequest portletRequest) {
198 
199         HttpServletRequest request = PortalUtil.getHttpServletRequest(
200             portletRequest);
201         HttpSession session = request.getSession();
202 
203         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
204             WebKeys.THEME_DISPLAY);
205 
206         String key =
207             AssetPublisherUtil.class + "_" + themeDisplay.getScopeGroupId();
208 
209         Map<String, Long> recentFolderIds =
210             (Map<String, Long>)session.getAttribute(key);
211 
212         if (recentFolderIds == null) {
213             recentFolderIds = new HashMap<String, Long>();
214         }
215 
216         session.setAttribute(key, recentFolderIds);
217 
218         return recentFolderIds;
219     }
220 
221     private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
222 
223 }