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.
10 package eu
.etaxonomy
.cdm
.io
.common
;
12 import java
.util
.HashMap
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.springframework
.beans
.BeansException
;
17 import org
.springframework
.context
.ApplicationContext
;
18 import org
.springframework
.context
.ApplicationContextAware
;
19 import org
.springframework
.stereotype
.Component
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
22 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelImportState
;
23 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
24 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
27 import eu
.etaxonomy
.cdm
.model
.occurrence
.Specimen
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 @Component("defaultImport")
38 public class CdmApplicationAwareDefaultImport
<T
extends IImportConfigurator
> implements ICdmImporter
<T
>, ApplicationContextAware
{
39 private static final Logger logger
= Logger
.getLogger(CdmApplicationAwareDefaultImport
.class);
41 protected ApplicationContext applicationContext
;
44 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
46 public void setApplicationContext(ApplicationContext applicationContext
)
47 throws BeansException
{
48 this.applicationContext
= applicationContext
;
53 final boolean OBLIGATORY
= true;
54 final boolean FACULTATIVE
= false;
55 final int modCount
= 1000;
57 IService
<CdmBase
> service
= null;
59 //different type of stores that are used by the known imports
60 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
= new HashMap
<String
, MapWrapper
<?
extends CdmBase
>>();
62 public CdmApplicationAwareDefaultImport(){
65 stores
.put(ICdmIO
.PERSON_STORE
, new MapWrapper
<Person
>(service
));
66 stores
.put(ICdmIO
.TEAM_STORE
, new MapWrapper
<TeamOrPersonBase
<?
>>(service
));
67 stores
.put(ICdmIO
.REFERENCE_STORE
, new MapWrapper
<ReferenceBase
>(service
));
68 stores
.put(ICdmIO
.NOMREF_STORE
, new MapWrapper
<ReferenceBase
>(service
));
69 stores
.put(ICdmIO
.NOMREF_DETAIL_STORE
, new MapWrapper
<ReferenceBase
>(service
));
70 stores
.put(ICdmIO
.REF_DETAIL_STORE
, new MapWrapper
<ReferenceBase
>(service
));
71 stores
.put(ICdmIO
.TAXONNAME_STORE
, new MapWrapper
<TaxonNameBase
<?
,?
>>(service
));
72 stores
.put(ICdmIO
.TAXON_STORE
, new MapWrapper
<TaxonBase
>(service
));
73 stores
.put(ICdmIO
.SPECIMEN_STORE
, new MapWrapper
<Specimen
>(service
));
76 public boolean invoke(IImportConfigurator config
){
77 if (config
.getCheck().equals(IImportConfigurator
.CHECK
.CHECK_ONLY
)){
78 return doCheck(config
);
79 }else if (config
.getCheck().equals(IImportConfigurator
.CHECK
.CHECK_AND_IMPORT
)){
81 return doImport(config
);
82 }else if (config
.getCheck().equals(IImportConfigurator
.CHECK
.IMPORT_WITHOUT_CHECK
)){
83 return doImport(config
);
85 logger
.error("Unknown CHECK type");
91 @SuppressWarnings("unchecked")
92 protected <S
extends IImportConfigurator
> boolean doCheck(S config
){
93 boolean result
= true;
94 System
.out
.println("Start checking Source ("+ config
.getSourceNameString() + ") ...");
98 logger
.warn("CdmImportConfiguration is null");
100 }else if (! config
.isValid()){
101 logger
.warn("CdmImportConfiguration is not valid");
105 ImportStateBase state
= config
.getNewState();
106 state
.initialize(config
);
108 //do check for each class
109 for (Class
<ICdmIO
> ioClass
: config
.getIoClassList()){
111 String ioBeanName
= getComponentBeanName(ioClass
);
112 ICdmIO cdmIo
= (ICdmIO
)applicationContext
.getBean(ioBeanName
, ICdmIO
.class);
114 result
&= cdmIo
.check(state
);
116 logger
.error("cdmIO was null");
119 } catch (Exception e
) {
127 System
.out
.println("End checking Source ("+ config
.getSourceNameString() + ") for import to Cdm");
136 protected <S
extends IImportConfigurator
> boolean doImport(S config
){
137 boolean result
= true;
140 logger
.warn("Configuration is null");
142 }else if (! config
.isValid()){
143 logger
.warn("Configuration is not valid");
147 ReferenceBase sourceReference
= config
.getSourceReference();
148 logger
.info("Start import from Source '"+ config
.getSourceNameString() + "' to destination '" + config
.getDestinationNameString() + "'");
150 ImportStateBase state
= config
.getNewState();
151 state
.initialize(config
);
154 //do invoke for each class
155 for (Class
<ICdmIO
> ioClass
: config
.getIoClassList()){
157 String ioBeanName
= getComponentBeanName(ioClass
);
158 ICdmIO cdmIo
= (ICdmIO
)applicationContext
.getBean(ioBeanName
, ICdmIO
.class);
160 // result &= cdmIo.invoke(config, stores);
161 result
&= cdmIo
.invoke(state
);
163 logger
.error("cdmIO was null");
166 } catch (Exception e
) {
173 //do invoke for each class
174 // for (String ioBean: config.getIoBeans()){
176 // ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
177 // if (cdmIo != null){
178 // result &= cdmIo.invoke(config, stores);
180 // logger.error("cdmIO was null");
183 // } catch (Exception e) {
185 // e.printStackTrace();
192 logger
.info("End import from source '" + config
.getSourceNameString()
193 + "' to destination '" + config
.getDestinationNameString() + "'"+
194 (result?
"(successful)":"(with errors)")) ;
198 private String
getComponentBeanName(Class
<ICdmIO
> ioClass
){
199 Component component
= ioClass
.getAnnotation(Component
.class);
200 String ioBean
= component
.value();
201 if ("".equals(ioBean
)){
202 ioBean
= ioClass
.getSimpleName();
203 ioBean
= ioBean
.substring(0, 1).toLowerCase() + ioBean
.substring(1); //make camelcase