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