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