001    /**
002     * Copyright (c) 2000-2013 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.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.QueryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.servlet.SessionMessages;
027    import com.liferay.portal.kernel.util.Accessor;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.InstanceFactory;
031    import com.liferay.portal.kernel.util.ListUtil;
032    import com.liferay.portal.kernel.util.LocaleUtil;
033    import com.liferay.portal.kernel.util.LocalizationUtil;
034    import com.liferay.portal.kernel.util.ParamUtil;
035    import com.liferay.portal.kernel.util.PrefsPropsUtil;
036    import com.liferay.portal.kernel.util.PrimitiveLongList;
037    import com.liferay.portal.kernel.util.PropsKeys;
038    import com.liferay.portal.kernel.util.PropsUtil;
039    import com.liferay.portal.kernel.util.StringPool;
040    import com.liferay.portal.kernel.util.StringUtil;
041    import com.liferay.portal.kernel.util.Validator;
042    import com.liferay.portal.kernel.xml.Document;
043    import com.liferay.portal.kernel.xml.Element;
044    import com.liferay.portal.kernel.xml.SAXReaderUtil;
045    import com.liferay.portal.model.Group;
046    import com.liferay.portal.model.GroupConstants;
047    import com.liferay.portal.model.Layout;
048    import com.liferay.portal.model.PortletConstants;
049    import com.liferay.portal.model.User;
050    import com.liferay.portal.security.auth.PrincipalException;
051    import com.liferay.portal.security.auth.PrincipalThreadLocal;
052    import com.liferay.portal.security.permission.ActionKeys;
053    import com.liferay.portal.security.permission.PermissionChecker;
054    import com.liferay.portal.service.GroupLocalServiceUtil;
055    import com.liferay.portal.service.LayoutLocalServiceUtil;
056    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
057    import com.liferay.portal.service.SubscriptionLocalServiceUtil;
058    import com.liferay.portal.service.permission.GroupPermissionUtil;
059    import com.liferay.portal.service.permission.PortletPermissionUtil;
060    import com.liferay.portal.service.persistence.PortletPreferencesActionableDynamicQuery;
061    import com.liferay.portal.theme.ThemeDisplay;
062    import com.liferay.portal.util.PortalUtil;
063    import com.liferay.portal.util.PortletKeys;
064    import com.liferay.portal.util.PropsValues;
065    import com.liferay.portal.util.SubscriptionSender;
066    import com.liferay.portal.util.WebKeys;
067    import com.liferay.portlet.PortletPreferencesFactoryUtil;
068    import com.liferay.portlet.StrictPortletPreferencesImpl;
069    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
070    import com.liferay.portlet.asset.model.AssetCategory;
071    import com.liferay.portlet.asset.model.AssetEntry;
072    import com.liferay.portlet.asset.model.AssetRenderer;
073    import com.liferay.portlet.asset.model.AssetRendererFactory;
074    import com.liferay.portlet.asset.model.AssetTag;
075    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
076    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
077    import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
078    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
079    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
080    import com.liferay.portlet.expando.model.ExpandoBridge;
081    import com.liferay.portlet.sites.util.SitesUtil;
082    import com.liferay.util.ContentUtil;
083    
084    import java.io.IOException;
085    import java.io.Serializable;
086    
087    import java.util.ArrayList;
088    import java.util.HashMap;
089    import java.util.Iterator;
090    import java.util.List;
091    import java.util.Locale;
092    import java.util.Map;
093    import java.util.concurrent.ConcurrentHashMap;
094    
095    import javax.portlet.PortletException;
096    import javax.portlet.PortletPreferences;
097    import javax.portlet.PortletRequest;
098    
099    import javax.servlet.http.HttpServletRequest;
100    import javax.servlet.http.HttpSession;
101    
102    /**
103     * @author Raymond Aug??
104     * @author Julio Camarero
105     */
106    public class AssetPublisherImpl implements AssetPublisher {
107    
108            public AssetPublisherImpl() {
109                    for (String assetEntryQueryProcessorClassName :
110                                    PropsValues.ASSET_PUBLISHER_ASSET_ENTRY_QUERY_PROCESSORS) {
111    
112                            try {
113                                    AssetEntryQueryProcessor assetEntryQueryProcessor =
114                                            (AssetEntryQueryProcessor)InstanceFactory.newInstance(
115                                                    assetEntryQueryProcessorClassName);
116    
117                                    registerAssetQueryProcessor(
118                                            assetEntryQueryProcessorClassName,
119                                            assetEntryQueryProcessor);
120                            }
121                            catch (Exception e) {
122                                    _log.error(e, e);
123                            }
124                    }
125            }
126    
127            @Override
128            public void addAndStoreSelection(
129                            PortletRequest portletRequest, String className, long classPK,
130                            int assetEntryOrder)
131                    throws Exception {
132    
133                    String referringPortletResource = ParamUtil.getString(
134                            portletRequest, "referringPortletResource");
135    
136                    if (Validator.isNull(referringPortletResource)) {
137                            return;
138                    }
139    
140                    String rootPortletId = PortletConstants.getRootPortletId(
141                            referringPortletResource);
142    
143                    if (!rootPortletId.equals(PortletKeys.ASSET_PUBLISHER)) {
144                            return;
145                    }
146    
147                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
148                            WebKeys.THEME_DISPLAY);
149    
150                    Layout layout = LayoutLocalServiceUtil.getLayout(
151                            themeDisplay.getRefererPlid());
152    
153                    PortletPreferences portletPreferences =
154                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
155                                    layout, referringPortletResource);
156    
157                    if (portletPreferences instanceof StrictPortletPreferencesImpl) {
158                            return;
159                    }
160    
161                    String selectionStyle = portletPreferences.getValue(
162                            "selectionStyle", "dynamic");
163    
164                    if (selectionStyle.equals("dynamic")) {
165                            return;
166                    }
167    
168                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
169                            className, classPK);
170    
171                    addSelection(
172                            themeDisplay, portletPreferences, referringPortletResource,
173                            assetEntry.getEntryId(), assetEntryOrder, className);
174    
175                    portletPreferences.store();
176            }
177    
178            @Override
179            public void addRecentFolderId(
180                    PortletRequest portletRequest, String className, long classPK) {
181    
182                    _getRecentFolderIds(portletRequest).put(className, classPK);
183            }
184    
185            @Override
186            public void addSelection(
187                            PortletRequest portletRequest,
188                            PortletPreferences portletPreferences, String portletId)
189                    throws Exception {
190    
191                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
192                            WebKeys.THEME_DISPLAY);
193    
194                    long assetEntryId = ParamUtil.getLong(portletRequest, "assetEntryId");
195                    int assetEntryOrder = ParamUtil.getInteger(
196                            portletRequest, "assetEntryOrder");
197                    String assetEntryType = ParamUtil.getString(
198                            portletRequest, "assetEntryType");
199    
200                    addSelection(
201                            themeDisplay, portletPreferences, portletId, assetEntryId,
202                            assetEntryOrder, assetEntryType);
203            }
204    
205            @Override
206            public void addSelection(
207                            ThemeDisplay themeDisplay, PortletPreferences portletPreferences,
208                            String portletId, long assetEntryId, int assetEntryOrder,
209                            String assetEntryType)
210                    throws Exception {
211    
212                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
213                            assetEntryId);
214    
215                    String[] assetEntryXmls = portletPreferences.getValues(
216                            "assetEntryXml", new String[0]);
217    
218                    String assetEntryXml = _getAssetEntryXml(
219                            assetEntryType, assetEntry.getClassUuid());
220    
221                    if (!ArrayUtil.contains(assetEntryXmls, assetEntryXml)) {
222                            if (assetEntryOrder > -1) {
223                                    assetEntryXmls[assetEntryOrder] = assetEntryXml;
224                            }
225                            else {
226                                    assetEntryXmls = ArrayUtil.append(
227                                            assetEntryXmls, assetEntryXml);
228                            }
229    
230                            portletPreferences.setValues("assetEntryXml", assetEntryXmls);
231                    }
232    
233                    long plid = themeDisplay.getRefererPlid();
234    
235                    if (plid == 0) {
236                            plid = themeDisplay.getPlid();
237                    }
238    
239                    List<AssetEntry> assetEntries = new ArrayList<AssetEntry>();
240    
241                    assetEntries.add(assetEntry);
242    
243                    notifySubscribers(portletPreferences, plid, portletId, assetEntries);
244            }
245    
246            @Override
247            public void addUserAttributes(
248                            User user, String[] customUserAttributeNames,
249                            AssetEntryQuery assetEntryQuery)
250                    throws Exception {
251    
252                    if ((user == null) || (customUserAttributeNames.length == 0)) {
253                            return;
254                    }
255    
256                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
257                            user.getCompanyId());
258    
259                    long[] allCategoryIds = assetEntryQuery.getAllCategoryIds();
260    
261                    PrimitiveLongList allCategoryIdsList = new PrimitiveLongList(
262                            allCategoryIds.length + customUserAttributeNames.length);
263    
264                    allCategoryIdsList.addAll(allCategoryIds);
265    
266                    for (String customUserAttributeName : customUserAttributeNames) {
267                            ExpandoBridge userCustomAttributes = user.getExpandoBridge();
268    
269                            Serializable userCustomFieldValue = null;
270    
271                            try {
272                                    userCustomFieldValue = userCustomAttributes.getAttribute(
273                                            customUserAttributeName);
274                            }
275                            catch (Exception e) {
276                            }
277    
278                            if (userCustomFieldValue == null) {
279                                    continue;
280                            }
281    
282                            String userCustomFieldValueString = userCustomFieldValue.toString();
283    
284                            List<AssetCategory> assetCategories =
285                                    AssetCategoryLocalServiceUtil.search(
286                                            companyGroup.getGroupId(), userCustomFieldValueString,
287                                            new String[0], QueryUtil.ALL_POS, QueryUtil.ALL_POS);
288    
289                            for (AssetCategory assetCategory : assetCategories) {
290                                    allCategoryIdsList.add(assetCategory.getCategoryId());
291                            }
292                    }
293    
294                    assetEntryQuery.setAllCategoryIds(allCategoryIdsList.getArray());
295            }
296    
297            @Override
298            public void checkAssetEntries() throws Exception {
299                    ActionableDynamicQuery actionableDynamicQuery =
300                            new PortletPreferencesActionableDynamicQuery() {
301    
302                            @Override
303                            protected void addCriteria(DynamicQuery dynamicQuery) {
304                                    Property property = PropertyFactoryUtil.forName("portletId");
305    
306                                    String portletId =
307                                            PortletKeys.ASSET_PUBLISHER +
308                                                    PortletConstants.INSTANCE_SEPARATOR +
309                                                            StringPool.PERCENT;
310    
311                                    dynamicQuery.add(property.like(portletId));
312                            }
313    
314                            @Override
315                            protected void performAction(Object object)
316                                    throws PortalException, SystemException {
317    
318                                    _checkAssetEntries(
319                                            (com.liferay.portal.model.PortletPreferences)object);
320                            }
321    
322                    };
323    
324                    actionableDynamicQuery.performActions();
325            }
326    
327            @Override
328            public long[] getAssetCategoryIds(PortletPreferences portletPreferences)
329                    throws Exception {
330    
331                    long[] assetCategoryIds = new long[0];
332    
333                    for (int i = 0; true; i++) {
334                            String[] queryValues = portletPreferences.getValues(
335                                    "queryValues" + i, null);
336    
337                            if (ArrayUtil.isEmpty(queryValues)) {
338                                    break;
339                            }
340    
341                            boolean queryContains = GetterUtil.getBoolean(
342                                    portletPreferences.getValue(
343                                            "queryContains" + i, StringPool.BLANK));
344                            boolean queryAndOperator = GetterUtil.getBoolean(
345                                    portletPreferences.getValue(
346                                            "queryAndOperator" + i, StringPool.BLANK));
347                            String queryName = portletPreferences.getValue(
348                                    "queryName" + i, StringPool.BLANK);
349    
350                            if (Validator.equals(queryName, "assetCategories") &&
351                                    queryContains && queryAndOperator) {
352    
353                                    assetCategoryIds = GetterUtil.getLongValues(queryValues);
354                            }
355                    }
356    
357                    return assetCategoryIds;
358            }
359    
360            @Override
361            public List<AssetEntry> getAssetEntries(
362                            PortletPreferences portletPreferences, Layout layout,
363                            long scopeGroupId, int max, boolean checkPermission)
364                    throws PortalException, SystemException {
365    
366                    long[] groupIds = getGroupIds(portletPreferences, scopeGroupId, layout);
367    
368                    AssetEntryQuery assetEntryQuery = getAssetEntryQuery(
369                            portletPreferences, groupIds);
370    
371                    assetEntryQuery.setGroupIds(groupIds);
372    
373                    boolean anyAssetType = GetterUtil.getBoolean(
374                            portletPreferences.getValue("anyAssetType", null), true);
375    
376                    if (!anyAssetType) {
377                            long[] availableClassNameIds =
378                                    AssetRendererFactoryRegistryUtil.getClassNameIds(
379                                            layout.getCompanyId());
380    
381                            long[] classNameIds = getClassNameIds(
382                                    portletPreferences, availableClassNameIds);
383    
384                            assetEntryQuery.setClassNameIds(classNameIds);
385                    }
386    
387                    long[] classTypeIds = GetterUtil.getLongValues(
388                            portletPreferences.getValues("classTypeIds", null));
389    
390                    assetEntryQuery.setClassTypeIds(classTypeIds);
391    
392                    boolean enablePermissions = GetterUtil.getBoolean(
393                            portletPreferences.getValue("enablePermissions", null));
394    
395                    assetEntryQuery.setEnablePermissions(enablePermissions);
396    
397                    assetEntryQuery.setEnd(max);
398    
399                    boolean excludeZeroViewCount = GetterUtil.getBoolean(
400                            portletPreferences.getValue("excludeZeroViewCount", null));
401    
402                    assetEntryQuery.setExcludeZeroViewCount(excludeZeroViewCount);
403    
404                    boolean showOnlyLayoutAssets = GetterUtil.getBoolean(
405                            portletPreferences.getValue("showOnlyLayoutAssets", null));
406    
407                    if (showOnlyLayoutAssets) {
408                            assetEntryQuery.setLayout(layout);
409                    }
410    
411                    String orderByColumn1 = GetterUtil.getString(
412                            portletPreferences.getValue("orderByColumn1", "modifiedDate"));
413    
414                    assetEntryQuery.setOrderByCol1(orderByColumn1);
415    
416                    String orderByColumn2 = GetterUtil.getString(
417                            portletPreferences.getValue("orderByColumn2", "title"));
418    
419                    assetEntryQuery.setOrderByCol2(orderByColumn2);
420    
421                    String orderByType1 = GetterUtil.getString(
422                            portletPreferences.getValue("orderByType1", "DESC"));
423    
424                    assetEntryQuery.setOrderByType1(orderByType1);
425    
426                    String orderByType2 = GetterUtil.getString(
427                            portletPreferences.getValue("orderByType2", "ASC"));
428    
429                    assetEntryQuery.setOrderByType2(orderByType2);
430    
431                    assetEntryQuery.setStart(0);
432    
433                    if (checkPermission) {
434                            return AssetEntryServiceUtil.getEntries(assetEntryQuery);
435                    }
436                    else {
437                            return AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);
438                    }
439            }
440    
441            @Override
442            public List<AssetEntry> getAssetEntries(
443                            PortletRequest portletRequest,
444                            PortletPreferences portletPreferences,
445                            PermissionChecker permissionChecker, long[] groupIds,
446                            long[] allCategoryIds, String[] assetEntryXmls,
447                            String[] allTagNames, boolean deleteMissingAssetEntries,
448                            boolean checkPermission)
449                    throws Exception {
450    
451                    List<AssetEntry> assetEntries = getAssetEntries(
452                            portletRequest, portletPreferences, permissionChecker, groupIds,
453                            assetEntryXmls, deleteMissingAssetEntries, checkPermission);
454    
455                    if (assetEntries.isEmpty() ||
456                            (ArrayUtil.isEmpty(allCategoryIds) &&
457                             ArrayUtil.isEmpty(allTagNames))) {
458    
459                            return assetEntries;
460                    }
461    
462                    if (!ArrayUtil.isEmpty(allCategoryIds)) {
463                            assetEntries = _filterAssetCategoriesAssetEntries(
464                                    assetEntries, allCategoryIds);
465                    }
466    
467                    if (!ArrayUtil.isEmpty(allTagNames)) {
468                            assetEntries = _filterAssetTagNamesAssetEntries(
469                                    assetEntries, allTagNames);
470                    }
471    
472                    return assetEntries;
473            }
474    
475            @Override
476            public List<AssetEntry> getAssetEntries(
477                            PortletRequest portletRequest,
478                            PortletPreferences portletPreferences,
479                            PermissionChecker permissionChecker, long[] groupIds,
480                            String[] assetEntryXmls, boolean deleteMissingAssetEntries,
481                            boolean checkPermission)
482                    throws Exception {
483    
484                    List<AssetEntry> assetEntries = new ArrayList<AssetEntry>();
485    
486                    List<String> missingAssetEntryUuids = new ArrayList<String>();
487    
488                    for (String assetEntryXml : assetEntryXmls) {
489                            Document document = SAXReaderUtil.read(assetEntryXml);
490    
491                            Element rootElement = document.getRootElement();
492    
493                            String assetEntryUuid = rootElement.elementText("asset-entry-uuid");
494    
495                            AssetEntry assetEntry = null;
496    
497                            for (long groupId : groupIds) {
498                                    assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
499                                            groupId, assetEntryUuid);
500    
501                                    if (assetEntry != null) {
502                                            break;
503                                    }
504                            }
505    
506                            if (assetEntry == null) {
507                                    if (deleteMissingAssetEntries) {
508                                            missingAssetEntryUuids.add(assetEntryUuid);
509                                    }
510    
511                                    continue;
512                            }
513    
514                            if (!assetEntry.isVisible()) {
515                                    continue;
516                            }
517    
518                            AssetRendererFactory assetRendererFactory =
519                                    AssetRendererFactoryRegistryUtil.
520                                            getAssetRendererFactoryByClassName(
521                                                    assetEntry.getClassName());
522    
523                            AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
524                                    assetEntry.getClassPK());
525    
526                            if (!assetRendererFactory.isActive(
527                                            permissionChecker.getCompanyId())) {
528    
529                                    if (deleteMissingAssetEntries) {
530                                            missingAssetEntryUuids.add(assetEntryUuid);
531                                    }
532    
533                                    continue;
534                            }
535    
536                            if (checkPermission &&
537                                    (!assetRenderer.isDisplayable() ||
538                                     !assetRenderer.hasViewPermission(permissionChecker))) {
539    
540                                    continue;
541                            }
542    
543                            assetEntries.add(assetEntry);
544                    }
545    
546                    if (deleteMissingAssetEntries) {
547                            AssetPublisherUtil.removeAndStoreSelection(
548                                    missingAssetEntryUuids, portletPreferences);
549    
550                            if (!missingAssetEntryUuids.isEmpty()) {
551                                    SessionMessages.add(
552                                            portletRequest, "deletedMissingAssetEntries",
553                                            missingAssetEntryUuids);
554                            }
555                    }
556    
557                    return assetEntries;
558            }
559    
560            @Override
561            public AssetEntryQuery getAssetEntryQuery(
562                            PortletPreferences portletPreferences, long[] scopeGroupIds)
563                    throws PortalException, SystemException {
564    
565                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
566    
567                    long[] allAssetCategoryIds = new long[0];
568                    long[] anyAssetCategoryIds = new long[0];
569                    long[] notAllAssetCategoryIds = new long[0];
570                    long[] notAnyAssetCategoryIds = new long[0];
571    
572                    String[] allAssetTagNames = new String[0];
573                    String[] anyAssetTagNames = new String[0];
574                    String[] notAllAssetTagNames = new String[0];
575                    String[] notAnyAssetTagNames = new String[0];
576    
577                    for (int i = 0; true; i++) {
578                            String[] queryValues = portletPreferences.getValues(
579                                    "queryValues" + i, null);
580    
581                            if (ArrayUtil.isEmpty(queryValues)) {
582                                    break;
583                            }
584    
585                            boolean queryContains = GetterUtil.getBoolean(
586                                    portletPreferences.getValue(
587                                            "queryContains" + i, StringPool.BLANK));
588                            boolean queryAndOperator = GetterUtil.getBoolean(
589                                    portletPreferences.getValue(
590                                            "queryAndOperator" + i, StringPool.BLANK));
591                            String queryName = portletPreferences.getValue(
592                                    "queryName" + i, StringPool.BLANK);
593    
594                            if (Validator.equals(queryName, "assetCategories")) {
595                                    long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);
596    
597                                    if (queryContains && queryAndOperator) {
598                                            allAssetCategoryIds = assetCategoryIds;
599                                    }
600                                    else if (queryContains && !queryAndOperator) {
601                                            anyAssetCategoryIds = assetCategoryIds;
602                                    }
603                                    else if (!queryContains && queryAndOperator) {
604                                            notAllAssetCategoryIds = assetCategoryIds;
605                                    }
606                                    else {
607                                            notAnyAssetCategoryIds = assetCategoryIds;
608                                    }
609                            }
610                            else {
611                                    if (queryContains && queryAndOperator) {
612                                            allAssetTagNames = queryValues;
613                                    }
614                                    else if (queryContains && !queryAndOperator) {
615                                            anyAssetTagNames = queryValues;
616                                    }
617                                    else if (!queryContains && queryAndOperator) {
618                                            notAllAssetTagNames = queryValues;
619                                    }
620                                    else {
621                                            notAnyAssetTagNames = queryValues;
622                                    }
623                            }
624                    }
625    
626                    allAssetCategoryIds = _filterAssetCategoryIds(allAssetCategoryIds);
627    
628                    assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);
629    
630                    for (String assetTagName : allAssetTagNames) {
631                            long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
632                                    scopeGroupIds, assetTagName);
633    
634                            assetEntryQuery.addAllTagIdsArray(allAssetTagIds);
635                    }
636    
637                    assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);
638    
639                    long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
640                            scopeGroupIds, anyAssetTagNames);
641    
642                    assetEntryQuery.setAnyTagIds(anyAssetTagIds);
643    
644                    assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);
645    
646                    for (String assetTagName : notAllAssetTagNames) {
647                            long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
648                                    scopeGroupIds, assetTagName);
649    
650                            assetEntryQuery.addNotAllTagIdsArray(notAllAssetTagIds);
651                    }
652    
653                    assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);
654    
655                    long[] notAnyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
656                            scopeGroupIds, notAnyAssetTagNames);
657    
658                    assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);
659    
660                    return assetEntryQuery;
661            }
662    
663            @Override
664            public String[] getAssetTagNames(
665                            PortletPreferences portletPreferences, long scopeGroupId)
666                    throws Exception {
667    
668                    String[] allAssetTagNames = new String[0];
669    
670                    for (int i = 0; true; i++) {
671                            String[] queryValues = portletPreferences.getValues(
672                                    "queryValues" + i, null);
673    
674                            if (ArrayUtil.isEmpty(queryValues)) {
675                                    break;
676                            }
677    
678                            boolean queryContains = GetterUtil.getBoolean(
679                                    portletPreferences.getValue(
680                                            "queryContains" + i, StringPool.BLANK));
681                            boolean queryAndOperator = GetterUtil.getBoolean(
682                                    portletPreferences.getValue(
683                                            "queryAndOperator" + i, StringPool.BLANK));
684                            String queryName = portletPreferences.getValue(
685                                    "queryName" + i, StringPool.BLANK);
686    
687                            if (!Validator.equals(queryName, "assetCategories") &&
688                                    queryContains && queryAndOperator) {
689    
690                                    allAssetTagNames = queryValues;
691                            }
692                    }
693    
694                    return allAssetTagNames;
695            }
696    
697            @Override
698            public String getClassName(AssetRendererFactory assetRendererFactory) {
699                    Class<?> clazz = assetRendererFactory.getClass();
700    
701                    String className = clazz.getName();
702    
703                    int pos = className.lastIndexOf(StringPool.PERIOD);
704    
705                    return className.substring(pos + 1);
706            }
707    
708            @Override
709            public long[] getClassNameIds(
710                    PortletPreferences portletPreferences, long[] availableClassNameIds) {
711    
712                    boolean anyAssetType = GetterUtil.getBoolean(
713                            portletPreferences.getValue(
714                                    "anyAssetType", Boolean.TRUE.toString()));
715                    String selectionStyle = portletPreferences.getValue(
716                            "selectionStyle", "dynamic");
717    
718                    if (anyAssetType || selectionStyle.equals("manual")) {
719                            return availableClassNameIds;
720                    }
721    
722                    long defaultClassNameId = GetterUtil.getLong(
723                            portletPreferences.getValue("anyAssetType", null));
724    
725                    if (defaultClassNameId > 0) {
726                            return new long[] {defaultClassNameId};
727                    }
728    
729                    long[] classNameIds = GetterUtil.getLongValues(
730                            portletPreferences.getValues("classNameIds", null));
731    
732                    if (ArrayUtil.isNotEmpty(classNameIds)) {
733                            return classNameIds;
734                    }
735                    else {
736                            return availableClassNameIds;
737                    }
738            }
739    
740            @Override
741            public Long[] getClassTypeIds(
742                    PortletPreferences portletPreferences, String className,
743                    Long[] availableClassTypeIds) {
744    
745                    boolean anyAssetType = GetterUtil.getBoolean(
746                            portletPreferences.getValue(
747                                    "anyClassType" + className, Boolean.TRUE.toString()));
748    
749                    if (anyAssetType) {
750                            return availableClassTypeIds;
751                    }
752    
753                    long defaultClassTypeId = GetterUtil.getLong(
754                            portletPreferences.getValue("anyClassType" + className, null));
755    
756                    if (defaultClassTypeId > 0) {
757                            return new Long[] {defaultClassTypeId};
758                    }
759    
760                    Long[] classTypeIds = ArrayUtil.toArray(
761                            StringUtil.split(
762                                    portletPreferences.getValue(
763                                            "classTypeIds" + className, null), 0L));
764    
765                    if (classTypeIds != null) {
766                            return classTypeIds;
767                    }
768                    else {
769                            return availableClassTypeIds;
770                    }
771            }
772    
773            @Override
774            public Map<Locale, String> getEmailAssetEntryAddedBodyMap(
775                    PortletPreferences portletPreferences) {
776    
777                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
778                            portletPreferences, "emailAssetEntryAddedBody");
779    
780                    Locale defaultLocale = LocaleUtil.getSiteDefault();
781    
782                    String defaultValue = map.get(defaultLocale);
783    
784                    if (Validator.isNotNull(defaultValue)) {
785                            return map;
786                    }
787    
788                    map.put(
789                            defaultLocale,
790                            ContentUtil.get(
791                                    PropsUtil.get(
792                                            PropsKeys.ASSET_PUBLISHER_EMAIL_ASSET_ENTRY_ADDED_BODY)));
793    
794                    return map;
795            }
796    
797            @Override
798            public boolean getEmailAssetEntryAddedEnabled(
799                    PortletPreferences portletPreferences) {
800    
801                    String emailAssetEntryAddedEnabled = portletPreferences.getValue(
802                            "emailAssetEntryAddedEnabled", StringPool.BLANK);
803    
804                    if (Validator.isNotNull(emailAssetEntryAddedEnabled)) {
805                            return GetterUtil.getBoolean(emailAssetEntryAddedEnabled);
806                    }
807                    else {
808                            return PropsValues.ASSET_PUBLISHER_EMAIL_ASSET_ENTRY_ADDED_ENABLED;
809                    }
810            }
811    
812            @Override
813            public Map<Locale, String> getEmailAssetEntryAddedSubjectMap(
814                    PortletPreferences portletPreferences) {
815    
816                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
817                            portletPreferences, "emailAssetEntryAddedSubject");
818    
819                    Locale defaultLocale = LocaleUtil.getSiteDefault();
820    
821                    String defaultValue = map.get(defaultLocale);
822    
823                    if (Validator.isNotNull(defaultValue)) {
824                            return map;
825                    }
826    
827                    map.put(
828                            defaultLocale,
829                            ContentUtil.get(
830                                    PropsUtil.get(
831                                            PropsKeys.ASSET_PUBLISHER_EMAIL_ASSET_ENTRY_ADDED_SUBJECT))
832                    );
833    
834                    return map;
835            }
836    
837            @Override
838            public String getEmailFromAddress(
839                            PortletPreferences portletPreferences, long companyId)
840                    throws SystemException {
841    
842                    return PortalUtil.getEmailFromAddress(
843                            portletPreferences, companyId,
844                            PropsValues.ASSET_PUBLISHER_EMAIL_FROM_ADDRESS);
845            }
846    
847            @Override
848            public String getEmailFromName(
849                            PortletPreferences portletPreferences, long companyId)
850                    throws SystemException {
851    
852                    return PortalUtil.getEmailFromName(
853                            portletPreferences, companyId,
854                            PropsValues.ASSET_PUBLISHER_EMAIL_FROM_NAME);
855            }
856    
857            @Override
858            public long getGroupIdFromScopeId(
859                            String scopeId, long siteGroupId, boolean privateLayout)
860                    throws PortalException, SystemException {
861    
862                    if (scopeId.startsWith(SCOPE_ID_CHILD_GROUP_PREFIX)) {
863                            String scopeIdSuffix = scopeId.substring(
864                                    SCOPE_ID_CHILD_GROUP_PREFIX.length());
865    
866                            long childGroupId = GetterUtil.getLong(scopeIdSuffix);
867    
868                            Group childGroup = GroupLocalServiceUtil.getGroup(childGroupId);
869    
870                            if (!childGroup.hasAncestor(siteGroupId)) {
871                                    throw new PrincipalException();
872                            }
873    
874                            return childGroupId;
875                    }
876                    else if (scopeId.startsWith(SCOPE_ID_GROUP_PREFIX)) {
877                            String scopeIdSuffix = scopeId.substring(
878                                    SCOPE_ID_GROUP_PREFIX.length());
879    
880                            if (scopeIdSuffix.equals(GroupConstants.DEFAULT)) {
881                                    return siteGroupId;
882                            }
883    
884                            long scopeGroupId = GetterUtil.getLong(scopeIdSuffix);
885    
886                            Group scopeGroup = GroupLocalServiceUtil.fetchGroup(scopeGroupId);
887    
888                            if (scopeGroup == null) {
889                                    throw new PrincipalException();
890                            }
891    
892                            return scopeGroupId;
893                    }
894                    else if (scopeId.startsWith(SCOPE_ID_LAYOUT_UUID_PREFIX)) {
895                            String layoutUuid = scopeId.substring(
896                                    SCOPE_ID_LAYOUT_UUID_PREFIX.length());
897    
898                            Layout scopeIdLayout =
899                                    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
900                                            layoutUuid, siteGroupId, privateLayout);
901    
902                            Group scopeIdGroup = null;
903    
904                            if (scopeIdLayout.hasScopeGroup()) {
905                                    scopeIdGroup = scopeIdLayout.getScopeGroup();
906                            }
907                            else {
908                                    scopeIdGroup = GroupLocalServiceUtil.addGroup(
909                                            PrincipalThreadLocal.getUserId(),
910                                            GroupConstants.DEFAULT_PARENT_GROUP_ID,
911                                            Layout.class.getName(), scopeIdLayout.getPlid(),
912                                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
913                                            String.valueOf(scopeIdLayout.getPlid()), null, 0, true,
914                                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null, false,
915                                            true, null);
916                            }
917    
918                            return scopeIdGroup.getGroupId();
919                    }
920                    else if (scopeId.startsWith(SCOPE_ID_LAYOUT_PREFIX)) {
921    
922                            // Legacy portlet preferences
923    
924                            String scopeIdSuffix = scopeId.substring(
925                                    SCOPE_ID_LAYOUT_PREFIX.length());
926    
927                            long scopeIdLayoutId = GetterUtil.getLong(scopeIdSuffix);
928    
929                            Layout scopeIdLayout = LayoutLocalServiceUtil.getLayout(
930                                    siteGroupId, privateLayout, scopeIdLayoutId);
931    
932                            Group scopeIdGroup = scopeIdLayout.getScopeGroup();
933    
934                            return scopeIdGroup.getGroupId();
935                    }
936                    else if (scopeId.startsWith(SCOPE_ID_PARENT_GROUP_PREFIX)) {
937                            String scopeIdSuffix = scopeId.substring(
938                                    SCOPE_ID_PARENT_GROUP_PREFIX.length());
939    
940                            long parentGroupId = GetterUtil.getLong(scopeIdSuffix);
941    
942                            Group parentGroup = GroupLocalServiceUtil.getGroup(parentGroupId);
943    
944                            if (!SitesUtil.isContentSharingWithChildrenEnabled(parentGroup)) {
945                                    throw new PrincipalException();
946                            }
947    
948                            Group group = GroupLocalServiceUtil.getGroup(siteGroupId);
949    
950                            if (!group.hasAncestor(parentGroupId)) {
951                                    throw new PrincipalException();
952                            }
953    
954                            return parentGroupId;
955                    }
956                    else {
957                            throw new IllegalArgumentException("Invalid scope ID " + scopeId);
958                    }
959            }
960    
961            @Override
962            public long[] getGroupIds(
963                    PortletPreferences portletPreferences, long scopeGroupId,
964                    Layout layout) {
965    
966                    String[] scopeIds = portletPreferences.getValues(
967                            "scopeIds", new String[] {SCOPE_ID_GROUP_PREFIX + scopeGroupId});
968    
969                    List<Long> groupIds = new ArrayList<Long>();
970    
971                    for (String scopeId : scopeIds) {
972                            try {
973                                    long groupId = getGroupIdFromScopeId(
974                                            scopeId, scopeGroupId, layout.isPrivateLayout());
975    
976                                    groupIds.add(groupId);
977                            }
978                            catch (Exception e) {
979                                    continue;
980                            }
981                    }
982    
983                    return ArrayUtil.toLongArray(groupIds);
984            }
985    
986            @Override
987            public long getRecentFolderId(
988                    PortletRequest portletRequest, String className) {
989    
990                    Long classPK = _getRecentFolderIds(portletRequest).get(className);
991    
992                    if (classPK == null) {
993                            return 0;
994                    }
995                    else {
996                            return classPK.longValue();
997                    }
998            }
999    
1000            @Override
1001            public String getScopeId(Group group, long scopeGroupId)
1002                    throws PortalException, SystemException {
1003    
1004                    String key = null;
1005    
1006                    if (group.isLayout()) {
1007                            Layout layout = LayoutLocalServiceUtil.getLayout(
1008                                    group.getClassPK());
1009    
1010                            key = SCOPE_ID_LAYOUT_UUID_PREFIX + layout.getUuid();
1011                    }
1012                    else if (group.isLayoutPrototype() ||
1013                                     (group.getGroupId() == scopeGroupId)) {
1014    
1015                            key = SCOPE_ID_GROUP_PREFIX + GroupConstants.DEFAULT;
1016                    }
1017                    else {
1018                            Group scopeGroup = GroupLocalServiceUtil.getGroup(scopeGroupId);
1019    
1020                            if (scopeGroup.hasAncestor(group.getGroupId()) &&
1021                                    SitesUtil.isContentSharingWithChildrenEnabled(group)) {
1022    
1023                                    key = SCOPE_ID_PARENT_GROUP_PREFIX + group.getGroupId();
1024                            }
1025                            else if (group.hasAncestor(scopeGroup.getGroupId())) {
1026                                    key = SCOPE_ID_CHILD_GROUP_PREFIX + group.getGroupId();
1027                            }
1028                            else {
1029                                    key = SCOPE_ID_GROUP_PREFIX + group.getGroupId();
1030                            }
1031                    }
1032    
1033                    return key;
1034            }
1035    
1036            @Override
1037            public long getSubscriptionClassPK(long plid, String portletId)
1038                    throws PortalException, SystemException {
1039    
1040                    com.liferay.portal.model.PortletPreferences portletPreferencesModel =
1041                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1042                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
1043                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, plid, portletId);
1044    
1045                    return portletPreferencesModel.getPortletPreferencesId();
1046            }
1047    
1048            @Override
1049            public boolean isScopeIdSelectable(
1050                            PermissionChecker permissionChecker, String scopeId,
1051                            long companyGroupId, Layout layout)
1052                    throws PortalException, SystemException {
1053    
1054                    long groupId = getGroupIdFromScopeId(
1055                            scopeId, layout.getGroupId(), layout.isPrivateLayout());
1056    
1057                    if (scopeId.startsWith(SCOPE_ID_CHILD_GROUP_PREFIX)) {
1058                            Group group = GroupLocalServiceUtil.getGroup(groupId);
1059    
1060                            if (!group.hasAncestor(layout.getGroupId())) {
1061                                    return false;
1062                            }
1063                    }
1064                    else if (scopeId.startsWith(SCOPE_ID_PARENT_GROUP_PREFIX)) {
1065                            Group siteGroup = layout.getGroup();
1066    
1067                            if (!siteGroup.hasAncestor(groupId)) {
1068                                    return false;
1069                            }
1070    
1071                            Group group = GroupLocalServiceUtil.getGroup(groupId);
1072    
1073                            if (SitesUtil.isContentSharingWithChildrenEnabled(group)) {
1074                                    return true;
1075                            }
1076    
1077                            if (!PrefsPropsUtil.getBoolean(
1078                                            layout.getCompanyId(),
1079                                            PropsKeys.
1080                                            SITES_CONTENT_SHARING_THROUGH_ADMINISTRATORS_ENABLED)) {
1081    
1082                                    return false;
1083                            }
1084    
1085                            return GroupPermissionUtil.contains(
1086                                    permissionChecker, groupId, ActionKeys.UPDATE);
1087                    }
1088                    else if (groupId != companyGroupId) {
1089                            return GroupPermissionUtil.contains(
1090                                    permissionChecker, groupId, ActionKeys.UPDATE);
1091                    }
1092    
1093                    return true;
1094            }
1095    
1096            @Override
1097            public boolean isSubscribed(
1098                            long companyId, long userId, long plid, String portletId)
1099                    throws PortalException, SystemException {
1100    
1101                    return SubscriptionLocalServiceUtil.isSubscribed(
1102                            companyId, userId,
1103                            com.liferay.portal.model.PortletPreferences.class.getName(),
1104                            getSubscriptionClassPK(plid, portletId));
1105            }
1106    
1107            @Override
1108            public void notifySubscribers(
1109                            PortletPreferences portletPreferences, long plid, String portletId,
1110                            List<AssetEntry> assetEntries)
1111                    throws PortalException, SystemException {
1112    
1113                    if (!getEmailAssetEntryAddedEnabled(portletPreferences) ||
1114                            assetEntries.isEmpty()) {
1115    
1116                            return;
1117                    }
1118    
1119                    AssetEntry assetEntry = assetEntries.get(0);
1120    
1121                    String fromName = getEmailFromName(
1122                            portletPreferences, assetEntry.getCompanyId());
1123                    String fromAddress = getEmailFromAddress(
1124                            portletPreferences, assetEntry.getCompanyId());
1125    
1126                    Map<Locale, String> localizedSubjectMap =
1127                            getEmailAssetEntryAddedSubjectMap(portletPreferences);
1128                    Map<Locale, String> localizedBodyMap = getEmailAssetEntryAddedBodyMap(
1129                            portletPreferences);
1130    
1131                    SubscriptionSender subscriptionSender = new SubscriptionSender();
1132    
1133                    subscriptionSender.setCompanyId(assetEntry.getCompanyId());
1134                    subscriptionSender.setContextAttributes(
1135                            "[$ASSET_ENTRIES$]",
1136                            ListUtil.toString(
1137                                    assetEntries, _titleAccessor, StringPool.COMMA_AND_SPACE));
1138                    subscriptionSender.setContextUserPrefix("ASSET_PUBLISHER");
1139                    subscriptionSender.setFrom(fromAddress, fromName);
1140                    subscriptionSender.setHtmlFormat(true);
1141                    subscriptionSender.setLocalizedBodyMap(localizedBodyMap);
1142                    subscriptionSender.setLocalizedSubjectMap(localizedSubjectMap);
1143                    subscriptionSender.setMailId("asset_entry", assetEntry.getEntryId());
1144                    subscriptionSender.setPortletId(PortletKeys.ASSET_PUBLISHER);
1145                    subscriptionSender.setReplyToAddress(fromAddress);
1146    
1147                    subscriptionSender.addPersistedSubscribers(
1148                            com.liferay.portal.model.PortletPreferences.class.getName(),
1149                            getSubscriptionClassPK(plid, portletId));
1150    
1151                    subscriptionSender.flushNotificationsAsync();
1152            }
1153    
1154            @Override
1155            public void processAssetEntryQuery(
1156                            User user, PortletPreferences portletPreferences,
1157                            AssetEntryQuery assetEntryQuery)
1158                    throws Exception {
1159    
1160                    for (AssetEntryQueryProcessor assetEntryQueryProcessor :
1161                                    _assetEntryQueryProcessor.values()) {
1162    
1163                            assetEntryQueryProcessor.processAssetEntryQuery(
1164                                    user, portletPreferences, assetEntryQuery);
1165                    }
1166            }
1167    
1168            @Override
1169            public void registerAssetQueryProcessor(
1170                    String assetQueryProcessorClassName,
1171                    AssetEntryQueryProcessor assetQueryProcessor) {
1172    
1173                    if (assetQueryProcessor == null) {
1174                            return;
1175                    }
1176    
1177                    _assetEntryQueryProcessor.put(
1178                            assetQueryProcessorClassName, assetQueryProcessor);
1179            }
1180    
1181            @Override
1182            public void removeAndStoreSelection(
1183                            List<String> assetEntryUuids, PortletPreferences portletPreferences)
1184                    throws Exception {
1185    
1186                    if (assetEntryUuids.size() == 0) {
1187                            return;
1188                    }
1189    
1190                    String[] assetEntryXmls = portletPreferences.getValues(
1191                            "assetEntryXml", new String[0]);
1192    
1193                    List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls);
1194    
1195                    Iterator<String> itr = assetEntryXmlsList.iterator();
1196    
1197                    while (itr.hasNext()) {
1198                            String assetEntryXml = itr.next();
1199    
1200                            Document document = SAXReaderUtil.read(assetEntryXml);
1201    
1202                            Element rootElement = document.getRootElement();
1203    
1204                            String assetEntryUuid = rootElement.elementText("asset-entry-uuid");
1205    
1206                            if (assetEntryUuids.contains(assetEntryUuid)) {
1207                                    itr.remove();
1208                            }
1209                    }
1210    
1211                    portletPreferences.setValues(
1212                            "assetEntryXml",
1213                            assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()]));
1214    
1215                    portletPreferences.store();
1216            }
1217    
1218            @Override
1219            public void removeRecentFolderId(
1220                    PortletRequest portletRequest, String className, long classPK) {
1221    
1222                    if (getRecentFolderId(portletRequest, className) == classPK) {
1223                            _getRecentFolderIds(portletRequest).remove(className);
1224                    }
1225            }
1226    
1227            @Override
1228            public void subscribe(
1229                            PermissionChecker permissionChecker, long groupId, long plid,
1230                            String portletId)
1231                    throws PortalException, SystemException {
1232    
1233                    PortletPermissionUtil.check(
1234                            permissionChecker, plid, portletId, ActionKeys.SUBSCRIBE);
1235    
1236                    SubscriptionLocalServiceUtil.addSubscription(
1237                            permissionChecker.getUserId(), groupId,
1238                            com.liferay.portal.model.PortletPreferences.class.getName(),
1239                            getSubscriptionClassPK(plid, portletId));
1240            }
1241    
1242            @Override
1243            public void unregisterAssetQueryProcessor(
1244                    String assetQueryProcessorClassName) {
1245    
1246                    _assetEntryQueryProcessor.remove(assetQueryProcessorClassName);
1247            }
1248    
1249            @Override
1250            public void unsubscribe(
1251                            PermissionChecker permissionChecker, long plid, String portletId)
1252                    throws PortalException, SystemException {
1253    
1254                    PortletPermissionUtil.check(
1255                            permissionChecker, plid, portletId, ActionKeys.SUBSCRIBE);
1256    
1257                    SubscriptionLocalServiceUtil.deleteSubscription(
1258                            permissionChecker.getUserId(),
1259                            com.liferay.portal.model.PortletPreferences.class.getName(),
1260                            getSubscriptionClassPK(plid, portletId));
1261            }
1262    
1263            private void _checkAssetEntries(
1264                            com.liferay.portal.model.PortletPreferences
1265                            portletPreferencesModel)
1266                    throws PortalException, SystemException {
1267    
1268                    Layout layout = LayoutLocalServiceUtil.fetchLayout(
1269                            portletPreferencesModel.getPlid());
1270    
1271                    if (layout == null) {
1272                            return;
1273                    }
1274    
1275                    PortletPreferences portletPreferences =
1276                            PortletPreferencesFactoryUtil.fromXML(
1277                                    layout.getCompanyId(), portletPreferencesModel.getOwnerId(),
1278                                    portletPreferencesModel.getOwnerType(),
1279                                    portletPreferencesModel.getPlid(),
1280                                    portletPreferencesModel.getPortletId(),
1281                                    portletPreferencesModel.getPreferences());
1282    
1283                    if (!getEmailAssetEntryAddedEnabled(portletPreferences)) {
1284                            return;
1285                    }
1286    
1287                    List<AssetEntry> assetEntries = getAssetEntries(
1288                            portletPreferences, layout, layout.getGroupId(),
1289                            PropsValues.ASSET_PUBLISHER_DYNAMIC_SUBSCRIPTION_LIMIT, false);
1290    
1291                    if (assetEntries.isEmpty()) {
1292                            return;
1293                    }
1294    
1295                    long[] notifiedAssetEntryIds = GetterUtil.getLongValues(
1296                            portletPreferences.getValues("notifiedAssetEntryIds", null));
1297    
1298                    List<AssetEntry> newAssetEntries = new ArrayList<AssetEntry>();
1299    
1300                    for (int i = 0; i < assetEntries.size(); i++) {
1301                            AssetEntry assetEntry = assetEntries.get(i);
1302    
1303                            if (!ArrayUtil.contains(
1304                                            notifiedAssetEntryIds, assetEntry.getEntryId())) {
1305    
1306                                    newAssetEntries.add(assetEntry);
1307                            }
1308                    }
1309    
1310                    notifySubscribers(
1311                            portletPreferences, portletPreferencesModel.getPlid(),
1312                            portletPreferencesModel.getPortletId(), newAssetEntries);
1313    
1314                    try {
1315                            portletPreferences.setValues(
1316                                    "notifiedAssetEntryIds",
1317                                    StringUtil.split(
1318                                            ListUtil.toString(
1319                                                    assetEntries, AssetEntry.ENTRY_ID_ACCESSOR)));
1320    
1321                            portletPreferences.store();
1322                    }
1323                    catch (IOException ioe) {
1324                            throw new SystemException(ioe);
1325                    }
1326                    catch (PortletException pe) {
1327                            throw new SystemException(pe);
1328                    }
1329            }
1330    
1331            private List<AssetEntry> _filterAssetCategoriesAssetEntries(
1332                            List<AssetEntry> assetEntries, long[] assetCategoryIds)
1333                    throws Exception {
1334    
1335                    List<AssetEntry> filteredAssetEntries = new ArrayList<AssetEntry>();
1336    
1337                    for (AssetEntry assetEntry : assetEntries) {
1338                            if (ArrayUtil.containsAll(
1339                                            assetEntry.getCategoryIds(), assetCategoryIds)) {
1340    
1341                                    filteredAssetEntries.add(assetEntry);
1342                            }
1343                    }
1344    
1345                    return filteredAssetEntries;
1346            }
1347    
1348            private static long[] _filterAssetCategoryIds(long[] assetCategoryIds)
1349                    throws SystemException {
1350    
1351                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
1352    
1353                    for (long assetCategoryId : assetCategoryIds) {
1354                            AssetCategory category =
1355                                    AssetCategoryLocalServiceUtil.fetchAssetCategory(
1356                                            assetCategoryId);
1357    
1358                            if (category == null) {
1359                                    continue;
1360                            }
1361    
1362                            assetCategoryIdsList.add(assetCategoryId);
1363                    }
1364    
1365                    return ArrayUtil.toArray(
1366                            assetCategoryIdsList.toArray(
1367                                    new Long[assetCategoryIdsList.size()]));
1368            }
1369    
1370            private List<AssetEntry> _filterAssetTagNamesAssetEntries(
1371                            List<AssetEntry> assetEntries, String[] assetTagNames)
1372                    throws Exception {
1373    
1374                    List<AssetEntry> filteredAssetEntries = new ArrayList<AssetEntry>();
1375    
1376                    for (AssetEntry assetEntry : assetEntries) {
1377                            List<AssetTag> assetTags = assetEntry.getTags();
1378    
1379                            String[] assetEntryAssetTagNames = new String[assetTags.size()];
1380    
1381                            for (int i = 0; i < assetTags.size(); i++) {
1382                                    AssetTag assetTag = assetTags.get(i);
1383    
1384                                    assetEntryAssetTagNames[i] = assetTag.getName();
1385                            }
1386    
1387                            if (ArrayUtil.containsAll(assetEntryAssetTagNames, assetTagNames)) {
1388                                    filteredAssetEntries.add(assetEntry);
1389                            }
1390                    }
1391    
1392                    return filteredAssetEntries;
1393            }
1394    
1395            private String _getAssetEntryXml(
1396                    String assetEntryType, String assetEntryUuid) {
1397    
1398                    String xml = null;
1399    
1400                    try {
1401                            Document document = SAXReaderUtil.createDocument(StringPool.UTF8);
1402    
1403                            Element assetEntryElement = document.addElement("asset-entry");
1404    
1405                            Element assetEntryTypeElement = assetEntryElement.addElement(
1406                                    "asset-entry-type");
1407    
1408                            assetEntryTypeElement.addText(assetEntryType);
1409    
1410                            Element assetEntryUuidElement = assetEntryElement.addElement(
1411                                    "asset-entry-uuid");
1412    
1413                            assetEntryUuidElement.addText(assetEntryUuid);
1414    
1415                            xml = document.formattedString(StringPool.BLANK);
1416                    }
1417                    catch (IOException ioe) {
1418                            if (_log.isWarnEnabled()) {
1419                                    _log.warn(ioe);
1420                            }
1421                    }
1422    
1423                    return xml;
1424            }
1425    
1426            private Map<String, Long> _getRecentFolderIds(
1427                    PortletRequest portletRequest) {
1428    
1429                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
1430                            portletRequest);
1431                    HttpSession session = request.getSession();
1432    
1433                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
1434                            WebKeys.THEME_DISPLAY);
1435    
1436                    String key =
1437                            AssetPublisherUtil.class + StringPool.UNDERLINE +
1438                                    themeDisplay.getScopeGroupId();
1439    
1440                    Map<String, Long> recentFolderIds =
1441                            (Map<String, Long>)session.getAttribute(key);
1442    
1443                    if (recentFolderIds == null) {
1444                            recentFolderIds = new HashMap<String, Long>();
1445                    }
1446    
1447                    session.setAttribute(key, recentFolderIds);
1448    
1449                    return recentFolderIds;
1450            }
1451    
1452            private static Log _log = LogFactoryUtil.getLog(AssetPublisherImpl.class);
1453    
1454            private Map<String, AssetEntryQueryProcessor> _assetEntryQueryProcessor =
1455                    new ConcurrentHashMap<String, AssetEntryQueryProcessor>();
1456    
1457            private Accessor<AssetEntry, String> _titleAccessor =
1458                    new Accessor<AssetEntry, String>() {
1459    
1460                            @Override
1461                            public String get(AssetEntry assetEntry) {
1462                                    return assetEntry.getTitle(LocaleUtil.getSiteDefault());
1463                            }
1464    
1465                    };
1466    
1467    }