001    /**
002     * Copyright (c) 2000-2010 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.xml.Document;
027    import com.liferay.portal.kernel.xml.Element;
028    import com.liferay.portal.kernel.xml.SAXReaderUtil;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.GroupConstants;
031    import com.liferay.portal.model.Layout;
032    import com.liferay.portal.service.LayoutLocalServiceUtil;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.PortletPreferencesFactoryUtil;
037    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
038    import com.liferay.portlet.asset.model.AssetEntry;
039    import com.liferay.portlet.asset.model.AssetRenderer;
040    import com.liferay.portlet.asset.model.AssetRendererFactory;
041    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
042    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
043    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
044    
045    import java.io.IOException;
046    
047    import java.util.HashMap;
048    import java.util.Iterator;
049    import java.util.List;
050    import java.util.Map;
051    
052    import javax.portlet.ActionRequest;
053    import javax.portlet.PortletPreferences;
054    import javax.portlet.PortletRequest;
055    
056    import javax.servlet.http.HttpServletRequest;
057    import javax.servlet.http.HttpSession;
058    
059    /**
060     * @author Raymond Augé
061     */
062    public class AssetPublisherUtil {
063    
064            public static void addAndStoreSelection(
065                            ActionRequest actionRequest, String className, long classPK,
066                            int assetEntryOrder)
067                    throws Exception {
068    
069                    String referringPortletResource =
070                            ParamUtil.getString(actionRequest, "referringPortletResource");
071    
072                    if (Validator.isNull(referringPortletResource)) {
073                            return;
074                    }
075    
076                    PortletPreferences preferences =
077                            PortletPreferencesFactoryUtil.getPortletSetup(
078                                    actionRequest, referringPortletResource);
079    
080                    String selectionStyle = preferences.getValue(
081                            "selection-style", "dynamic");
082    
083                    if (selectionStyle.equals("dynamic")) {
084                            return;
085                    }
086    
087                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
088                            className, classPK);
089    
090                    addSelection(
091                            className, assetEntry.getEntryId(), assetEntryOrder, preferences);
092    
093                    preferences.store();
094            }
095    
096            public static void addRecentFolderId(
097                    PortletRequest portletRequest, String className, long classPK) {
098    
099                    _getRecentFolderIds(portletRequest).put(className, classPK);
100            }
101    
102            public static void addSelection(
103                            ActionRequest actionRequest, PortletPreferences preferences)
104                    throws Exception {
105    
106                    String assetEntryType = ParamUtil.getString(
107                            actionRequest, "assetEntryType");
108                    long assetEntryId = ParamUtil.getLong(actionRequest, "assetEntryId");
109                    int assetEntryOrder = ParamUtil.getInteger(
110                            actionRequest, "assetEntryOrder");
111    
112                    addSelection(
113                            assetEntryType, assetEntryId, assetEntryOrder, preferences);
114            }
115    
116            public static void addSelection(
117                            String assetEntryType, long assetEntryId, int assetEntryOrder,
118                            PortletPreferences preferences)
119                    throws Exception {
120    
121                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
122                            assetEntryId);
123    
124                    AssetRendererFactory assetRendererFactory =
125                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
126                                    assetEntry.getClassName());
127    
128                    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
129                            assetEntry.getClassPK(), AssetRendererFactory.TYPE_LATEST);
130    
131                    String[] assetEntryXmls = preferences.getValues(
132                            "asset-entry-xml", new String[0]);
133    
134                    String assetEntryXml = _getAssetEntryXml(
135                            assetEntryType, assetRenderer.getUuid());
136    
137                    if (assetEntryOrder > -1) {
138                            assetEntryXmls[assetEntryOrder] = assetEntryXml;
139                    }
140                    else {
141                            assetEntryXmls = ArrayUtil.append(assetEntryXmls, assetEntryXml);
142                    }
143    
144                    preferences.setValues("asset-entry-xml", assetEntryXmls);
145            }
146    
147            public static AssetEntryQuery getAssetEntryQuery(
148                            PortletPreferences preferences, long scopeGroupId)
149                    throws Exception {
150    
151                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
152    
153                    long[] allAssetCategoryIds = new long[0];
154                    long[] anyAssetCategoryIds = new long[0];
155                    long[] notAllAssetCategoryIds = new long[0];
156                    long[] notAnyAssetCategoryIds = new long[0];
157    
158                    String[] allAssetTagNames = new String[0];
159                    String[] anyAssetTagNames = new String[0];
160                    String[] notAllAssetTagNames = new String[0];
161                    String[] notAnyAssetTagNames = new String[0];
162    
163                    for (int i = 0; true; i++) {
164                            String[] queryValues = preferences.getValues(
165                                    "queryValues" + i, null);
166    
167                            if ((queryValues == null) || (queryValues.length == 0)) {
168                                    break;
169                            }
170    
171                            boolean queryContains = GetterUtil.getBoolean(
172                                    preferences.getValue("queryContains" + i, StringPool.BLANK));
173                            boolean queryAndOperator = GetterUtil.getBoolean(
174                                    preferences.getValue("queryAndOperator" + i, StringPool.BLANK));
175                            String queryName = preferences.getValue(
176                                    "queryName" + i, StringPool.BLANK);
177    
178                            if (Validator.equals(queryName, "assetCategories")) {
179                                    long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);
180    
181                                    if (queryContains && queryAndOperator) {
182                                            allAssetCategoryIds = assetCategoryIds;
183                                    }
184                                    else if (queryContains && !queryAndOperator) {
185                                            anyAssetCategoryIds = assetCategoryIds;
186                                    }
187                                    else if (!queryContains && queryAndOperator) {
188                                            notAllAssetCategoryIds = assetCategoryIds;
189                                    }
190                                    else {
191                                            notAnyAssetCategoryIds = assetCategoryIds;
192                                    }
193                            }
194                            else {
195                                    if (queryContains && queryAndOperator) {
196                                            allAssetTagNames = queryValues;
197                                    }
198                                    else if (queryContains && !queryAndOperator) {
199                                            anyAssetTagNames = queryValues;
200                                    }
201                                    else if (!queryContains && queryAndOperator) {
202                                            notAllAssetTagNames = queryValues;
203                                    }
204                                    else {
205                                            notAnyAssetTagNames = queryValues;
206                                    }
207                            }
208                    }
209    
210                    long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
211                            scopeGroupId, allAssetTagNames);
212                    long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
213                            scopeGroupId, anyAssetTagNames);
214                    long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
215                            scopeGroupId, notAllAssetTagNames);
216                    long[] notAnyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
217                            scopeGroupId, notAnyAssetTagNames);
218    
219                    assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);
220                    assetEntryQuery.setAllTagIds(allAssetTagIds);
221                    assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);
222                    assetEntryQuery.setAnyTagIds(anyAssetTagIds);
223                    assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);
224                    assetEntryQuery.setNotAllTagIds(notAllAssetTagIds);
225                    assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);
226                    assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);
227    
228                    return assetEntryQuery;
229            }
230    
231            public static String[] getAssetTagNames(
232                            PortletPreferences preferences, long scopeGroupId)
233                    throws Exception {
234    
235                    String[] allAssetTagNames = new String[0];
236    
237                    for (int i = 0; true; i++) {
238                            String[] queryValues = preferences.getValues(
239                                    "queryValues" + i, null);
240    
241                            if ((queryValues == null) || (queryValues.length == 0)) {
242                                    break;
243                            }
244    
245                            boolean queryContains = GetterUtil.getBoolean(
246                                    preferences.getValue("queryContains" + i, StringPool.BLANK));
247                            boolean queryAndOperator = GetterUtil.getBoolean(
248                                    preferences.getValue("queryAndOperator" + i, StringPool.BLANK));
249                            String queryName = preferences.getValue(
250                                    "queryName" + i, StringPool.BLANK);
251    
252                            if (!Validator.equals(queryName, "assetCategories") &&
253                                    queryContains && queryAndOperator) {
254    
255                                    allAssetTagNames = queryValues;
256                            }
257                    }
258    
259                    return allAssetTagNames;
260            }
261    
262            public static long[] getClassNameIds(
263                    PortletPreferences preferences, long[] availableClassNameIds) {
264    
265                    boolean anyAssetType = GetterUtil.getBoolean(
266                            preferences.getValue("any-asset-type", Boolean.TRUE.toString()));
267    
268                    long[] classNameIds = null;
269    
270                    if (!anyAssetType &&
271                            (preferences.getValues("class-name-ids", null) != null)) {
272    
273                            classNameIds = GetterUtil.getLongValues(
274                                    preferences.getValues("class-name-ids", null));
275                    }
276                    else {
277                            classNameIds = availableClassNameIds;
278                    }
279    
280                    return classNameIds;
281            }
282    
283            public static long[] getGroupIds(
284                    PortletPreferences preferences, long scopeGroupId, Layout layout) {
285    
286                    long[] groupIds = new long[] {scopeGroupId};
287    
288                    boolean defaultScope = GetterUtil.getBoolean(
289                            preferences.getValue("default-scope", null), true);
290    
291                    if (!defaultScope) {
292                            String[] scopeIds = preferences.getValues(
293                                    "scope-ids",
294                                    new String[] {"group" + StringPool.UNDERLINE + scopeGroupId});
295    
296                            groupIds = new long[scopeIds.length];
297    
298                            for (int i = 0; i < scopeIds.length; i++) {
299                                    try {
300                                            String[] scopeIdFragments = StringUtil.split(
301                                                    scopeIds[i], StringPool.UNDERLINE);
302    
303                                            if (scopeIdFragments[0].equals("Layout")) {
304                                                    long scopeIdLayoutId = GetterUtil.getLong(
305                                                            scopeIdFragments[1]);
306    
307                                                    Layout scopeIdLayout =
308                                                            LayoutLocalServiceUtil.getLayout(
309                                                                    scopeGroupId, layout.isPrivateLayout(),
310                                                                    scopeIdLayoutId);
311    
312                                                    Group scopeIdGroup = scopeIdLayout.getScopeGroup();
313    
314                                                    groupIds[i] = scopeIdGroup.getGroupId();
315                                            }
316                                            else {
317                                                    if (scopeIdFragments[1].equals(
318                                                                    GroupConstants.DEFAULT)) {
319    
320                                                            groupIds[i] = scopeGroupId;
321                                                    }
322                                                    else {
323                                                            long scopeIdGroupId = GetterUtil.getLong(
324                                                                    scopeIdFragments[1]);
325    
326                                                            groupIds[i] = scopeIdGroupId;
327                                                    }
328                                            }
329                                    }
330                                    catch (Exception e) {
331                                            continue;
332                                    }
333                            }
334                    }
335    
336                    return groupIds;
337            }
338    
339            public static long getRecentFolderId(
340                    PortletRequest portletRequest, String className) {
341    
342                    Long classPK = _getRecentFolderIds(portletRequest).get(className);
343    
344                    if (classPK == null) {
345                            return 0;
346                    }
347                    else {
348                            return classPK.longValue();
349                    }
350            }
351    
352            public static void removeAndStoreSelection(
353                            List<String> assetEntryUuids, PortletPreferences preferences)
354                    throws Exception {
355    
356                    if (assetEntryUuids.size() == 0) {
357                            return;
358                    }
359    
360                    String[] assetEntryXmls = preferences.getValues(
361                            "asset-entry-xml", new String[0]);
362    
363                    List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls);
364    
365                    Iterator<String> itr = assetEntryXmlsList.iterator();
366    
367                    while (itr.hasNext()) {
368                            String assetEntryXml = itr.next();
369    
370                            Document document = SAXReaderUtil.read(assetEntryXml);
371    
372                            Element rootElement = document.getRootElement();
373    
374                            String assetEntryUuid = rootElement.elementText("asset-entry-uuid");
375    
376                            if (assetEntryUuids.contains(assetEntryUuid)) {
377                                    itr.remove();
378                            }
379                    }
380    
381                    preferences.setValues(
382                            "asset-entry-xml",
383                            assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()]));
384    
385                    preferences.store();
386            }
387    
388            public static void removeRecentFolderId(
389                    PortletRequest portletRequest, String className, long classPK) {
390    
391                    if (getRecentFolderId(portletRequest, className) == classPK) {
392                            _getRecentFolderIds(portletRequest).remove(className);
393                    }
394            }
395    
396            private static String _getAssetEntryXml(
397                    String assetEntryType, String assetEntryUuid) {
398    
399                    String xml = null;
400    
401                    try {
402                            Document document = SAXReaderUtil.createDocument(StringPool.UTF8);
403    
404                            Element assetEntryElement = document.addElement("asset-entry");
405    
406                            Element assetEntryTypeElement = assetEntryElement.addElement(
407                                    "asset-entry-type");
408    
409                            assetEntryTypeElement.addText(assetEntryType);
410    
411                            Element assetEntryUuidElement = assetEntryElement.addElement(
412                                    "asset-entry-uuid");
413    
414                            assetEntryUuidElement.addText(assetEntryUuid);
415    
416                            xml = document.formattedString(StringPool.BLANK);
417                    }
418                    catch (IOException ioe) {
419                            if (_log.isWarnEnabled()) {
420                                    _log.warn(ioe);
421                            }
422                    }
423    
424                    return xml;
425            }
426    
427            private static Map<String, Long> _getRecentFolderIds(
428                    PortletRequest portletRequest) {
429    
430                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
431                            portletRequest);
432                    HttpSession session = request.getSession();
433    
434                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
435                            WebKeys.THEME_DISPLAY);
436    
437                    String key =
438                            AssetPublisherUtil.class + StringPool.UNDERLINE +
439                                    themeDisplay.getScopeGroupId();
440    
441                    Map<String, Long> recentFolderIds =
442                            (Map<String, Long>)session.getAttribute(key);
443    
444                    if (recentFolderIds == null) {
445                            recentFolderIds = new HashMap<String, Long>();
446                    }
447    
448                    session.setAttribute(key, recentFolderIds);
449    
450                    return recentFolderIds;
451            }
452    
453            private static Log _log = LogFactoryUtil.getLog(AssetPublisherUtil.class);
454    
455    }