Project

General

Profile

Download (7.58 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.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.Reference;
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("defaultImport")
38
public class CdmApplicationAwareDefaultImport<T extends IImportConfigurator> implements ICdmImporter<T>, ApplicationContextAware {
39
	private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultImport.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<CdmBase> 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 CdmApplicationAwareDefaultImport(){
63
		
64
		
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<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
	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)){
80
			doCheck(config);
81
			return doImport(config);
82
		}else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){
83
			return doImport(config);
84
		}else{
85
			logger.error("Unknown CHECK type");
86
			return false;
87
		}
88
	}
89
	
90
	
91
	@SuppressWarnings("unchecked")
92
	protected <S extends IImportConfigurator> boolean doCheck(S  config){
93
		boolean result = true;
94
		
95
		//check
96
		if (config == null){
97
			logger.warn("CdmImportConfiguration is null");
98
			return false;
99
		}
100
		System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
101
		if (! config.isValid()){
102
			logger.warn("CdmImportConfiguration is not valid");
103
			return false;
104
		}
105
		
106
		ImportStateBase state = config.getNewState();
107
		state.initialize(config);
108
		
109
		//do check for each class
110
		for (Class<ICdmIO> ioClass: config.getIoClassList()){
111
			try {
112
				String ioBeanName = getComponentBeanName(ioClass);
113
				ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class);
114
				if (cdmIo != null){
115
					registerObservers(config, cdmIo);
116
					state.setCurrentIO(cdmIo);
117
					result &= cdmIo.check(state);
118
					unRegisterObservers(config, cdmIo);
119
				}else{
120
					logger.error("cdmIO was null");
121
					result = false;
122
				}
123
			} catch (Exception e) {
124
					logger.error(e);
125
					e.printStackTrace();
126
					result = false;
127
			}
128
		}
129
		
130
		//return
131
		System.out.println("End checking Source ("+ config.getSourceNameString() + ") for import to Cdm");
132
		return result;
133

    
134
	}
135
	
136
	private void registerObservers(IImportConfigurator config, ICdmIO io){
137
		for (IIoObserver observer : config.getObservers()){
138
			io.addObserver(observer);
139
		}
140
	}
141
	
142
	private void unRegisterObservers(IImportConfigurator 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 <S extends IImportConfigurator>  boolean doImport(S 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
		Reference sourceReference = config.getSourceReference();
164
		logger.info("Start import from Source '"+ config.getSourceNameString() + "' to destination '" + config.getDestinationNameString() + "'");
165
		
166
		
167
		ImportStateBase state = config.getNewState();
168
		state.initialize(config);
169

    
170
		state.setSuccess(true);
171
		//do invoke for each class
172
		for (Class<ICdmIO> ioClass: config.getIoClassList()){
173
			try {
174
				String ioBeanName = getComponentBeanName(ioClass);
175
				ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class);
176
				if (cdmIo != null){
177
					registerObservers(config, cdmIo);
178
					state.setCurrentIO(cdmIo);
179
					result &= cdmIo.invoke(state);
180
					unRegisterObservers(config, cdmIo);
181
				}else{
182
					logger.error("cdmIO was null");
183
					result = false;
184
				}
185
			} catch (Exception e) {
186
					logger.error(e);
187
					e.printStackTrace();
188
					result = false;
189
			}
190
		}
191
		
192
		//do invoke for each class
193
//		for (String ioBean: config.getIoBeans()){
194
//			try {
195
//				ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
196
//				if (cdmIo != null){
197
//					result &= cdmIo.invoke(config, stores);
198
//				}else{
199
//					logger.error("cdmIO was null");
200
//					result = false;
201
//				}
202
//			} catch (Exception e) {
203
//					logger.error(e);
204
//					e.printStackTrace();
205
//					result = false;
206
//			}
207
//			
208
//		}
209
		
210
		logger.info("End import from source '" + config.getSourceNameString() 
211
				+ "' to destination '" + config.getDestinationNameString() + "'"+
212
				(result? "(successful)":"(with errors)")) ;
213
		return result;
214
	}
215

    
216
	/**
217
	 * Returns the name of a component bean. If the name is defined in the Component annotation this name is returned.
218
	 * Otherwise the class name is returned with starting lower case.
219
	 * @param ioClass
220
	 * @return
221
	 * @throws IllegalArgumentException if the class does not have a "Component" annotation
222
	 */
223
	public static String getComponentBeanName(Class<ICdmIO> ioClass) throws IllegalArgumentException {
224
		Component component = ioClass.getAnnotation(Component.class);
225
		if (component == null){
226
			throw new IllegalArgumentException("Class " + ioClass.getName() + " is missing a @Component annotation." );
227
		}
228
		String ioBean = component.value();
229
		if ("".equals(ioBean)){
230
			ioBean = ioClass.getSimpleName();
231
			ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
232
		}
233
		return ioBean;
234
	}
235

    
236
}
(4-4/48)