View Javadoc

1   /**
2   * Copyright (C) 2007 EDIT
3   * European Distributed Institute of Taxonomy 
4   * http://www.e-taxonomy.eu
5   * 
6   * The contents of this file are subject to the Mozilla Public License Version 1.1
7   * See LICENSE.TXT at the top of this package for the full license terms.
8   */
9   
10  package eu.etaxonomy.cdm.io.specimen;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  
15  import org.apache.commons.lang.StringUtils;
16  
17  import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
18  import eu.etaxonomy.cdm.api.service.IOccurrenceService;
19  import eu.etaxonomy.cdm.model.location.NamedArea;
20  import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
21  import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
22  
23  /**
24   * @author p.kelbert
25   * @created 20.10.2008
26   * @version 1.0
27   */
28  public class UnitsGatheringArea {
29  
30  	private NamedArea area = NamedArea.NewInstance();
31  	private ArrayList<NamedArea> areas = new ArrayList<NamedArea>();
32  
33  
34  	/*
35  	 * Constructor
36  	 * Set/create country
37  	 * @param isoCountry (try to used the isocode first)
38  	 * @param country
39  	 * @param app
40  	 */
41  	public UnitsGatheringArea(String isoCountry, String country, IOccurrenceService occurrenceService){
42  		this.setCountry(isoCountry, country, occurrenceService);
43  	}
44  	
45  	/*
46  	 * Constructor
47  	 * Set a list of NamedAreas
48  	 */
49  	public UnitsGatheringArea(ArrayList<String> namedAreas){
50  		this.setAreaNames(namedAreas);
51  	}
52  
53  	/*
54  	 * Return the current NamedArea
55  	 */
56  	public NamedArea getArea(){
57  		return this.area;
58  	}
59  	
60  	/*
61  	 * Return the current list of NamedAreas
62  	 */
63  	public ArrayList<NamedArea> getAreas(){
64  		return this.areas;
65  	}
66  	
67  	/*
68  	 * Set the list of NamedAreas
69  	 * @param namedAreas
70  	 */
71  	public void setAreaNames(ArrayList<String> namedAreas){
72  		for (String strNamedArea : namedAreas){
73  			this.area.setLabel(strNamedArea);
74  			this.areas.add(this.area);
75  			this.area = NamedArea.NewInstance();
76  		}
77  	}
78  	
79  	/*
80  	 * Set the current Country
81  	 * Search in the DB if the isoCode is known
82  	 * If not, search if the country name is in the DB
83  	 * If not, create a new Label with the Level Country
84  	 * @param iso: the country iso code
85  	 * @param fullName: the country's full name
86  	 * @param app: the CDM application controller
87  	 */
88  	public void setCountry(String iso, String fullName, IOccurrenceService occurrenceService){
89  		WaterbodyOrCountry country = null;
90  		List<WaterbodyOrCountry> countries = new ArrayList<WaterbodyOrCountry>();
91  		if (StringUtils.isBlank(iso)){
92  			//TODO move to termservice
93  			country = occurrenceService.getCountryByIso(iso);
94  		}
95  		if (country != null){
96  			this.area.addWaterbodyOrCountry(country);
97  		}else{
98  			if (fullName != ""){
99  				//TODO move to termservice
100 				countries = occurrenceService.getWaterbodyOrCountryByName(fullName);
101 			}
102 			if (countries.size() >0){
103 				this.area.addWaterbodyOrCountry(countries.get(0));
104 			}else{
105 				this.area.setLabel(fullName);
106 				this.area.setLevel(NamedAreaLevel.COUNTRY()); 
107 			}
108 		}
109 	}
110 	
111 }