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.importer;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.hibernate.FlushMode;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.beans.factory.annotation.Required;
33  
34  import com.gisgraphy.domain.geoloc.entity.Language;
35  import com.gisgraphy.domain.repository.ILanguageDao;
36  import com.gisgraphy.domain.valueobject.NameValueDTO;
37  
38  /**
39   * Import the languages.
40   * 
41   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
42   */
43  public class GeonamesLanguageImporter extends AbstractSimpleImporterProcessor {
44  	
45  	protected static final Logger logger = LoggerFactory.getLogger(GeonamesLanguageImporter.class);
46  
47      private ILanguageDao languageDao;
48  
49      /**
50       * Default constructor
51       */
52      public GeonamesLanguageImporter() {
53  	super();
54      }
55  
56      /*
57       * (non-Javadoc)
58       * 
59       * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#processData(java.lang.String)
60       */
61      @Override
62      protected void processData(String line) {
63  	String[] fields = line.split("\t");
64  
65  	/*
66  	 * line table has the following fields :
67  	 * --------------------------------------------------- 0 ISO 639-3 1 ISO
68  	 * 639-2 (unused here) 2 ISO 639-1 3 Language Name
69  	 */
70  	// checkNumberOfColumn(fields);
71  	Language lang = new Language();
72  	String iso639Alpha3LanguageCode;
73  	if (!isEmptyField(fields, 0, false)) {
74  	    iso639Alpha3LanguageCode = fields[0].toUpperCase();
75  	} else if (!isEmptyField(fields, 1, false)) {
76  	    iso639Alpha3LanguageCode = fields[1].toUpperCase();
77  	} else {
78  	    throw new MissingRequiredFieldException("ISO 639-3 and ISO 639-2 are both empty for : "+dumpFields(fields));
79  	}
80  	lang.setIso639Alpha3LanguageCode(iso639Alpha3LanguageCode);
81  
82  	if (!isEmptyField(fields, 2, false)) {
83  	    lang.setIso639Alpha2LanguageCode(fields[2].toUpperCase());
84  	}
85  
86  	if (!isEmptyField(fields, 3, true)) {
87  	    lang.setIso639LanguageName(fields[3]);
88  	}
89  
90  	if (this.languageDao.getByIso639Alpha3Code(iso639Alpha3LanguageCode)==null){
91  	    this.languageDao.save(lang);
92  	} else {
93  	    logger.warn("language "+iso639Alpha3LanguageCode + " is already present...we ignore the line");
94  	}
95  
96      }
97  
98      /* (non-Javadoc)
99       * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldBeSkiped()
100      */
101     @Override
102     public boolean shouldBeSkipped() {
103     	//should never be skiped
104     	return false;
105     }
106     
107     @Override
108     protected void onFileProcessed(File file) {
109     	//we don't want to rename files so we overide the method
110     }
111     
112     /*
113      * (non-Javadoc)
114      * 
115      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreFirstLine()
116      */
117     @Override
118     protected boolean shouldIgnoreFirstLine() {
119 	return true;
120     }
121 
122     /*
123      * (non-Javadoc)
124      * 
125      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreComments()
126      */
127     @Override
128     protected boolean shouldIgnoreComments() {
129 	return true;
130     }
131 
132     /*
133      * (non-Javadoc)
134      * 
135      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#setCommitFlushMode()
136      */
137     @Override
138     protected void setCommitFlushMode() {
139 	this.languageDao.setFlushMode(FlushMode.COMMIT);
140 
141     }
142 
143     /*
144      * (non-Javadoc)
145      * 
146      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#flushAndClear()
147      */
148     @Override
149     protected void flushAndClear() {
150 	this.languageDao.flushAndClear();
151     }
152 
153     /*
154      * (non-Javadoc)
155      * 
156      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getNumberOfColumns()
157      */
158     @Override
159     protected int getNumberOfColumns() {
160 	return 4;
161     }
162 
163     /**
164      * @param languageDao
165      *                the languageDao to set
166      */
167     @Required
168     public void setLanguageDao(ILanguageDao languageDao) {
169 	this.languageDao = languageDao;
170     }
171 
172     /*
173      * (non-Javadoc)
174      * 
175      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getFiles()
176      */
177     @Override
178     protected File[] getFiles() {
179 	File[] files = new File[1];
180 	files[0] = new File(importerConfig.getGeonamesDir()
181 		+ importerConfig.getLanguageFileName());
182 	return files;
183     }
184 
185     /*
186      * (non-Javadoc)
187      * 
188      * @see com.gisgraphy.domain.geoloc.importer.IGeonamesProcessor#rollback()
189      */
190     public List<NameValueDTO<Integer>> rollback() {
191 	List<NameValueDTO<Integer>> deletedObjectInfo = new ArrayList<NameValueDTO<Integer>>();
192 	logger.info("deleting languages...");
193 	int deletedlang = languageDao.deleteAll();
194 	if (deletedlang != 0) {
195 	    deletedObjectInfo.add(new NameValueDTO<Integer>(Language.class
196 		    .getSimpleName(), deletedlang));
197 	}
198 	logger.info(deletedlang + " languages have been deleted");
199 	resetStatus();
200 	return deletedObjectInfo;
201     }
202 
203 }