2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.berlinModel
.out
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.stereotype
.Component
;
19 import org
.springframework
.transaction
.TransactionStatus
;
21 import eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
;
22 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.RefDetailMapper
;
23 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
24 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.CdmDbExportMapping
;
25 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.CreatedAndNotesMapper
;
26 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.DbObjectMapper
;
27 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.IExportTransformer
;
28 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.IdMapper
;
29 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.MethodMapper
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
32 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
33 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
36 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
45 public class BerlinModelNameRelationExport
extends BerlinModelExportBase
<RelationshipBase
> {
46 private static final Logger logger
= Logger
.getLogger(BerlinModelNameRelationExport
.class);
48 private static int modCount
= 100;
49 private static final String dbTableName
= "RelName";
50 private static final String pluralString
= "NameRelationships";
51 private static final Class
<?
extends CdmBase
> standardMethodParameter
= RelationshipBase
.class;
54 public BerlinModelNameRelationExport(){
59 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
62 protected boolean doCheck(BerlinModelExportState state
){
63 boolean result
= true;
64 logger
.warn("Checking for " + pluralString
+ " not yet implemented");
65 //result &= checkArticlesWithoutJournal(bmiConfig);
66 //result &= checkPartOfJournal(bmiConfig);
71 private CdmDbExportMapping
<BerlinModelExportState
, BerlinModelExportConfigurator
, IExportTransformer
> getMapping(){
72 String tableName
= dbTableName
;
73 CdmDbExportMapping
<BerlinModelExportState
, BerlinModelExportConfigurator
, IExportTransformer
> mapping
= new CdmDbExportMapping
<BerlinModelExportState
, BerlinModelExportConfigurator
, IExportTransformer
>(tableName
);
74 mapping
.addMapper(IdMapper
.NewInstance("RelNameId"));
76 mapping
.addMapper(DbObjectMapper
.NewInstance("fromName", "NameFk1"));
77 mapping
.addMapper(DbObjectMapper
.NewInstance("toName", "NameFk2"));
80 mapping
.addMapper(MethodMapper
.NewInstance("RelNameQualifierFk", this));
82 mapping
.addMapper(DbObjectMapper
.NewInstance("citation", "RefFk"));
83 mapping
.addMapper(RefDetailMapper
.NewInstance("citationMicroReference","citation", "RefDetailFk"));
84 mapping
.addMapper(CreatedAndNotesMapper
.NewInstance());
89 protected void doInvoke(BerlinModelExportState state
){
91 logger
.info("start make " + pluralString
+ " ...");
92 boolean success
= true ;
95 TransactionStatus txStatus
= startTransaction(true);
97 List
<RelationshipBase
> list
= getNameService().getAllRelationships(100000000, 0);
99 CdmDbExportMapping
<BerlinModelExportState
, BerlinModelExportConfigurator
, IExportTransformer
> mapping
= getMapping();
100 mapping
.initialize(state
);
103 for (RelationshipBase
<?
,?
,?
> rel
: list
){
104 if (rel
.isInstanceOf(NameRelationship
.class) || rel
.isInstanceOf(HybridRelationship
.class )){
105 doCount(count
++, modCount
, pluralString
);
106 success
&= mapping
.invoke(rel
);
109 commitTransaction(txStatus
);
111 success
&= makeIsHomotypicRelation(state
, mapping
);
113 logger
.info("end make " + pluralString
+ " ..." + getSuccessString(success
));
115 state
.setUnsuccessfull();
118 }catch(SQLException e
){
120 logger
.error(e
.getMessage());
121 state
.setUnsuccessfull();
127 private boolean makeIsHomotypicRelation(BerlinModelExportState state
, CdmDbExportMapping
<BerlinModelExportState
, BerlinModelExportConfigurator
, IExportTransformer
> mapping
){
128 boolean success
= true ;
130 Integer homotypicId
= state
.getConfig().getIsHomotypicId();
131 if (homotypicId
== null){
134 logger
.info("start make IsHomotypicRelations ...");
136 TransactionStatus txStatus
= startTransaction(true);
138 List
<HomotypicalGroup
> list
= getNameService().getAllHomotypicalGroups(100000000, 0);
142 Set
<NameRelationship
> basionymNameRels
= new HashSet
<NameRelationship
>();
143 for (HomotypicalGroup homoGroup
: list
){
144 doCount(count
++, modCount
, "homotypical groups");
145 Set
<TaxonNameBase
> allNames
= homoGroup
.getTypifiedNames();
146 if (allNames
.size() > 1){
147 Set
<TaxonNameBase
> readyNames
= new HashSet
<TaxonNameBase
>();
148 Set
<TaxonNameBase
> unrelateds
= homoGroup
.getUnrelatedNames();
149 for (TaxonNameBase unrelated
: unrelateds
){
150 for (TaxonNameBase oneOfAllNames
: allNames
){
151 if(!unrelated
.equals(oneOfAllNames
) && ! readyNames
.contains(oneOfAllNames
)){
152 success
&= invokeIsHomotypic(state
, mapping
, unrelated
, oneOfAllNames
, null, null);
155 readyNames
.add(unrelated
);
159 commitTransaction(txStatus
);
161 logger
.info("end make homotypical groups ... " + getSuccessString(success
));
163 }catch(SQLException e
){
164 logger
.error(e
.getMessage());
170 private boolean invokeIsHomotypic(BerlinModelExportState state
, CdmDbExportMapping
<BerlinModelExportState
, BerlinModelExportConfigurator
, IExportTransformer
> mapping
, TaxonNameBase fromName
, TaxonNameBase toName
, Reference refId
, String microCitation
) throws SQLException
{
172 logger
.info(fromName
.getTitleCache() + "->" + toName
.getTitleCache());
173 String maxQuery
= " SELECT max(relNameId) as max FROM relName ";
174 ResultSet rs
= state
.getConfig().getDestination().getResultSet(maxQuery
);
177 maxId
= rs
.getInt("max") + 1;
179 int fromNameId
= state
.getDbId(fromName
);
180 int toNameId
= state
.getDbId(toName
);
181 int catId
= state
.getConfig().getIsHomotypicId();
182 String query
= "INSERT INTO relName (relNameId, nameFk1, nameFk2, RelNameQualifierFk) " +
183 " VALUES ("+maxId
+","+fromNameId
+","+toNameId
+","+catId
+")";
184 int ui
= state
.getConfig().getDestination().getConnection().createStatement().executeUpdate(query
);
185 }catch(SQLException e
){
191 private Set
<TaxonNameBase
> getAllRelatedNames(Set
<NameRelationship
> rels
){
192 Set
<TaxonNameBase
> result
= new HashSet
<TaxonNameBase
>();
193 for (NameRelationship rel
: rels
){
194 result
.add(rel
.getFromName());
195 result
.add(rel
.getToName());
200 protected boolean doDelete(BerlinModelExportState state
){
201 BerlinModelExportConfigurator bmeConfig
= state
.getConfig();
204 Source destination
= bmeConfig
.getDestination();
206 sql
= "DELETE FROM RelName";
207 destination
.setQuery(sql
);
208 destination
.update(sql
);
215 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
217 protected boolean isIgnore(BerlinModelExportState state
){
218 return ! state
.getConfig().isDoRelNames();
221 //called by MethodMapper
222 @SuppressWarnings("unused")
223 private static Integer
getRelNameQualifierFk(RelationshipBase
<?
, ?
, ?
> rel
) throws Exception
{
224 // if (config.getRelNameQualifierMethod() != null){
226 // return (Integer)config.getRelNameQualifierMethod().invoke(rel);
227 // } catch (Exception e) {
228 // logger.error(e.getMessage());
232 return BerlinModelTransformer
.nameRel2RelNameQualifierFk(rel
);
237 * @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
240 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
241 return standardMethodParameter
;