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