1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.taggedcontent.action;
24  
25  import com.liferay.portal.kernel.portlet.ConfigurationAction;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portlet.PortletPreferencesFactoryUtil;
30  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
31  import com.liferay.util.servlet.SessionErrors;
32  import com.liferay.util.servlet.SessionMessages;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.PortletPreferences;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  /**
42   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class ConfigurationActionImpl implements ConfigurationAction {
48  
49      public void processAction(
50              PortletConfig config, ActionRequest req, ActionResponse res)
51          throws Exception {
52  
53          String cmd = ParamUtil.getString(req, Constants.CMD);
54  
55          String portletResource = ParamUtil.getString(req, "portletResource");
56  
57          PortletPreferences prefs =
58              PortletPreferencesFactoryUtil.getPortletSetup(
59                  req, portletResource, true, true);
60  
61          if (cmd.equals("add-selection")) {
62              AssetPublisherUtil.addSelection(req, prefs);
63          }
64          else if (cmd.equals("move-selection-down")) {
65              moveSelectionDown(req, prefs);
66          }
67          else if (cmd.equals("move-selection-up")) {
68              moveSelectionUp(req, prefs);
69          }
70          else if (cmd.equals("remove-selection")) {
71              removeSelection(req, prefs);
72          }
73          else if (cmd.equals("selection-style")) {
74              setSelectionStyle(req, prefs);
75          }
76          else if (cmd.equals(Constants.UPDATE)) {
77              String selectionStyle = prefs.getValue(
78                  "selection-style", "dynamic");
79  
80              if (selectionStyle.equals("dynamic")) {
81                  updateDynamicSettings(req, prefs);
82              }
83              else if (selectionStyle.equals("manual")) {
84                  updateManualSettings(req, prefs);
85              }
86          }
87  
88          if (SessionErrors.isEmpty(req)) {
89              prefs.store();
90  
91              SessionMessages.add(req, config.getPortletName() + ".doConfigure");
92          }
93      }
94  
95      public String render(
96              PortletConfig config, RenderRequest req, RenderResponse res)
97          throws Exception {
98  
99          return "/html/portlet/tagged_content/configuration.jsp";
100     }
101 
102     protected void moveSelectionDown(
103             ActionRequest req, PortletPreferences prefs)
104         throws Exception {
105 
106         int assetOrder = ParamUtil.getInteger(req, "assetOrder");
107 
108         String[] manualEntries = prefs.getValues(
109             "manual-entries", new String[0]);
110 
111         if ((assetOrder >= (manualEntries.length - 1)) || (assetOrder < 0)) {
112             return;
113         }
114 
115         String temp = manualEntries[assetOrder + 1];
116 
117         manualEntries[assetOrder + 1] = manualEntries[assetOrder];
118         manualEntries[assetOrder] = temp;
119 
120         prefs.setValues("manual-entries", manualEntries);
121     }
122 
123     protected void moveSelectionUp(ActionRequest req, PortletPreferences prefs)
124         throws Exception {
125 
126         int assetOrder = ParamUtil.getInteger(req, "assetOrder");
127 
128         String[] manualEntries = prefs.getValues(
129             "manual-entries", new String[0]);
130 
131         if ((assetOrder >= manualEntries.length) || (assetOrder <= 0)) {
132             return;
133         }
134 
135         String temp = manualEntries[assetOrder - 1];
136 
137         manualEntries[assetOrder - 1] = manualEntries[assetOrder];
138         manualEntries[assetOrder] = temp;
139 
140         prefs.setValues("manual-entries", manualEntries);
141     }
142 
143     protected void removeSelection(ActionRequest req, PortletPreferences prefs)
144         throws Exception {
145 
146         int assetOrder = ParamUtil.getInteger(req, "assetOrder");
147 
148         String[] manualEntries = prefs.getValues(
149             "manual-entries", new String[0]);
150 
151         if (assetOrder >= manualEntries.length) {
152             return;
153         }
154 
155         String[] newEntries = new String[manualEntries.length -1];
156 
157         int i = 0;
158         int j = 0;
159 
160         for (; i < manualEntries.length; i++) {
161             if (i != assetOrder) {
162                 newEntries[j++] = manualEntries[i];
163             }
164         }
165 
166         prefs.setValues("manual-entries", newEntries);
167     }
168 
169     protected void setSelectionStyle(
170             ActionRequest req, PortletPreferences prefs)
171         throws Exception {
172 
173         String selectionStyle = ParamUtil.getString(req, "selectionStyle");
174 
175         prefs.setValue("selection-style", selectionStyle);
176 
177         if (selectionStyle.equals("manual")) {
178             prefs.setValue("show-query-logic", String.valueOf(false));
179         }
180     }
181 
182     protected void updateDynamicSettings(
183             ActionRequest req, PortletPreferences prefs)
184         throws Exception {
185 
186         String[] entries = StringUtil.split(
187             ParamUtil.getString(req, "entries"));
188         String[] notEntries = StringUtil.split(
189             ParamUtil.getString(req, "notEntries"));
190         boolean andOperator = ParamUtil.getBoolean(req, "andOperator");
191 
192         String category = ParamUtil.getString(req, "category");
193         String displayStyle = ParamUtil.getString(req, "displayStyle");
194         boolean showQueryLogic = ParamUtil.getBoolean(req, "showQueryLogic");
195         boolean showAvailableLocales = ParamUtil.getBoolean(
196             req, "showAvailableLocales");
197 
198         prefs.setValues("entries", entries);
199         prefs.setValues("not-entries", notEntries);
200         prefs.setValue("and-operator", String.valueOf(andOperator));
201 
202         prefs.setValue("category", category);
203         prefs.setValue("display-style", displayStyle);
204         prefs.setValue("show-query-logic", String.valueOf(showQueryLogic));
205         prefs.setValue(
206             "show-available-locales", String.valueOf(showAvailableLocales));
207     }
208 
209     protected void updateManualSettings(
210             ActionRequest req, PortletPreferences prefs)
211         throws Exception {
212 
213         String displayStyle = ParamUtil.getString(req, "displayStyle");
214         boolean showAvailableLocales = ParamUtil.getBoolean(
215             req, "showAvailableLocales");
216 
217         prefs.setValue("display-style", displayStyle);
218         prefs.setValue(
219             "show-available-locales", String.valueOf(showAvailableLocales));
220     }
221 
222 }