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 {
250                            jsonObject.put("value", (String)fieldValue);
251                    }
252    
253                    resourceResponse.setContentType(ContentTypes.APPLICATION_JSON);
254    
255                    PortletResponseUtil.write(resourceResponse, jsonObject.toString());
256            }
257    
258            protected void addScope(
259                            ActionRequest actionRequest, PortletPreferences preferences)
260                    throws Exception {
261    
262                    String[] scopeIds = preferences.getValues(
263                            "scopeIds",
264                            new String[] {
265                                    AssetPublisher.SCOPE_ID_GROUP_PREFIX + GroupConstants.DEFAULT
266                            });
267    
268                    String scopeId = ParamUtil.getString(actionRequest, "scopeId");
269    
270                    checkPermission(actionRequest, scopeId);
271    
272                    if (!ArrayUtil.contains(scopeIds, scopeId)) {
273                            scopeIds = ArrayUtil.append(scopeIds, scopeId);
274                    }
275    
276                    preferences.setValues("scopeIds", scopeIds);
277            }
278    
279            protected void checkPermission(ActionRequest actionRequest, String scopeId)
280                    throws Exception {
281    
282                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
283                            WebKeys.THEME_DISPLAY);
284    
285                    Layout layout = themeDisplay.getLayout();
286    
287                    if (!AssetPublisherUtil.isScopeIdSelectable(
288                                    themeDisplay.getPermissionChecker(), scopeId,
289                                    themeDisplay.getCompanyGroupId(), layout)) {
290    
291                            throw new PrincipalException();
292                    }
293            }
294    
295            protected String getAssetClassName(
296                            ActionRequest actionRequest, String[] classNameIds)
297                    throws Exception {
298    
299                    String anyAssetTypeString = getParameter(actionRequest, "anyAssetType");
300    
301                    boolean anyAssetType = GetterUtil.getBoolean(anyAssetTypeString);
302    
303                    if (anyAssetType) {
304                            return null;
305                    }
306    
307                    long defaultAssetTypeId = GetterUtil.getLong(anyAssetTypeString);
308    
309                    if ((defaultAssetTypeId == 0) && (classNameIds.length == 1)) {
310                            defaultAssetTypeId = GetterUtil.getLong(classNameIds[0]);
311                    }
312    
313                    if (defaultAssetTypeId <= 0 ) {
314                            return null;
315                    }
316    
317                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
318                            WebKeys.THEME_DISPLAY);
319    
320                    String className = PortalUtil.getClassName(defaultAssetTypeId);
321    
322                    AssetRendererFactory assetRendererFactory =
323                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
324                                    className);
325    
326                    long[] groupIds = {
327                            themeDisplay.getCompanyGroupId(), themeDisplay.getSiteGroupId()
328                    };
329    
330                    Map<Long, String> classTypes = assetRendererFactory.getClassTypes(
331                            groupIds, themeDisplay.getLocale());
332    
333                    if (classTypes.isEmpty()) {
334                            return null;
335                    }
336    
337                    String assetClassName = AssetPublisherUtil.getClassName(
338                            assetRendererFactory);
339    
340                    return assetClassName;
341            }
342    
343            protected String[] getClassTypeIds(
344                            ActionRequest actionRequest, String[] classNameIds)
345                    throws Exception {
346    
347                    String assetClassName = getAssetClassName(actionRequest, classNameIds);
348    
349                    if (assetClassName == null) {
350                            return null;
351                    }
352    
353                    String anyAssetClassTypeString = getParameter(
354                            actionRequest, "anyClassType" + assetClassName);
355    
356                    boolean anyAssetClassType = GetterUtil.getBoolean(
357                            anyAssetClassTypeString);
358    
359                    if (anyAssetClassType) {
360                            return null;
361                    }
362    
363                    long defaultAssetClassTypeId = GetterUtil.getLong(
364                            anyAssetClassTypeString);
365    
366                    if (defaultAssetClassTypeId > 0) {
367                            return new String[] {String.valueOf(defaultAssetClassTypeId)};
368                    }
369                    else {
370                            return StringUtil.split(
371                                    getParameter(actionRequest, "classTypeIds" + assetClassName));
372                    }
373            }
374    
375            protected AssetQueryRule getQueryRule(
376                    ActionRequest actionRequest, int index) {
377    
378                    boolean contains = ParamUtil.getBoolean(
379                            actionRequest, "queryContains" + index);
380                    boolean andOperator = ParamUtil.getBoolean(
381                            actionRequest, "queryAndOperator" + index);
382    
383                    String name = ParamUtil.getString(actionRequest, "queryName" + index);
384    
385                    String[] values = null;
386    
387                    if (name.equals("assetTags")) {
388                            values = StringUtil.split(
389                                    ParamUtil.getString(actionRequest, "queryTagNames" + index));
390                    }
391                    else {
392                            values = StringUtil.split(
393                                    ParamUtil.getString(actionRequest, "queryCategoryIds" + index));
394                    }
395    
396                    return new AssetQueryRule(contains, andOperator, name, values);
397            }
398    
399            protected boolean getSubtypesFieldsFilterEnabled(
400                            ActionRequest actionRequest, String[] classNameIds)
401                    throws Exception {
402    
403                    String assetClassName = getAssetClassName(actionRequest, classNameIds);
404    
405                    if (assetClassName == null) {
406                            return false;
407                    }
408    
409                    return GetterUtil.getBoolean(
410                            getParameter(
411                                    actionRequest, "subtypeFieldsFilterEnabled" + assetClassName));
412            }
413    
414            protected void moveSelectionDown(
415                            ActionRequest actionRequest, PortletPreferences preferences)
416                    throws Exception {
417    
418                    int assetEntryOrder = ParamUtil.getInteger(
419                            actionRequest, "assetEntryOrder");
420    
421                    String[] manualEntries = preferences.getValues(
422                            "assetEntryXml", new String[0]);
423    
424                    if ((assetEntryOrder >= (manualEntries.length - 1)) ||
425                            (assetEntryOrder < 0)) {
426    
427                            return;
428                    }
429    
430                    String temp = manualEntries[assetEntryOrder + 1];
431    
432                    manualEntries[assetEntryOrder + 1] = manualEntries[assetEntryOrder];
433                    manualEntries[assetEntryOrder] = temp;
434    
435                    preferences.setValues("assetEntryXml", manualEntries);
436            }
437    
438            protected void moveSelectionUp(
439                            ActionRequest actionRequest, PortletPreferences preferences)
440                    throws Exception {
441    
442                    int assetEntryOrder = ParamUtil.getInteger(
443                            actionRequest, "assetEntryOrder");
444    
445                    String[] manualEntries = preferences.getValues(
446                            "assetEntryXml", new String[0]);
447    
448                    if ((assetEntryOrder >= manualEntries.length) ||
449                            (assetEntryOrder <= 0)) {
450    
451                            return;
452                    }
453    
454                    String temp = manualEntries[assetEntryOrder - 1];
455    
456                    manualEntries[assetEntryOrder - 1] = manualEntries[assetEntryOrder];
457                    manualEntries[assetEntryOrder] = temp;
458    
459                    preferences.setValues("assetEntryXml", manualEntries);
460            }
461    
462            protected void removeScope(
463                            ActionRequest actionRequest, PortletPreferences preferences)
464                    throws Exception {
465    
466                    String[] scopeIds = preferences.getValues(
467                            "scopeIds",
468                            new String[] {
469                                    AssetPublisher.SCOPE_ID_GROUP_PREFIX + GroupConstants.DEFAULT
470                            });
471    
472                    String scopeId = ParamUtil.getString(actionRequest, "scopeId");
473    
474                    scopeIds = ArrayUtil.remove(scopeIds, scopeId);
475    
476                    if (scopeId.startsWith(AssetPublisher.SCOPE_ID_PARENT_GROUP_PREFIX)) {
477                            scopeId = scopeId.substring("Parent".length());
478    
479                            scopeIds = ArrayUtil.remove(scopeIds, scopeId);
480                    }
481    
482                    preferences.setValues("scopeIds", scopeIds);
483            }
484    
485            protected void removeSelection(
486                            ActionRequest actionRequest, PortletPreferences preferences)
487                    throws Exception {
488    
489                    int assetEntryOrder = ParamUtil.getInteger(
490                            actionRequest, "assetEntryOrder");
491    
492                    String[] manualEntries = preferences.getValues(
493                            "assetEntryXml", new String[0]);
494    
495                    if (assetEntryOrder >= manualEntries.length) {
496                            return;
497                    }
498    
499                    String[] newEntries = new String[manualEntries.length -1];
500    
501                    int i = 0;
502                    int j = 0;
503    
504                    for (; i < manualEntries.length; i++) {
505                            if (i != assetEntryOrder) {
506                                    newEntries[j++] = manualEntries[i];
507                            }
508                    }
509    
510                    preferences.setValues("assetEntryXml", newEntries);
511            }
512    
513            protected void setScopes(
514                            ActionRequest actionRequest, PortletPreferences preferences)
515                    throws Exception {
516    
517                    String[] scopeIds = StringUtil.split(
518                            getParameter(actionRequest, "scopeIds"));
519    
520                    preferences.setValues("scopeIds", scopeIds);
521            }
522    
523            protected void setSelectionStyle(
524                            ActionRequest actionRequest, PortletPreferences preferences)
525                    throws Exception {
526    
527                    String selectionStyle = getParameter(actionRequest, "selectionStyle");
528                    String displayStyle = getParameter(actionRequest, "displayStyle");
529    
530                    preferences.setValue("selectionStyle", selectionStyle);
531    
532                    if (selectionStyle.equals("manual") ||
533                            selectionStyle.equals("view-count")) {
534    
535                            preferences.setValue("enableRss", String.valueOf(false));
536                            preferences.setValue("showQueryLogic", Boolean.FALSE.toString());
537    
538                            preferences.reset("rssDelta");
539                            preferences.reset("rssDisplayStyle");
540                            preferences.reset("rssFormat");
541                            preferences.reset("rssName");
542                    }
543    
544                    if (!selectionStyle.equals("view-count") &&
545                            displayStyle.equals("view-count-details")) {
546    
547                            preferences.setValue("displayStyle", "full-content");
548                    }
549            }
550    
551            protected void updateDefaultAssetPublisher(ActionRequest actionRequest)
552                    throws Exception {
553    
554                    boolean defaultAssetPublisher = ParamUtil.getBoolean(
555                            actionRequest, "defaultAssetPublisher");
556    
557                    Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
558    
559                    String portletResource = ParamUtil.getString(
560                            actionRequest, "portletResource");
561    
562                    UnicodeProperties typeSettingsProperties =
563                            layout.getTypeSettingsProperties();
564    
565                    if (defaultAssetPublisher) {
566                            typeSettingsProperties.setProperty(
567                                    LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
568                                    portletResource);
569                    }
570                    else {
571                            String defaultAssetPublisherPortletId =
572                                    typeSettingsProperties.getProperty(
573                                            LayoutTypePortletConstants.
574                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID);
575    
576                            if (Validator.isNotNull(defaultAssetPublisherPortletId) &&
577                                    defaultAssetPublisherPortletId.equals(portletResource)) {
578    
579                                    typeSettingsProperties.setProperty(
580                                            LayoutTypePortletConstants.
581                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
582                                            StringPool.BLANK);
583                            }
584                    }
585    
586                    layout = LayoutLocalServiceUtil.updateLayout(
587                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
588                            layout.getTypeSettings());
589    
590                    if (LayoutStagingUtil.isBranchingLayout(layout)) {
591                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
592                                    actionRequest);
593    
594                            LayoutSetBranch layoutSetBranch =
595                                    LayoutStagingUtil.getLayoutSetBranch(layout.getLayoutSet());
596    
597                            long layoutSetBranchId = layoutSetBranch.getLayoutSetBranchId();
598    
599                            long layoutRevisionId = StagingUtil.getRecentLayoutRevisionId(
600                                    request, layoutSetBranchId, layout.getPlid());
601    
602                            LayoutRevision layoutRevision =
603                                    LayoutRevisionLocalServiceUtil.getLayoutRevision(
604                                            layoutRevisionId);
605    
606                            PortletPreferencesImpl portletPreferences =
607                                    (PortletPreferencesImpl)actionRequest.getPreferences();
608    
609                            if (layoutRevision != null) {
610                                    portletPreferences.setPlid(
611                                            layoutRevision.getLayoutRevisionId());
612                            }
613                    }
614            }
615    
616            protected void updateDisplaySettings(ActionRequest actionRequest)
617                    throws Exception {
618    
619                    String[] classNameIds = StringUtil.split(
620                            getParameter(actionRequest, "classNameIds"));
621                    String[] classTypeIds = getClassTypeIds(actionRequest, classNameIds);
622                    String[] extensions = actionRequest.getParameterValues("extensions");
623                    boolean subtypeFieldsFilterEnabled = getSubtypesFieldsFilterEnabled(
624                            actionRequest, classNameIds);
625    
626                    setPreference(actionRequest, "classNameIds", classNameIds);
627                    setPreference(actionRequest, "classTypeIds", classTypeIds);
628                    setPreference(actionRequest, "extensions", extensions);
629                    setPreference(
630                            actionRequest, "subtypeFieldsFilterEnabled",
631                            String.valueOf(subtypeFieldsFilterEnabled));
632            }
633    
634            protected void updateQueryLogic(
635                            ActionRequest actionRequest, PortletPreferences preferences)
636                    throws Exception {
637    
638                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
639                            WebKeys.THEME_DISPLAY);
640    
641                    long userId = themeDisplay.getUserId();
642                    long groupId = themeDisplay.getSiteGroupId();
643    
644                    int[] queryRulesIndexes = StringUtil.split(
645                            ParamUtil.getString(actionRequest, "queryLogicIndexes"), 0);
646    
647                    int i = 0;
648    
649                    List<AssetQueryRule> queryRules = new ArrayList<AssetQueryRule>();
650    
651                    for (int queryRulesIndex : queryRulesIndexes) {
652                            AssetQueryRule queryRule = getQueryRule(
653                                    actionRequest, queryRulesIndex);
654    
655                            validateQueryRule(userId, groupId, queryRules, queryRule);
656    
657                            queryRules.add(queryRule);
658    
659                            setPreference(
660                                    actionRequest, "queryContains" + i,
661                                    String.valueOf(queryRule.isContains()));
662                            setPreference(
663                                    actionRequest, "queryAndOperator" + i,
664                                    String.valueOf(queryRule.isAndOperator()));
665                            setPreference(actionRequest, "queryName" + i, queryRule.getName());
666                            setPreference(
667                                    actionRequest, "queryValues" + i, queryRule.getValues());
668    
669                            i++;
670                    }
671    
672                    // Clear previous preferences that are now blank
673    
674                    String[] values = preferences.getValues(
675                            "queryValues" + i, new String[0]);
676    
677                    while (values.length > 0) {
678                            setPreference(actionRequest, "queryContains" + i, StringPool.BLANK);
679                            setPreference(
680                                    actionRequest, "queryAndOperator" + i, StringPool.BLANK);
681                            setPreference(actionRequest, "queryName" + i, StringPool.BLANK);
682                            setPreference(actionRequest, "queryValues" + i, new String[0]);
683    
684                            i++;
685    
686                            values = preferences.getValues("queryValues" + i, new String[0]);
687                    }
688            }
689    
690            protected void validateEmailAssetEntryAdded(ActionRequest actionRequest)
691                    throws Exception {
692    
693                    String emailAssetEntryAddedSubject = getLocalizedParameter(
694                            actionRequest, "emailAssetEntryAddedSubject");
695                    String emailAssetEntryAddedBody = getLocalizedParameter(
696                            actionRequest, "emailAssetEntryAddedBody");
697    
698                    if (Validator.isNull(emailAssetEntryAddedSubject)) {
699                            SessionErrors.add(actionRequest, "emailAssetEntryAddedSubject");
700                    }
701                    else if (Validator.isNull(emailAssetEntryAddedBody)) {
702                            SessionErrors.add(actionRequest, "emailAssetEntryAddedBody");
703                    }
704            }
705    
706            protected void validateEmailFrom(ActionRequest actionRequest)
707                    throws Exception {
708    
709                    String emailFromName = getParameter(actionRequest, "emailFromName");
710                    String emailFromAddress = getParameter(
711                            actionRequest, "emailFromAddress");
712    
713                    if (Validator.isNull(emailFromName)) {
714                            SessionErrors.add(actionRequest, "emailFromName");
715                    }
716                    else if (!Validator.isEmailAddress(emailFromAddress) &&
717                                     !Validator.isVariableTerm(emailFromAddress)) {
718    
719                            SessionErrors.add(actionRequest, "emailFromAddress");
720                    }
721            }
722    
723            protected void validateQueryRule(
724                            long userId, long groupId, List<AssetQueryRule> queryRules,
725                            AssetQueryRule queryRule)
726                    throws Exception {
727    
728                    String name = queryRule.getName();
729    
730                    if (name.equals("assetTags")) {
731                            AssetTagLocalServiceUtil.checkTags(
732                                    userId, groupId, queryRule.getValues());
733                    }
734    
735                    if (queryRules.contains(queryRule)) {
736                            throw new DuplicateQueryRuleException(
737                                    queryRule.isContains(), queryRule.isAndOperator(),
738                                    queryRule.getName());
739                    }
740            }
741    
742    }