Project

General

Profile

Download (12.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.cdm.io.tcs;
5

    
6
import java.util.Calendar;
7
import java.util.List;
8
import java.util.Map;
9

    
10
import org.apache.log4j.Logger;
11
import org.jdom.Attribute;
12
import org.jdom.Element;
13
import org.jdom.Namespace;
14

    
15
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
16
import eu.etaxonomy.cdm.api.service.INameService;
17
import eu.etaxonomy.cdm.api.service.IReferenceService;
18
import eu.etaxonomy.cdm.common.XmlHelp;
19
import eu.etaxonomy.cdm.io.common.CdmIoBase;
20
import eu.etaxonomy.cdm.io.common.ICdmIO;
21
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
22
import eu.etaxonomy.cdm.io.common.ImportHelper;
23
import eu.etaxonomy.cdm.io.common.MapWrapper;
24
import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
25
import eu.etaxonomy.cdm.model.agent.Team;
26
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.TimePeriod;
29
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
30
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
31
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
32
import eu.etaxonomy.cdm.model.name.NonViralName;
33
import eu.etaxonomy.cdm.model.name.Rank;
34
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35
import eu.etaxonomy.cdm.model.reference.Generic;
36
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
37
import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;
38
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
39

    
40
/**
41
 * @author a.mueller
42
 *
43
 */
44
public class TcsReferenceIO extends CdmIoBase implements ICdmIO {
45
	private static final Logger logger = Logger.getLogger(TcsReferenceIO.class);
46

    
47
	private static int modCount = 1000;
48
	
49
	public TcsReferenceIO(){
50
		super();
51
	}
52
	
53
	@Override
54
	public boolean doCheck(IImportConfigurator config){
55
		boolean result = true;
56
		result &= checkArticlesWithoutJournal(config);
57
		result &= checkPartOfJournal(config);
58
		
59
		return result;
60
	}
61
		
62
	private static boolean checkArticlesWithoutJournal(IImportConfigurator bmiConfig){
63
		try {
64
			boolean result = true;
65
//			Source source = bmiConfig.getSource();
66
//			String strQueryArticlesWithoutJournal = "SELECT Reference.RefId, InRef.RefId AS InRefID, Reference.RefCategoryFk, InRef.RefCategoryFk AS InRefCatFk, Reference.RefCache, Reference.NomRefCache, Reference.Title, RefCategory.RefCategoryAbbrev, InRefCategory.RefCategoryAbbrev AS InRefCat, InRef.Title AS InRefTitle " + 
67
//						" FROM Reference INNER JOIN Reference AS InRef ON Reference.InRefFk = InRef.RefId INNER JOIN RefCategory ON Reference.RefCategoryFk = RefCategory.RefCategoryId INNER JOIN RefCategory AS InRefCategory ON InRef.RefCategoryFk = InRefCategory.RefCategoryId " +
68
//						" WHERE (Reference.RefCategoryFk = 1) AND (InRef.RefCategoryFk <> 9) ";
69
//			ResultSet resulSetarticlesWithoutJournal = source.getResultSet(strQueryArticlesWithoutJournal);
70
//			boolean firstRow = true;
71
//			while (resulSetarticlesWithoutJournal.next()){
72
//				if (firstRow){
73
//					System.out.println("========================================================");
74
//					logger.warn("There are Articles with wrong inRef type!");
75
//					System.out.println("========================================================");
76
//				}
77
//				int refId = resulSetarticlesWithoutJournal.getInt("RefId");
78
//				int categoryFk = resulSetarticlesWithoutJournal.getInt("RefCategoryFk");
79
//				String cat = resulSetarticlesWithoutJournal.getString("RefCategoryAbbrev");
80
//				int inRefFk = resulSetarticlesWithoutJournal.getInt("InRefId");
81
//				int inRefCategoryFk = resulSetarticlesWithoutJournal.getInt("InRefCatFk");
82
//				String inRefCat = resulSetarticlesWithoutJournal.getString("InRefCat");
83
//				String refCache = resulSetarticlesWithoutJournal.getString("RefCache");
84
//				String nomRefCache = resulSetarticlesWithoutJournal.getString("nomRefCache");
85
//				String title = resulSetarticlesWithoutJournal.getString("title");
86
//				String inRefTitle = resulSetarticlesWithoutJournal.getString("InRefTitle");
87
//				
88
//				System.out.println("RefID:" + refId + "\n  cat: " + cat + 
89
//						"\n  refCache: " + refCache + "\n  nomRefCache: " + nomRefCache + "\n  title: " + title + 
90
//						"\n  inRefFk: " + inRefFk + "\n  inRefCategory: " + inRefCat + 
91
//						"\n  inRefTitle: " + inRefTitle );
92
//				result = firstRow = false;
93
//			}
94
//			
95
			return result;
96
		} catch (Exception e) {
97
			e.printStackTrace();
98
			return false;
99
		}
100
	}
101
	
102
	private boolean checkPartOfJournal(IImportConfigurator bmiConfig){
103
		try {
104
			boolean result = true;
105
//			Source source = bmiConfig.getSource();
106
//			String strQueryPartOfJournal = "SELECT Reference.RefId, InRef.RefId AS InRefID, Reference.RefCategoryFk, InRef.RefCategoryFk AS InRefCatFk, Reference.RefCache, Reference.NomRefCache, Reference.Title, RefCategory.RefCategoryAbbrev, InRefCategory.RefCategoryAbbrev AS InRefCat, InRef.Title AS InRefTitle " + 
107
//			" FROM Reference INNER JOIN Reference AS InRef ON Reference.InRefFk = InRef.RefId INNER JOIN RefCategory ON Reference.RefCategoryFk = RefCategory.RefCategoryId INNER JOIN RefCategory AS InRefCategory ON InRef.RefCategoryFk = InRefCategory.RefCategoryId " +
108
//						" WHERE (Reference.RefCategoryFk = 2) AND (InRef.RefCategoryFk = 9) ";
109
//			ResultSet rs = source.getResultSet(strQueryPartOfJournal);
110
//			boolean firstRow = true;
111
//			while (rs.next()){
112
//				if (firstRow){
113
//					System.out.println("========================================================");
114
//					logger.warn("There are part-of-references that have a Journal as in-reference!");
115
//					System.out.println("========================================================");
116
//				}
117
//				int refId = rs.getInt("RefId");
118
//				int categoryFk = rs.getInt("RefCategoryFk");
119
//				String cat = rs.getString("RefCategoryAbbrev");
120
//				int inRefFk = rs.getInt("InRefId");
121
//				int inRefCategoryFk = rs.getInt("InRefCatFk");
122
//				String inRefCat = rs.getString("InRefCat");
123
//				String refCache = rs.getString("RefCache");
124
//				String nomRefCache = rs.getString("nomRefCache");
125
//				String title = rs.getString("title");
126
//				String inRefTitle = rs.getString("InRefTitle");
127
//				
128
//				System.out.println("RefID:" + refId + "\n  cat: " + cat + 
129
//						"\n  refCache: " + refCache + "\n  nomRefCache: " + nomRefCache + "\n  title: " + title + 
130
//						"\n  inRefFk: " + inRefFk + "\n  inRefCategory: " + inRefCat + 
131
//						"\n  inRefTitle: " + inRefTitle );
132
//				result = firstRow = false;
133
//			}
134
//			
135
			return result;
136
		} catch (Exception e) {
137
			e.printStackTrace();
138
			return false;
139
		}
140
	}
141
	
142
	
143
	@Override
144
	public boolean doInvoke(IImportConfigurator config, CdmApplicationController cdmApp,
145
			Map<String, MapWrapper<? extends CdmBase>> stores){
146
		
147
		MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
148
		MapWrapper<TeamOrPersonBase> authorMap = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.AUTHOR_STORE);
149
		
150
		TcsImportConfigurator tcsConfig = (TcsImportConfigurator)config;
151
		Element source = tcsConfig.getSourceRoot();
152
		Element root = tcsConfig.getSourceRoot();
153
		logger.info("start makeReferences ...");
154
		
155
		String tcsElementName;
156
		Namespace tcsNamespace;
157
		String cdmAttrName;
158
		String value;
159
		boolean success = true;
160
		IReferenceService referenceService = cdmApp.getReferenceService();
161
		
162
		
163
		MapWrapper<ReferenceBase> referenceStore= new MapWrapper<ReferenceBase>(null);
164
		//Map<Integer, ReferenceBase> referenceCollectionMap = new HashMap<Integer, ReferenceBase>();
165
		
166
		Namespace rdfNamespace = root.getNamespace();
167
		String prefix = "tn";
168
		Namespace taxonNameNamespace = root.getNamespace(prefix);
169
		prefix = "tc";
170
		Namespace taxonConceptNamespace = root.getNamespace(prefix);
171
		prefix = "tcom";
172
		Namespace commonNamespace = root.getNamespace(prefix);
173
		prefix = "tpub";
174
		Namespace publicationNamespace = root.getNamespace(prefix);
175

    
176
		
177
		
178
		tcsElementName = "PublicationCitation";
179
		tcsNamespace = publicationNamespace;
180
		List<Element> elPublicationCitations = root.getChildren(tcsElementName, tcsNamespace);
181

    
182
		
183
		int i = 0;
184
		//for each taxonName
185
		for (Element elPublicationCitation : elPublicationCitations){
186
			
187
			if ((++i % modCount) == 0){ logger.info("Names handled: " + (i-1));}
188
			
189
			Attribute about = elPublicationCitation.getAttribute("about", rdfNamespace);
190

    
191
			//create TaxonName element
192
			String strAbout = elPublicationCitation.getAttributeValue("about", rdfNamespace);
193
			
194
			String pages = 
195
			tcsElementName = "publicationType";
196
			tcsNamespace = publicationNamespace;
197
			String strPubType = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
198
			
199
			try {
200
				ReferenceBase refBase = TcsTransformer.pubTypeStr2PubType(strPubType);
201
				
202
				//attributes
203
				tcsElementName = "publisher";
204
				tcsNamespace = publicationNamespace;
205
				cdmAttrName = "genusOrUninomial";
206
				success &= ImportHelper.addXmlStringValue(elPublicationCitation, refBase, tcsElementName, tcsNamespace, cdmAttrName);
207

    
208
				tcsElementName = "shortTitle";
209
				tcsNamespace = publicationNamespace;
210
				cdmAttrName = "title";
211
				success &= ImportHelper.addXmlStringValue(elPublicationCitation, refBase, tcsElementName, tcsNamespace, cdmAttrName);
212

    
213
				tcsElementName = "title";
214
				tcsNamespace = publicationNamespace;
215
				cdmAttrName = "title";
216
				success &= ImportHelper.addXmlStringValue(elPublicationCitation, refBase, tcsElementName, tcsNamespace, cdmAttrName);
217

    
218
				tcsElementName = "year";
219
				tcsNamespace = publicationNamespace;
220
				cdmAttrName = "year";
221
				success &= ImportHelper.addXmlStringValue(elPublicationCitation, refBase, tcsElementName, tcsNamespace, cdmAttrName);
222

    
223
				//TODO
224
				tcsElementName = "year";
225
				tcsNamespace = publicationNamespace;
226
				Integer year = null;
227
				try {
228
					value = (String)ImportHelper.getXmlInputValue(elPublicationCitation, tcsElementName, tcsNamespace);
229
					year = Integer.valueOf(value);
230
					Calendar cal = Calendar.getInstance();
231
					//FIXME
232
					cal.set(year, 1, 1);
233
					if (refBase instanceof StrictReferenceBase){
234
						StrictReferenceBase ref = (StrictReferenceBase)refBase;
235
						ref.setDatePublished(TimePeriod.NewInstance(cal));
236
					}else{
237
						logger.warn("year not implemented for ReferenceBase type " +  ((refBase == null) ? "(null)" : refBase.getClass().getSimpleName()));
238
					}
239
				} catch (RuntimeException e) {
240
					logger.warn("year could not be parsed");
241
				}
242
			
243

    
244
				
245
				
246
				tcsElementName = "pages";
247
				tcsNamespace = publicationNamespace;
248
				cdmAttrName = "pages";
249
				success &= ImportHelper.addXmlStringValue(elPublicationCitation, refBase, tcsElementName, tcsNamespace, cdmAttrName);
250

    
251
				tcsElementName = "volume";
252
				tcsNamespace = publicationNamespace;
253
				cdmAttrName = "volume";
254
				success &= ImportHelper.addXmlStringValue(elPublicationCitation, refBase, tcsElementName, tcsNamespace, cdmAttrName);
255
				
256
				
257
				//Reference
258
				//TODO
259
				tcsElementName = "parentPublication";
260
				tcsNamespace = publicationNamespace;
261
				String strParent = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
262
				ReferenceBase parent = referenceMap.get(strParent);
263
				if (parent != null){
264
					logger.warn("parent not yet implemented");
265
					//TODO refBase.setParent(parent);
266
				}
267
				
268
				//ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
269
				referenceMap.put(strAbout, refBase);
270
				
271
			} catch (UnknownCdmTypeException e) {
272
				//FIXME
273
				logger.warn("Name with id " + strAbout + " has unknown type " + strPubType + " and could not be saved.");
274
				success = false; 
275
			}
276
		}
277
		logger.info(i + " names handled");
278
		referenceService.saveReferenceAll(referenceMap.objects());
279
		logger.info("end makeReferences ...");
280
		return success;
281
	}
282
	
283
	/* (non-Javadoc)
284
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
285
	 */
286
	protected boolean isIgnore(IImportConfigurator config){
287
		return (config.getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
288
	}
289
	
290
}
(2-2/7)