KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > PlanetConfigAction


1 /*
2  * Copyright 2005 Sun Microsystems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.roller.ui.authoring.struts.actions;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.ui.admin.struts.actions.*;
28 import org.apache.roller.ui.core.tasks.*;
29 import org.apache.roller.ui.core.tasks.RefreshEntriesTask;
30 import org.apache.roller.ui.core.tasks.SyncWebsitesTask;
31 import org.apache.struts.action.ActionError;
32 import org.apache.struts.action.ActionErrors;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37 import org.apache.struts.action.ActionMessages;
38 import org.apache.struts.actions.DispatchAction;
39 import org.apache.roller.config.RollerRuntimeConfig;
40 import org.apache.roller.model.PlanetManager;
41 import org.apache.roller.model.Roller;
42 import org.apache.roller.model.RollerFactory;
43 import org.apache.roller.pojos.PlanetConfigData;
44 import org.apache.roller.pojos.PlanetGroupData;
45 import org.apache.roller.ui.core.BasePageModel;
46 import org.apache.roller.ui.core.RollerRequest;
47 import org.apache.roller.ui.core.RollerSession;
48 import org.apache.roller.ui.authoring.struts.forms.PlanetConfigForm;
49
50 /////////////////////////////////////////////////////////////////////////////
51
/**
52  * Allows configuration of Planet Roller.
53  *
54  * @struts.action name="planetConfigForm" path="/roller-ui/admin/planetConfig"
55  * scope="request" parameter="method"
56  *
57  * @struts.action-forward name="planetConfig.page"
58  * path=".PlanetConfig"
59  */

60 public final class PlanetConfigAction extends DispatchAction
61 {
62     private static Log logger =
63         LogFactory.getFactory().getInstance(PlanetConfigAction.class);
64
65     /** Populate config form and forward to config page */
66     public ActionForward getConfig(ActionMapping mapping,
67             ActionForm actionForm, HttpServletRequest JavaDoc request,
68             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
69     {
70         ActionForward forward = mapping.findForward("planetConfig.page");
71         try
72         {
73             RollerRequest rreq = RollerRequest.getRollerRequest(request);
74             if (RollerSession.getRollerSession(request).isGlobalAdminUser())
75             {
76                 BasePageModel pageModel = new BasePageModel(
77                     "planetConfig.pageTitle", request, response, mapping);
78                 request.setAttribute("model",pageModel);
79                 Roller roller = RollerFactory.getRoller();
80                 PlanetManager planet = roller.getPlanetManager();
81                 PlanetConfigData config = planet.getConfiguration();
82                 PlanetConfigForm form = (PlanetConfigForm)actionForm;
83                 if (config != null)
84                 {
85                     form.copyFrom(config, request.getLocale());
86                 }
87                 else
88                 {
89                     form.setTitle("Planet Roller");
90                     form.setAdminEmail(RollerRuntimeConfig.getProperty("site.adminemail"));
91                     form.setSiteURL(RollerRuntimeConfig.getProperty("site.absoluteurl"));
92                     form.setCacheDir("/tmp");
93                 }
94             }
95             else
96             {
97                 forward = mapping.findForward("access-denied");
98             }
99         }
100         catch (Exception JavaDoc e)
101         {
102             request.getSession().getServletContext().log("ERROR", e);
103             throw new ServletException JavaDoc(e);
104         }
105         return forward;
106     }
107
108     /** Save posted config form data */
109     public ActionForward saveConfig(ActionMapping mapping,
110             ActionForm actionForm, HttpServletRequest JavaDoc request,
111             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
112     {
113         ActionForward forward = mapping.findForward("planetConfig.page");
114         try
115         {
116             RollerRequest rreq = RollerRequest.getRollerRequest(request);
117             if (RollerSession.getRollerSession(request).isGlobalAdminUser())
118             {
119                 BasePageModel pageModel = new BasePageModel(
120                     "planetConfig.pageTitle", request, response, mapping);
121                 request.setAttribute("model",pageModel);
122                 Roller roller = RollerFactory.getRoller();
123                 PlanetManager planet = roller.getPlanetManager();
124                 PlanetConfigData config = planet.getConfiguration();
125                 if (config == null)
126                 {
127                     config = new PlanetConfigData();
128                 }
129                 PlanetConfigForm form = (PlanetConfigForm) actionForm;
130                 ActionErrors errors = validate(form);
131                 if (errors.isEmpty())
132                 {
133                     form.copyTo(config, request.getLocale());
134                     planet.saveConfiguration(config);
135                     if (planet.getGroup("external") == null)
136                     {
137                         PlanetGroupData group = new PlanetGroupData();
138                         group.setHandle("external");
139                         group.setTitle("external");
140                         planet.saveGroup(group);
141                     }
142                     roller.flush();
143                     ActionMessages messages = new ActionMessages();
144                     messages.add(null, new ActionMessage("planetConfig.success.saved"));
145                     saveMessages(request, messages);
146                 }
147                 else
148                 {
149                     saveErrors(request, errors);
150                 }
151             }
152             else
153             {
154                 forward = mapping.findForward("access-denied");
155             }
156         }
157         catch (Exception JavaDoc e)
158         {
159             request.getSession().getServletContext().log("ERROR", e);
160             throw new ServletException JavaDoc(e);
161         }
162         return forward;
163     }
164
165     /** Refresh entries in backgrounded thread (for testing) */
166     public ActionForward refreshEntries(ActionMapping mapping,
167             ActionForm actionForm, HttpServletRequest JavaDoc request,
168             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
169     {
170         ActionForward forward = mapping.findForward("planetConfig.page");
171         try
172         {
173             RollerRequest rreq = RollerRequest.getRollerRequest(request);
174             if (RollerSession.getRollerSession(request).isGlobalAdminUser())
175             {
176                 BasePageModel pageModel = new BasePageModel(
177                     "planetConfig.pageTitle", request, response, mapping);
178                 request.setAttribute("model",pageModel);
179                 Roller roller = RollerFactory.getRoller();
180                 RefreshEntriesTask task = new RefreshEntriesTask();
181                 task.init(roller, "dummy");
182                 roller.getThreadManager().executeInBackground(task);
183                 
184                 ActionMessages messages = new ActionMessages();
185                 messages.add(null,
186                         new ActionMessage("planetConfig.success.refreshed"));
187                 saveMessages(request, messages);
188             }
189             else
190             {
191                 forward = mapping.findForward("access-denied");
192             }
193         }
194         catch (Exception JavaDoc e)
195         {
196             request.getSession().getServletContext().log("ERROR", e);
197             throw new ServletException JavaDoc(e);
198         }
199         return forward;
200     }
201
202     /** Sync websites in backgrounded thread (for testing) */
203     public ActionForward syncWebsites(ActionMapping mapping,
204             ActionForm actionForm, HttpServletRequest JavaDoc request,
205             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
206     {
207         ActionForward forward = mapping.findForward("planetConfig.page");
208         try
209         {
210             RollerRequest rreq = RollerRequest.getRollerRequest(request);
211             if (RollerSession.getRollerSession(request).isGlobalAdminUser())
212             {
213                 BasePageModel pageModel = new BasePageModel(
214                     "planetConfig.pageTitle", request, response, mapping);
215                 request.setAttribute("model",pageModel);
216                 Roller roller = (Roller)RollerFactory.getRoller();
217                 SyncWebsitesTask task = new SyncWebsitesTask();
218                 task.init(roller, "dummy");
219                 roller.getThreadManager().executeInBackground(task);
220                 ActionMessages messages = new ActionMessages();
221                 messages.add(null,
222                         new ActionMessage("planetConfig.success.synced"));
223                 saveMessages(request, messages);
224             }
225             else
226             {
227                 forward = mapping.findForward("access-denied");
228             }
229         }
230         catch (Exception JavaDoc e)
231         {
232             request.getSession().getServletContext().log("ERROR", e);
233             throw new ServletException JavaDoc(e);
234         }
235         return forward;
236     }
237     
238     /** Validate config form, returns empty collection if all OK */
239     public ActionErrors validate(PlanetConfigForm form)
240     {
241         ActionErrors errors = new ActionErrors();
242         if (form.getProxyHost()!=null && form.getProxyHost().trim().length()>0)
243         {
244             if (form.getProxyPort()<1)
245             {
246                 errors.add(null, new ActionError(
247                         "planetConfig.error.badProxyPort"));
248             }
249         }
250         return errors;
251     }
252 }
253
254
Popular Tags