001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.assetpublisher.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
020    import com.liferay.portal.kernel.portlet.PortletResponseUtil;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.servlet.SessionMessages;
023    import com.liferay.portal.kernel.staging.LayoutStagingUtil;
024    import com.liferay.portal.kernel.staging.StagingUtil;
025    import com.liferay.portal.kernel.util.ArrayUtil;
026    import com.liferay.portal.kernel.util.Constants;
027    import com.liferay.portal.kernel.util.ContentTypes;
028    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.ParamUtil;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.StringUtil;
033    import com.liferay.portal.kernel.util.UnicodeProperties;
034    import com.liferay.portal.kernel.util.Validator;
035    import com.liferay.portal.model.GroupConstants;
036    import com.liferay.portal.model.Layout;
037    import com.liferay.portal.model.LayoutRevision;
038    import com.liferay.portal.model.LayoutSetBranch;
039    import com.liferay.portal.model.LayoutTypePortletConstants;
040    import com.liferay.portal.security.auth.PrincipalException;
041    import com.liferay.portal.service.LayoutLocalServiceUtil;
042    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
043    import com.liferay.portal.service.ServiceContext;
044    import com.liferay.portal.service.ServiceContextFactory;
045    import com.liferay.portal.theme.ThemeDisplay;
046    import com.liferay.portal.util.PortalUtil;
047    import com.liferay.portal.util.WebKeys;
048    import com.liferay.portlet.PortletPreferencesImpl;
049    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
050    import com.liferay.portlet.asset.AssetTagException;
051    import com.liferay.portlet.asset.DuplicateQueryRuleException;
052    import com.liferay.portlet.asset.model.AssetQueryRule;
053    import com.liferay.portlet.asset.model.AssetRendererFactory;
054    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
055    import com.liferay.portlet.assetpublisher.util.AssetPublisher;
056    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
057    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
058    import com.liferay.portlet.dynamicdatamapping.storage.Field;
059    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
060    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
061    
062    import java.io.Serializable;
063    
064    import java.text.DateFormat;
065    
066    import java.util.ArrayList;
067    import java.util.Date;
068    import java.util.List;
069    import java.util.Map;
070    
071    import javax.portlet.ActionRequest;
072    import javax.portlet.ActionResponse;
073    import javax.portlet.PortletConfig;
074    import javax.portlet.PortletPreferences;
075    import javax.portlet.ResourceRequest;
076    import javax.portlet.ResourceResponse;
077    
078    import javax.servlet.http.HttpServletRequest;
079    
080    /**
081     * @author Brian Wing Shun Chan
082     * @author Juan Fern??ndez
083     */
084    public class ConfigurationActionImpl extends DefaultConfigurationAction {
085    
086            @Override
087            public void processAction(
088                            PortletConfig portletConfig, ActionRequest actionRequest,
089                            ActionResponse actionResponse)
090                    throws Exception {
091    
092                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
093    
094                    String portletResource = ParamUtil.getString(
095                            actionRequest, "portletResource");
096    
097                    PortletPreferences preferences = actionRequest.getPreferences();
098    
099                    if (cmd.equals(Constants.TRANSLATE)) {
100                            super.processAction(portletConfig, actionRequest, actionResponse);
101                    }
102                    else if (cmd.equals(Constants.UPDATE)) {
103                            try {
104                                    validateEmailAssetEntryAdded(actionRequest);
105                                    validateEmailFrom(actionRequest);
106    
107                                    updateDisplaySettings(actionRequest);
108    
109                                    String selectionStyle = getParameter(
110                                            actionRequest, "selectionStyle");
111    
112                                    if (selectionStyle.equals("dynamic")) {
113                                            updateQueryLogic(actionRequest, preferences);
114                                    }
115    
116                                    updateDefaultAssetPublisher(actionRequest);
117    
118                                    super.processAction(
119                                            portletConfig, actionRequest, actionResponse);
120                            }
121                            catch (Exception e) {
122                                    if (e instanceof AssetTagException ||
123                                            e instanceof DuplicateQueryRuleException) {
124    
125                                            SessionErrors.add(actionRequest, e.getClass(), e);
126                                    }
127                                    else {
128                                            throw e;
129                                    }
130                            }
131                    }
132                    else {
133                            if (cmd.equals("add-scope")) {
134                                    addScope(actionRequest, preferences);
135                            }
136                            else if (cmd.equals("add-selection")) {
137                                    AssetPublisherUtil.addSelection(
138                                            actionRequest, preferences, portletResource);
139                            }
140                            else if (cmd.equals("move-selection-down")) {
141                                    moveSelectionDown(actionRequest, preferences);
142                            }
143                            else if (cmd.equals("move-selection-up")) {
144                                    moveSelectionUp(actionRequest, preferences);
145                            }
146                            else if (cmd.equals("remove-selection")) {
147                                    removeSelection(actionRequest, preferences);
148                            }
149                            else if (cmd.equals("remove-scope")) {
150                                    removeScope(actionRequest, preferences);
151                            }
152                            else if (cmd.equals("select-scope")) {
153                                    setScopes(actionRequest, preferences);
154                            }
155                            else if (cmd.equals("selection-style")) {
156                                    setSelectionStyle(actionRequest, preferences);
157                            }
158    
159                            if (SessionErrors.isEmpty(actionRequest)) {
160                                    preferences.store();
161    
162                                    SessionMessages.add(
163                                            actionRequest,
164                                            PortalUtil.getPortletId(actionRequest) +
165                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
166                                            portletResource);
167    
168                                    SessionMessages.add(
169                                            actionRequest,
170                                            PortalUtil.getPortletId(actionRequest) +
171                                                    SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
172                            }
173    
174                            String redirect = PortalUtil.escapeRedirect(
175                                    ParamUtil.getString(actionRequest, "redirect"));
176    
177                            if (Validator.isNotNull(redirect)) {
178                                    actionResponse.sendRedirect(redirect);
179                            }
180                    }
181            }
182    
183            @Override
184            public void serveResource(
185                            PortletConfig portletConfig, ResourceRequest resourceRequest,
186                            ResourceResponse resourceResponse)
187                    throws Exception {
188    
189                    ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
190                            WebKeys.THEME_DISPLAY);
191    
192                    String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
193    
194                    if (!cmd.equals("getFieldValue")) {
195                            return;
196                    }
197    
198                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
199                            resourceRequest);
200    
201                    long structureId = ParamUtil.getLong(resourceRequest, "structureId");
202    
203                    Fields fields = (Fields)serviceContext.getAttribute(
204                            Fields.class.getName() + structureId);
205    
206                    if (fields == null) {
207                            String fieldsNamespace = ParamUtil.getString(
208                                    resourceRequest, "fieldsNamespace");
209    
210                            fields = DDMUtil.getFields(
211                                    structureId, fieldsNamespace, serviceContext);
212                    }
213    
214                    String fieldName = ParamUtil.getString(resourceRequest, "name");
215    
216                    Field field = fields.get(fieldName);
217    
218                    Serializable fieldValue = field.getValue(themeDisplay.getLocale(), 0);
219    
220                    DDMStructure ddmStructure = field.getDDMStructure();
221    
222                    String type = ddmStructure.getFieldType(fieldName);
223    
224                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
225    
226                    Serializable displayValue = DDMUtil.getDisplayFieldValue(
227                            themeDisplay, fieldValue, type);
228    
229                    jsonObject.put("displayValue", String.valueOf(displayValue));
230    
231                    if (fieldValue instanceof Boolean) {
232                            jsonObject.put("value", (Boolean)fieldValue);
233                    }
234                    else if (fieldValue instanceof Date) {
235                            DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
236                                    "yyyyMMddHHmmss");
237    
238                            jsonObject.put("value", dateFormat.format(fieldValue));
239                    }
240                    else if (fieldValue instanceof Double) {
241                            jsonObject.put("value", (Double)fieldValue);
242                    }
243                    else if (fieldValue instanceof Float) {
244                            jsonObject.put("value", (Float)fieldValue);
245                    }
246                    else if (fieldValue instanceof Integer) {
247                            jsonObject.put("value", (Integer)fieldValue);
248                    }
249                    else if (fieldValue instanceof Number) {
250                            jsonObject.put("value", String.valueOf(fieldValue));
251                    }
252                    else {
253                            jsonObject.put("value", (String)fieldValue);
254                    }
255    
256                    resourceResponse.setContentType(ContentTypes.APPLICATION_JSON);
257    
258                    PortletResponseUtil.write(resourceResponse, jsonObject.toString());
259            }
260    
261            protected void addScope(
262                            ActionRequest actionRequest, PortletPreferences preferences)
263                    throws Exception {
264    
265                    String[] scopeIds = preferences.getValues(
266                            "scopeIds",
267                            new String[] {
268                                    AssetPublisher.SCOPE_ID_GROUP_PREFIX + GroupConstants.DEFAULT
269                            });
270    
271                    String scopeId = ParamUtil.getString(actionRequest, "scopeId");
272    
273                    checkPermission(actionRequest, scopeId);
274    
275                    if (!ArrayUtil.contains(scopeIds, scopeId)) {
276                            scopeIds = ArrayUtil.append(scopeIds, scopeId);
277                    }
278    
279                    preferences.setValues("scopeIds", scopeIds);
280            }
281    
282            protected void checkPermission(ActionRequest actionRequest, String scopeId)
283                    throws Exception {
284    
285                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
286                            WebKeys.THEME_DISPLAY);
287    
288                    Layout layout = themeDisplay.getLayout();
289    
290                    if (!AssetPublisherUtil.isScopeIdSelectable(
291                                    themeDisplay.getPermissionChecker(), scopeId,
292                                    themeDisplay.getCompanyGroupId(), layout)) {
293    
294                            throw new PrincipalException();
295                    }
296            }
297    
298            protected String getAssetClassName(
299                            ActionRequest actionRequest, String[] classNameIds)
300                    throws Exception {
301    
302                    String anyAssetTypeString = getParameter(actionRequest, "anyAssetType");
303    
304                    boolean anyAssetType = GetterUtil.getBoolean(anyAssetTypeString);
305    
306                    if (anyAssetType) {
307                            return null;
308                    }
309    
310                    long defaultAssetTypeId = GetterUtil.getLong(anyAssetTypeString);
311    
312                    if ((defaultAssetTypeId == 0) && (classNameIds.length == 1)) {
313                            defaultAssetTypeId = GetterUtil.getLong(classNameIds[0]);
314                    }
315    
316                    if (defaultAssetTypeId <= 0 ) {
317                            return null;
318                    }
319    
320                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
321                            WebKeys.THEME_DISPLAY);
322    
323                    String className = PortalUtil.getClassName(defaultAssetTypeId);
324    
325                    AssetRendererFactory assetRendererFactory =
326                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
327                                    className);
328    
329                    long[] groupIds = {
330                            themeDisplay.getCompanyGroupId(), themeDisplay.getSiteGroupId()
331                    };
332    
333                    Map<Long, String> classTypes = assetRendererFactory.getClassTypes(
334                            groupIds, themeDisplay.getLocale());
335    
336                    if (classTypes.isEmpty()) {
337                            return null;
338                    }
339    
340                    String assetClassName = AssetPublisherUtil.getClassName(
341                            assetRendererFactory);
342    
343                    return assetClassName;
344            }
345    
346            protected String[] getClassTypeIds(
347                            ActionRequest actionRequest, String[] classNameIds)
348                    throws Exception {
349    
350                    String assetClassName = getAssetClassName(actionRequest, classNameIds);
351    
352                    if (assetClassName == null) {
353                            return null;
354                    }
355    
356                    String anyAssetClassTypeString = getParameter(
357                            actionRequest, "anyClassType" + assetClassName);
358    
359                    boolean anyAssetClassType = GetterUtil.getBoolean(
360                            anyAssetClassTypeString);
361    
362                    if (anyAssetClassType) {
363                            return null;
364                    }
365    
366                    long defaultAssetClassTypeId = GetterUtil.getLong(
367                            anyAssetClassTypeString);
368    
369                    if (defaultAssetClassTypeId > 0) {
370                            return new String[] {String.valueOf(defaultAssetClassTypeId)};
371                    }
372                    else {
373                            return StringUtil.split(
374                                    getParameter(actionRequest, "classTypeIds" + assetClassName));
375                    }
376            }
377    
378            protected AssetQueryRule getQueryRule(
379                    ActionRequest actionRequest, int index) {
380    
381                    boolean contains = ParamUtil.getBoolean(
382                            actionRequest, "queryContains" + index);
383                    boolean andOperator = ParamUtil.getBoolean(
384                            actionRequest, "queryAndOperator" + index);
385    
386                    String name = ParamUtil.getString(actionRequest, "queryName" + index);
387    
388                    String[] values = null;
389    
390                    if (name.equals("assetTags")) {
391                            values = StringUtil.split(
392                                    ParamUtil.getString(actionRequest, "queryTagNames" + index));
393                    }
394                    else {
395                            values = StringUtil.split(
396                                    ParamUtil.getString(actionRequest, "queryCategoryIds" + index));
397                    }
398    
399                    return new AssetQueryRule(contains, andOperator, name, values);
400            }
401    
402            protected boolean getSubtypesFieldsFilterEnabled(
403                            ActionRequest actionRequest, String[] classNameIds)
404                    throws Exception {
405    
406                    String assetClassName = getAssetClassName(actionRequest, classNameIds);
407    
408                    if (assetClassName == null) {
409                            return false;
410                    }
411    
412                    return GetterUtil.getBoolean(
413                            getParameter(
414                                    actionRequest, "subtypeFieldsFilterEnabled" + assetClassName));
415            }
416    
417            protected void moveSelectionDown(
418                            ActionRequest actionRequest, PortletPreferences preferences)
419                    throws Exception {
420    
421                    int assetEntryOrder = ParamUtil.getInteger(
422                            actionRequest, "assetEntryOrder");
423    
424                    String[] manualEntries = preferences.getValues(
425                            "assetEntryXml", new String[0]);
426    
427                    if ((assetEntryOrder >= (manualEntries.length - 1)) ||
428                            (assetEntryOrder < 0)) {
429    
430                            return;
431                    }
432    
433                    String temp = manualEntries[assetEntryOrder + 1];
434    
435                    manualEntries[assetEntryOrder + 1] = manualEntries[assetEntryOrder];
436                    manualEntries[assetEntryOrder] = temp;
437    
438                    preferences.setValues("assetEntryXml", manualEntries);
439            }
440    
441            protected void moveSelectionUp(
442                            ActionRequest actionRequest, PortletPreferences preferences)
443                    throws Exception {
444    
445                    int assetEntryOrder = ParamUtil.getInteger(
446                            actionRequest, "assetEntryOrder");
447    
448                    String[] manualEntries = preferences.getValues(
449                            "assetEntryXml", new String[0]);
450    
451                    if ((assetEntryOrder >= manualEntries.length) ||
452                            (assetEntryOrder <= 0)) {
453    
454                            return;
455                    }
456    
457                    String temp = manualEntries[assetEntryOrder - 1];
458    
459                    manualEntries[assetEntryOrder - 1] = manualEntries[assetEntryOrder];
460                    manualEntries[assetEntryOrder] = temp;
461    
462                    preferences.setValues("assetEntryXml", manualEntries);
463            }
464    
465            protected void removeScope(
466                            ActionRequest actionRequest, PortletPreferences preferences)
467                    throws Exception {
468    
469                    String[] scopeIds = preferences.getValues(
470                            "scopeIds",
471                            new String[] {
472                                    AssetPublisher.SCOPE_ID_GROUP_PREFIX + GroupConstants.DEFAULT
473                            });
474    
475                    String scopeId = ParamUtil.getString(actionRequest, "scopeId");
476    
477                    scopeIds = ArrayUtil.remove(scopeIds, scopeId);
478    
479                    if (scopeId.startsWith(AssetPublisher.SCOPE_ID_PARENT_GROUP_PREFIX)) {
480                            scopeId = scopeId.substring("Parent".length());
481    
482                            scopeIds = ArrayUtil.remove(scopeIds, scopeId);
483                    }
484    
485                    preferences.setValues("scopeIds", scopeIds);
486            }
487    
488            protected void removeSelection(
489                            ActionRequest actionRequest, PortletPreferences preferences)
490                    throws Exception {
491    
492                    int assetEntryOrder = ParamUtil.getInteger(
493                            actionRequest, "assetEntryOrder");
494    
495                    String[] manualEntries = preferences.getValues(
496                            "assetEntryXml", new String[0]);
497    
498                    if (assetEntryOrder >= manualEntries.length) {
499                            return;
500                    }
501    
502                    String[] newEntries = new String[manualEntries.length -1];
503    
504                    int i = 0;
505                    int j = 0;
506    
507                    for (; i < manualEntries.length; i++) {
508                            if (i != assetEntryOrder) {
509                                    newEntries[j++] = manualEntries[i];
510                            }
511                    }
512    
513                    preferences.setValues("assetEntryXml", newEntries);
514            }
515    
516            protected void setScopes(
517                            ActionRequest actionRequest, PortletPreferences preferences)
518                    throws Exception {
519    
520                    String[] scopeIds = StringUtil.split(
521                            getParameter(actionRequest, "scopeIds"));
522    
523                    preferences.setValues("scopeIds", scopeIds);
524            }
525    
526            protected void setSelectionStyle(
527                            ActionRequest actionRequest, PortletPreferences preferences)
528                    throws Exception {
529    
530                    String selectionStyle = getParameter(actionRequest, "selectionStyle");
531                    String displayStyle = getParameter(actionRequest, "displayStyle");
532    
533                    preferences.setValue("selectionStyle", selectionStyle);
534    
535                    if (selectionStyle.equals("manual") ||
536                            selectionStyle.equals("view-count")) {
537    
538                            preferences.setValue("enableRss", String.valueOf(false));
539                            preferences.setValue("showQueryLogic", Boolean.FALSE.toString());
540    
541                            preferences.reset("rssDelta");
542                            preferences.reset("rssDisplayStyle");
543                            preferences.reset("rssFormat");
544                            preferences.reset("rssName");
545                    }
546    
547                    if (!selectionStyle.equals("view-count") &&
548                            displayStyle.equals("view-count-details")) {
549    
550                            preferences.setValue("displayStyle", "full-content");
551                    }
552            }
553    
554            protected void updateDefaultAssetPublisher(ActionRequest actionRequest)
555                    throws Exception {
556    
557                    boolean defaultAssetPublisher = ParamUtil.getBoolean(
558                            actionRequest, "defaultAssetPublisher");
559    
560                    Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
561    
562                    String portletResource = ParamUtil.getString(
563                            actionRequest, "portletResource");
564    
565                    UnicodeProperties typeSettingsProperties =
566                            layout.getTypeSettingsProperties();
567    
568                    if (defaultAssetPublisher) {
569                            typeSettingsProperties.setProperty(
570                                    LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
571                                    portletResource);
572                    }
573                    else {
574                            String defaultAssetPublisherPortletId =
575                                    typeSettingsProperties.getProperty(
576                                            LayoutTypePortletConstants.
577                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID);
578    
579                            if (Validator.isNotNull(defaultAssetPublisherPortletId) &&
580                                    defaultAssetPublisherPortletId.equals(portletResource)) {
581    
582                                    typeSettingsProperties.setProperty(
583                                            LayoutTypePortletConstants.
584                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
585                                            StringPool.BLANK);
586                            }
587                    }
588    
589                    layout = LayoutLocalServiceUtil.updateLayout(
590                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
591                            layout.getTypeSettings());
592    
593                    if (LayoutStagingUtil.isBranchingLayout(layout)) {
594                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
595                                    actionRequest);
596    
597                            LayoutSetBranch layoutSetBranch =
598                                    LayoutStagingUtil.getLayoutSetBranch(layout.getLayoutSet());
599    
600                            long layoutSetBranchId = layoutSetBranch.getLayoutSetBranchId();
601    
602                            long layoutRevisionId = StagingUtil.getRecentLayoutRevisionId(
603                                    request, layoutSetBranchId, layout.getPlid());
604    
605                            LayoutRevision layoutRevision =
606                                    LayoutRevisionLocalServiceUtil.getLayoutRevision(
607                                            layoutRevisionId);
608    
609                            PortletPreferencesImpl portletPreferences =
610                                    (PortletPreferencesImpl)actionRequest.getPreferences();
611    
612                            if (layoutRevision != null) {
613                                    portletPreferences.setPlid(
614                                            layoutRevision.getLayoutRevisionId());
615                            }
616                    }
617            }
618    
619            protected void updateDisplaySettings(ActionRequest actionRequest)
620                    throws Exception {
621    
622                    String[] classNameIds = StringUtil.split(
623                            getParameter(actionRequest, "classNameIds"));
624                    String[] classTypeIds = getClassTypeIds(actionRequest, classNameIds);
625                    String[] extensions = actionRequest.getParameterValues("extensions");
626                    boolean subtypeFieldsFilterEnabled = getSubtypesFieldsFilterEnabled(
627                            actionRequest, classNameIds);
628    
629                    setPreference(actionRequest, "classNameIds", classNameIds);
630                    setPreference(actionRequest, "classTypeIds", classTypeIds);
631                    setPreference(actionRequest, "extensions", extensions);
632                    setPreference(
633                            actionRequest, "subtypeFieldsFilterEnabled",
634                            String.valueOf(subtypeFieldsFilterEnabled));
635            }
636    
637            protected void updateQueryLogic(
638                            ActionRequest actionRequest, PortletPreferences preferences)
639                    throws Exception {
640    
641                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
642                            WebKeys.THEME_DISPLAY);
643    
644                    long userId = themeDisplay.getUserId();
645                    long groupId = themeDisplay.getSiteGroupId();
646    
647                    int[] queryRulesIndexes = StringUtil.split(
648                            ParamUtil.getString(actionRequest, "queryLogicIndexes"), 0);
649    
650                    int i = 0;
651    
652                    List<AssetQueryRule> queryRules = new ArrayList<AssetQueryRule>();
653    
654                    for (int queryRulesIndex : queryRulesIndexes) {
655                            AssetQueryRule queryRule = getQueryRule(
656                                    actionRequest, queryRulesIndex);
657    
658                            validateQueryRule(userId, groupId, queryRules, queryRule);
659    
660                            queryRules.add(queryRule);
661    
662                            setPreference(
663                                    actionRequest, "queryContains" + i,
664                                    String.valueOf(queryRule.isContains()));
665                            setPreference(
666                                    actionRequest, "queryAndOperator" + i,
667                                    String.valueOf(queryRule.isAndOperator()));
668                            setPreference(actionRequest, "queryName" + i, queryRule.getName());
669                            setPreference(
670                                    actionRequest, "queryValues" + i, queryRule.getValues());
671    
672                            i++;
673                    }
674    
675                    // Clear previous preferences that are now blank
676    
677                    String[] values = preferences.getValues(
678                            "queryValues" + i, new String[0]);
679    
680                    while (values.length > 0) {
681                            setPreference(actionRequest, "queryContains" + i, StringPool.BLANK);
682                            setPreference(
683                                    actionRequest, "queryAndOperator" + i, StringPool.BLANK);
684                            setPreference(actionRequest, "queryName" + i, StringPool.BLANK);
685                            setPreference(actionRequest, "queryValues" + i, new String[0]);
686    
687                            i++;
688    
689                            values = preferences.getValues("queryValues" + i, new String[0]);
690                    }
691            }
692    
693            protected void validateEmailAssetEntryAdded(ActionRequest actionRequest)
694                    throws Exception {
695    
696                    String emailAssetEntryAddedSubject = getLocalizedParameter(
697                            actionRequest, "emailAssetEntryAddedSubject");
698                    String emailAssetEntryAddedBody = getLocalizedParameter(
699                            actionRequest, "emailAssetEntryAddedBody");
700    
701                    if (Validator.isNull(emailAssetEntryAddedSubject)) {
702                            SessionErrors.add(actionRequest, "emailAssetEntryAddedSubject");
703                    }
704                    else if (Validator.isNull(emailAssetEntryAddedBody)) {
705                            SessionErrors.add(actionRequest, "emailAssetEntryAddedBody");
706                    }
707            }
708    
709            protected void validateEmailFrom(ActionRequest actionRequest)
710                    throws Exception {
711    
712                    String emailFromName = getParameter(actionRequest, "emailFromName");
713                    String emailFromAddress = getParameter(
714                            actionRequest, "emailFromAddress");
715    
716                    if (Validator.isNull(emailFromName)) {
717                            SessionErrors.add(actionRequest, "emailFromName");
718                    }
719                    else if (!Validator.isEmailAddress(emailFromAddress) &&
720                                     !Validator.isVariableTerm(emailFromAddress)) {
721    
722                            SessionErrors.add(actionRequest, "emailFromAddress");
723                    }
724            }
725    
726            protected void validateQueryRule(
727                            long userId, long groupId, List<AssetQueryRule> queryRules,
728                            AssetQueryRule queryRule)
729                    throws Exception {
730    
731                    String name = queryRule.getName();
732    
733                    if (name.equals("assetTags")) {
734                            AssetTagLocalServiceUtil.checkTags(
735                                    userId, groupId, queryRule.getValues());
736                    }
737    
738                    if (queryRules.contains(queryRule)) {
739                            throw new DuplicateQueryRuleException(
740                                    queryRule.isContains(), queryRule.isAndOperator(),
741                                    queryRule.getName());
742                    }
743            }
744    
745    }