1   /**
2    * Copyright (c) 2000-2008 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.util.ArrayUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.PortletPreferencesFactoryUtil;
33  import com.liferay.portlet.tags.model.TagsAsset;
34  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
35  import com.liferay.util.xml.XMLFormatter;
36  
37  import java.io.IOException;
38  
39  import java.util.HashMap;
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  import org.apache.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  
52  import org.dom4j.Document;
53  import org.dom4j.DocumentFactory;
54  import org.dom4j.Element;
55  
56  /**
57   * <a href="AssetPublisherUtil.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Raymond Augé
60   *
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     private static String _assetConfiguration(String assetType, long assetId) {
139         String xml = null;
140 
141         try {
142             DocumentFactory docFactory = DocumentFactory.getInstance();
143 
144             Document doc = docFactory.createDocument(StringPool.UTF8);
145 
146             Element asset = doc.addElement("asset");
147 
148             asset.addElement("asset-type").addText(assetType);
149             asset.addElement("asset-id").addText(String.valueOf(assetId));
150 
151             xml = XMLFormatter.toString(doc, StringPool.BLANK);
152         }
153         catch (IOException ioe) {
154             if (_log.isWarnEnabled()) {
155                 _log.warn(ioe);
156             }
157         }
158 
159         return xml;
160     }
161 
162     private static Map<String, Long> _getRecentFolderIds(
163         PortletRequest portletRequest) {
164 
165         HttpServletRequest request = PortalUtil.getHttpServletRequest(
166             portletRequest);
167         HttpSession session = request.getSession();
168 
169         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
170             WebKeys.THEME_DISPLAY);
171 
172         String key =
173             AssetPublisherUtil.class + "_" + themeDisplay.getPortletGroupId();
174 
175         Map<String, Long> recentFolderIds =
176             (Map<String, Long>)session.getAttribute(key);
177 
178         if (recentFolderIds == null) {
179             recentFolderIds = new HashMap<String, Long>();
180         }
181 
182         session.setAttribute(key, recentFolderIds);
183 
184         return recentFolderIds;
185     }
186 
187     private static Log _log = LogFactory.getLog(AssetPublisherUtil.class);
188 
189 }