Project

General

Profile

Download (6.72 KB) Statistics
| Branch: | Tag: | Revision:
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.service.IService;
22
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
23
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
26
import eu.etaxonomy.cdm.model.occurrence.Specimen;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
29

    
30
/**
31
 * This class is an default exporter class that is a spring bean and therefore it knows all other IO classes that are beans
32
 * 
33
 * @author a.mueller
34
 * @created 20.06.2008
35
 * @version 1.0
36
 */
37

    
38
@Component("defaultExport")
39
public class CdmApplicationAwareDefaultExport<T extends IExportConfigurator> implements ICdmExporter<T>, ApplicationContextAware {
40
	private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultExport.class);
41

    
42
	protected ApplicationContext applicationContext;
43
	
44
	/* (non-Javadoc)
45
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
46
	 */
47
	public void setApplicationContext(ApplicationContext applicationContext)
48
			throws BeansException {
49
		this.applicationContext = applicationContext;
50
	}
51

    
52
//	DbExportStateBase<T> state;
53

    
54
	
55
	//Constants
56
	final static boolean OBLIGATORY = true; 
57
	final static boolean FACULTATIVE = false; 
58
	final int modCount = 1000;
59

    
60
	private IService service = null;
61
	
62
	//different type of stores that are used by the known imports
63
	Map<String, MapWrapper<? extends CdmBase>> stores = new HashMap<String, MapWrapper<? extends CdmBase>>();
64

    
65
	public CdmApplicationAwareDefaultExport(){
66
		stores.put(ICdmIO.TEAM_STORE, new MapWrapper<TeamOrPersonBase>(service));
67
		stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<Reference>(service));
68
		stores.put(ICdmIO.NOMREF_STORE, new MapWrapper<Reference>(service));
69
		stores.put(ICdmIO.NOMREF_DETAIL_STORE, new MapWrapper<Reference>(service));
70
		stores.put(ICdmIO.REF_DETAIL_STORE, new MapWrapper<Reference>(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));
74
	}
75
	
76
	
77
	public boolean invoke(IExportConfigurator config){
78
		if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_ONLY)){
79
			return doCheck(config);
80
		}else if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_AND_EXPORT)){
81
			doCheck(config);
82
			return doExport(config);
83
		}else if (config.getCheck().equals(IExportConfigurator.CHECK.EXPORT_WITHOUT_CHECK)){
84
			return doExport(config);
85
		}else{
86
			logger.error("Unknown CHECK type");
87
			return false;
88
		}
89
	}
90
	
91
	
92
	@SuppressWarnings("unchecked")
93
	protected <S extends IExportConfigurator> boolean doCheck(S  config){
94
		boolean result = true;
95
		
96
		//check
97
		if (config == null){
98
			logger.warn("CdmExportConfiguration is null");
99
			return false;
100
		}else if (! config.isValid()){
101
			logger.warn("CdmExportConfiguration is not valid");
102
			return false;
103
		}
104
		System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
105
		
106
		ExportStateBase state = config.getNewState();
107
		state.initialize(config);
108
		
109
		//do check for each class
110
		for (Class<ICdmExport> ioClass: config.getIoClassList()){
111
			try {
112
				String ioBeanName = getComponentBeanName(ioClass);
113
				ICdmIO cdmIo = applicationContext.getBean(ioBeanName, ICdmIO.class);
114
				if (cdmIo != null){
115
					registerObservers(config, cdmIo);
116
					result &= cdmIo.check(state);
117
					unRegisterObservers(config, cdmIo);
118
				}else{
119
					logger.error("cdmIO for class " + (ioClass == null ? "(null)" : ioClass.getSimpleName()) + " was null");
120
					result = false;
121
				}
122
			} catch (Exception e) {
123
					logger.error(e);
124
					e.printStackTrace();
125
					result = false;
126
			}
127
		}
128
		
129
		//return
130
		System.out.println("End checking Source ("+ config.getSourceNameString() + ") for export from Cdm");
131
		return result;
132

    
133
	}
134
	
135
	
136
	private void registerObservers(IExportConfigurator config, ICdmIO io){
137
		for (IIoObserver observer : config.getObservers()){
138
			io.addObserver(observer);
139
		}
140
	}
141
	
142
	private void unRegisterObservers(IExportConfigurator config, ICdmIO io){
143
		for (IIoObserver observer : config.getObservers()){
144
			io.deleteObserver(observer);
145
		}
146
	}
147
	
148
	
149
	/**
150
	 * Executes the whole 
151
	 */
152
	protected <CONFIG extends IExportConfigurator>  boolean doExport(CONFIG config){
153
		boolean result = true;
154
		//validate
155
		if (config == null){
156
			logger.warn("Configuration is null");
157
			return false;
158
		}else if (! config.isValid()){
159
			logger.warn("Configuration is not valid");
160
			return false;
161
		}
162
			
163
		System.out.println("Start export from source '" + config.getSourceNameString() 
164
				+ "' to destination '" + config.getDestinationNameString() + "'");
165
		
166
		ExportStateBase state = config.getNewState();
167
		state.initialize(config);
168
		
169
		//do invoke for each class
170
		for (Class<ICdmExport> ioClass: config.getIoClassList()){
171
			try {
172
				String ioBeanName = getComponentBeanName(ioClass);
173
				ICdmExport cdmIo = (ICdmExport)applicationContext.getBean(ioBeanName, ICdmIO.class);
174
				if (cdmIo != null){
175
					//result &= cdmIo.invoke(config, stores);
176
					state.setCurrentIO(cdmIo);
177
					result &= cdmIo.invoke(state);
178
//					IoState<S> state = null;
179
//					result &= cdmIo.invoke(state);
180
				}else{
181
					logger.error("cdmIO was null");
182
					result = false;
183
				}
184
			} catch (Exception e) {
185
					logger.error(e);
186
					e.printStackTrace();
187
					result = false;
188
			}
189
		}
190
		
191
		System.out.println("End export from source '" + config.getSourceNameString() 
192
				+ "' to destination '" + config.getDestinationNameString() + "' " +
193
				(result? "(successful)":"(with errors)")) ;
194
		return result;
195
	}
196
	
197
	private String getComponentBeanName(Class<ICdmExport> ioClass){
198
		Component component = ioClass.getAnnotation(Component.class);
199
		String ioBean = component.value();
200
		if ("".equals(ioBean)){
201
			ioBean = ioClass.getSimpleName();
202
			ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
203
		}
204
		return ioBean;
205
	}
206

    
207
}
(3-3/48)