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.domain.valueobject;
24  
25  import com.gisgraphy.importer.IImporterProcessor;
26  
27  /**
28   * Represents a status of an IgeonamesProcessor
29   * 
30   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
31   * @see ImporterStatus
32   */
33  public class ImporterStatusDto {
34  
35      private static final int NUMBER_OF_FIELDS = 7;
36      /**
37       * The csv line separator
38       * 
39       * @see #toCSV()
40       */
41      public static final String CSV_LINE_SEPARATOR = "\r\n";
42      /**
43       * The csv field separator (we have chosen a very improbable one)
44       * 
45       * @see #toCSV()
46       */
47      public static final String CSV_FIELD_SEPARATOR = "ŝ";
48      /**
49       * the default value of the currentFile if it is null
50       */
51      public static final String DEFAULT_CURRENT_FILE = " any file ";
52      
53      private String processorName = "";
54      private String currentFileName = DEFAULT_CURRENT_FILE;
55      private long currentLine = 0;
56      private long numberOfLinelefts = 0;
57      private long numberOfLineToProcess = 0;
58      private long numberOfLineProcessed = 0;
59      private int percent = 0;
60      private String statusMessage = "";
61      private ImporterStatus status = ImporterStatus.UNKNOW;
62  
63      /**
64       * @param processorName
65       *                The name of the processor (typically the className)
66       * @param currentFileName
67       *                wich file is currently processed
68       * @param currentLine
69       *                which line of the currentFileName is processed
70       * @param numberOfLineToProcess
71       *                the total of line to be process by the importer
72       * @param numberOfLineProcessed
73       *                The total of line already process by this importer
74       * @param statusMessage
75       *                a message
76       * @param status
77       */
78      public ImporterStatusDto(String processorName, String currentFileName,
79  	    long currentLine, long numberOfLineToProcess,
80  	    long numberOfLineProcessed, String statusMessage,
81  	    ImporterStatus status) {
82  	super();
83  	this.processorName = processorName;
84  	setCurrentFileName(currentFileName);
85  	this.currentLine = currentLine;
86  	this.numberOfLineToProcess = numberOfLineToProcess;
87  	this.numberOfLineProcessed = numberOfLineProcessed;
88  	this.statusMessage = statusMessage;
89  	this.status = status;
90  	calculateFields();
91      }
92  
93  
94      public ImporterStatusDto(IImporterProcessor processor) {
95  	this.processorName = processor.getClass().getSimpleName();
96  	setCurrentFileName(processor.getCurrentFileName());
97  
98  	this.status = processor.getStatus();
99  	this.statusMessage = processor.getStatusMessage();
100 	this.currentLine = processor.getReadFileLine();
101 	this.numberOfLineToProcess = processor.getNumberOfLinesToProcess();
102 	this.numberOfLineProcessed = processor.getTotalReadLine();
103 	calculateFields();
104     }
105 
106     /**
107      * Construct a {@linkplain ImporterStatusDto} from a csv line
108      * 
109      * @param csv
110      *                the String that represent the
111      *                {@linkplain ImporterStatusDto}
112      */
113     public ImporterStatusDto(String csv) {
114 	if (csv.endsWith(CSV_LINE_SEPARATOR)) {
115 	    // it is not a csv from a readline
116 	    csv = csv.substring(0, csv.length() - CSV_LINE_SEPARATOR.length());
117 	}
118 	String[] fields = csv.split(CSV_FIELD_SEPARATOR);
119 	if (fields.length != NUMBER_OF_FIELDS) {
120 	    throw new IllegalArgumentException("CSV must have "
121 		    + NUMBER_OF_FIELDS + " fields");
122 	}
123 	this.processorName = fields[0];
124 	setCurrentFileName(fields[1]);
125 	this.currentLine = Long.valueOf(fields[2]);
126 	this.numberOfLineToProcess = Long.valueOf(fields[3]);
127 	this.numberOfLineProcessed = Long.valueOf(fields[4]);
128 	this.statusMessage = fields[5];
129 	try {
130 	    this.status = ImporterStatus.valueOf(fields[6]);
131 	} catch (Exception e) {
132 	    
133 	   
134 	}
135 	calculateFields();
136     }
137 
138     private void calculateFields() {
139     	if (numberOfLineToProcess <0 || numberOfLineProcessed < 0){
140     		status=ImporterStatus.UNKNOW;//todo test
141     		this.numberOfLinelefts = 0;
142     		this.percent = 0;
143     		return;
144     	}
145 	this.numberOfLinelefts = (this.numberOfLineToProcess - this.numberOfLineProcessed);
146 	if (numberOfLineToProcess != 0) {
147 	    this.percent = new Long((numberOfLineProcessed * 100)
148 		    / numberOfLineToProcess).intValue();
149 	}
150 	else if (numberOfLineProcessed==0 && (status == ImporterStatus.PROCESSED || status == ImporterStatus.PROCESSING )){
151 	    percent = 100;
152 	}
153     }
154 
155     /**
156      * @return the processorName
157      */
158     public String getProcessorName() {
159 	return processorName;
160     }
161 
162     /**
163      * @return the currentFile
164      */
165     public String getCurrentFileName() {
166 	return currentFileName;
167     }
168 
169     /**
170      * @return the currentLine
171      */
172     public long getCurrentLine() {
173 	return currentLine;
174     }
175 
176     /**
177      * @return the numberOfLineToProcess
178      */
179     public long getNumberOfLineToProcess() {
180 	return numberOfLineToProcess;
181     }
182 
183     /**
184      * @return the numberOfLineProcessed
185      */
186     public long getNumberOfLineProcessed() {
187 	return numberOfLineProcessed;
188     }
189 
190     /**
191      * @return the percent
192      */
193     public int getPercent() {
194 	return percent;
195     }
196 
197     /**
198      * @return the status
199      */
200     public ImporterStatus getStatus() {
201 	return status;
202     }
203 
204     /**
205      * @return the numberOfLinelefts
206      */
207     public long getNumberOfLinelefts() {
208 	return numberOfLinelefts;
209     }
210 
211     /**
212      * @return the errorMessage
213      */
214     public String getStatusMessage() {
215 	return statusMessage;
216     }
217 
218     /**
219      * @param currentFileName
220      *                the currentFileName to set, the CurrentFileName will be
221      *                set to {@link #DEFAULT_CURRENT_FILE} if the argument is
222      *                null
223      */
224     private void setCurrentFileName(String currentFileName) {
225 	if (currentFileName != null) {
226 	    this.currentFileName = currentFileName;
227 	} else {
228 	    this.currentFileName = DEFAULT_CURRENT_FILE;
229 	}
230     }
231 
232     public String toCSV() {
233 	StringBuffer sb = new StringBuffer();
234 	return sb.append(this.processorName).append(CSV_FIELD_SEPARATOR)
235 		.append(this.currentFileName).append(CSV_FIELD_SEPARATOR)
236 		.append(this.currentLine).append(CSV_FIELD_SEPARATOR).append(
237 			this.numberOfLineToProcess).append(CSV_FIELD_SEPARATOR)
238 		.append(this.numberOfLineProcessed).append(CSV_FIELD_SEPARATOR)
239 		.append(this.statusMessage).append(CSV_FIELD_SEPARATOR).append(
240 			status.name()).append(CSV_LINE_SEPARATOR).toString();
241     }
242 
243 }