Project

General

Profile

Download (4.03 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.view.datasource;
12

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

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

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

    
31

    
32

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

    
36
	private ICdmSource cdmSource;
37

    
38
	private boolean running;
39

    
40
	private Map<MetaDataPropertyName, String> metaDataMap;
41

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

    
51
		metaDataMap = getDefaultMetaDataMap();
52

    
53
	}
54

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

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

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

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

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

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

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

    
105
		return resultMap;
106
	}
107

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

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

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

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

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