KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > planet > 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.roller.presentation.planet;
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.struts.action.ActionError;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33 import org.apache.struts.action.ActionMessages;
34 import org.apache.struts.actions.DispatchAction;
35 import org.roller.presentation.forms.PlanetConfigForm;
36 import org.roller.config.RollerRuntimeConfig;
37 import org.roller.model.PlanetManager;
38 import org.roller.model.Roller;
39 import org.roller.pojos.PlanetConfigData;
40 import org.roller.pojos.PlanetGroupData;
41 import org.roller.presentation.RollerRequest;
42 import org.roller.presentation.planet.RefreshEntriesTask;
43 import org.roller.presentation.planet.SyncWebsitesTask;
44
45 /////////////////////////////////////////////////////////////////////////////
46
/**
47  * Allows configuration of Planet Roller.
48  *
49  * @struts.action name="planetConfigForm" path="/admin/planetConfig"
50  * scope="request" parameter="method"
51  *
52  * @struts.action-forward name="planetConfig.page"
53  * path="/planet/PlanetConfig.jsp"
54  */

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