1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.pesi.out;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Iterator;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.io.common.DbExportBase;
|
20
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
21
|
import eu.etaxonomy.cdm.model.common.Marker;
|
22
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
24
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
25
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
28
|
|
29
|
/**
|
30
|
* @author e.-m.lee
|
31
|
* @date 12.02.2010
|
32
|
*
|
33
|
*/
|
34
|
public abstract class PesiExportBase extends DbExportBase<PesiExportConfigurator, PesiExportState> {
|
35
|
@SuppressWarnings("unused")
|
36
|
private static final Logger logger = Logger.getLogger(PesiExportBase.class);
|
37
|
|
38
|
public PesiExportBase() {
|
39
|
super();
|
40
|
}
|
41
|
|
42
|
|
43
|
protected <CLASS extends TaxonBase<?>> List<CLASS> getNextTaxonPartition(Class<CLASS> clazz,int limit, int partitionCount, List<String> propertyPath) {
|
44
|
List<CLASS> list = (List<CLASS>)getTaxonService().list(clazz, limit, partitionCount * limit, null, propertyPath);
|
45
|
|
46
|
Iterator<CLASS> it = list.iterator();
|
47
|
while (it.hasNext()){
|
48
|
TaxonBase<?> taxonBase = it.next();
|
49
|
if (! isPesiTaxon(taxonBase)){
|
50
|
it.remove();
|
51
|
}
|
52
|
}
|
53
|
return list;
|
54
|
}
|
55
|
|
56
|
|
57
|
/**
|
58
|
* Returns the next list of pure names. If finished result will be null. If list is empty there may be result in further partitions.
|
59
|
* @param clazz
|
60
|
* @param limit
|
61
|
* @param partitionCount
|
62
|
* @return
|
63
|
*/
|
64
|
protected List<NonViralName<?>> getNextPureNamePartition(Class<? extends NonViralName> clazz,int limit, int partitionCount) {
|
65
|
List<NonViralName<?>> list = (List)getNameService().list(clazz, limit, partitionCount * limit, null, null);
|
66
|
if (list.isEmpty()){
|
67
|
return null;
|
68
|
}
|
69
|
Iterator<NonViralName<?>> it = list.iterator();
|
70
|
while (it.hasNext()){
|
71
|
NonViralName<?> taxonName = it.next();
|
72
|
if (! isPurePesiName(taxonName)){
|
73
|
it.remove();
|
74
|
}
|
75
|
}
|
76
|
return list;
|
77
|
}
|
78
|
|
79
|
// protected List<TaxonBase> getNextDescriptionPartition(Class<? extends DescriptionElementBase> clazz,int limit, int partitionCount) {
|
80
|
// List<DescriptionElementBase> list = getDescriptionService().listDescriptionElements(null, null, pageSize, pageNumber, propPath);
|
81
|
//
|
82
|
// Iterator<TaxonBase> it = list.iterator();
|
83
|
// while (it.hasNext()){
|
84
|
// TaxonBase<?> taxonBase = it.next();
|
85
|
// if (! isPesiTaxon(taxonBase)){
|
86
|
// it.remove();
|
87
|
// }
|
88
|
// }
|
89
|
// return list;
|
90
|
// }
|
91
|
|
92
|
|
93
|
protected boolean isPurePesiName(TaxonNameBase<?,?> taxonName){
|
94
|
if (hasPesiTaxon(taxonName)){
|
95
|
return false;
|
96
|
}
|
97
|
|
98
|
for (NameRelationship rel :taxonName.getNameRelations()){
|
99
|
TaxonNameBase<?,?> relatedName = (rel.getFromName().equals(taxonName)? rel.getToName(): rel.getFromName());
|
100
|
if (hasPesiTaxon(relatedName)){
|
101
|
return true;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
return false;
|
106
|
}
|
107
|
|
108
|
|
109
|
protected boolean hasPesiTaxon(TaxonNameBase<?,?> taxonName) {
|
110
|
for (TaxonBase<?> taxon : taxonName.getTaxonBases()){
|
111
|
if (isPesiTaxon(taxon)){
|
112
|
return true;
|
113
|
}
|
114
|
}
|
115
|
return false;
|
116
|
}
|
117
|
|
118
|
protected Set<TaxonBase<?>> getPesiTaxa(TaxonNameBase<?,?> name){
|
119
|
Set<TaxonBase<?>> result = new HashSet<TaxonBase<?>>();
|
120
|
for (TaxonBase<?> taxonBase : name.getTaxonBases()){
|
121
|
if (isPesiTaxon(taxonBase)){
|
122
|
result.add(taxonBase);
|
123
|
}
|
124
|
}
|
125
|
return result;
|
126
|
}
|
127
|
|
128
|
|
129
|
/**
|
130
|
* Checks if this taxon is taxon that is to be exported to PESI. This is generally the case
|
131
|
* except for those taxa marked as "DON't PUBLISH".
|
132
|
* The list of conditions may change in future.
|
133
|
* @param taxonBase
|
134
|
* @return
|
135
|
*/
|
136
|
protected static boolean isPesiTaxon(TaxonBase taxonBase) {
|
137
|
for (Marker marker : taxonBase.getMarkers()){
|
138
|
if (marker.getValue() == false && marker.getMarkerType().equals(MarkerType.PUBLISH())){
|
139
|
return false;
|
140
|
}
|
141
|
}
|
142
|
return true;
|
143
|
}
|
144
|
|
145
|
protected Object getDbIdCdmWithExceptions(CdmBase cdmBase, PesiExportState state) {
|
146
|
if (cdmBase.isInstanceOf(TaxonNameBase.class)){
|
147
|
return ( cdmBase.getId() + state.getConfig().getNameIdStart() );
|
148
|
}else{
|
149
|
return super.getDbIdCdmWithExceptions(cdmBase, state);
|
150
|
}
|
151
|
}
|
152
|
}
|