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.List;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.beans.factory.annotation.Required;
30  
31  import com.gisgraphy.domain.valueobject.ImporterStatus;
32  import com.gisgraphy.domain.valueobject.ImporterStatusDto;
33  import com.gisgraphy.helper.StringHelper;
34  import com.gisgraphy.importer.IImporterManager;
35  import com.gisgraphy.importer.ImporterManager;
36  import com.gisgraphy.importer.ImporterMetaDataException;
37  import com.opensymphony.xwork2.Action;
38  import com.opensymphony.xwork2.ActionSupport;
39  
40  /**
41   * Action that do the all import
42   * 
43   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
44   * @see ImporterManager
45   */
46  public class ImportAction extends ActionSupport {
47      /**
48       * 
49       */
50      private static final long serialVersionUID = -1459641055753044129L;
51  
52      private static Logger logger = LoggerFactory.getLogger(ImportAction.class);
53  
54      public static String WAIT = "wait";
55      
56      private String errorMessage ="";
57      
58  
59      private IImporterManager importerManager;
60      
61      public String getImportFormatedTimeElapsed(){
62  	return importerManager.getFormatedTimeElapsed();
63      }
64      
65      public boolean isImportInProgress(){
66  	return importerManager.isInProgress();
67      }
68  
69      
70      public boolean isImportAlreadyDone() throws ImporterMetaDataException{
71  	return importerManager.isAlreadyDone();
72      }
73  
74      /**
75       * Run import if not in progress or already done, otherwise return Wait view
76       * 
77       * @return 'success'
78       * @throws Exception
79       *                 When errors occurred
80       */
81      public String doImport()  {
82  	boolean alreadyDone;
83  	try {
84  	    alreadyDone = isImportAlreadyDone();
85  	} catch (ImporterMetaDataException e) {
86  	   errorMessage = e.getMessage();
87  	   return ERROR;
88  	}
89  	if (importerManager.isInProgress() || alreadyDone) {
90  	    return WAIT;
91  	}
92  	ImportAction.logger.info("doImport");
93  	this.importerManager.importAll();
94  	return Action.SUCCESS;
95      }
96  
97      
98      /**
99       * @return 'Wait'
100      * @throws Exception
101      *                 if errors occurred
102      */
103     public String status() throws Exception {
104 	try {
105 	    //we check if we can determine if import is already done
106 	    isImportAlreadyDone();
107 	} catch (ImporterMetaDataException e) {
108 	   errorMessage = e.getMessage();
109 	   return ERROR;
110 	}
111 	ImportAction.logger.debug("do status");
112 	return WAIT;
113     }
114 
115     /*
116      * (non-Javadoc)
117      * 
118      * @see com.opensymphony.xwork2.ActionSupport#execute()
119      */
120     @Override
121     public String execute() throws Exception {
122 	return this.doImport();
123     }
124     
125     public  ImporterStatus[] getStatusEnumList(){
126 	return ImporterStatus.values();
127     }
128 
129     /**
130      * @param importerManager
131      *                the importerManager to set
132      */
133     @Required
134     public void setImporterManager(IImporterManager importerManager) {
135 	this.importerManager = importerManager;
136     }
137 
138     /**
139      * @return the importerStatusDtoList
140      */
141     public List<ImporterStatusDto> getImporterStatusDtoList() {
142 	return importerManager.getStatusDtoList();
143     }
144     
145     /**
146      * @param string a CamelCaseString
147      * @return a human readable String 
148      */
149     public String  splitCamelCase(String string){
150 	return StringHelper.splitCamelCase(string);
151     }
152 
153     /**
154      * @return the importerManager
155      */
156     @Required
157     public IImporterManager getImporterManager() {
158 	return importerManager;
159     }
160 
161 
162     public String getErrorMessage() {
163         return errorMessage;
164     }
165 
166 }