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