Berlin Model import ->in and import state class
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / CdmApplicationAwareDefaultExport.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.io.common;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
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;
20
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;
30
31 /**
32 * @author a.mueller
33 * @created 20.06.2008
34 * @version 1.0
35 */
36
37 @Component("defaultExport")
38 public class CdmApplicationAwareDefaultExport<T extends IExportConfigurator> implements ICdmExport<T>, ApplicationContextAware {
39 private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultExport.class);
40
41 protected ApplicationContext applicationContext;
42
43 /* (non-Javadoc)
44 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
45 */
46 public void setApplicationContext(ApplicationContext applicationContext)
47 throws BeansException {
48 this.applicationContext = applicationContext;
49 }
50
51
52 //Constants
53 final boolean OBLIGATORY = true;
54 final boolean FACULTATIVE = false;
55 final int modCount = 1000;
56
57 IService service = null;
58
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>>();
61
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));
71 }
72
73
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)){
78 doCheck(config);
79 return doExport(config);
80 }else if (config.getCheck().equals(IExportConfigurator.CHECK.EXPORT_WITHOUT_CHECK)){
81 return doExport(config);
82 }else{
83 logger.error("Unknown CHECK type");
84 return false;
85 }
86 }
87
88
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() + ") ...");
93
94 //check
95 if (config == null){
96 logger.warn("CdmExportConfiguration is null");
97 return false;
98 }else if (! config.isValid()){
99 logger.warn("CdmExportConfiguration is not valid");
100 return false;
101 }
102
103 //do check for each class
104 for (Class<ICdmIO> ioClass: config.getIoClassList()){
105 try {
106 String ioBeanName = getComponentBeanName(ioClass);
107 ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBeanName, ICdmIO.class);
108 if (cdmIo != null){
109 result &= cdmIo.check(config);
110 }else{
111 logger.error("cdmIO for class " + (ioClass == null ? "(null)" : ioClass.getSimpleName()) + " was null");
112 result = false;
113 }
114 } catch (Exception e) {
115 logger.error(e);
116 e.printStackTrace();
117 result = false;
118 }
119 }
120
121 //return
122 System.out.println("End checking Source ("+ config.getSourceNameString() + ") for export from Cdm");
123 return result;
124
125 }
126
127
128 /**
129 * Executes the whole
130 */
131 protected <S extends IExportConfigurator> boolean doExport(S config){
132 boolean result = true;
133 //validate
134 if (config == null){
135 logger.warn("Configuration is null");
136 return false;
137 }else if (! config.isValid()){
138 logger.warn("Configuration is not valid");
139 return false;
140 }
141
142 System.out.println("Start export from source '" + config.getSourceNameString()
143 + "' to destination '" + config.getDestinationNameString() + "'");
144
145 //do invoke for each class
146 for (Class<ICdmIO> ioClass: config.getIoClassList()){
147 try {
148 String ioBeanName = getComponentBeanName(ioClass);
149 ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBeanName, ICdmIO.class);
150 if (cdmIo != null){
151 result &= cdmIo.invoke(config, stores);
152 // IoState<S> state = null;
153 // result &= cdmIo.invoke(state);
154 }else{
155 logger.error("cdmIO was null");
156 result = false;
157 }
158 } catch (Exception e) {
159 logger.error(e);
160 e.printStackTrace();
161 result = false;
162 }
163 }
164
165 //do invoke for each class
166 // for (String ioBean: config.getIoBeans()){
167 // try {
168 // ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
169 // if (cdmIo != null){
170 // result &= cdmIo.invoke(config, stores);
171 // }else{
172 // logger.error("cdmIO was null");
173 // result = false;
174 // }
175 // } catch (Exception e) {
176 // logger.error(e);
177 // e.printStackTrace();
178 // result = false;
179 // }
180 //
181 // }
182
183
184 System.out.println("End export from source '" + config.getSourceNameString()
185 + "' to destination '" + config.getDestinationNameString() + "'");
186 return result;
187 }
188
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
195 }
196 return ioBean;
197 }
198
199 }