KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > backup > RestoreAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.webapp.action.core.backup;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.DateUtil;
20 import com.blandware.atleap.service.core.BackupManager;
21 import com.blandware.atleap.service.core.ContentLocaleManager;
22 import com.blandware.atleap.service.core.MailTemplateManager;
23 import com.blandware.atleap.webapp.action.core.BaseAction;
24 import com.blandware.atleap.webapp.util.core.*;
25 import com.blandware.atleap.webapp.menu.MenuRepository;
26 import com.blandware.atleap.search.SearchManager;
27 import org.apache.struts.action.*;
28 import org.apache.struts.util.RequestUtils;
29 import org.springframework.dao.DataAccessException;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.ServletContext JavaDoc;
34 import java.text.ParseException JavaDoc;
35 import java.text.SimpleDateFormat JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.Locale JavaDoc;
38
39 /**
40  * <p>This action restore database data from some backup archive</p>
41  * <p/>
42  * <p><a HREF="RestoreAction.java.htm"><i>View Source</i></a></p>
43  *
44  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
45  * @version $Revision: 1.2 $ $Date: 2006/03/10 17:10:16 $
46  * @struts.action path="/core/backup/restore"
47  * validate="false"
48  * roles="core-backup-restore"
49  * @struts.action-forward name="listArchives"
50  * path="/core/backup/list.do"
51  * redirect="true"
52  * @struts.action-forward name="inputForward"
53  * path="/core/backup/list.do"
54  * @struts.action-forward name="unsatisfiable"
55  * path="/core/backup/list.do"
56  */

57 public class RestoreAction extends BaseAction {
58
59     protected static final SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(DateUtil.getDatePattern(Locale.ENGLISH), Locale.ENGLISH);
60
61     /**
62      * @param mapping The ActionMapping used to select this instance
63      * @param form The optional ActionForm bean for this request (if any)
64      * @param request The HTTP request we are proceeding
65      * @param response The HTTP response we are creating
66      * @return an ActionForward instance describing where and how
67      * control should be forwarded, or null if response
68      * has already been completed
69      */

70     public ActionForward execute(ActionMapping mapping, ActionForm form,
71                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
72
73         String JavaDoc dateString = request.getParameter("date");
74
75         Date JavaDoc date = null;
76         if (dateString != null) {
77             try {
78                 Locale JavaDoc locale = RequestUtils.getUserLocale(request, null);
79                 date = DateUtil.parseDateTime(dateString, locale);
80             } catch(ParseException JavaDoc ex) {
81                 ActionMessages errors = new ActionMessages();
82                 errors.add("cannotRestore", new ActionMessage("core.backup.errors.cannotRestore"));
83                 saveErrors(request, errors);
84                 return mapping.findForward("inputForward");
85             }
86         }
87
88         BackupManager backupManager = (BackupManager) getBean(Constants.BACKUP_MANAGER_BEAN);
89         try {
90             backupManager.restore(date, Boolean.TRUE);
91         } catch(DataAccessException ex) {
92             ActionMessages errors = new ActionMessages();
93             errors.add("cannotRestore", new ActionMessage("core.backup.errors.cannotRestore"));
94             saveErrors(request, errors);
95             return mapping.findForward("inputForward");
96         }
97
98         ServletContext JavaDoc servletContext = getServlet().getServletContext();
99
100         //load content locales into cache
101
ContentLocaleManager contentLocaleManager = (ContentLocaleManager)getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN);
102         contentLocaleManager.initialize();
103         LocaleUtil.getInstance(servletContext).synchronizeCache();
104
105         //initialize menu repository and replace it in the servlet context
106
MenuRepository menuRepository = new MenuUtil(request).initializeMenuRepository();
107         servletContext.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, menuRepository);
108
109         //global properties
110
GlobalProperties.getInstance(servletContext).synchronizeCache();
111
112         //mail templates
113
MailTemplateManager mailTemplateManager = (MailTemplateManager) getBean(Constants.MAIL_TEMPLATE_MANAGER_BEAN);
114         mailTemplateManager.initialize();
115
116         // initialize application resources
117
ApplicationResources.getInstance(servletContext).loadResources();
118         
119         // flush all cache
120
CacheUtil.getInstance(request).flushAllCache();
121
122         // reindex all entities
123
SearchManager.getInstance(servlet.getServletContext()).reIndexAll(request);
124
125         return mapping.findForward("listArchives");
126     }
127 }
128
Popular Tags