Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:
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.tcsrdf;
11

    
12
import static eu.etaxonomy.cdm.io.common.ImportHelper.OBLIGATORY;
13
import static eu.etaxonomy.cdm.io.common.ImportHelper.OVERWRITE;
14

    
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19

    
20
import org.apache.log4j.Logger;
21
import org.jdom.Element;
22
import org.jdom.Namespace;
23
import org.springframework.stereotype.Component;
24

    
25
import eu.etaxonomy.cdm.common.CdmUtils;
26
import eu.etaxonomy.cdm.common.XmlHelp;
27
import eu.etaxonomy.cdm.io.common.ICdmIO;
28
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
29
import eu.etaxonomy.cdm.io.common.ImportHelper;
30
import eu.etaxonomy.cdm.io.common.MapWrapper;
31
import eu.etaxonomy.cdm.model.agent.Team;
32
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.common.TimePeriod;
35
import eu.etaxonomy.cdm.model.reference.Article;
36
import eu.etaxonomy.cdm.model.reference.Book;
37
import eu.etaxonomy.cdm.model.reference.BookSection;
38
import eu.etaxonomy.cdm.model.reference.Generic;
39
import eu.etaxonomy.cdm.model.reference.Journal;
40
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
41
import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;
42
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
43

    
44
/**
45
 * @author a.mueller
46
 * @created 29.05.2008
47
 * @version 1.0
48
 */
49
@Component
50
public class TcsRdfReferenceImport extends TcsRdfImportBase implements ICdmIO<IImportConfigurator> {
51
	private static final Logger logger = Logger.getLogger(TcsRdfReferenceImport.class);
52

    
53
	private static int modCount = 1000;
54
	
55
	public TcsRdfReferenceImport(){
56
		super();
57
	}
58
	
59
	@Override
60
	public boolean doCheck(IImportConfigurator config){
61
		boolean result = true;
62
		result &= checkArticlesWithoutJournal(config);
63
		//result &= checkPartOfJournal(config);
64
		
65
		return result;
66
	}
67
		
68
	private static boolean checkArticlesWithoutJournal(IImportConfigurator bmiConfig){
69
		try {
70
			boolean result = true;
71
			//TODO
72
			//				result = firstRow = false;
73
//			}
74
//			
75
			return result;
76
		} catch (Exception e) {
77
			e.printStackTrace();
78
			return false;
79
		}
80
	}
81
	
82
	
83
	protected static CdmIoXmlMapperBase[] standardMappers = new CdmIoXmlMapperBase[]{
84
		//new CdmTextElementMapper("edition", "edition"),
85
		new CdmTextElementMapper("volume", "volume"),
86
		new CdmTextElementMapper("placePublished", "placePublished"),
87
		new CdmTextElementMapper("publisher", "publisher"),
88
		//new CdmTextElementMapper("isbn", "isbn"),
89
		new CdmTextElementMapper("pages", "pages"),
90
		//new CdmTextElementMapper("series", "series"),
91
		//new CdmTextElementMapper("issn", "issn"),
92
		//new CdmTextElementMapper("url", "uri")
93
	};
94
	
95
	protected static CdmIoXmlMapperBase[] operationalMappers = new CdmIoXmlMapperBase[]{
96
		new CdmUnclearMapper("year")
97
		, new CdmUnclearMapper("title")
98
		, new CdmUnclearMapper("shortTitle")
99
		, new CdmUnclearMapper("publicationType")
100
		, new CdmUnclearMapper("parentPublication")
101
		, new CdmUnclearMapper("authorship")
102
		
103
	};
104
	
105
//	protected static String[] createdAndNotesAttributes = new String[]{
106
//			"created_When", "updated_When", "created_Who", "updated_Who", "notes"
107
//	};
108
	
109
	protected static CdmIoXmlMapperBase[] unclearMappers = new CdmIoXmlMapperBase[]{
110
		
111
	};
112

    
113

    
114
	
115
	private boolean makeStandardMapper(Element parentElement, StrictReferenceBase ref, Set<String> omitAttributes){
116
		if (omitAttributes == null){
117
			omitAttributes = new HashSet<String>();
118
		}
119
		boolean result = true;	
120
		for (CdmIoXmlMapperBase mapper : standardMappers){
121
			Object value = getValue(mapper, parentElement);
122
			//write to destination
123
			if (value != null){
124
				String destinationAttribute = mapper.getDestinationAttribute();
125
				if (! omitAttributes.contains(destinationAttribute)){
126
					result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
127
				}
128
			}
129
		}
130
		return true;
131
	}
132
	
133
	private Object getValue(CdmIoXmlMapperBase mapper, Element parentElement){
134
		String sourceAttribute = mapper.getSourceAttribute().toLowerCase();
135
		Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
136
		Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
137
		if (child == null){
138
			return null;
139
		}
140
		if (child.getContentSize() > 1){
141
			logger.warn("Element is not String");
142
		}
143
		Object value = child.getTextTrim();
144
		return value;
145
	}
146
	
147
	@Override
148
	public boolean doInvoke(IImportConfigurator config,
149
			Map<String, MapWrapper<? extends CdmBase>> stores){
150
		
151
		MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
152
		MapWrapper<ReferenceBase> nomRefMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.NOMREF_STORE);
153
		
154
		TcsRdfImportConfigurator tcsConfig = (TcsRdfImportConfigurator)config;
155
		Element root = tcsConfig.getSourceRoot();
156
		logger.info("start makeReferences ...");
157
		
158
		String tcsElementName;
159
		Namespace tcsNamespace;
160
		boolean success = true;
161
		
162
		Namespace rdfNamespace = tcsConfig.getRdfNamespace();
163
		String prefix = "tpub";
164
		Namespace publicationNamespace = tcsConfig.getPublicationNamespace();
165

    
166
		
167
		String idNamespace = "PublicationCitation";
168
		tcsElementName = "PublicationCitation";
169
		tcsNamespace = publicationNamespace;
170
		List<Element> elPublicationCitations = root.getChildren(tcsElementName, tcsNamespace);
171

    
172
		int nomRefCount = 0;
173
		int biblioRefsCount = 0;
174
		
175
		int i = 0;
176
		//for each taxonName
177
		for (Element elPublicationCitation : elPublicationCitations){
178
			
179
			if ((++i % modCount) == 0){ logger.info("references handled: " + (i-1));}
180
			
181
			//create TaxonName element
182
			String strAbout = elPublicationCitation.getAttributeValue("about", rdfNamespace);
183
			
184
			tcsElementName = "publicationType";
185
			tcsNamespace = publicationNamespace;
186
			String strPubType = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
187
			
188
			try {
189
				StrictReferenceBase ref = TcsRdfTransformer.pubTypeStr2PubType(strPubType);
190
				if (ref==null){
191
					ref = Generic.NewInstance();
192
				}
193
				
194
				Set<String> omitAttributes = null;
195
				makeStandardMapper(elPublicationCitation, ref, omitAttributes);
196
				
197
				
198
				tcsElementName = "authorship";
199
				tcsNamespace = publicationNamespace;
200
				String strAuthorship = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
201
				//TODO
202
				TeamOrPersonBase authorTeam = Team.NewInstance();
203
				authorTeam.setTitleCache(strAuthorship);
204
				ref.setAuthorTeam(authorTeam);
205
				
206
				tcsElementName = "year";
207
				tcsNamespace = publicationNamespace;
208
				String strYear = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
209
				TimePeriod datePublished = ImportHelper.getDatePublished(strYear);
210
				ref.setDatePublished(datePublished);
211
				
212
				//Reference
213
				//TODO
214
				tcsElementName = "parentPublication";
215
				tcsNamespace = publicationNamespace;
216
				String strParent = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
217
				ReferenceBase parent = referenceMap.get(strParent);
218
				if (parent != null){
219
					if ((ref instanceof Article) && (parent instanceof Journal)){
220
						((Article)ref).setInJournal((Journal)parent);
221
					}else if ((ref instanceof BookSection) && (parent instanceof Book)){
222
						((BookSection)ref).setInBook((Book)parent);
223
					}else{
224
						logger.warn("parent type (parent: " + parent.getClass().getSimpleName() +", child("+strAbout+"): " + ref.getClass().getSimpleName() +  ")not yet implemented");
225
						//ref.setParent(parent);
226
					}
227
				}
228

    
229
				
230
				//FIXME
231
				//nomRef and reference
232
				tcsElementName = "shortTitle";
233
				tcsNamespace = publicationNamespace;
234
				boolean nomRefExists = false;
235
				String strShortTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
236
				if (! CdmUtils.Nz(strShortTitle).trim().equals("")){
237
					ref.setTitle(strShortTitle);
238
					ImportHelper.setOriginalSource(ref, config.getSourceReference(), strAbout, idNamespace);
239
					nomRefMap.put(strAbout, ref);
240
					nomRefCount++;
241
					nomRefExists = true;
242
				}
243
				
244
				tcsElementName = "title";
245
				tcsNamespace = publicationNamespace;
246
				String strTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
247
				tcsNamespace = publicationNamespace;
248
				if (! CdmUtils.Nz(strTitle).trim().equals("")  || nomRefExists == false){
249
					//TODO
250
					StrictReferenceBase biblioRef = (StrictReferenceBase)ref.clone();
251
					biblioRef.setTitle(strTitle);
252
					ImportHelper.setOriginalSource(ref, config.getSourceReference(), strAbout, idNamespace);
253
					referenceMap.put(strAbout, biblioRef);
254
					biblioRefsCount++;
255
				}
256
				
257
				
258
				checkAdditionalContents(elPublicationCitation, standardMappers, operationalMappers, unclearMappers);
259

    
260
				
261
				//ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
262
				
263
			} catch (UnknownCdmTypeException e) {
264
				//FIXME
265
				logger.warn("Name with id " + strAbout + " has unknown type " + strPubType + " and could not be saved.");
266
				success = false; 
267
			}
268
		}
269
		
270
		//change conceptRef uuid
271
		ReferenceBase sec = referenceMap.get(config.getSourceSecId());
272
		if (sec == null){
273
			sec = nomRefMap.get(config.getSourceSecId());	
274
		}
275
		if (sec != null){
276
			sec.setUuid(config.getSecUuid());
277
			logger.info("concept reference uuid changed to: " + config.getSecUuid());
278
		}
279
		
280
		
281
		//save and store in map
282
		logger.info("Save nomenclatural references (" + nomRefCount + ")");
283
		getReferenceService().saveReferenceAll(nomRefMap.objects());
284
		logger.info("Save bibliographical references (" + biblioRefsCount +")");
285
		getReferenceService().saveReferenceAll(referenceMap.objects());
286
		
287
		//referenceService.saveReferenceAll(referenceMap.objects());
288
		logger.info("end makeReferences ...");
289
		return success;
290
	}
291
	
292
	/* (non-Javadoc)
293
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
294
	 */
295
	protected boolean isIgnore(IImportConfigurator config){
296
		return (config.getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
297
	}
298
	
299
}
(6-6/11)