1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.taggedcontent.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.ListUtil;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.PortletPreferencesFactoryUtil;
32  import com.liferay.portlet.tags.model.TagsAsset;
33  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
34  
35  import java.io.IOException;
36  
37  import java.util.HashMap;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.PortletPreferences;
44  import javax.portlet.PortletRequest;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpSession;
48  
49  /**
50   * <a href="AssetPublisherUtil.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Raymond Augé
53   */
54  public class AssetPublisherUtil {
55  
56      public static void addAndStoreSelection(
57              ActionRequest actionRequest, String className, long classPK,
58              int assetOrder)
59          throws Exception {
60  
61          String referringPortletResource =
62              ParamUtil.getString(actionRequest, "referringPortletResource");
63  
64          if (Validator.isNull(referringPortletResource)) {
65              return;
66          }
67  
68          TagsAsset asset = TagsAssetLocalServiceUtil.getAsset(
69              className, classPK);
70  
71          PortletPreferences prefs =
72              PortletPreferencesFactoryUtil.getPortletSetup(
73                  actionRequest, referringPortletResource);
74  
75          addSelection(className, asset.getAssetId(), assetOrder, prefs);
76  
77          prefs.store();
78      }
79  
80      public static void addSelection(
81              ActionRequest actionRequest, PortletPreferences prefs)
82          throws Exception {
83  
84          String assetType = ParamUtil.getString(actionRequest, "assetType");
85          long assetId = ParamUtil.getLong(actionRequest, "assetId");
86          int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
87  
88          addSelection(assetType, assetId, assetOrder, prefs);
89      }
90  
91      public static void addSelection(
92              String assetType, long assetId, int assetOrder,
93              PortletPreferences prefs)
94          throws Exception {
95  
96          String[] manualEntries = prefs.getValues(
97              "manual-entries", new String[0]);
98  
99          String assetConfig = _assetConfiguration(assetType, assetId);
100 
101         if (assetOrder > -1) {
102             manualEntries[assetOrder] = assetConfig;
103         }
104         else {
105             manualEntries = ArrayUtil.append(manualEntries, assetConfig);
106         }
107 
108         prefs.setValues("manual-entries", manualEntries);
109     }
110 
111     public static void addRecentFolderId(
112         PortletRequest portletRequest, String className, long classPK) {
113 
114         _getRecentFolderIds(portletRequest).put(className, classPK);
115     }
116 
117     public static long getRecentFolderId(
118         PortletRequest portletRequest, String className) {
119 
120         Long classPK = _getRecentFolderIds(portletRequest).get(className);
121 
122         if (classPK == null) {
123             return 0;
124         }
125         else {
126             return classPK.longValue();
127         }
128     }
129 
130     public static void removeAndStoreSelection(
131         List<Long> assetIds, PortletPreferences prefs) throws Exception {
132 
133         if (assetIds.size() == 0) {
134             return;
135         }
136 
137         String[] manualEntries = prefs.getValues(
138             "manual-entries", new String[0]);
139 
140         List<String> manualEntriesList = ListUtil.fromArray(manualEntries);
141 
142         Iterator<String> itr = manualEntriesList.iterator();
143 
144         while (itr.hasNext()) {
145             String assetEntry = itr.next();
146 
147             Document doc = SAXReaderUtil.read(assetEntry);
148 
149             Element root = doc.getRootElement();
150 
151             long assetId = GetterUtil.getLong(
152                 root.element("asset-id").getText());
153 
154             if (assetIds.contains(assetId)) {
155                 itr.remove();
156             }
157         }
158 
159         prefs.setValues(
160             "manual-entries",
161             manualEntriesList.toArray(new String[manualEntriesList.size()]));
162 
163         prefs.store();
164     }
165 
166     private static String _assetConfiguration(String assetType, long assetId) {
167         String xml = null;
168 
169         try {
170             Document doc = SAXReaderUtil.createDocument(StringPool.UTF8);
171 
172             Element asset = doc.addElement("asset");
173 
174             asset.addElement("asset-type").addText(assetType);
175             asset.addElement("asset-id").addText(String.valueOf(assetId));
176 
177             xml = doc.formattedString(StringPool.BLANK);
178         }
179         catch (IOException ioe) {
180             if (_log.isWarnEnabled()) {
181                 _log.warn(ioe);
182             }
183         }
184 
185         return xml;
186     }
187 
188     private static Map<String, Long> _getRecentFolderIds(
189         PortletRequest portletRequest) {
190 
191         HttpServletRequest request = PortalUtil.getHttpServletRequest(
192             portletRequest);
193         HttpSession session = request.getSession();
194 
195         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
196             WebKeys.THEME_DISPLAY);
197 
198         String key =
199             AssetPublisherUtil.class + "_" + themeDisplay.getScopeGroupId();
200 
201         Map<String, Long> recentFolderIds =
202             (Map<String, Long>)session.getAttribute(key);
203 
204         if (recentFolderIds == null) {
205             recentFolderIds = new HashMap<String, Long>();
206         }
207 
208         session.setAttribute(key, recentFolderIds);
209 
210         return recentFolderIds;
211     }
212 
213     private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
214 
215 }