1
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.StringUtil;
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.kernel.xml.Document;
27 import com.liferay.portal.kernel.xml.Element;
28 import com.liferay.portal.kernel.xml.SAXReaderUtil;
29 import com.liferay.portal.model.Group;
30 import com.liferay.portal.model.Layout;
31 import com.liferay.portal.service.LayoutLocalServiceUtil;
32 import com.liferay.portal.theme.ThemeDisplay;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.portal.util.WebKeys;
35 import com.liferay.portlet.PortletPreferencesFactoryUtil;
36 import com.liferay.portlet.asset.model.AssetEntry;
37 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
38 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
39 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
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
60 public class AssetPublisherUtil {
61
62 public static void addAndStoreSelection(
63 ActionRequest actionRequest, String className, long classPK,
64 int assetEntryOrder)
65 throws Exception {
66
67 String referringPortletResource =
68 ParamUtil.getString(actionRequest, "referringPortletResource");
69
70 if (Validator.isNull(referringPortletResource)) {
71 return;
72 }
73
74 AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
75 className, classPK);
76
77 PortletPreferences preferences =
78 PortletPreferencesFactoryUtil.getPortletSetup(
79 actionRequest, referringPortletResource);
80
81 addSelection(
82 className, assetEntry.getEntryId(), assetEntryOrder, preferences);
83
84 preferences.store();
85 }
86
87 public static void addRecentFolderId(
88 PortletRequest portletRequest, String className, long classPK) {
89
90 _getRecentFolderIds(portletRequest).put(className, classPK);
91 }
92
93 public static void addSelection(
94 ActionRequest actionRequest, PortletPreferences preferences)
95 throws Exception {
96
97 String assetEntryType = ParamUtil.getString(
98 actionRequest, "assetEntryType");
99 long assetEntryId = ParamUtil.getLong(actionRequest, "assetEntryId");
100 int assetEntryOrder = ParamUtil.getInteger(
101 actionRequest, "assetEntryOrder");
102
103 addSelection(
104 assetEntryType, assetEntryId, assetEntryOrder, preferences);
105 }
106
107 public static void addSelection(
108 String assetEntryType, long assetEntryId, int assetEntryOrder,
109 PortletPreferences preferences)
110 throws Exception {
111
112 String[] assetEntryXmls = preferences.getValues(
113 "asset-entry-xml", new String[0]);
114
115 String assetEntryXml = _getAssetEntryXml(assetEntryType, assetEntryId);
116
117 if (assetEntryOrder > -1) {
118 assetEntryXmls[assetEntryOrder] = assetEntryXml;
119 }
120 else {
121 assetEntryXmls = ArrayUtil.append(assetEntryXmls, assetEntryXml);
122 }
123
124 preferences.setValues("asset-entry-xml", assetEntryXmls);
125 }
126
127 public static AssetEntryQuery getAssetEntryQuery(
128 PortletPreferences preferences, long scopeGroupId)
129 throws Exception {
130
131 AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
132
133 long[] allAssetCategoryIds = new long[0];
134 long[] anyAssetCategoryIds = new long[0];
135 long[] notAllAssetCategoryIds = new long[0];
136 long[] notAnyAssetCategoryIds = new long[0];
137
138 String[] allAssetTagNames = new String[0];
139 String[] anyAssetTagNames = new String[0];
140 String[] notAllAssetTagNames = new String[0];
141 String[] notAnyAssetTagNames = new String[0];
142
143 for (int i = 0; true; i++) {
144 String[] queryValues = preferences.getValues(
145 "queryValues" + i, null);
146
147 if ((queryValues == null) || (queryValues.length == 0)) {
148 break;
149 }
150
151 boolean queryContains = GetterUtil.getBoolean(
152 preferences.getValue("queryContains" + i, StringPool.BLANK));
153 boolean queryAndOperator = GetterUtil.getBoolean(
154 preferences.getValue("queryAndOperator" + i, StringPool.BLANK));
155 String queryName = preferences.getValue(
156 "queryName" + i, StringPool.BLANK);
157
158 if (Validator.equals(queryName, "assetCategories")) {
159 long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);
160
161 if (queryContains && queryAndOperator) {
162 allAssetCategoryIds = assetCategoryIds;
163 }
164 else if (queryContains && !queryAndOperator) {
165 anyAssetCategoryIds = assetCategoryIds;
166 }
167 else if (!queryContains && queryAndOperator) {
168 notAllAssetCategoryIds = assetCategoryIds;
169 }
170 else {
171 notAnyAssetCategoryIds = assetCategoryIds;
172 }
173 }
174 else {
175 if (queryContains && queryAndOperator) {
176 allAssetTagNames = queryValues;
177 }
178 else if (queryContains && !queryAndOperator) {
179 anyAssetTagNames = queryValues;
180 }
181 else if (!queryContains && queryAndOperator) {
182 notAllAssetTagNames = queryValues;
183 }
184 else {
185 notAnyAssetTagNames = queryValues;
186 }
187 }
188 }
189
190 long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
191 scopeGroupId, allAssetTagNames);
192 long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
193 scopeGroupId, anyAssetTagNames);
194 long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
195 scopeGroupId, notAllAssetTagNames);
196 long[] notAnyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
197 scopeGroupId, notAnyAssetTagNames);
198
199 assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);
200 assetEntryQuery.setAllTagIds(allAssetTagIds);
201 assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);
202 assetEntryQuery.setAnyTagIds(anyAssetTagIds);
203 assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);
204 assetEntryQuery.setNotAllTagIds(notAllAssetTagIds);
205 assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);
206 assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);
207
208 return assetEntryQuery;
209 }
210
211 public static String[] getAssetTagNames(
212 PortletPreferences preferences, long scopeGroupId)
213 throws Exception {
214
215 String[] allAssetTagNames = new String[0];
216
217 for (int i = 0; true; i++) {
218 String[] queryValues = preferences.getValues(
219 "queryValues" + i, null);
220
221 if ((queryValues == null) || (queryValues.length == 0)) {
222 break;
223 }
224
225 boolean queryContains = GetterUtil.getBoolean(
226 preferences.getValue("queryContains" + i, StringPool.BLANK));
227 boolean queryAndOperator = GetterUtil.getBoolean(
228 preferences.getValue("queryAndOperator" + i, StringPool.BLANK));
229 String queryName = preferences.getValue(
230 "queryName" + i, StringPool.BLANK);
231
232 if (!Validator.equals(queryName, "assetCategories") &&
233 queryContains && queryAndOperator) {
234
235 allAssetTagNames = queryValues;
236 }
237 }
238
239 return allAssetTagNames;
240 }
241
242 public static long[] getClassNameIds(
243 PortletPreferences preferences, long[] availableClassNameIds) {
244
245 boolean anyAssetType = GetterUtil.getBoolean(
246 preferences.getValue("any-asset-type", Boolean.TRUE.toString()));
247
248 long[] classNameIds = null;
249
250 if (!anyAssetType &&
251 (preferences.getValues("class-name-ids", null) != null)) {
252
253 classNameIds = GetterUtil.getLongValues(
254 preferences.getValues("class-name-ids", null));
255 }
256 else {
257 classNameIds = availableClassNameIds;
258 }
259
260 return classNameIds;
261 }
262
263 public static long[] getGroupIds(
264 PortletPreferences preferences, long scopeGroupId, Layout layout) {
265
266 long[] groupIds = new long[] {scopeGroupId};
267
268 boolean defaultScope = GetterUtil.getBoolean(
269 preferences.getValue("default-scope", null), true);
270
271 if (!defaultScope) {
272 String[] scopeIds = preferences.getValues(
273 "scope-ids",
274 new String[] {"group" + StringPool.UNDERLINE + scopeGroupId});
275
276 groupIds = new long[scopeIds.length];
277
278 for (int i = 0; i < scopeIds.length; i++) {
279 try {
280 String[] scopeIdFragments = StringUtil.split(
281 scopeIds[i], StringPool.UNDERLINE);
282
283 if (scopeIdFragments[0].equals("Layout")) {
284 long scopeIdLayoutId = GetterUtil.getLong(
285 scopeIdFragments[1]);
286
287 Layout scopeIdLayout =
288 LayoutLocalServiceUtil.getLayout(
289 scopeGroupId, layout.isPrivateLayout(),
290 scopeIdLayoutId);
291
292 Group scopeIdGroup = scopeIdLayout.getScopeGroup();
293
294 groupIds[i] = scopeIdGroup.getGroupId();
295 }
296 else {
297 long scopeIdGroupId = GetterUtil.getLong(
298 scopeIdFragments[1]);
299
300 groupIds[i] = scopeIdGroupId;
301 }
302 }
303 catch (Exception e) {
304 continue;
305 }
306 }
307 }
308
309 return groupIds;
310 }
311
312 public static long getRecentFolderId(
313 PortletRequest portletRequest, String className) {
314
315 Long classPK = _getRecentFolderIds(portletRequest).get(className);
316
317 if (classPK == null) {
318 return 0;
319 }
320 else {
321 return classPK.longValue();
322 }
323 }
324
325 public static void removeAndStoreSelection(
326 List<Long> assetEntryIds, PortletPreferences preferences)
327 throws Exception {
328
329 if (assetEntryIds.size() == 0) {
330 return;
331 }
332
333 String[] assetEntryXmls = preferences.getValues(
334 "asset-entry-xml", new String[0]);
335
336 List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls);
337
338 Iterator<String> itr = assetEntryXmlsList.iterator();
339
340 while (itr.hasNext()) {
341 String assetEntryXml = itr.next();
342
343 Document doc = SAXReaderUtil.read(assetEntryXml);
344
345 Element root = doc.getRootElement();
346
347 long assetEntryId = GetterUtil.getLong(
348 root.element("asset-entry-id").getText());
349
350 if (assetEntryIds.contains(assetEntryId)) {
351 itr.remove();
352 }
353 }
354
355 preferences.setValues(
356 "asset-entry-xml",
357 assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()]));
358
359 preferences.store();
360 }
361
362 public static void removeRecentFolderId(
363 PortletRequest portletRequest, String className, long classPK) {
364
365 if (getRecentFolderId(portletRequest, className) == classPK) {
366 _getRecentFolderIds(portletRequest).remove(className);
367 }
368 }
369
370 private static String _getAssetEntryXml(
371 String assetEntryType, long assetEntryId) {
372
373 String xml = null;
374
375 try {
376 Document doc = SAXReaderUtil.createDocument(StringPool.UTF8);
377
378 Element assetEntryEl = doc.addElement("asset-entry");
379
380 assetEntryEl.addElement("asset-entry-type").addText(assetEntryType);
381 assetEntryEl.addElement("asset-entry-id").addText(
382 String.valueOf(assetEntryId));
383
384 xml = doc.formattedString(StringPool.BLANK);
385 }
386 catch (IOException ioe) {
387 if (_log.isWarnEnabled()) {
388 _log.warn(ioe);
389 }
390 }
391
392 return xml;
393 }
394
395 private static Map<String, Long> _getRecentFolderIds(
396 PortletRequest portletRequest) {
397
398 HttpServletRequest request = PortalUtil.getHttpServletRequest(
399 portletRequest);
400 HttpSession session = request.getSession();
401
402 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
403 WebKeys.THEME_DISPLAY);
404
405 String key =
406 AssetPublisherUtil.class + "_" + themeDisplay.getScopeGroupId();
407
408 Map<String, Long> recentFolderIds =
409 (Map<String, Long>)session.getAttribute(key);
410
411 if (recentFolderIds == null) {
412 recentFolderIds = new HashMap<String, Long>();
413 }
414
415 session.setAttribute(key, recentFolderIds);
416
417 return recentFolderIds;
418 }
419
420 private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
421
422 }