001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
066     * @author Raymond Augé
067     */
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 (!ArrayUtil.contains(assetEntryXmls, assetEntryXml)) {
146                            if (assetEntryOrder > -1) {
147                                    assetEntryXmls[assetEntryOrder] = assetEntryXml;
148                            }
149                            else {
150                                    assetEntryXmls = ArrayUtil.append(
151                                            assetEntryXmls, assetEntryXml);
152                            }
153    
154                            portletPreferences.setValues("assetEntryXml", assetEntryXmls);
155                    }
156            }
157    
158            public static void addUserAttributes(
159                            User user, String[] customUserAttributeNames,
160                            AssetEntryQuery assetEntryQuery)
161                    throws Exception {
162    
163                    if ((user == null) || (customUserAttributeNames.length == 0)) {
164                            return;
165                    }
166    
167                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
168                            user.getCompanyId());
169    
170                    long[] allCategoryIds = assetEntryQuery.getAllCategoryIds();
171    
172                    PrimitiveLongList allCategoryIdsList = new PrimitiveLongList(
173                            allCategoryIds.length + customUserAttributeNames.length);
174    
175                    allCategoryIdsList.addAll(allCategoryIds);
176    
177                    for (String customUserAttributeName : customUserAttributeNames) {
178                            ExpandoBridge userCustomAttributes = user.getExpandoBridge();
179    
180                            Serializable userCustomFieldValue =
181                                    userCustomAttributes.getAttribute(customUserAttributeName);
182    
183                            if (userCustomFieldValue == null) {
184                                    continue;
185                            }
186    
187                            String userCustomFieldValueString = userCustomFieldValue.toString();
188    
189                            List<AssetCategory> assetCategories =
190                                    AssetCategoryLocalServiceUtil.search(
191                                            companyGroup.getGroupId(), userCustomFieldValueString,
192                                            new String[0], QueryUtil.ALL_POS, QueryUtil.ALL_POS);
193    
194                            for (AssetCategory assetCategory : assetCategories) {
195                                    allCategoryIdsList.add(assetCategory.getCategoryId());
196                            }
197                    }
198    
199                    assetEntryQuery.setAllCategoryIds(allCategoryIdsList.getArray());
200            }
201    
202            public static AssetEntryQuery getAssetEntryQuery(
203                            PortletPreferences portletPreferences, long[] scopeGroupIds)
204                    throws Exception {
205    
206                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
207    
208                    long[] allAssetCategoryIds = new long[0];
209                    long[] anyAssetCategoryIds = new long[0];
210                    long[] notAllAssetCategoryIds = new long[0];
211                    long[] notAnyAssetCategoryIds = new long[0];
212    
213                    String[] allAssetTagNames = new String[0];
214                    String[] anyAssetTagNames = new String[0];
215                    String[] notAllAssetTagNames = new String[0];
216                    String[] notAnyAssetTagNames = new String[0];
217    
218                    for (int i = 0; true; i++) {
219                            String[] queryValues = portletPreferences.getValues(
220                                    "queryValues" + i, null);
221    
222                            if ((queryValues == null) || (queryValues.length == 0)) {
223                                    break;
224                            }
225    
226                            boolean queryContains = GetterUtil.getBoolean(
227                                    portletPreferences.getValue(
228                                            "queryContains" + i, StringPool.BLANK));
229                            boolean queryAndOperator = GetterUtil.getBoolean(
230                                    portletPreferences.getValue(
231                                            "queryAndOperator" + i, StringPool.BLANK));
232                            String queryName = portletPreferences.getValue(
233                                    "queryName" + i, StringPool.BLANK);
234    
235                            if (Validator.equals(queryName, "assetCategories")) {
236                                    long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);
237    
238                                    if (queryContains &&
239                                            (queryAndOperator || (assetCategoryIds.length == 1))) {
240    
241                                            allAssetCategoryIds = assetCategoryIds;
242                                    }
243                                    else if (queryContains && !queryAndOperator) {
244                                            anyAssetCategoryIds = assetCategoryIds;
245                                    }
246                                    else if (!queryContains && queryAndOperator) {
247                                            notAllAssetCategoryIds = assetCategoryIds;
248                                    }
249                                    else {
250                                            notAnyAssetCategoryIds = assetCategoryIds;
251                                    }
252                            }
253                            else {
254                                    if (queryContains && queryAndOperator) {
255                                            allAssetTagNames = queryValues;
256                                    }
257                                    else if (queryContains && !queryAndOperator) {
258                                            anyAssetTagNames = queryValues;
259                                    }
260                                    else if (!queryContains && queryAndOperator) {
261                                            notAllAssetTagNames = queryValues;
262                                    }
263                                    else {
264                                            notAnyAssetTagNames = queryValues;
265                                    }
266                            }
267                    }
268    
269                    assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);
270    
271                    for (String assetTagName : allAssetTagNames) {
272                            long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
273                                    scopeGroupIds, assetTagName);
274    
275                            assetEntryQuery.addAllTagIdsArray(allAssetTagIds);
276                    }
277    
278                    assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);
279    
280                    long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
281                            scopeGroupIds, anyAssetTagNames);
282    
283                    assetEntryQuery.setAnyTagIds(anyAssetTagIds);
284    
285                    assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);
286    
287                    for (String assetTagName : notAllAssetTagNames) {
288                            long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
289                                    scopeGroupIds, assetTagName);
290    
291                            assetEntryQuery.addNotAllTagIdsArray(notAllAssetTagIds);
292                    }
293    
294                    assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);
295    
296                    long[] notAnyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
297                            scopeGroupIds, notAnyAssetTagNames);
298    
299                    assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);
300    
301                    return assetEntryQuery;
302            }
303    
304            public static String[] getAssetTagNames(
305                            PortletPreferences portletPreferences, long scopeGroupId)
306                    throws Exception {
307    
308                    String[] allAssetTagNames = new String[0];
309    
310                    for (int i = 0; true; i++) {
311                            String[] queryValues = portletPreferences.getValues(
312                                    "queryValues" + i, null);
313    
314                            if ((queryValues == null) || (queryValues.length == 0)) {
315                                    break;
316                            }
317    
318                            boolean queryContains = GetterUtil.getBoolean(
319                                    portletPreferences.getValue(
320                                            "queryContains" + i, StringPool.BLANK));
321                            boolean queryAndOperator = GetterUtil.getBoolean(
322                                    portletPreferences.getValue(
323                                            "queryAndOperator" + i, StringPool.BLANK));
324                            String queryName = portletPreferences.getValue(
325                                    "queryName" + i, StringPool.BLANK);
326    
327                            if (!Validator.equals(queryName, "assetCategories") &&
328                                    queryContains &&
329                                    (queryAndOperator || (queryValues.length == 1))) {
330    
331                                    allAssetTagNames = queryValues;
332                            }
333                    }
334    
335                    return allAssetTagNames;
336            }
337    
338            public static String getClassName(
339                    AssetRendererFactory assetRendererFactory) {
340    
341                    Class<?> clazz = assetRendererFactory.getClass();
342    
343                    String className = clazz.getName();
344    
345                    int pos = className.lastIndexOf(StringPool.PERIOD);
346    
347                    return className.substring(pos + 1);
348            }
349    
350            public static long[] getClassNameIds(
351                    PortletPreferences portletPreferences, long[] availableClassNameIds) {
352    
353                    boolean anyAssetType = GetterUtil.getBoolean(
354                            portletPreferences.getValue(
355                                    "anyAssetType", Boolean.TRUE.toString()));
356    
357                    if (anyAssetType) {
358                            return availableClassNameIds;
359                    }
360    
361                    long defaultClassNameId = GetterUtil.getLong(
362                            portletPreferences.getValue("anyAssetType", null));
363    
364                    if (defaultClassNameId > 0) {
365                            return new long[] {defaultClassNameId};
366                    }
367    
368                    long[] classNameIds = GetterUtil.getLongValues(
369                            portletPreferences.getValues("classNameIds", null));
370    
371                    if (classNameIds != null) {
372                            return classNameIds;
373                    }
374                    else {
375                            return availableClassNameIds;
376                    }
377            }
378    
379            public static Long[] getClassTypeIds(
380                    PortletPreferences portletPreferences, String className,
381                    Long[] availableClassTypeIds) {
382    
383                    boolean anyAssetType = GetterUtil.getBoolean(
384                            portletPreferences.getValue(
385                                    "anyClassType" + className, Boolean.TRUE.toString()));
386    
387                    if (anyAssetType) {
388                            return availableClassTypeIds;
389                    }
390    
391                    long defaultClassTypeId = GetterUtil.getLong(
392                            portletPreferences.getValue("anyClassType" + className, null));
393    
394                    if (defaultClassTypeId > 0) {
395                            return new Long[] {defaultClassTypeId};
396                    }
397    
398                    Long[] classTypeIds = ArrayUtil.toArray(
399                            StringUtil.split(
400                                    portletPreferences.getValue(
401                                            "classTypeIds" + className, null), 0L));
402    
403                    if (classTypeIds != null) {
404                            return classTypeIds;
405                    }
406                    else {
407                            return availableClassTypeIds;
408                    }
409            }
410    
411            public static long[] getGroupIds(
412                    PortletPreferences portletPreferences, long scopeGroupId,
413                    Layout layout) {
414    
415                    String defaultScopeId = GetterUtil.getString(
416                            portletPreferences.getValue("defaultScope", null));
417    
418                    if (Validator.isNull(defaultScopeId) ||
419                            defaultScopeId.equals(StringPool.FALSE)) {
420    
421                            String[] scopeIds = portletPreferences.getValues(
422                                    "scopeIds",
423                                    new String[] {"group" + StringPool.UNDERLINE + scopeGroupId});
424    
425                            long[] groupIds = new long[scopeIds.length];
426    
427                            for (int i = 0; i < scopeIds.length; i++) {
428                                    try {
429                                            groupIds[i] = _getGroupId(
430                                                    scopeIds[i], scopeGroupId, layout.isPrivateLayout());
431                                    }
432                                    catch (Exception e) {
433                                            continue;
434                                    }
435                            }
436    
437                            return groupIds;
438                    }
439    
440                    if (defaultScopeId.equals(StringPool.TRUE)) {
441                            return new long[] {scopeGroupId};
442                    }
443    
444                    try {
445                            long groupId = _getGroupId(
446                                    defaultScopeId, scopeGroupId, layout.isPrivateLayout());
447    
448                            return new long[] {groupId};
449                    }
450                    catch (Exception e) {
451                            return new long[0];
452                    }
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 long _getGroupId(
544                            String scopeId, long scopeGroupId, boolean privateLayout)
545                    throws Exception {
546    
547                    String[] scopeIdParts = StringUtil.split(scopeId, CharPool.UNDERLINE);
548    
549                    if (scopeIdParts[0].equals("Layout")) {
550                            long scopeIdLayoutId = GetterUtil.getLong(scopeIdParts[1]);
551    
552                            Layout scopeIdLayout = LayoutLocalServiceUtil.getLayout(
553                                    scopeGroupId, privateLayout, scopeIdLayoutId);
554    
555                            Group scopeIdGroup = scopeIdLayout.getScopeGroup();
556    
557                            return scopeIdGroup.getGroupId();
558                    }
559    
560                    if (scopeIdParts[1].equals(GroupConstants.DEFAULT)) {
561                            return scopeGroupId;
562                    }
563    
564                    return GetterUtil.getLong(scopeIdParts[1]);
565            }
566    
567            private static Map<String, Long> _getRecentFolderIds(
568                    PortletRequest portletRequest) {
569    
570                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
571                            portletRequest);
572                    HttpSession session = request.getSession();
573    
574                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
575                            WebKeys.THEME_DISPLAY);
576    
577                    String key =
578                            AssetPublisherUtil.class + StringPool.UNDERLINE +
579                                    themeDisplay.getScopeGroupId();
580    
581                    Map<String, Long> recentFolderIds =
582                            (Map<String, Long>)session.getAttribute(key);
583    
584                    if (recentFolderIds == null) {
585                            recentFolderIds = new HashMap<String, Long>();
586                    }
587    
588                    session.setAttribute(key, recentFolderIds);
589    
590                    return recentFolderIds;
591            }
592    
593            private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
594    
595    }