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