001
014
015 package com.liferay.portlet.assetpublisher.util;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.ParamUtil;
025 import com.liferay.portal.kernel.util.PrimitiveLongList;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.kernel.xml.Document;
030 import com.liferay.portal.kernel.xml.Element;
031 import com.liferay.portal.kernel.xml.SAXReaderUtil;
032 import com.liferay.portal.model.Group;
033 import com.liferay.portal.model.GroupConstants;
034 import com.liferay.portal.model.Layout;
035 import com.liferay.portal.model.User;
036 import com.liferay.portal.service.GroupLocalServiceUtil;
037 import com.liferay.portal.service.LayoutLocalServiceUtil;
038 import com.liferay.portal.theme.ThemeDisplay;
039 import com.liferay.portal.util.PortalUtil;
040 import com.liferay.portal.util.WebKeys;
041 import com.liferay.portlet.PortletPreferencesFactoryUtil;
042 import com.liferay.portlet.asset.model.AssetCategory;
043 import com.liferay.portlet.asset.model.AssetEntry;
044 import com.liferay.portlet.asset.model.AssetRendererFactory;
045 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
046 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
047 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
048 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
049 import com.liferay.portlet.expando.model.ExpandoBridge;
050
051 import java.io.IOException;
052 import java.io.Serializable;
053
054 import java.util.HashMap;
055 import java.util.Iterator;
056 import java.util.List;
057 import java.util.Map;
058
059 import javax.portlet.PortletPreferences;
060 import javax.portlet.PortletRequest;
061
062 import javax.servlet.http.HttpServletRequest;
063 import javax.servlet.http.HttpSession;
064
065
068 public class AssetPublisherUtil {
069
070 public static void addAndStoreSelection(
071 PortletRequest portletRequest, String className, long classPK,
072 int assetEntryOrder)
073 throws Exception {
074
075 String referringPortletResource = ParamUtil.getString(
076 portletRequest, "referringPortletResource");
077
078 if (Validator.isNull(referringPortletResource)) {
079 return;
080 }
081
082 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
083 WebKeys.THEME_DISPLAY);
084
085 Layout layout = LayoutLocalServiceUtil.getLayout(
086 themeDisplay.getRefererPlid());
087
088 PortletPreferences portletPreferences =
089 PortletPreferencesFactoryUtil.getPortletSetup(
090 themeDisplay.getScopeGroupId(), layout,
091 referringPortletResource, null);
092
093 String selectionStyle = portletPreferences.getValue(
094 "selectionStyle", "dynamic");
095
096 if (selectionStyle.equals("dynamic")) {
097 return;
098 }
099
100 AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
101 className, classPK);
102
103 addSelection(
104 className, assetEntry.getEntryId(), assetEntryOrder,
105 portletPreferences);
106
107 portletPreferences.store();
108 }
109
110 public static void addRecentFolderId(
111 PortletRequest portletRequest, String className, long classPK) {
112
113 _getRecentFolderIds(portletRequest).put(className, classPK);
114 }
115
116 public static void addSelection(
117 PortletRequest portletRequest,
118 PortletPreferences portletPreferences)
119 throws Exception {
120
121 String assetEntryType = ParamUtil.getString(
122 portletRequest, "assetEntryType");
123 long assetEntryId = ParamUtil.getLong(portletRequest, "assetEntryId");
124 int assetEntryOrder = ParamUtil.getInteger(
125 portletRequest, "assetEntryOrder");
126
127 addSelection(
128 assetEntryType, assetEntryId, assetEntryOrder, portletPreferences);
129 }
130
131 public static void addSelection(
132 String assetEntryType, long assetEntryId, int assetEntryOrder,
133 PortletPreferences portletPreferences)
134 throws Exception {
135
136 AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
137 assetEntryId);
138
139 String[] assetEntryXmls = portletPreferences.getValues(
140 "assetEntryXml", new String[0]);
141
142 String assetEntryXml = _getAssetEntryXml(
143 assetEntryType, assetEntry.getClassUuid());
144
145 if (assetEntryOrder > -1) {
146 assetEntryXmls[assetEntryOrder] = assetEntryXml;
147 }
148 else {
149 assetEntryXmls = ArrayUtil.append(assetEntryXmls, assetEntryXml);
150 }
151
152 portletPreferences.setValues("assetEntryXml", assetEntryXmls);
153 }
154
155 public static void addUserAttributes(
156 User user, String[] customUserAttributeNames,
157 AssetEntryQuery assetEntryQuery)
158 throws Exception {
159
160 if ((user == null) || (customUserAttributeNames.length == 0)) {
161 return;
162 }
163
164 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
165 user.getCompanyId());
166
167 long[] allCategoryIds = assetEntryQuery.getAllCategoryIds();
168
169 PrimitiveLongList allCategoryIdsList = new PrimitiveLongList(
170 allCategoryIds.length + customUserAttributeNames.length);
171
172 allCategoryIdsList.addAll(allCategoryIds);
173
174 for (String customUserAttributeName : customUserAttributeNames) {
175 ExpandoBridge userCustomAttributes = user.getExpandoBridge();
176
177 Serializable userCustomFieldValue =
178 userCustomAttributes.getAttribute(customUserAttributeName);
179
180 if (userCustomFieldValue == null) {
181 continue;
182 }
183
184 String userCustomFieldValueString = userCustomFieldValue.toString();
185
186 List<AssetCategory> assetCategories =
187 AssetCategoryLocalServiceUtil.search(
188 companyGroup.getGroupId(), userCustomFieldValueString,
189 new String[0], QueryUtil.ALL_POS, QueryUtil.ALL_POS);
190
191 for (AssetCategory assetCategory : assetCategories) {
192 allCategoryIdsList.add(assetCategory.getCategoryId());
193 }
194 }
195
196 assetEntryQuery.setAllCategoryIds(allCategoryIdsList.getArray());
197 }
198
199 public static AssetEntryQuery getAssetEntryQuery(
200 PortletPreferences portletPreferences, long[] scopeGroupIds)
201 throws Exception {
202
203 AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
204
205 long[] allAssetCategoryIds = new long[0];
206 long[] anyAssetCategoryIds = new long[0];
207 long[] notAllAssetCategoryIds = new long[0];
208 long[] notAnyAssetCategoryIds = new long[0];
209
210 String[] allAssetTagNames = new String[0];
211 String[] anyAssetTagNames = new String[0];
212 String[] notAllAssetTagNames = new String[0];
213 String[] notAnyAssetTagNames = new String[0];
214
215 for (int i = 0; true; i++) {
216 String[] queryValues = portletPreferences.getValues(
217 "queryValues" + i, null);
218
219 if ((queryValues == null) || (queryValues.length == 0)) {
220 break;
221 }
222
223 boolean queryContains = GetterUtil.getBoolean(
224 portletPreferences.getValue(
225 "queryContains" + i, StringPool.BLANK));
226 boolean queryAndOperator = GetterUtil.getBoolean(
227 portletPreferences.getValue(
228 "queryAndOperator" + i, StringPool.BLANK));
229 String queryName = portletPreferences.getValue(
230 "queryName" + i, StringPool.BLANK);
231
232 if (Validator.equals(queryName, "assetCategories")) {
233 long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);
234
235 if (queryContains &&
236 (queryAndOperator || (assetCategoryIds.length == 1))) {
237
238 allAssetCategoryIds = assetCategoryIds;
239 }
240 else if (queryContains && !queryAndOperator) {
241 anyAssetCategoryIds = assetCategoryIds;
242 }
243 else if (!queryContains && queryAndOperator) {
244 notAllAssetCategoryIds = assetCategoryIds;
245 }
246 else {
247 notAnyAssetCategoryIds = assetCategoryIds;
248 }
249 }
250 else {
251 if (queryContains && queryAndOperator) {
252 allAssetTagNames = queryValues;
253 }
254 else if (queryContains && !queryAndOperator) {
255 anyAssetTagNames = queryValues;
256 }
257 else if (!queryContains && queryAndOperator) {
258 notAllAssetTagNames = queryValues;
259 }
260 else {
261 notAnyAssetTagNames = queryValues;
262 }
263 }
264 }
265
266 long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
267 scopeGroupIds, allAssetTagNames);
268 long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
269 scopeGroupIds, anyAssetTagNames);
270 long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
271 scopeGroupIds, notAllAssetTagNames);
272 long[] notAnyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
273 scopeGroupIds, notAnyAssetTagNames);
274
275 assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);
276 assetEntryQuery.setAllTagIds(allAssetTagIds);
277 assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);
278 assetEntryQuery.setAnyTagIds(anyAssetTagIds);
279 assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);
280 assetEntryQuery.setNotAllTagIds(notAllAssetTagIds);
281 assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);
282 assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);
283
284 return assetEntryQuery;
285 }
286
287 public static String[] getAssetTagNames(
288 PortletPreferences portletPreferences, long scopeGroupId)
289 throws Exception {
290
291 String[] allAssetTagNames = new String[0];
292
293 for (int i = 0; true; i++) {
294 String[] queryValues = portletPreferences.getValues(
295 "queryValues" + i, null);
296
297 if ((queryValues == null) || (queryValues.length == 0)) {
298 break;
299 }
300
301 boolean queryContains = GetterUtil.getBoolean(
302 portletPreferences.getValue(
303 "queryContains" + i, StringPool.BLANK));
304 boolean queryAndOperator = GetterUtil.getBoolean(
305 portletPreferences.getValue(
306 "queryAndOperator" + i, StringPool.BLANK));
307 String queryName = portletPreferences.getValue(
308 "queryName" + i, StringPool.BLANK);
309
310 if (!Validator.equals(queryName, "assetCategories") &&
311 queryContains &&
312 (queryAndOperator || (queryValues.length == 1))) {
313
314 allAssetTagNames = queryValues;
315 }
316 }
317
318 return allAssetTagNames;
319 }
320
321 public static String getClassName(
322 AssetRendererFactory assetRendererFactory) {
323
324 Class<?> clazz = assetRendererFactory.getClass();
325
326 String className = clazz.getName();
327
328 int pos = className.lastIndexOf(StringPool.PERIOD);
329
330 return className.substring(pos + 1);
331 }
332
333 public static long[] getClassNameIds(
334 PortletPreferences portletPreferences, long[] availableClassNameIds) {
335
336 boolean anyAssetType = GetterUtil.getBoolean(
337 portletPreferences.getValue(
338 "anyAssetType", Boolean.TRUE.toString()));
339
340 if (anyAssetType) {
341 return availableClassNameIds;
342 }
343
344 long defaultClassNameId = GetterUtil.getLong(
345 portletPreferences.getValue("anyAssetType", null));
346
347 if (defaultClassNameId > 0) {
348 return new long[] {defaultClassNameId};
349 }
350
351 long[] classNameIds = GetterUtil.getLongValues(
352 portletPreferences.getValues("classNameIds", null));
353
354 if (classNameIds != null) {
355 return classNameIds;
356 }
357 else {
358 return availableClassNameIds;
359 }
360 }
361
362 public static Long[] getClassTypeIds(
363 PortletPreferences portletPreferences, String className,
364 Long[] availableClassTypeIds) {
365
366 boolean anyAssetType = GetterUtil.getBoolean(
367 portletPreferences.getValue(
368 "anyClassType" + className, Boolean.TRUE.toString()));
369
370 if (anyAssetType) {
371 return availableClassTypeIds;
372 }
373
374 long defaultClassTypeId = GetterUtil.getLong(
375 portletPreferences.getValue("anyClassType" + className, null));
376
377 if (defaultClassTypeId > 0) {
378 return new Long[] {defaultClassTypeId};
379 }
380
381 Long[] classTypeIds = ArrayUtil.toArray(
382 StringUtil.split(
383 portletPreferences.getValue(
384 "classTypeIds" + className, null), 0L));
385
386 if (classTypeIds != null) {
387 return classTypeIds;
388 }
389 else {
390 return availableClassTypeIds;
391 }
392 }
393
394 public static long[] getGroupIds(
395 PortletPreferences portletPreferences, long scopeGroupId,
396 Layout layout) {
397
398 boolean defaultScope = GetterUtil.getBoolean(
399 portletPreferences.getValue("defaultScope", null), true);
400
401 if (defaultScope) {
402 return new long[] {scopeGroupId};
403 }
404
405 long defaultScopeId = GetterUtil.getLong(
406 portletPreferences.getValue("defaultScope", null));
407
408 if (defaultScopeId > 0) {
409 return new long[] {defaultScopeId};
410 }
411
412 String[] scopeIds = portletPreferences.getValues(
413 "scopeIds",
414 new String[] {"group" + StringPool.UNDERLINE + scopeGroupId});
415
416 long[] groupIds = new long[scopeIds.length];
417
418 for (int i = 0; i < scopeIds.length; i++) {
419 try {
420 String[] scopeIdFragments = StringUtil.split(
421 scopeIds[i], CharPool.UNDERLINE);
422
423 if (scopeIdFragments[0].equals("Layout")) {
424 long scopeIdLayoutId = GetterUtil.getLong(
425 scopeIdFragments[1]);
426
427 Layout scopeIdLayout = LayoutLocalServiceUtil.getLayout(
428 scopeGroupId, layout.isPrivateLayout(),
429 scopeIdLayoutId);
430
431 Group scopeIdGroup = scopeIdLayout.getScopeGroup();
432
433 groupIds[i] = scopeIdGroup.getGroupId();
434 }
435 else {
436 if (scopeIdFragments[1].equals(GroupConstants.DEFAULT)) {
437 groupIds[i] = scopeGroupId;
438 }
439 else {
440 long scopeIdGroupId = GetterUtil.getLong(
441 scopeIdFragments[1]);
442
443 groupIds[i] = scopeIdGroupId;
444 }
445 }
446 }
447 catch (Exception e) {
448 continue;
449 }
450 }
451
452 return groupIds;
453 }
454
455 public static long getRecentFolderId(
456 PortletRequest portletRequest, String className) {
457
458 Long classPK = _getRecentFolderIds(portletRequest).get(className);
459
460 if (classPK == null) {
461 return 0;
462 }
463 else {
464 return classPK.longValue();
465 }
466 }
467
468 public static void removeAndStoreSelection(
469 List<String> assetEntryUuids, PortletPreferences portletPreferences)
470 throws Exception {
471
472 if (assetEntryUuids.size() == 0) {
473 return;
474 }
475
476 String[] assetEntryXmls = portletPreferences.getValues(
477 "assetEntryXml", new String[0]);
478
479 List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls);
480
481 Iterator<String> itr = assetEntryXmlsList.iterator();
482
483 while (itr.hasNext()) {
484 String assetEntryXml = itr.next();
485
486 Document document = SAXReaderUtil.read(assetEntryXml);
487
488 Element rootElement = document.getRootElement();
489
490 String assetEntryUuid = rootElement.elementText("asset-entry-uuid");
491
492 if (assetEntryUuids.contains(assetEntryUuid)) {
493 itr.remove();
494 }
495 }
496
497 portletPreferences.setValues(
498 "assetEntryXml",
499 assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()]));
500
501 portletPreferences.store();
502 }
503
504 public static void removeRecentFolderId(
505 PortletRequest portletRequest, String className, long classPK) {
506
507 if (getRecentFolderId(portletRequest, className) == classPK) {
508 _getRecentFolderIds(portletRequest).remove(className);
509 }
510 }
511
512 private static String _getAssetEntryXml(
513 String assetEntryType, String assetEntryUuid) {
514
515 String xml = null;
516
517 try {
518 Document document = SAXReaderUtil.createDocument(StringPool.UTF8);
519
520 Element assetEntryElement = document.addElement("asset-entry");
521
522 Element assetEntryTypeElement = assetEntryElement.addElement(
523 "asset-entry-type");
524
525 assetEntryTypeElement.addText(assetEntryType);
526
527 Element assetEntryUuidElement = assetEntryElement.addElement(
528 "asset-entry-uuid");
529
530 assetEntryUuidElement.addText(assetEntryUuid);
531
532 xml = document.formattedString(StringPool.BLANK);
533 }
534 catch (IOException ioe) {
535 if (_log.isWarnEnabled()) {
536 _log.warn(ioe);
537 }
538 }
539
540 return xml;
541 }
542
543 private static Map<String, Long> _getRecentFolderIds(
544 PortletRequest portletRequest) {
545
546 HttpServletRequest request = PortalUtil.getHttpServletRequest(
547 portletRequest);
548 HttpSession session = request.getSession();
549
550 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
551 WebKeys.THEME_DISPLAY);
552
553 String key =
554 AssetPublisherUtil.class + StringPool.UNDERLINE +
555 themeDisplay.getScopeGroupId();
556
557 Map<String, Long> recentFolderIds =
558 (Map<String, Long>)session.getAttribute(key);
559
560 if (recentFolderIds == null) {
561 recentFolderIds = new HashMap<String, Long>();
562 }
563
564 session.setAttribute(key, recentFolderIds);
565
566 return recentFolderIds;
567 }
568
569 private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
570
571 }