Project

General

Profile

Download (7.54 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
import java.util.Set;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.beans.BeansException;
18
import org.springframework.context.ApplicationContext;
19
import org.springframework.context.ApplicationContextAware;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.api.service.IService;
23
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
24
import eu.etaxonomy.cdm.model.agent.Person;
25
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
28
import eu.etaxonomy.cdm.model.occurrence.Specimen;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31

    
32
/**
33
 * @author a.mueller
34
 * @created 20.06.2008
35
 * @version 1.0
36
 */
37

    
38
@Component("defaultImport")
39
public class CdmApplicationAwareDefaultImport<T extends IImportConfigurator> implements ICdmImporter<T>, ApplicationContextAware {
40
	private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultImport.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

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

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

    
63
	public CdmApplicationAwareDefaultImport(){
64
		
65
		
66
		stores.put(ICdmIO.PERSON_STORE, new MapWrapper<Person>(service));
67
		stores.put(ICdmIO.TEAM_STORE, new MapWrapper<TeamOrPersonBase<?>>(service));
68
		stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<Reference>(service));
69
		stores.put(ICdmIO.NOMREF_STORE, new MapWrapper<Reference>(service));
70
		stores.put(ICdmIO.NOMREF_DETAIL_STORE, new MapWrapper<Reference>(service));
71
		stores.put(ICdmIO.REF_DETAIL_STORE, new MapWrapper<Reference>(service));
72
		stores.put(ICdmIO.TAXONNAME_STORE, new MapWrapper<TaxonNameBase<?,?>>(service));
73
		stores.put(ICdmIO.TAXON_STORE, new MapWrapper<TaxonBase>(service));
74
		stores.put(ICdmIO.SPECIMEN_STORE, new MapWrapper<Specimen>(service));
75
	}
76
	
77
	public boolean invoke(IImportConfigurator config){
78
		if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){
79
			return doCheck(config);
80
		}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){
81
			doCheck(config);
82
			return doImport(config);
83
		}else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){
84
			return doImport(config);
85
		}else{
86
			logger.error("Unknown CHECK type");
87
			return false;
88
		}
89
	}
90
	
91
	
92
	@SuppressWarnings("unchecked")
93
	protected <S extends IImportConfigurator> boolean doCheck(S  config){
94
		boolean result = true;
95
		
96
		//check
97
		if (config == null){
98
			logger.warn("CdmImportConfiguration is null");
99
			return false;
100
		}
101
		System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
102
		if (! config.isValid()){
103
			logger.warn("CdmImportConfiguration is not valid");
104
			return false;
105
		}
106
		
107
		ImportStateBase state = config.getNewState();
108
		state.initialize(config);
109
		
110
		//do check for each class
111
		for (Class<ICdmIO> ioClass: config.getIoClassList()){
112
			try {
113
				String ioBeanName = getComponentBeanName(ioClass);
114
				ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class);
115
				if (cdmIo != null){
116
					registerObservers(config, 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
//					result &= cdmIo.invoke(config, stores);
178
					state.setCurrentIO(cdmIo);
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
		//do invoke for each class
192
//		for (String ioBean: config.getIoBeans()){
193
//			try {
194
//				ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
195
//				if (cdmIo != null){
196
//					result &= cdmIo.invoke(config, stores);
197
//				}else{
198
//					logger.error("cdmIO was null");
199
//					result = false;
200
//				}
201
//			} catch (Exception e) {
202
//					logger.error(e);
203
//					e.printStackTrace();
204
//					result = false;
205
//			}
206
//			
207
//		}
208
		
209
		logger.info("End import from source '" + config.getSourceNameString() 
210
				+ "' to destination '" + config.getDestinationNameString() + "'"+
211
				(result? "(successful)":"(with errors)")) ;
212
		return result;
213
	}
214

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

    
235
}
(4-4/48)