Project

General

Profile

Download (4.02 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.taxeditor.view.datasource;
11

    
12
import java.util.HashMap;
13
import java.util.Map;
14

    
15
import eu.etaxonomy.cdm.config.CdmSourceException;
16
import eu.etaxonomy.cdm.config.ICdmSource;
17
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
18
import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
19
import eu.etaxonomy.taxeditor.model.MessagingUtils;
20

    
21
/**
22
 * <p>CdmMetaDataAwareDataSourceContainer class.</p>
23
 *
24
 * @author n.hoffmann
25
 * @created Sep 22, 2010
26
 * @version 1.0
27
 */
28
public class CdmMetaDataAwareDataSourceContainer {
29

    
30

    
31

    
32
	/** Constant <code>DEFAULT_ENTRY="-"</code> */
33
	public static final String DEFAULT_ENTRY = "-";
34

    
35
	private ICdmSource cdmSource;
36

    
37
	private boolean running;
38

    
39
	private Map<MetaDataPropertyName, String> metaDataMap;
40

    
41
	/**
42
	 * <p>Constructor for CdmMetaDataAwareDataSourceContainer.</p>
43
	 *
44
	 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
45
	 * @param view a {@link eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart} object.
46
	 */
47
	public CdmMetaDataAwareDataSourceContainer(ICdmSource cdmSource){
48
		this.cdmSource = cdmSource;
49

    
50
		metaDataMap = getDefaultMetaDataMap();
51

    
52
	}
53

    
54
//	/**
55
//	 * <p>Getter for the field <code>dataSource</code>.</p>
56
//	 *
57
//	 * @return the dataSource
58
//	 */
59
//	public ICdmDataSource getDataSource() {
60
//		return dataSource;
61
//	}
62

    
63
	public ICdmSource getCdmSource() {
64
	return cdmSource;
65
}
66

    
67
	public void getMetaDataFromDataSource(){
68
		try {
69
			running = cdmSource.checkConnection();
70
		} catch (Exception e) {
71
			running = false;
72
			for(MetaDataPropertyName metaDataPropertyName : MetaDataPropertyName.values()){
73
				metaDataMap.put(metaDataPropertyName, DEFAULT_ENTRY);
74
			}
75
		}
76

    
77
		try {
78
			if(isRunning()){
79

    
80
				//				try {
81
				metaDataMap = cdmSource.getMetaDataMap();
82
				cdmSource.closeOpenConnections();
83
				//				} catch (CdmSourceException e) {
84
				//					StoreUtil.warn(this.getClass(), "SQLException when trying to access: " + cdmSource.getName() + ". " + e.getMessage());
85
				//				}
86
			}
87
		} catch (Exception e) {
88
            if(e instanceof CdmSourceException && e.getMessage().toLowerCase().contains("\"cdmmetadata\"")){
89
                MessagingUtils.error(this.getClass(), "Data source "+((CdmSourceException) e).getSourceName()+" does not have a CDMMETADATA table", null);
90
            }
91
            else{
92
                MessagingUtils.error(this.getClass(), e);
93
            }
94
		}
95
	}
96

    
97
	private Map<MetaDataPropertyName, String> getDefaultMetaDataMap(){
98
		Map<MetaDataPropertyName, String> resultMap = new HashMap<MetaDataPropertyName, String>(MetaDataPropertyName.values().length);
99

    
100
		for(MetaDataPropertyName metaDataPropertyName : MetaDataPropertyName.values()){
101
			resultMap.put(metaDataPropertyName, null);
102
		}
103

    
104
		return resultMap;
105
	}
106

    
107
	/**
108
	 * <p>Getter for the field <code>metaDataMap</code>.</p>
109
	 *
110
	 * @return the metaDataMap
111
	 */
112
	public Map<MetaDataPropertyName, String> getMetaDataMap() {
113
		return metaDataMap;
114
	}
115

    
116
	/**
117
	 * <p>isDataSourceCompatible</p>
118
	 *
119
	 * @return a boolean.
120
	 */
121
	public boolean isDataSourceCompatible(){
122
		String version = metaDataMap.get(MetaDataPropertyName.DB_SCHEMA_VERSION);
123
		return version == null || version.equals(DEFAULT_ENTRY) ? false : CdmMetaData.isDbSchemaVersionCompatible(version);
124
	}
125

    
126
	/**
127
	 * <p>getMetaData</p>
128
	 *
129
	 * @param metaDataPropertyName a {@link eu.etaxonomy.cdm.model.common.CdmMetaData.MetaDataPropertyName} object.
130
	 * @return a {@link java.lang.String} object.
131
	 */
132
	public String getMetaData(MetaDataPropertyName metaDataPropertyName){
133
		String property = metaDataMap.get(metaDataPropertyName);
134
		return property != null ? property : DEFAULT_ENTRY;
135
	}
136

    
137
	/**
138
	 * <p>isRunning</p>
139
	 *
140
	 * @return the running
141
	 */
142
	public boolean isRunning() {
143
		return running;
144
	}
145

    
146
	/** {@inheritDoc} */
147
	@Override
148
	public String toString() {
149
		return cdmSource.getName();
150
	}
151
}
(5-5/5)