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.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.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 final String TYPE_BLOG = "blog";
57  
58      public static final String TYPE_BOOKMARK = "bookmark";
59  
60      public static final String TYPE_CONTENT = "content";
61  
62      public static final String TYPE_DOCUMENT = "document";
63  
64      public static final String TYPE_IMAGE = "image";
65  
66      public static final String TYPE_THREAD = "thread";
67  
68      public static final String TYPE_WIKI = "wiki";
69  
70      public static void addAndStoreSelection(
71              ActionRequest actionRequest, String className, long classPK,
72              int assetOrder)
73          throws Exception {
74  
75          String referringPortletResource =
76              ParamUtil.getString(actionRequest, "referringPortletResource");
77  
78          if (Validator.isNull(referringPortletResource)) {
79              return;
80          }
81  
82          TagsAsset asset = TagsAssetLocalServiceUtil.getAsset(
83              className, classPK);
84  
85          PortletPreferences preferences =
86              PortletPreferencesFactoryUtil.getPortletSetup(
87                  actionRequest, referringPortletResource);
88  
89          addSelection(className, asset.getAssetId(), assetOrder, preferences);
90  
91          preferences.store();
92      }
93  
94      public static void addSelection(
95              ActionRequest actionRequest, PortletPreferences preferences)
96          throws Exception {
97  
98          String assetType = ParamUtil.getString(actionRequest, "assetType");
99          long assetId = ParamUtil.getLong(actionRequest, "assetId");
100         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
101 
102         addSelection(assetType, assetId, assetOrder, preferences);
103     }
104 
105     public static void addSelection(
106             String assetType, long assetId, int assetOrder,
107             PortletPreferences preferences)
108         throws Exception {
109 
110         String[] manualEntries = preferences.getValues(
111             "manual-entries", new String[0]);
112 
113         String assetConfig = _assetConfiguration(assetType, assetId);
114 
115         if (assetOrder > -1) {
116             manualEntries[assetOrder] = assetConfig;
117         }
118         else {
119             manualEntries = ArrayUtil.append(manualEntries, assetConfig);
120         }
121 
122         preferences.setValues("manual-entries", manualEntries);
123     }
124 
125     public static void addRecentFolderId(
126         PortletRequest portletRequest, String className, long classPK) {
127 
128         _getRecentFolderIds(portletRequest).put(className, classPK);
129     }
130 
131     public static long getRecentFolderId(
132         PortletRequest portletRequest, String className) {
133 
134         Long classPK = _getRecentFolderIds(portletRequest).get(className);
135 
136         if (classPK == null) {
137             return 0;
138         }
139         else {
140             return classPK.longValue();
141         }
142     }
143 
144     public static void removeAndStoreSelection(
145             List<Long> assetIds, PortletPreferences preferences)
146         throws Exception {
147 
148         if (assetIds.size() == 0) {
149             return;
150         }
151 
152         String[] manualEntries = preferences.getValues(
153             "manual-entries", new String[0]);
154 
155         List<String> manualEntriesList = ListUtil.fromArray(manualEntries);
156 
157         Iterator<String> itr = manualEntriesList.iterator();
158 
159         while (itr.hasNext()) {
160             String assetEntry = itr.next();
161 
162             Document doc = SAXReaderUtil.read(assetEntry);
163 
164             Element root = doc.getRootElement();
165 
166             long assetId = GetterUtil.getLong(
167                 root.element("asset-id").getText());
168 
169             if (assetIds.contains(assetId)) {
170                 itr.remove();
171             }
172         }
173 
174         preferences.setValues(
175             "manual-entries",
176             manualEntriesList.toArray(new String[manualEntriesList.size()]));
177 
178         preferences.store();
179     }
180 
181     public static void removeRecentFolderId(
182         PortletRequest portletRequest, String className, long classPK) {
183 
184         if (getRecentFolderId(portletRequest, className) == classPK) {
185             _getRecentFolderIds(portletRequest).remove(className);
186         }
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 }