001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    return getAssetEntries(
485                            portletRequest, portletPreferences, permissionChecker, groupIds,
486                            assetEntryXmls, deleteMissingAssetEntries, checkPermission, false);
487            }
488    
489            @Override
490            public List<AssetEntry> getAssetEntries(
491                            PortletRequest portletRequest,
492                            PortletPreferences portletPreferences,
493                            PermissionChecker permissionChecker, long[] groupIds,
494                            String[] assetEntryXmls, boolean deleteMissingAssetEntries,
495                            boolean checkPermission, boolean includeNonVisibleAssets)
496                    throws Exception {
497    
498                    List<AssetEntry> assetEntries = new ArrayList<AssetEntry>();
499    
500                    List<String> missingAssetEntryUuids = new ArrayList<String>();
501    
502                    for (String assetEntryXml : assetEntryXmls) {
503                            Document document = SAXReaderUtil.read(assetEntryXml);
504    
505                            Element rootElement = document.getRootElement();
506    
507                            String assetEntryUuid = rootElement.elementText("asset-entry-uuid");
508    
509                            AssetEntry assetEntry = null;
510    
511                            for (long groupId : groupIds) {
512                                    assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
513                                            groupId, assetEntryUuid);
514    
515                                    if (assetEntry != null) {
516                                            break;
517                                    }
518                            }
519    
520                            if (assetEntry == null) {
521                                    if (deleteMissingAssetEntries) {
522                                            missingAssetEntryUuids.add(assetEntryUuid);
523                                    }
524    
525                                    continue;
526                            }
527    
528                            if (!assetEntry.isVisible() && !includeNonVisibleAssets) {
529                                    continue;
530                            }
531    
532                            AssetRendererFactory assetRendererFactory =
533                                    AssetRendererFactoryRegistryUtil.
534                                            getAssetRendererFactoryByClassName(
535                                                    assetEntry.getClassName());
536    
537                            AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
538                                    assetEntry.getClassPK(),
539                                    AssetRendererFactory.TYPE_LATEST_APPROVED);
540    
541                            if (!assetRendererFactory.isActive(
542                                            permissionChecker.getCompanyId())) {
543    
544                                    if (deleteMissingAssetEntries) {
545                                            missingAssetEntryUuids.add(assetEntryUuid);
546                                    }
547    
548                                    continue;
549                            }
550    
551                            if (checkPermission) {
552                                    if (!assetRenderer.isDisplayable() &&
553                                            !includeNonVisibleAssets) {
554    
555                                            continue;
556                                    }
557                                    else if (!assetRenderer.hasViewPermission(permissionChecker)) {
558                                            assetRenderer = assetRendererFactory.getAssetRenderer(
559                                                    assetEntry.getClassPK(),
560                                                    AssetRendererFactory.TYPE_LATEST_APPROVED);
561    
562                                            if (!assetRenderer.hasViewPermission(permissionChecker)) {
563                                                    continue;
564                                            }
565                                    }
566                            }
567    
568                            assetEntries.add(assetEntry);
569                    }
570    
571                    if (deleteMissingAssetEntries) {
572                            AssetPublisherUtil.removeAndStoreSelection(
573                                    missingAssetEntryUuids, portletPreferences);
574    
575                            if (!missingAssetEntryUuids.isEmpty()) {
576                                    SessionMessages.add(
577                                            portletRequest, "deletedMissingAssetEntries",
578                                            missingAssetEntryUuids);
579                            }
580                    }
581    
582                    return assetEntries;
583            }
584    
585            @Override
586            public AssetEntryQuery getAssetEntryQuery(
587                            PortletPreferences portletPreferences, long[] scopeGroupIds)
588                    throws PortalException, SystemException {
589    
590                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
591    
592                    long[] allAssetCategoryIds = new long[0];
593                    long[] anyAssetCategoryIds = new long[0];
594                    long[] notAllAssetCategoryIds = new long[0];
595                    long[] notAnyAssetCategoryIds = new long[0];
596    
597                    String[] allAssetTagNames = new String[0];
598                    String[] anyAssetTagNames = new String[0];
599                    String[] notAllAssetTagNames = new String[0];
600                    String[] notAnyAssetTagNames = new String[0];
601    
602                    for (int i = 0; true; i++) {
603                            String[] queryValues = portletPreferences.getValues(
604                                    "queryValues" + i, null);
605    
606                            if (ArrayUtil.isEmpty(queryValues)) {
607                                    break;
608                            }
609    
610                            boolean queryContains = GetterUtil.getBoolean(
611                                    portletPreferences.getValue(
612                                            "queryContains" + i, StringPool.BLANK));
613                            boolean queryAndOperator = GetterUtil.getBoolean(
614                                    portletPreferences.getValue(
615                                            "queryAndOperator" + i, StringPool.BLANK));
616                            String queryName = portletPreferences.getValue(
617                                    "queryName" + i, StringPool.BLANK);
618    
619                            if (Validator.equals(queryName, "assetCategories")) {
620                                    long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);
621    
622                                    if (queryContains && queryAndOperator) {
623                                            allAssetCategoryIds = assetCategoryIds;
624                                    }
625                                    else if (queryContains && !queryAndOperator) {
626                                            anyAssetCategoryIds = assetCategoryIds;
627                                    }
628                                    else if (!queryContains && queryAndOperator) {
629                                            notAllAssetCategoryIds = assetCategoryIds;
630                                    }
631                                    else {
632                                            notAnyAssetCategoryIds = assetCategoryIds;
633                                    }
634                            }
635                            else {
636                                    if (queryContains && queryAndOperator) {
637                                            allAssetTagNames = queryValues;
638                                    }
639                                    else if (queryContains && !queryAndOperator) {
640                                            anyAssetTagNames = queryValues;
641                                    }
642                                    else if (!queryContains && queryAndOperator) {
643                                            notAllAssetTagNames = queryValues;
644                                    }
645                                    else {
646                                            notAnyAssetTagNames = queryValues;
647                                    }
648                            }
649                    }
650    
651                    allAssetCategoryIds = _filterAssetCategoryIds(allAssetCategoryIds);
652    
653                    assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);
654    
655                    for (String assetTagName : allAssetTagNames) {
656                            long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
657                                    scopeGroupIds, assetTagName);
658    
659                            assetEntryQuery.addAllTagIdsArray(allAssetTagIds);
660                    }
661    
662                    assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);
663    
664                    long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
665                            scopeGroupIds, anyAssetTagNames);
666    
667                    assetEntryQuery.setAnyTagIds(anyAssetTagIds);
668    
669                    assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);
670    
671                    for (String assetTagName : notAllAssetTagNames) {
672                            long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
673                                    scopeGroupIds, assetTagName);
674    
675                            assetEntryQuery.addNotAllTagIdsArray(notAllAssetTagIds);
676                    }
677    
678                    assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);
679    
680                    long[] notAnyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
681                            scopeGroupIds, notAnyAssetTagNames);
682    
683                    assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);
684    
685                    return assetEntryQuery;
686            }
687    
688            @Override
689            public String[] getAssetTagNames(
690                            PortletPreferences portletPreferences, long scopeGroupId)
691                    throws Exception {
692    
693                    String[] allAssetTagNames = new String[0];
694    
695                    for (int i = 0; true; i++) {
696                            String[] queryValues = portletPreferences.getValues(
697                                    "queryValues" + i, null);
698    
699                            if (ArrayUtil.isEmpty(queryValues)) {
700                                    break;
701                            }
702    
703                            boolean queryContains = GetterUtil.getBoolean(
704                                    portletPreferences.getValue(
705                                            "queryContains" + i, StringPool.BLANK));
706                            boolean queryAndOperator = GetterUtil.getBoolean(
707                                    portletPreferences.getValue(
708                                            "queryAndOperator" + i, StringPool.BLANK));
709                            String queryName = portletPreferences.getValue(
710                                    "queryName" + i, StringPool.BLANK);
711    
712                            if (!Validator.equals(queryName, "assetCategories") &&
713                                    queryContains && queryAndOperator) {
714    
715                                    allAssetTagNames = queryValues;
716                            }
717                    }
718    
719                    return allAssetTagNames;
720            }
721    
722            @Override
723            public String getClassName(AssetRendererFactory assetRendererFactory) {
724                    Class<?> clazz = assetRendererFactory.getClass();
725    
726                    String className = clazz.getName();
727    
728                    int pos = className.lastIndexOf(StringPool.PERIOD);
729    
730                    return className.substring(pos + 1);
731            }
732    
733            @Override
734            public long[] getClassNameIds(
735                    PortletPreferences portletPreferences, long[] availableClassNameIds) {
736    
737                    boolean anyAssetType = GetterUtil.getBoolean(
738                            portletPreferences.getValue(
739                                    "anyAssetType", Boolean.TRUE.toString()));
740                    String selectionStyle = portletPreferences.getValue(
741                            "selectionStyle", "dynamic");
742    
743                    if (anyAssetType || selectionStyle.equals("manual")) {
744                            return availableClassNameIds;
745                    }
746    
747                    long defaultClassNameId = GetterUtil.getLong(
748                            portletPreferences.getValue("anyAssetType", null));
749    
750                    if (defaultClassNameId > 0) {
751                            return new long[] {defaultClassNameId};
752                    }
753    
754                    long[] classNameIds = GetterUtil.getLongValues(
755                            portletPreferences.getValues("classNameIds", null));
756    
757                    if (ArrayUtil.isNotEmpty(classNameIds)) {
758                            return classNameIds;
759                    }
760                    else {
761                            return availableClassNameIds;
762                    }
763            }
764    
765            @Override
766            public Long[] getClassTypeIds(
767                    PortletPreferences portletPreferences, String className,
768                    Long[] availableClassTypeIds) {
769    
770                    boolean anyAssetType = GetterUtil.getBoolean(
771                            portletPreferences.getValue(
772                                    "anyClassType" + className, Boolean.TRUE.toString()));
773    
774                    if (anyAssetType) {
775                            return availableClassTypeIds;
776                    }
777    
778                    long defaultClassTypeId = GetterUtil.getLong(
779                            portletPreferences.getValue("anyClassType" + className, null));
780    
781                    if (defaultClassTypeId > 0) {
782                            return new Long[] {defaultClassTypeId};
783                    }
784    
785                    Long[] classTypeIds = ArrayUtil.toArray(
786                            StringUtil.split(
787                                    portletPreferences.getValue(
788                                            "classTypeIds" + className, null), 0L));
789    
790                    if (classTypeIds != null) {
791                            return classTypeIds;
792                    }
793                    else {
794                            return availableClassTypeIds;
795                    }
796            }
797    
798            @Override
799            public Map<Locale, String> getEmailAssetEntryAddedBodyMap(
800                    PortletPreferences portletPreferences) {
801    
802                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
803                            portletPreferences, "emailAssetEntryAddedBody");
804    
805                    Locale defaultLocale = LocaleUtil.getSiteDefault();
806    
807                    String defaultValue = map.get(defaultLocale);
808    
809                    if (Validator.isNotNull(defaultValue)) {
810                            return map;
811                    }
812    
813                    map.put(
814                            defaultLocale,
815                            ContentUtil.get(
816                                    PropsUtil.get(
817                                            PropsKeys.ASSET_PUBLISHER_EMAIL_ASSET_ENTRY_ADDED_BODY)));
818    
819                    return map;
820            }
821    
822            @Override
823            public boolean getEmailAssetEntryAddedEnabled(
824                    PortletPreferences portletPreferences) {
825    
826                    String emailAssetEntryAddedEnabled = portletPreferences.getValue(
827                            "emailAssetEntryAddedEnabled", StringPool.BLANK);
828    
829                    if (Validator.isNotNull(emailAssetEntryAddedEnabled)) {
830                            return GetterUtil.getBoolean(emailAssetEntryAddedEnabled);
831                    }
832                    else {
833                            return PropsValues.ASSET_PUBLISHER_EMAIL_ASSET_ENTRY_ADDED_ENABLED;
834                    }
835            }
836    
837            @Override
838            public Map<Locale, String> getEmailAssetEntryAddedSubjectMap(
839                    PortletPreferences portletPreferences) {
840    
841                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
842                            portletPreferences, "emailAssetEntryAddedSubject");
843    
844                    Locale defaultLocale = LocaleUtil.getSiteDefault();
845    
846                    String defaultValue = map.get(defaultLocale);
847    
848                    if (Validator.isNotNull(defaultValue)) {
849                            return map;
850                    }
851    
852                    map.put(
853                            defaultLocale,
854                            ContentUtil.get(
855                                    PropsUtil.get(
856                                            PropsKeys.ASSET_PUBLISHER_EMAIL_ASSET_ENTRY_ADDED_SUBJECT))
857                    );
858    
859                    return map;
860            }
861    
862            @Override
863            public String getEmailFromAddress(
864                            PortletPreferences portletPreferences, long companyId)
865                    throws SystemException {
866    
867                    return PortalUtil.getEmailFromAddress(
868                            portletPreferences, companyId,
869                            PropsValues.ASSET_PUBLISHER_EMAIL_FROM_ADDRESS);
870            }
871    
872            @Override
873            public String getEmailFromName(
874                            PortletPreferences portletPreferences, long companyId)
875                    throws SystemException {
876    
877                    return PortalUtil.getEmailFromName(
878                            portletPreferences, companyId,
879                            PropsValues.ASSET_PUBLISHER_EMAIL_FROM_NAME);
880            }
881    
882            @Override
883            public long getGroupIdFromScopeId(
884                            String scopeId, long siteGroupId, boolean privateLayout)
885                    throws PortalException, SystemException {
886    
887                    if (scopeId.startsWith(SCOPE_ID_CHILD_GROUP_PREFIX)) {
888                            String scopeIdSuffix = scopeId.substring(
889                                    SCOPE_ID_CHILD_GROUP_PREFIX.length());
890    
891                            long childGroupId = GetterUtil.getLong(scopeIdSuffix);
892    
893                            Group childGroup = GroupLocalServiceUtil.getGroup(childGroupId);
894    
895                            if (!childGroup.hasAncestor(siteGroupId)) {
896                                    throw new PrincipalException();
897                            }
898    
899                            return childGroupId;
900                    }
901                    else if (scopeId.startsWith(SCOPE_ID_GROUP_PREFIX)) {
902                            String scopeIdSuffix = scopeId.substring(
903                                    SCOPE_ID_GROUP_PREFIX.length());
904    
905                            if (scopeIdSuffix.equals(GroupConstants.DEFAULT)) {
906                                    return siteGroupId;
907                            }
908    
909                            long scopeGroupId = GetterUtil.getLong(scopeIdSuffix);
910    
911                            Group scopeGroup = GroupLocalServiceUtil.fetchGroup(scopeGroupId);
912    
913                            if (scopeGroup == null) {
914                                    throw new PrincipalException();
915                            }
916    
917                            return scopeGroupId;
918                    }
919                    else if (scopeId.startsWith(SCOPE_ID_LAYOUT_UUID_PREFIX)) {
920                            String layoutUuid = scopeId.substring(
921                                    SCOPE_ID_LAYOUT_UUID_PREFIX.length());
922    
923                            Layout scopeIdLayout =
924                                    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
925                                            layoutUuid, siteGroupId, privateLayout);
926    
927                            Group scopeIdGroup = null;
928    
929                            if (scopeIdLayout.hasScopeGroup()) {
930                                    scopeIdGroup = scopeIdLayout.getScopeGroup();
931                            }
932                            else {
933                                    scopeIdGroup = GroupLocalServiceUtil.addGroup(
934                                            PrincipalThreadLocal.getUserId(),
935                                            GroupConstants.DEFAULT_PARENT_GROUP_ID,
936                                            Layout.class.getName(), scopeIdLayout.getPlid(),
937                                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
938                                            String.valueOf(scopeIdLayout.getPlid()), null, 0, true,
939                                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null, false,
940                                            true, null);
941                            }
942    
943                            return scopeIdGroup.getGroupId();
944                    }
945                    else if (scopeId.startsWith(SCOPE_ID_LAYOUT_PREFIX)) {
946    
947                            // Legacy portlet preferences
948    
949                            String scopeIdSuffix = scopeId.substring(
950                                    SCOPE_ID_LAYOUT_PREFIX.length());
951    
952                            long scopeIdLayoutId = GetterUtil.getLong(scopeIdSuffix);
953    
954                            Layout scopeIdLayout = LayoutLocalServiceUtil.getLayout(
955                                    siteGroupId, privateLayout, scopeIdLayoutId);
956    
957                            Group scopeIdGroup = scopeIdLayout.getScopeGroup();
958    
959                            return scopeIdGroup.getGroupId();
960                    }
961                    else if (scopeId.startsWith(SCOPE_ID_PARENT_GROUP_PREFIX)) {
962                            String scopeIdSuffix = scopeId.substring(
963                                    SCOPE_ID_PARENT_GROUP_PREFIX.length());
964    
965                            long parentGroupId = GetterUtil.getLong(scopeIdSuffix);
966    
967                            Group parentGroup = GroupLocalServiceUtil.getGroup(parentGroupId);
968    
969                            if (!SitesUtil.isContentSharingWithChildrenEnabled(parentGroup)) {
970                                    throw new PrincipalException();
971                            }
972    
973                            Group group = GroupLocalServiceUtil.getGroup(siteGroupId);
974    
975                            if (!group.hasAncestor(parentGroupId)) {
976                                    throw new PrincipalException();
977                            }
978    
979                            return parentGroupId;
980                    }
981                    else {
982                            throw new IllegalArgumentException("Invalid scope ID " + scopeId);
983                    }
984            }
985    
986            @Override
987            public long[] getGroupIds(
988                    PortletPreferences portletPreferences, long scopeGroupId,
989                    Layout layout) {
990    
991                    String[] scopeIds = portletPreferences.getValues(
992                            "scopeIds", new String[] {SCOPE_ID_GROUP_PREFIX + scopeGroupId});
993    
994                    List<Long> groupIds = new ArrayList<Long>();
995    
996                    for (String scopeId : scopeIds) {
997                            try {
998                                    long groupId = getGroupIdFromScopeId(
999                                            scopeId, scopeGroupId, layout.isPrivateLayout());
1000    
1001                                    groupIds.add(groupId);
1002                            }
1003                            catch (Exception e) {
1004                                    continue;
1005                            }
1006                    }
1007    
1008                    return ArrayUtil.toLongArray(groupIds);
1009            }
1010    
1011            @Override
1012            public long getRecentFolderId(
1013                    PortletRequest portletRequest, String className) {
1014    
1015                    Long classPK = _getRecentFolderIds(portletRequest).get(className);
1016    
1017                    if (classPK == null) {
1018                            return 0;
1019                    }
1020                    else {
1021                            return classPK.longValue();
1022                    }
1023            }
1024    
1025            @Override
1026            public String getScopeId(Group group, long scopeGroupId)
1027                    throws PortalException, SystemException {
1028    
1029                    String key = null;
1030    
1031                    if (group.isLayout()) {
1032                            Layout layout = LayoutLocalServiceUtil.getLayout(
1033                                    group.getClassPK());
1034    
1035                            key = SCOPE_ID_LAYOUT_UUID_PREFIX + layout.getUuid();
1036                    }
1037                    else if (group.isLayoutPrototype() ||
1038                                     (group.getGroupId() == scopeGroupId)) {
1039    
1040                            key = SCOPE_ID_GROUP_PREFIX + GroupConstants.DEFAULT;
1041                    }
1042                    else {
1043                            Group scopeGroup = GroupLocalServiceUtil.getGroup(scopeGroupId);
1044    
1045                            if (scopeGroup.hasAncestor(group.getGroupId()) &&
1046                                    SitesUtil.isContentSharingWithChildrenEnabled(group)) {
1047    
1048                                    key = SCOPE_ID_PARENT_GROUP_PREFIX + group.getGroupId();
1049                            }
1050                            else if (group.hasAncestor(scopeGroup.getGroupId())) {
1051                                    key = SCOPE_ID_CHILD_GROUP_PREFIX + group.getGroupId();
1052                            }
1053                            else {
1054                                    key = SCOPE_ID_GROUP_PREFIX + group.getGroupId();
1055                            }
1056                    }
1057    
1058                    return key;
1059            }
1060    
1061            @Override
1062            public long getSubscriptionClassPK(long plid, String portletId)
1063                    throws PortalException, SystemException {
1064    
1065                    com.liferay.portal.model.PortletPreferences portletPreferencesModel =
1066                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1067                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
1068                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, plid, portletId);
1069    
1070                    return portletPreferencesModel.getPortletPreferencesId();
1071            }
1072    
1073            @Override
1074            public long getSubscriptionClassPK(
1075                            long ownerId, int ownerType, long plid, String portletId)
1076                    throws PortalException, SystemException {
1077    
1078                    com.liferay.portal.model.PortletPreferences portletPreferencesModel =
1079                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1080                                    ownerId, ownerType, plid, portletId);
1081    
1082                    return portletPreferencesModel.getPortletPreferencesId();
1083            }
1084    
1085            @Override
1086            public boolean isScopeIdSelectable(
1087                            PermissionChecker permissionChecker, String scopeId,
1088                            long companyGroupId, Layout layout)
1089                    throws PortalException, SystemException {
1090    
1091                    long groupId = getGroupIdFromScopeId(
1092                            scopeId, layout.getGroupId(), layout.isPrivateLayout());
1093    
1094                    if (scopeId.startsWith(SCOPE_ID_CHILD_GROUP_PREFIX)) {
1095                            Group group = GroupLocalServiceUtil.getGroup(groupId);
1096    
1097                            if (!group.hasAncestor(layout.getGroupId())) {
1098                                    return false;
1099                            }
1100                    }
1101                    else if (scopeId.startsWith(SCOPE_ID_PARENT_GROUP_PREFIX)) {
1102                            Group siteGroup = layout.getGroup();
1103    
1104                            if (!siteGroup.hasAncestor(groupId)) {
1105                                    return false;
1106                            }
1107    
1108                            Group group = GroupLocalServiceUtil.getGroup(groupId);
1109    
1110                            if (SitesUtil.isContentSharingWithChildrenEnabled(group)) {
1111                                    return true;
1112                            }
1113    
1114                            if (!PrefsPropsUtil.getBoolean(
1115                                            layout.getCompanyId(),
1116                                            PropsKeys.
1117                                            SITES_CONTENT_SHARING_THROUGH_ADMINISTRATORS_ENABLED)) {
1118    
1119                                    return false;
1120                            }
1121    
1122                            return GroupPermissionUtil.contains(
1123                                    permissionChecker, groupId, ActionKeys.UPDATE);
1124                    }
1125                    else if (groupId != companyGroupId) {
1126                            return GroupPermissionUtil.contains(
1127                                    permissionChecker, groupId, ActionKeys.UPDATE);
1128                    }
1129    
1130                    return true;
1131            }
1132    
1133            @Override
1134            public boolean isSubscribed(
1135                            long companyId, long userId, long plid, String portletId)
1136                    throws PortalException, SystemException {
1137    
1138                    return SubscriptionLocalServiceUtil.isSubscribed(
1139                            companyId, userId,
1140                            com.liferay.portal.model.PortletPreferences.class.getName(),
1141                            getSubscriptionClassPK(plid, portletId));
1142            }
1143    
1144            @Override
1145            public void notifySubscribers(
1146                            PortletPreferences portletPreferences, long plid, String portletId,
1147                            List<AssetEntry> assetEntries)
1148                    throws PortalException, SystemException {
1149    
1150                    if (!getEmailAssetEntryAddedEnabled(portletPreferences) ||
1151                            assetEntries.isEmpty()) {
1152    
1153                            return;
1154                    }
1155    
1156                    AssetEntry assetEntry = assetEntries.get(0);
1157    
1158                    String fromName = getEmailFromName(
1159                            portletPreferences, assetEntry.getCompanyId());
1160                    String fromAddress = getEmailFromAddress(
1161                            portletPreferences, assetEntry.getCompanyId());
1162    
1163                    Map<Locale, String> localizedSubjectMap =
1164                            getEmailAssetEntryAddedSubjectMap(portletPreferences);
1165                    Map<Locale, String> localizedBodyMap = getEmailAssetEntryAddedBodyMap(
1166                            portletPreferences);
1167    
1168                    SubscriptionSender subscriptionSender = new SubscriptionSender();
1169    
1170                    subscriptionSender.setCompanyId(assetEntry.getCompanyId());
1171                    subscriptionSender.setContextAttributes(
1172                            "[$ASSET_ENTRIES$]",
1173                            ListUtil.toString(
1174                                    assetEntries, _titleAccessor, StringPool.COMMA_AND_SPACE));
1175                    subscriptionSender.setContextUserPrefix("ASSET_PUBLISHER");
1176                    subscriptionSender.setFrom(fromAddress, fromName);
1177                    subscriptionSender.setHtmlFormat(true);
1178                    subscriptionSender.setLocalizedBodyMap(localizedBodyMap);
1179                    subscriptionSender.setLocalizedSubjectMap(localizedSubjectMap);
1180                    subscriptionSender.setMailId("asset_entry", assetEntry.getEntryId());
1181                    subscriptionSender.setPortletId(PortletKeys.ASSET_PUBLISHER);
1182                    subscriptionSender.setReplyToAddress(fromAddress);
1183    
1184                    subscriptionSender.addPersistedSubscribers(
1185                            com.liferay.portal.model.PortletPreferences.class.getName(),
1186                            getSubscriptionClassPK(plid, portletId));
1187    
1188                    subscriptionSender.flushNotificationsAsync();
1189            }
1190    
1191            @Override
1192            public void processAssetEntryQuery(
1193                            User user, PortletPreferences portletPreferences,
1194                            AssetEntryQuery assetEntryQuery)
1195                    throws Exception {
1196    
1197                    for (AssetEntryQueryProcessor assetEntryQueryProcessor :
1198                                    _assetEntryQueryProcessor.values()) {
1199    
1200                            assetEntryQueryProcessor.processAssetEntryQuery(
1201                                    user, portletPreferences, assetEntryQuery);
1202                    }
1203            }
1204    
1205            @Override
1206            public void registerAssetQueryProcessor(
1207                    String assetQueryProcessorClassName,
1208                    AssetEntryQueryProcessor assetQueryProcessor) {
1209    
1210                    if (assetQueryProcessor == null) {
1211                            return;
1212                    }
1213    
1214                    _assetEntryQueryProcessor.put(
1215                            assetQueryProcessorClassName, assetQueryProcessor);
1216            }
1217    
1218            @Override
1219            public void removeAndStoreSelection(
1220                            List<String> assetEntryUuids, PortletPreferences portletPreferences)
1221                    throws Exception {
1222    
1223                    if (assetEntryUuids.size() == 0) {
1224                            return;
1225                    }
1226    
1227                    String[] assetEntryXmls = portletPreferences.getValues(
1228                            "assetEntryXml", new String[0]);
1229    
1230                    List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls);
1231    
1232                    Iterator<String> itr = assetEntryXmlsList.iterator();
1233    
1234                    while (itr.hasNext()) {
1235                            String assetEntryXml = itr.next();
1236    
1237                            Document document = SAXReaderUtil.read(assetEntryXml);
1238    
1239                            Element rootElement = document.getRootElement();
1240    
1241                            String assetEntryUuid = rootElement.elementText("asset-entry-uuid");
1242    
1243                            if (assetEntryUuids.contains(assetEntryUuid)) {
1244                                    itr.remove();
1245                            }
1246                    }
1247    
1248                    portletPreferences.setValues(
1249                            "assetEntryXml",
1250                            assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()]));
1251    
1252                    portletPreferences.store();
1253            }
1254    
1255            @Override
1256            public void removeRecentFolderId(
1257                    PortletRequest portletRequest, String className, long classPK) {
1258    
1259                    if (getRecentFolderId(portletRequest, className) == classPK) {
1260                            _getRecentFolderIds(portletRequest).remove(className);
1261                    }
1262            }
1263    
1264            @Override
1265            public void subscribe(
1266                            PermissionChecker permissionChecker, long groupId, long plid,
1267                            String portletId)
1268                    throws PortalException, SystemException {
1269    
1270                    PortletPermissionUtil.check(
1271                            permissionChecker, plid, portletId, ActionKeys.SUBSCRIBE);
1272    
1273                    SubscriptionLocalServiceUtil.addSubscription(
1274                            permissionChecker.getUserId(), groupId,
1275                            com.liferay.portal.model.PortletPreferences.class.getName(),
1276                            getSubscriptionClassPK(plid, portletId));
1277            }
1278    
1279            @Override
1280            public void unregisterAssetQueryProcessor(
1281                    String assetQueryProcessorClassName) {
1282    
1283                    _assetEntryQueryProcessor.remove(assetQueryProcessorClassName);
1284            }
1285    
1286            @Override
1287            public void unsubscribe(
1288                            PermissionChecker permissionChecker, long plid, String portletId)
1289                    throws PortalException, SystemException {
1290    
1291                    PortletPermissionUtil.check(
1292                            permissionChecker, plid, portletId, ActionKeys.SUBSCRIBE);
1293    
1294                    SubscriptionLocalServiceUtil.deleteSubscription(
1295                            permissionChecker.getUserId(),
1296                            com.liferay.portal.model.PortletPreferences.class.getName(),
1297                            getSubscriptionClassPK(plid, portletId));
1298            }
1299    
1300            private void _checkAssetEntries(
1301                            com.liferay.portal.model.PortletPreferences
1302                            portletPreferencesModel)
1303                    throws PortalException, SystemException {
1304    
1305                    Layout layout = LayoutLocalServiceUtil.fetchLayout(
1306                            portletPreferencesModel.getPlid());
1307    
1308                    if (layout == null) {
1309                            return;
1310                    }
1311    
1312                    PortletPreferences portletPreferences =
1313                            PortletPreferencesFactoryUtil.fromXML(
1314                                    layout.getCompanyId(), portletPreferencesModel.getOwnerId(),
1315                                    portletPreferencesModel.getOwnerType(),
1316                                    portletPreferencesModel.getPlid(),
1317                                    portletPreferencesModel.getPortletId(),
1318                                    portletPreferencesModel.getPreferences());
1319    
1320                    if (!getEmailAssetEntryAddedEnabled(portletPreferences)) {
1321                            return;
1322                    }
1323    
1324                    List<AssetEntry> assetEntries = getAssetEntries(
1325                            portletPreferences, layout, layout.getGroupId(),
1326                            PropsValues.ASSET_PUBLISHER_DYNAMIC_SUBSCRIPTION_LIMIT, false);
1327    
1328                    if (assetEntries.isEmpty()) {
1329                            return;
1330                    }
1331    
1332                    long[] notifiedAssetEntryIds = GetterUtil.getLongValues(
1333                            portletPreferences.getValues("notifiedAssetEntryIds", null));
1334    
1335                    List<AssetEntry> newAssetEntries = new ArrayList<AssetEntry>();
1336    
1337                    for (int i = 0; i < assetEntries.size(); i++) {
1338                            AssetEntry assetEntry = assetEntries.get(i);
1339    
1340                            if (!ArrayUtil.contains(
1341                                            notifiedAssetEntryIds, assetEntry.getEntryId())) {
1342    
1343                                    newAssetEntries.add(assetEntry);
1344                            }
1345                    }
1346    
1347                    notifySubscribers(
1348                            portletPreferences, portletPreferencesModel.getPlid(),
1349                            portletPreferencesModel.getPortletId(), newAssetEntries);
1350    
1351                    try {
1352                            portletPreferences.setValues(
1353                                    "notifiedAssetEntryIds",
1354                                    StringUtil.split(
1355                                            ListUtil.toString(
1356                                                    assetEntries, AssetEntry.ENTRY_ID_ACCESSOR)));
1357    
1358                            portletPreferences.store();
1359                    }
1360                    catch (IOException ioe) {
1361                            throw new SystemException(ioe);
1362                    }
1363                    catch (PortletException pe) {
1364                            throw new SystemException(pe);
1365                    }
1366            }
1367    
1368            private List<AssetEntry> _filterAssetCategoriesAssetEntries(
1369                            List<AssetEntry> assetEntries, long[] assetCategoryIds)
1370                    throws Exception {
1371    
1372                    List<AssetEntry> filteredAssetEntries = new ArrayList<AssetEntry>();
1373    
1374                    for (AssetEntry assetEntry : assetEntries) {
1375                            if (ArrayUtil.containsAll(
1376                                            assetEntry.getCategoryIds(), assetCategoryIds)) {
1377    
1378                                    filteredAssetEntries.add(assetEntry);
1379                            }
1380                    }
1381    
1382                    return filteredAssetEntries;
1383            }
1384    
1385            private static long[] _filterAssetCategoryIds(long[] assetCategoryIds)
1386                    throws SystemException {
1387    
1388                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
1389    
1390                    for (long assetCategoryId : assetCategoryIds) {
1391                            AssetCategory category =
1392                                    AssetCategoryLocalServiceUtil.fetchAssetCategory(
1393                                            assetCategoryId);
1394    
1395                            if (category == null) {
1396                                    continue;
1397                            }
1398    
1399                            assetCategoryIdsList.add(assetCategoryId);
1400                    }
1401    
1402                    return ArrayUtil.toArray(
1403                            assetCategoryIdsList.toArray(
1404                                    new Long[assetCategoryIdsList.size()]));
1405            }
1406    
1407            private List<AssetEntry> _filterAssetTagNamesAssetEntries(
1408                            List<AssetEntry> assetEntries, String[] assetTagNames)
1409                    throws Exception {
1410    
1411                    List<AssetEntry> filteredAssetEntries = new ArrayList<AssetEntry>();
1412    
1413                    for (AssetEntry assetEntry : assetEntries) {
1414                            List<AssetTag> assetTags = assetEntry.getTags();
1415    
1416                            String[] assetEntryAssetTagNames = new String[assetTags.size()];
1417    
1418                            for (int i = 0; i < assetTags.size(); i++) {
1419                                    AssetTag assetTag = assetTags.get(i);
1420    
1421                                    assetEntryAssetTagNames[i] = assetTag.getName();
1422                            }
1423    
1424                            if (ArrayUtil.containsAll(assetEntryAssetTagNames, assetTagNames)) {
1425                                    filteredAssetEntries.add(assetEntry);
1426                            }
1427                    }
1428    
1429                    return filteredAssetEntries;
1430            }
1431    
1432            private String _getAssetEntryXml(
1433                    String assetEntryType, String assetEntryUuid) {
1434    
1435                    String xml = null;
1436    
1437                    try {
1438                            Document document = SAXReaderUtil.createDocument(StringPool.UTF8);
1439    
1440                            Element assetEntryElement = document.addElement("asset-entry");
1441    
1442                            Element assetEntryTypeElement = assetEntryElement.addElement(
1443                                    "asset-entry-type");
1444    
1445                            assetEntryTypeElement.addText(assetEntryType);
1446    
1447                            Element assetEntryUuidElement = assetEntryElement.addElement(
1448                                    "asset-entry-uuid");
1449    
1450                            assetEntryUuidElement.addText(assetEntryUuid);
1451    
1452                            xml = document.formattedString(StringPool.BLANK);
1453                    }
1454                    catch (IOException ioe) {
1455                            if (_log.isWarnEnabled()) {
1456                                    _log.warn(ioe);
1457                            }
1458                    }
1459    
1460                    return xml;
1461            }
1462    
1463            private Map<String, Long> _getRecentFolderIds(
1464                    PortletRequest portletRequest) {
1465    
1466                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
1467                            portletRequest);
1468                    HttpSession session = request.getSession();
1469    
1470                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
1471                            WebKeys.THEME_DISPLAY);
1472    
1473                    String key =
1474                            AssetPublisherUtil.class + StringPool.UNDERLINE +
1475                                    themeDisplay.getScopeGroupId();
1476    
1477                    Map<String, Long> recentFolderIds =
1478                            (Map<String, Long>)session.getAttribute(key);
1479    
1480                    if (recentFolderIds == null) {
1481                            recentFolderIds = new HashMap<String, Long>();
1482                    }
1483    
1484                    session.setAttribute(key, recentFolderIds);
1485    
1486                    return recentFolderIds;
1487            }
1488    
1489            private static Log _log = LogFactoryUtil.getLog(AssetPublisherImpl.class);
1490    
1491            private Map<String, AssetEntryQueryProcessor> _assetEntryQueryProcessor =
1492                    new ConcurrentHashMap<String, AssetEntryQueryProcessor>();
1493    
1494            private Accessor<AssetEntry, String> _titleAccessor =
1495                    new Accessor<AssetEntry, String>() {
1496    
1497                            @Override
1498                            public String get(AssetEntry assetEntry) {
1499                                    return assetEntry.getTitle(LocaleUtil.getSiteDefault());
1500                            }
1501    
1502                    };
1503    
1504    }