View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.action;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.struts2.ServletActionContext;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.factory.annotation.Required;
32  
33  import com.gisgraphy.importer.IImporterManager;
34  import com.gisgraphy.importer.ImporterManager;
35  import com.opensymphony.xwork2.ActionSupport;
36  
37  /**
38   * Action that reset the import. SO USE THIS ACTION VERY CAREFULLY. The reset
39   * will be launched only if the confirm attribute in session is equals to true
40   * 
41   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
42   * @see ImporterManager
43   */
44  public class ResetImportAction extends ActionSupport {
45  
46      private static final String RESET_IMPORT_CONFIRM = "RESET_IMPORT_CONFIRM";
47  
48      /**
49       * Default serial ID
50       */
51      private static final long serialVersionUID = -259250843779247710L;
52  
53      private static Logger logger = LoggerFactory
54  	    .getLogger(ResetImportAction.class);
55  
56      private IImporterManager importerManager;
57  
58      private List<String> errorsAndWarningMessages = new ArrayList<String>();
59  
60      private boolean resetFailed = false;
61  
62      private String failedMessage;
63  
64      /**
65       * view of the page that ask for confirmation
66       */
67      public static String ASK = "ask";
68  
69      /**
70       * view of page that gives information after the reset
71       */
72      public static String RESET = "reset";
73  
74      /**
75       * view of page that gives information after the reset
76       */
77      public static String IMPORT_IN_PROGRESS = "impossible";
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see com.opensymphony.xwork2.ActionSupport#execute()
83       */
84      @Override
85      public String execute() throws Exception {
86  	logger.info("the page for reseting the import has been called");
87  	if (importerManager.isInProgress()){
88      	logger.info("can not reset the import because it is in progress");
89      	return IMPORT_IN_PROGRESS;
90      }
91  	return ASK;
92      }
93  
94      /**
95       * reset the import, only if {@linkplain #RESET_IMPORT_CONFIRM} is equals to
96       * true
97       * 
98       * @see #confirm()
99       * @return the reset view
100      */
101     public String reset() {
102     if (importerManager.isInProgress()){
103     	logger.info("can not reset the import because it is in progress");
104     	return IMPORT_IN_PROGRESS;
105     }
106 	if (isConfirmed()) {
107 	    logger.info("Reseting the import");
108 	    try {
109 		errorsAndWarningMessages = importerManager.resetImport();
110 	    } catch (Exception e) {
111 		resetFailed = true;
112 		failedMessage = e.getMessage()==null?e.toString():e.getMessage();
113 		logger.warn("The reset has failed : " + e.getMessage() +"due to "+e.getStackTrace());
114 	    }
115 	    unconfirm();
116 	    return RESET;
117 	} else {
118 	    logger
119 		    .info("Can not reset the import, because confirm is not true");
120 	    return ASK;
121 	}
122 
123     }
124 
125     public boolean isConfirmed() {
126 	Object confirmed = ServletActionContext.getRequest().getSession()
127 		.getAttribute(RESET_IMPORT_CONFIRM);
128 	logger.info("RESET_IMPORT_CONFIRM=" + confirmed + " in session");
129 	return confirmed == null ? false : ((Boolean) confirmed).booleanValue();
130     }
131 
132     /**
133      * set the {@linkplain #RESET_IMPORT_CONFIRM} in session to true
134      * 
135      * @return the reset view
136      */
137     public String confirm() {
138 	ServletActionContext.getRequest().getSession().setAttribute(
139 		RESET_IMPORT_CONFIRM, Boolean.TRUE);
140 	logger
141 		.info("Confirm has been set to true, the reset of the import will be possible");
142 	return ASK;
143     }
144 
145     /**
146      * remove the {@linkplain #RESET_IMPORT_CONFIRM} of the session
147      */
148     public void unconfirm() {
149 	ServletActionContext.getRequest().getSession().removeAttribute(
150 		RESET_IMPORT_CONFIRM);
151     }
152 
153     /**
154      * @param importerManager
155      *                the importerManager to set
156      */
157     @Required
158     public void setImporterManager(IImporterManager importerManager) {
159 	this.importerManager = importerManager;
160     }
161 
162     /**
163      * @return the deletedObjectInfo
164      */
165     public List<String> getErrorsAndWarningMessages() {
166 	return errorsAndWarningMessages;
167     }
168 
169     /**
170      * @return true if the reset has failed
171      */
172     public boolean isResetFailed() {
173 	return resetFailed;
174     }
175 
176     /**
177      * @return the failedMessage
178      */
179     public String getFailedMessage() {
180 	return failedMessage;
181     }
182 
183 }