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