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
.application
.CdmApplicationController
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
23 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
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("defaultExport")
38 public class CdmApplicationAwareDefaultExport
<T
extends IExportConfigurator
> implements ICdmExport
<T
>, ApplicationContextAware
{
39 private static final Logger logger
= Logger
.getLogger(CdmApplicationAwareDefaultExport
.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 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 CdmApplicationAwareDefaultExport(){
63 stores
.put(ICdmIO
.TEAM_STORE
, new MapWrapper
<TeamOrPersonBase
>(service
));
64 stores
.put(ICdmIO
.REFERENCE_STORE
, new MapWrapper
<ReferenceBase
>(service
));
65 stores
.put(ICdmIO
.NOMREF_STORE
, new MapWrapper
<ReferenceBase
>(service
));
66 stores
.put(ICdmIO
.NOMREF_DETAIL_STORE
, new MapWrapper
<ReferenceBase
>(service
));
67 stores
.put(ICdmIO
.REF_DETAIL_STORE
, new MapWrapper
<ReferenceBase
>(service
));
68 stores
.put(ICdmIO
.TAXONNAME_STORE
, new MapWrapper
<TaxonNameBase
>(service
));
69 stores
.put(ICdmIO
.TAXON_STORE
, new MapWrapper
<TaxonBase
>(service
));
70 stores
.put(ICdmIO
.SPECIMEN_STORE
, new MapWrapper
<Specimen
>(service
));
74 public boolean invoke(IExportConfigurator config
){
75 if (config
.getCheck().equals(IExportConfigurator
.CHECK
.CHECK_ONLY
)){
76 return doCheck(config
);
77 }else if (config
.getCheck().equals(IExportConfigurator
.CHECK
.CHECK_AND_EXPORT
)){
79 return doExport(config
);
80 }else if (config
.getCheck().equals(IExportConfigurator
.CHECK
.EXPORT_WITHOUT_CHECK
)){
81 return doExport(config
);
83 logger
.error("Unknown CHECK type");
89 @SuppressWarnings("unchecked")
90 protected <S
extends IExportConfigurator
> boolean doCheck(S config
){
91 boolean result
= true;
92 System
.out
.println("Start checking Source ("+ config
.getSourceNameString() + ") ...");
96 logger
.warn("CdmExportConfiguration is null");
98 }else if (! config
.isValid()){
99 logger
.warn("CdmExportConfiguration is not valid");
103 //do check for each class
104 for (Class
<ICdmIO
> ioClass
: config
.getIoClassList()){
106 String ioBeanName
= getComponentBeanName(ioClass
);
107 ICdmIO
<S
> cdmIo
= (ICdmIO
<S
>)applicationContext
.getBean(ioBeanName
, ICdmIO
.class);
109 result
&= cdmIo
.check(config
);
111 logger
.error("cdmIO for class " + (ioClass
== null ?
"(null)" : ioClass
.getSimpleName()) + " was null");
114 } catch (Exception e
) {
122 System
.out
.println("End checking Source ("+ config
.getSourceNameString() + ") for export from Cdm");
131 protected <S
extends IExportConfigurator
> boolean doExport(S config
){
132 boolean result
= true;
135 logger
.warn("Configuration is null");
137 }else if (! config
.isValid()){
138 logger
.warn("Configuration is not valid");
142 System
.out
.println("Start export from source '" + config
.getSourceNameString()
143 + "' to destination '" + config
.getDestinationNameString() + "'");
145 //do invoke for each class
146 for (Class
<ICdmIO
> ioClass
: config
.getIoClassList()){
148 String ioBeanName
= getComponentBeanName(ioClass
);
149 ICdmIO
<S
> cdmIo
= (ICdmIO
<S
>)applicationContext
.getBean(ioBeanName
, ICdmIO
.class);
151 result
&= cdmIo
.invoke(config
, stores
);
152 // IoState<S> state = null;
153 // result &= cdmIo.invoke(state);
155 logger
.error("cdmIO was null");
158 } catch (Exception e
) {
165 //do invoke for each class
166 // for (String ioBean: config.getIoBeans()){
168 // ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
169 // if (cdmIo != null){
170 // result &= cdmIo.invoke(config, stores);
172 // logger.error("cdmIO was null");
175 // } catch (Exception e) {
177 // e.printStackTrace();
184 System
.out
.println("End export from source '" + config
.getSourceNameString()
185 + "' to destination '" + config
.getDestinationNameString() + "'");
189 private String
getComponentBeanName(Class
<ICdmIO
> ioClass
){
190 Component component
= ioClass
.getAnnotation(Component
.class);
191 String ioBean
= component
.value();
192 if ("".equals(ioBean
)){
193 ioBean
= ioClass
.getSimpleName();
194 ioBean
= ioBean
.substring(0, 1).toLowerCase() + ioBean
.substring(1); //make camelcase