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.assetpublisher.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.asset.model.AssetEntry;
33  import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
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 assetEntryOrder)
59          throws Exception {
60  
61          String referringPortletResource =
62              ParamUtil.getString(actionRequest, "referringPortletResource");
63  
64          if (Validator.isNull(referringPortletResource)) {
65              return;
66          }
67  
68          AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
69              className, classPK);
70  
71          PortletPreferences preferences =
72              PortletPreferencesFactoryUtil.getPortletSetup(
73                  actionRequest, referringPortletResource);
74  
75          addSelection(
76              className, assetEntry.getEntryId(), assetEntryOrder, preferences);
77  
78          preferences.store();
79      }
80  
81      public static void addSelection(
82              ActionRequest actionRequest, PortletPreferences preferences)
83          throws Exception {
84  
85          String assetEntryType = ParamUtil.getString(
86              actionRequest, "assetEntryType");
87          long assetEntryId = ParamUtil.getLong(actionRequest, "assetEntryId");
88          int assetEntryOrder = ParamUtil.getInteger(
89              actionRequest, "assetEntryOrder");
90  
91          addSelection(
92              assetEntryType, assetEntryId, assetEntryOrder, preferences);
93      }
94  
95      public static void addSelection(
96              String assetEntryType, long assetEntryId, int assetEntryOrder,
97              PortletPreferences preferences)
98          throws Exception {
99  
100         String[] assetEntryXmls = preferences.getValues(
101             "asset-entry-xml", new String[0]);
102 
103         String assetEntryXml = _getAssetEntryXml(assetEntryType, assetEntryId);
104 
105         if (assetEntryOrder > -1) {
106             assetEntryXmls[assetEntryOrder] = assetEntryXml;
107         }
108         else {
109             assetEntryXmls = ArrayUtil.append(assetEntryXmls, assetEntryXml);
110         }
111 
112         preferences.setValues("asset-entry-xml", assetEntryXmls);
113     }
114 
115     public static void addRecentFolderId(
116         PortletRequest portletRequest, String className, long classPK) {
117 
118         _getRecentFolderIds(portletRequest).put(className, classPK);
119     }
120 
121     public static long getRecentFolderId(
122         PortletRequest portletRequest, String className) {
123 
124         Long classPK = _getRecentFolderIds(portletRequest).get(className);
125 
126         if (classPK == null) {
127             return 0;
128         }
129         else {
130             return classPK.longValue();
131         }
132     }
133 
134     public static void removeAndStoreSelection(
135             List<Long> assetEntryIds, PortletPreferences preferences)
136         throws Exception {
137 
138         if (assetEntryIds.size() == 0) {
139             return;
140         }
141 
142         String[] assetEntryXmls = preferences.getValues(
143             "asset-entry-xml", new String[0]);
144 
145         List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls);
146 
147         Iterator<String> itr = assetEntryXmlsList.iterator();
148 
149         while (itr.hasNext()) {
150             String assetEntryXml = itr.next();
151 
152             Document doc = SAXReaderUtil.read(assetEntryXml);
153 
154             Element root = doc.getRootElement();
155 
156             long assetEntryId = GetterUtil.getLong(
157                 root.element("asset-entry-id").getText());
158 
159             if (assetEntryIds.contains(assetEntryId)) {
160                 itr.remove();
161             }
162         }
163 
164         preferences.setValues(
165             "asset-entry-xml",
166             assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()]));
167 
168         preferences.store();
169     }
170 
171     private static String _getAssetEntryXml(
172         String assetEntryType, long assetEntryId) {
173 
174         String xml = null;
175 
176         try {
177             Document doc = SAXReaderUtil.createDocument(StringPool.UTF8);
178 
179             Element assetEntryEl = doc.addElement("asset-entry");
180 
181             assetEntryEl.addElement("asset-entry-type").addText(assetEntryType);
182             assetEntryEl.addElement("asset-entry-id").addText(
183                 String.valueOf(assetEntryId));
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 }