Project

General

Profile

Download (12.2 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.NO_OVERWRITE;
13
import static eu.etaxonomy.cdm.io.common.ImportHelper.OBLIGATORY;
14
import static eu.etaxonomy.cdm.io.common.ImportHelper.OVERWRITE;
15

    
16
import java.util.ArrayList;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Set;
20

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

    
26
import com.hp.hpl.jena.rdf.model.Model;
27
import com.hp.hpl.jena.rdf.model.Resource;
28
import com.hp.hpl.jena.rdf.model.Statement;
29

    
30
import eu.etaxonomy.cdm.common.CdmUtils;
31
import eu.etaxonomy.cdm.common.XmlHelp;
32
import eu.etaxonomy.cdm.io.common.mapping.berlinModel.CdmOneToManyMapper;
33
import eu.etaxonomy.cdm.io.common.ICdmIO;
34
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
35
import eu.etaxonomy.cdm.io.common.ImportHelper;
36
import eu.etaxonomy.cdm.io.common.MapWrapper;
37
import eu.etaxonomy.cdm.io.common.mapping.CdmSingleAttributeMapperBase;
38
import eu.etaxonomy.cdm.io.common.mapping.IRdfMapper;
39
import eu.etaxonomy.cdm.io.common.mapping.IXmlMapper;
40
import eu.etaxonomy.cdm.model.agent.Team;
41
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
42
import eu.etaxonomy.cdm.model.common.Marker;
43
import eu.etaxonomy.cdm.model.common.MarkerType;
44
import eu.etaxonomy.cdm.model.common.TimePeriod;
45
import eu.etaxonomy.cdm.model.reference.IArticle;
46
import eu.etaxonomy.cdm.model.reference.IBookSection;
47
import eu.etaxonomy.cdm.model.reference.Reference;
48
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
49
import eu.etaxonomy.cdm.model.reference.ReferenceType;
50
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
51

    
52
/**
53
 * @author a.mueller
54
 * @created 29.05.2008
55
 * @version 1.0
56
 */
57
@Component
58
public class TcsRdfReferenceImport extends TcsRdfImportBase implements ICdmIO<TcsRdfImportState> {
59
	private static final Logger logger = Logger.getLogger(TcsRdfReferenceImport.class);
60

    
61
	private static int modCount = 1000;
62

    
63
	public TcsRdfReferenceImport(){
64
		super();
65
	}
66

    
67
	@Override
68
	public boolean doCheck(TcsRdfImportState state){
69
		boolean result = true;
70
		result &= checkArticlesWithoutJournal(state.getConfig());
71
		//result &= checkPartOfJournal(config);
72

    
73
		return result;
74
	}
75

    
76
	private static boolean checkArticlesWithoutJournal(IImportConfigurator bmiConfig){
77
		try {
78
			boolean result = true;
79
			//TODO
80
			//				result = firstRow = false;
81
//			}
82
//
83
			return result;
84
		} catch (Exception e) {
85
			e.printStackTrace();
86
			return false;
87
		}
88
	}
89

    
90

    
91
	protected static IRdfMapper[] standardMappers = new IRdfMapper[]{
92
		//new CdmTextElementMapper("edition", "edition"),
93
		new CdmTextElementMapper("volume", "volume"),
94
		new CdmTextElementMapper("placePublished", "placePublished"),
95
		new CdmTextElementMapper("publisher", "publisher"),
96
		//new CdmTextElementMapper("isbn", "isbn"),
97
		new CdmTextElementMapper("pages", "pages"),
98
		//new CdmTextElementMapper("series", "series"),
99
		//new CdmTextElementMapper("issn", "issn"),
100
		//new CdmTextElementMapper("url", "uri")
101
	};
102

    
103
	protected static CdmSingleAttributeRDFMapperBase[] operationalMappers = new CdmSingleAttributeRDFMapperBase[]{
104
		new CdmUnclearMapper("year")
105
		, new CdmUnclearMapper("title")
106
		, new CdmUnclearMapper("shortTitle")
107
		, new CdmUnclearMapper("publicationType")
108
		, new CdmUnclearMapper("parentPublication")
109
		, new CdmUnclearMapper("authorship")
110

    
111
	};
112

    
113
//	protected static String[] createdAndNotesAttributes = new String[]{
114
//			"created_When", "updated_When", "created_Who", "updated_Who", "notes"
115
//	};
116

    
117
	protected static CdmSingleAttributeXmlMapperBase[] unclearMappers = new CdmSingleAttributeXmlMapperBase[]{
118

    
119
	};
120

    
121

    
122

    
123
	private boolean makeStandardMapper(Statement resource, Reference ref, Set<String> omitAttributes){
124
		if (omitAttributes == null){
125
			omitAttributes = new HashSet<String>();
126
		}
127
		boolean result = true;
128
		for (IRdfMapper mapper : standardMappers){
129
			if (mapper instanceof CdmSingleAttributeMapperBase){
130
				makeSingleAttributeMapper((CdmSingleAttributeRDFMapperBase)mapper, resource, ref, omitAttributes);
131
			}else if (mapper instanceof CdmOneToManyMapper){
132
				makeMultipleAttributeMapper((CdmOneToManyMapper)mapper, resource, ref, omitAttributes);
133
			}else{
134
				logger.error("Unrecognized mapper type");
135
				return false;
136
			}
137

    
138

    
139
		}
140
		return true;
141
	}
142

    
143
	private boolean makeSingleAttributeMapper(CdmSingleAttributeRDFMapperBase mapper, Statement resource, Reference ref, Set<String> omitAttributes){
144
		boolean result = true;
145
		Object value = getValue(mapper, resource);
146
		//write to destination
147
		if (value != null){
148
			String destinationAttribute = mapper.getDestinationAttribute();
149
			if (! omitAttributes.contains(destinationAttribute)){
150
				result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
151
			}
152
		}
153
		return result;
154
	}
155

    
156
	private boolean makeMultipleAttributeMapper(CdmOneToManyMapper<?,?,CdmTextElementMapper> mapper, Statement parentElement, Reference ref, Set<String> omitAttributes){
157
		if (omitAttributes == null){
158
			omitAttributes = new HashSet<String>();
159
		}
160
		boolean result = true;
161
		String destinationAttribute = mapper.getSingleAttributeName();
162
		List<Object> sourceValues = new ArrayList<Object>();
163
		List<Class> classes = new ArrayList<Class>();
164
		for (CdmTextElementMapper singleMapper : mapper.getSingleMappers()){
165
			String sourceAttribute = singleMapper.getSourceAttribute();
166
			Object value = getValue(singleMapper, parentElement);
167
			//Object value = valueMap.get(sourceAttribute);
168
			sourceValues.add(value);
169
			Class clazz = singleMapper.getTypeClass();
170
			classes.add(clazz);
171
		}
172

    
173
		result &= ImportHelper.addMultipleValues(sourceValues, ref, destinationAttribute, classes, NO_OVERWRITE, OBLIGATORY);
174
		return result;
175
	}
176

    
177
//	private Object getValue(CdmSingleAttributeRDFMapperBase mapper, Element parentElement){
178
//		String sourceAttribute = mapper.getSourceAttribute().toLowerCase();
179
//		Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
180
//		Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
181
//		if (child == null){
182
//			return null;
183
//		}
184
//		if (child.getContentSize() > 1){
185
//			logger.warn("Element is not String");
186
//		}
187
//		Object value = child.getTextTrim();
188
//		return value;
189
//	}
190

    
191
	@Override
192
    protected void doInvoke(TcsRdfImportState state){
193
		MapWrapper<Reference> referenceMap = (MapWrapper<Reference>)state.getStore(ICdmIO.REFERENCE_STORE);
194
		MapWrapper<Reference> nomRefMap = (MapWrapper<Reference>)state.getStore(ICdmIO.NOMREF_STORE);
195

    
196
		TcsRdfImportConfigurator config = state.getConfig();
197
		Model root = config.getSourceRoot();
198
		logger.info("start makeReferences ...");
199

    
200
		String tcsElementName;
201
		Namespace tcsNamespace;
202

    
203
		String rdfNamespace = config.getRdfNamespaceURIString();
204
		String publicationNamespace = config.getPublicationNamespaceURI();
205

    
206
		String idNamespace = "PublicationCitation";
207
		tcsElementName = "PublicationCitation";
208
		
209
		/*List<Element> elPublicationCitations = root.l
210

    
211
		int nomRefCount = 0;
212
		int biblioRefsCount = 0;
213

    
214
		int i = 0;
215
		//for each publication
216
		for (Element elPublicationCitation : elPublicationCitations){
217

    
218
			if ((++i % modCount) == 0){ logger.info("references handled: " + (i-1));}
219

    
220
			//create publication element
221
			String strAbout = elPublicationCitation.getAttributeValue("about", rdfNamespace);
222

    
223
			tcsElementName = "publicationType";
224
			tcsNamespace = publicationNamespace;
225
			String strPubType = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
226

    
227
			try {
228
				Reference<?> ref = TcsRdfTransformer.pubTypeStr2PubType(strPubType);
229
				if (ref==null){
230
					ref = ReferenceFactory.newGeneric();
231
				}
232

    
233
				Set<String> omitAttributes = null;
234
				makeStandardMapper(elPublicationCitation, ref, omitAttributes);
235

    
236

    
237
				tcsElementName = "authorship";
238
				tcsNamespace = publicationNamespace;
239
				String strAuthorship = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
240
				//TODO
241
				TeamOrPersonBase<?> authorship = Team.NewInstance();
242
				authorship.setTitleCache(strAuthorship, true);
243
				ref.setAuthorship(authorship);
244

    
245
				tcsElementName = "year";
246
				tcsNamespace = publicationNamespace;
247
				String strYear = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
248
				TimePeriod datePublished = ImportHelper.getDatePublished(strYear);
249
				ref.setDatePublished(datePublished);
250

    
251
				//Reference
252
				//TODO
253
				tcsElementName = "parentPublication";
254
				tcsNamespace = publicationNamespace;
255
				String strParent = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
256
				Reference<?> parent = referenceMap.get(strParent);
257
				if (parent != null){
258
					if ((ref.getType().equals(ReferenceType.Article)) && (parent.getType().equals(ReferenceType.Journal))){
259
						((IArticle)ref).setInJournal(parent);
260
					}else if ((ref.getType().equals(ReferenceType.BookSection)) && (parent.getType().equals(ReferenceType.Book))){
261
						((IBookSection)ref).setInBook(parent);
262
					}else{
263
						logger.warn("parent type (parent: " + parent.getType() +", child("+strAbout+"): " + ref.getType() +  ")not yet implemented");
264
						//ref.setParent(parent);
265
					}
266
				}
267

    
268

    
269
				//FIXME
270
				//nomRef and reference
271
				tcsElementName = "shortTitle";
272
				tcsNamespace = publicationNamespace;
273
				boolean nomRefExists = false;
274
				String strShortTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
275
				if (! CdmUtils.Nz(strShortTitle).trim().equals("")){
276
					ref.setTitle(strShortTitle);
277
					ImportHelper.setOriginalSource(ref, config.getSourceReference(), strAbout, idNamespace);
278
					nomRefMap.put(strAbout, ref);
279
					nomRefCount++;
280
					nomRefExists = true;
281
				}
282

    
283
				tcsElementName = "title";
284
				tcsNamespace = publicationNamespace;
285
				String strTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
286
				tcsNamespace = publicationNamespace;
287
				if (! CdmUtils.Nz(strTitle).trim().equals("")  || nomRefExists == false){
288
					//TODO
289
					Reference<?> biblioRef = (Reference<?>)ref.clone();
290
					biblioRef.setTitle(strTitle);
291
					ImportHelper.setOriginalSource(biblioRef, config.getSourceReference(), strAbout, idNamespace);
292
					referenceMap.put(strAbout, biblioRef);
293
					biblioRefsCount++;
294
				}
295

    
296

    
297
				checkAdditionalContents(elPublicationCitation, standardMappers, operationalMappers, unclearMappers);
298

    
299
				if (state.getConfig().isPublishReferences()){
300
					ref.addMarker(Marker.NewInstance(MarkerType.IN_BIBLIOGRAPHY(), false));
301
				}
302

    
303
				//ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
304

    
305
			} catch (UnknownCdmTypeException e) {
306
				//FIXME
307
				logger.warn("Name with id " + strAbout + " has unknown type " + strPubType + " and could not be saved.");
308
				state.setUnsuccessfull();
309
			}
310
		}
311

    
312
		//change conceptRef uuid
313
		Reference<?> sec = referenceMap.get(config.getSourceSecId());
314
		if (sec == null){
315
			sec = nomRefMap.get(config.getSourceSecId());
316
		}
317
		if (sec != null){
318
			sec.setUuid(config.getSecUuid());
319
			logger.info("concept reference uuid changed to: " + config.getSecUuid());
320
		}
321

    
322
		//save and store in map
323
		logger.info("Save nomenclatural references (" + nomRefCount + ")");
324
		getReferenceService().save(nomRefMap.objects());
325
		logger.info("Save bibliographical references (" + biblioRefsCount +")");
326
		getReferenceService().save(referenceMap.objects());
327

    
328
		//referenceService.saveReferenceAll(referenceMap.objects());
329
		logger.info("end makeReferences ...");*/
330
		return;
331

    
332
	}
333

    
334
	/* (non-Javadoc)
335
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
336
	 */
337
	@Override
338
    protected boolean isIgnore(TcsRdfImportState state){
339
		return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
340
	}
341

    
342
}
(9-9/14)