Project

General

Profile

Download (11.6 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.Namespace;
23
import org.springframework.stereotype.Component;
24

    
25
import com.hp.hpl.jena.rdf.model.Model;
26
import com.hp.hpl.jena.rdf.model.Statement;
27

    
28
import eu.etaxonomy.cdm.io.common.ICdmIO;
29
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
30
import eu.etaxonomy.cdm.io.common.ImportHelper;
31
import eu.etaxonomy.cdm.io.common.MapWrapper;
32
import eu.etaxonomy.cdm.io.common.mapping.CdmSingleAttributeMapperBase;
33
import eu.etaxonomy.cdm.io.common.mapping.IRdfMapper;
34
import eu.etaxonomy.cdm.io.common.mapping.berlinModel.CdmOneToManyMapper;
35
import eu.etaxonomy.cdm.io.tcsxml.CdmSingleAttributeXmlMapperBase;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37

    
38
/**
39
 * @author a.mueller
40
 * @since 29.05.2008
41
 */
42
@Component
43
public class TcsRdfReferenceImport extends TcsRdfImportBase implements ICdmIO<TcsRdfImportState> {
44

    
45
    private static final long serialVersionUID = -2441804641147165510L;
46

    
47
    private static final Logger logger = Logger.getLogger(TcsRdfReferenceImport.class);
48

    
49
	private static int modCount = 1000;
50

    
51
	public TcsRdfReferenceImport(){
52
		super();
53
	}
54

    
55
	@Override
56
	public boolean doCheck(TcsRdfImportState state){
57
		boolean result = true;
58
		result &= checkArticlesWithoutJournal(state.getConfig());
59
		//result &= checkPartOfJournal(config);
60

    
61
		return result;
62
	}
63

    
64
	private static boolean checkArticlesWithoutJournal(IImportConfigurator bmiConfig){
65
		try {
66
			boolean result = true;
67
			//TODO
68
			//				result = firstRow = false;
69
//			}
70
//
71
			return result;
72
		} catch (Exception e) {
73
			e.printStackTrace();
74
			return false;
75
		}
76
	}
77

    
78

    
79
	protected static IRdfMapper[] standardMappers = new IRdfMapper[]{
80
		//new CdmTextElementMapper("edition", "edition"),
81
		new CdmTextElementMapper("volume", "volume"),
82
		new CdmTextElementMapper("placePublished", "placePublished"),
83
		new CdmTextElementMapper("publisher", "publisher"),
84
		//new CdmTextElementMapper("isbn", "isbn"),
85
		new CdmTextElementMapper("pages", "pages"),
86
		//new CdmTextElementMapper("series", "series"),
87
		//new CdmTextElementMapper("issn", "issn"),
88
		//new CdmTextElementMapper("url", "uri")
89
	};
90

    
91
	protected static CdmSingleAttributeRDFMapperBase[] operationalMappers = new CdmSingleAttributeRDFMapperBase[]{
92
		new CdmUnclearMapper("year")
93
		, new CdmUnclearMapper("title")
94
		, new CdmUnclearMapper("shortTitle")
95
		, new CdmUnclearMapper("publicationType")
96
		, new CdmUnclearMapper("parentPublication")
97
		, new CdmUnclearMapper("authorship")
98

    
99
	};
100

    
101
//	protected static String[] createdAndNotesAttributes = new String[]{
102
//			"created_When", "updated_When", "created_Who", "updated_Who", "notes"
103
//	};
104

    
105
	protected static CdmSingleAttributeXmlMapperBase[] unclearMappers = new CdmSingleAttributeXmlMapperBase[]{
106

    
107
	};
108

    
109

    
110

    
111
	private boolean makeStandardMapper(Statement resource, Reference ref, Set<String> omitAttributes){
112
		if (omitAttributes == null){
113
			omitAttributes = new HashSet<String>();
114
		}
115
		boolean result = true;
116
		for (IRdfMapper mapper : standardMappers){
117
			if (mapper instanceof CdmSingleAttributeMapperBase){
118
				makeSingleAttributeMapper((CdmSingleAttributeRDFMapperBase)mapper, resource, ref, omitAttributes);
119
			}else if (mapper instanceof CdmOneToManyMapper){
120
				makeMultipleAttributeMapper((CdmOneToManyMapper)mapper, resource, ref, omitAttributes);
121
			}else{
122
				logger.error("Unrecognized mapper type");
123
				return false;
124
			}
125

    
126

    
127
		}
128
		return true;
129
	}
130

    
131
	private boolean makeSingleAttributeMapper(CdmSingleAttributeRDFMapperBase mapper, Statement resource, Reference ref, Set<String> omitAttributes){
132
		boolean result = true;
133
		Object value = getValue(mapper, resource);
134
		//write to destination
135
		if (value != null){
136
			String destinationAttribute = mapper.getDestinationAttribute();
137
			if (! omitAttributes.contains(destinationAttribute)){
138
				result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
139
			}
140
		}
141
		return result;
142
	}
143

    
144
	private boolean makeMultipleAttributeMapper(CdmOneToManyMapper<?,?,CdmTextElementMapper> mapper, Statement parentElement, Reference ref, Set<String> omitAttributes){
145
		if (omitAttributes == null){
146
			omitAttributes = new HashSet<String>();
147
		}
148
		boolean result = true;
149
		String destinationAttribute = mapper.getSingleAttributeName();
150
		List<Object> sourceValues = new ArrayList<Object>();
151
		List<Class> classes = new ArrayList<Class>();
152
		for (CdmTextElementMapper singleMapper : mapper.getSingleMappers()){
153
			String sourceAttribute = singleMapper.getSourceAttribute();
154
			Object value = getValue(singleMapper, parentElement);
155
			//Object value = valueMap.get(sourceAttribute);
156
			sourceValues.add(value);
157
			Class clazz = singleMapper.getTypeClass();
158
			classes.add(clazz);
159
		}
160

    
161
		result &= ImportHelper.addMultipleValues(sourceValues, ref, destinationAttribute, classes, NO_OVERWRITE, OBLIGATORY);
162
		return result;
163
	}
164

    
165
//	private Object getValue(CdmSingleAttributeRDFMapperBase mapper, Element parentElement){
166
//		String sourceAttribute = mapper.getSourceAttribute().toLowerCase();
167
//		Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
168
//		Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
169
//		if (child == null){
170
//			return null;
171
//		}
172
//		if (child.getContentSize() > 1){
173
//			logger.warn("Element is not String");
174
//		}
175
//		Object value = child.getTextTrim();
176
//		return value;
177
//	}
178

    
179
	@Override
180
    protected void doInvoke(TcsRdfImportState state){
181
		MapWrapper<Reference> referenceMap = (MapWrapper<Reference>)state.getStore(ICdmIO.REFERENCE_STORE);
182
		MapWrapper<Reference> nomRefMap = (MapWrapper<Reference>)state.getStore(ICdmIO.NOMREF_STORE);
183

    
184
		TcsRdfImportConfigurator config = state.getConfig();
185
		Model root = config.getSourceRoot();
186
		logger.info("start makeReferences ...");
187

    
188
		String tcsElementName;
189
		Namespace tcsNamespace;
190

    
191
		String rdfNamespace = config.getRdfNamespaceURIString();
192
		String publicationNamespace = config.getPublicationNamespaceURI();
193

    
194
		String idNamespace = "PublicationCitation";
195
		tcsElementName = "PublicationCitation";
196

    
197
		/*List<Element> elPublicationCitations = root.l
198

    
199
		int nomRefCount = 0;
200
		int biblioRefsCount = 0;
201

    
202
		int i = 0;
203
		//for each publication
204
		for (Element elPublicationCitation : elPublicationCitations){
205

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

    
208
			//create publication element
209
			String strAbout = elPublicationCitation.getAttributeValue("about", rdfNamespace);
210

    
211
			tcsElementName = "publicationType";
212
			tcsNamespace = publicationNamespace;
213
			String strPubType = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
214

    
215
			try {
216
				Reference<?> ref = TcsRdfTransformer.pubTypeStr2PubType(strPubType);
217
				if (ref==null){
218
					ref = ReferenceFactory.newGeneric();
219
				}
220

    
221
				Set<String> omitAttributes = null;
222
				makeStandardMapper(elPublicationCitation, ref, omitAttributes);
223

    
224

    
225
				tcsElementName = "authorship";
226
				tcsNamespace = publicationNamespace;
227
				String strAuthorship = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
228
				//TODO
229
				TeamOrPersonBase<?> authorship = Team.NewInstance();
230
				authorship.setTitleCache(strAuthorship, true);
231
				ref.setAuthorship(authorship);
232

    
233
				tcsElementName = "year";
234
				tcsNamespace = publicationNamespace;
235
				String strYear = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
236
				TimePeriod datePublished = ImportHelper.getDatePublished(strYear);
237
				ref.setDatePublished(datePublished);
238

    
239
				//Reference
240
				//TODO
241
				tcsElementName = "parentPublication";
242
				tcsNamespace = publicationNamespace;
243
				String strParent = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
244
				Reference<?> parent = referenceMap.get(strParent);
245
				if (parent != null){
246
					if ((ref.getType().equals(ReferenceType.Article)) && (parent.getType().equals(ReferenceType.Journal))){
247
						((IArticle)ref).setInJournal(parent);
248
					}else if ((ref.getType().equals(ReferenceType.BookSection)) && (parent.getType().equals(ReferenceType.Book))){
249
						((IBookSection)ref).setInBook(parent);
250
					}else{
251
						logger.warn("parent type (parent: " + parent.getType() +", child("+strAbout+"): " + ref.getType() +  ")not yet implemented");
252
						//ref.setParent(parent);
253
					}
254
				}
255

    
256

    
257
				//FIXME
258
				//nomRef and reference
259
				tcsElementName = "shortTitle";
260
				tcsNamespace = publicationNamespace;
261
				boolean nomRefExists = false;
262
				String strShortTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
263
				if (! CdmUtils.Nz(strShortTitle).trim().equals("")){
264
					ref.setTitle(strShortTitle);
265
					ImportHelper.setOriginalSource(ref, config.getSourceReference(), strAbout, idNamespace);
266
					nomRefMap.put(strAbout, ref);
267
					nomRefCount++;
268
					nomRefExists = true;
269
				}
270

    
271
				tcsElementName = "title";
272
				tcsNamespace = publicationNamespace;
273
				String strTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
274
				tcsNamespace = publicationNamespace;
275
				if (! CdmUtils.Nz(strTitle).trim().equals("")  || nomRefExists == false){
276
					//TODO
277
					Reference<?> biblioRef = (Reference<?>)ref.clone();
278
					biblioRef.setTitle(strTitle);
279
					ImportHelper.setOriginalSource(biblioRef, config.getSourceReference(), strAbout, idNamespace);
280
					referenceMap.put(strAbout, biblioRef);
281
					biblioRefsCount++;
282
				}
283

    
284

    
285
				checkAdditionalContents(elPublicationCitation, standardMappers, operationalMappers, unclearMappers);
286

    
287
				if (state.getConfig().isPublishReferences()){
288
					ref.addMarker(Marker.NewInstance(MarkerType.IN_BIBLIOGRAPHY(), false));
289
				}
290

    
291
				//ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
292

    
293
			} catch (UnknownCdmTypeException e) {
294
				//FIXME
295
				logger.warn("Name with id " + strAbout + " has unknown type " + strPubType + " and could not be saved.");
296
				state.setUnsuccessfull();
297
			}
298
		}
299

    
300
		//change conceptRef uuid
301
		Reference<?> sec = referenceMap.get(config.getSourceSecId());
302
		if (sec == null){
303
			sec = nomRefMap.get(config.getSourceSecId());
304
		}
305
		if (sec != null){
306
			sec.setUuid(config.getSecUuid());
307
			logger.info("concept reference uuid changed to: " + config.getSecUuid());
308
		}
309

    
310
		//save and store in map
311
		logger.info("Save nomenclatural references (" + nomRefCount + ")");
312
		getReferenceService().save(nomRefMap.objects());
313
		logger.info("Save bibliographical references (" + biblioRefsCount +")");
314
		getReferenceService().save(referenceMap.objects());
315

    
316
		//referenceService.saveReferenceAll(referenceMap.objects());
317
		logger.info("end makeReferences ...");*/
318
		return;
319

    
320
	}
321

    
322
	/* (non-Javadoc)
323
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
324
	 */
325
	@Override
326
    protected boolean isIgnore(TcsRdfImportState state){
327
		return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
328
	}
329

    
330
}
(8-8/13)