Project

General

Profile

Download (4.75 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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
package eu.etaxonomy.cdm.io.dwca.in;
10

    
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.io.dwca.TermUri;
21
import eu.etaxonomy.cdm.io.stream.StreamItem;
22
import eu.etaxonomy.cdm.model.agent.Institution;
23
import eu.etaxonomy.cdm.model.agent.Person;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.common.User;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27

    
28
/**
29
 * @author a.mueller
30
 * @date 22.11.2011
31
 *
32
 */
33
public class EolAgent2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
34
				implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String> {
35
	@SuppressWarnings("unused")
36
	private static final Logger logger = Logger.getLogger(EolAgent2CdmConverter.class);
37
	private static final String CORE_ID = "coreId";
38

    
39
	/**
40
	 * @param state
41
	 */
42
	public EolAgent2CdmConverter(DwcaDataImportStateBase state) {
43
		super(state);
44
	}
45

    
46

    
47
	@Override
48
    public IReader<MappedCdmBase> map(StreamItem item ){
49
		List<MappedCdmBase> resultList = new ArrayList<MappedCdmBase>();
50

    
51
		Map<String, String> csv = item.map;
52
		Reference sourceReference = state.getTransactionalSourceReference();
53
		String sourceReferecenDetail = null;
54

    
55
		//TODO is taxon needed here here?
56
//		String id = csv.get(CORE_ID);
57
//		Taxon taxon = getTaxonBase(id, item, Taxon.class, state);
58

    
59
		String id = item.get(TermUri.DC_IDENTIFIER);
60
		String firstName = item.get(TermUri.FOAF_FIRST_NAME);
61
		String familyName = item.get(TermUri.FOAF_FAMILY_NAME);
62
		String name = item.get(TermUri.FOAF_NAME);
63
		String organization = item.get(TermUri.EOL_ORGANIZATION);
64
		String accountName = item.get(TermUri.FOAF_ACCOUNT_NAME);
65

    
66
		Institution institution = null;
67
		User user = null;
68
		if (isNotBlank(organization)){
69
			institution = Institution.NewInstance();
70
			institution.setName(organization);
71
			MappedCdmBase<Institution>  mcb = new MappedCdmBase<Institution>(TermUri.EOL_ORGANIZATION, id, institution);
72
			resultList.add(mcb);
73
		}
74
		if (isNotBlank(accountName)){
75
			user = User.NewInstance(accountName, UUID.randomUUID().toString());   //TODO
76
			MappedCdmBase<User>  mcb = new MappedCdmBase<User>(TermUri.FOAF_ACCOUNT_NAME, id, user);
77
			resultList.add(mcb);
78
		}
79

    
80
		if (isPerson(item)){
81
			Person person = Person.NewInstance();
82
			person.setFirstname(firstName);
83
			person.setLastname(familyName);
84
			if (isNotBlank(name) && ! name.equalsIgnoreCase(person.getTitleCache())){
85
				//TODO FOAF_NAME allows multiple names per object
86
				person.setTitleCache(name, true);
87
			}
88
			if (institution != null){
89
				person.addInstitutionalMembership(institution, null, null, null);
90
			}
91
			if (isNotBlank(accountName)){
92

    
93
			}
94

    
95
			MappedCdmBase<Person>  mcb = new MappedCdmBase<Person>(item.term, id, person);
96
			resultList.add(mcb);
97

    
98
		}else{
99
			//still unclear, what does Agent all include? Teams, organizations, ...?
100
			String message = "Agent type unclear. Agent not handled.";
101
			fireWarningEvent(message, item, 8);
102
		}
103

    
104
//		resultList.add(mcb);
105

    
106

    
107
		//return
108
		return new ListReader<MappedCdmBase>(resultList);
109

    
110
	}
111

    
112

    
113
	private boolean isPerson(StreamItem item) {
114
		String firstName = item.get(TermUri.FOAF_FIRST_NAME);
115
		String familyName = item.get(TermUri.FOAF_FAMILY_NAME);
116
		String accountName = item.get(TermUri.FOAF_ACCOUNT_NAME);
117
		if (isNotBlank(firstName) || isNotBlank(familyName) || isNotBlank(accountName) ){
118
			return true;
119
		}else{
120
			return false;
121
		}
122

    
123
	}
124

    
125

    
126
	@Override
127
	public String getSourceId(StreamItem item) {
128
		String id = item.get(CORE_ID);
129
		return id;
130
	}
131

    
132
//**************************** PARTITIONABLE ************************************************
133

    
134
	@Override
135
	protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
136
		String value;
137
		String key;
138
		if ( hasValue(value = item.get(CORE_ID))){
139
			key = TermUri.DWC_TAXON.toString();
140
			Set<String> keySet = getKeySet(key, fkMap);
141
			keySet.add(value);
142
		}
143
	}
144

    
145

    
146
	@Override
147
	public final Set<String> requiredSourceNamespaces() {
148
		Set<String> result = new HashSet<String>();
149
 		result.add(TermUri.DWC_TAXON.toString());
150
 		return result;
151
	}
152

    
153
//************************ STRING ************************************************/
154

    
155

    
156

    
157
	@Override
158
	public String toString(){
159
		return this.getClass().getName();
160
	}
161

    
162

    
163

    
164

    
165

    
166
}
(14-14/37)