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