merging in latest changes from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / datasource / CdmMetaDataAwareDataSourceContainer.java
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.sql.SQLException;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import eu.etaxonomy.cdm.database.ICdmDataSource;
18 import eu.etaxonomy.cdm.model.common.CdmMetaData;
19 import eu.etaxonomy.cdm.model.common.CdmMetaData.MetaDataPropertyName;
20 import eu.etaxonomy.taxeditor.store.StoreUtil;
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 ICdmDataSource dataSource;
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(ICdmDataSource dataSource){
49 this.dataSource = dataSource;
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 void getMetaDataFromDataSource(){
65 try {
66 running = dataSource.testConnection();
67 } catch (Exception e) {
68 running = false;
69 for(MetaDataPropertyName metaDataPropertyName : MetaDataPropertyName.values()){
70 metaDataMap.put(metaDataPropertyName, DEFAULT_ENTRY);
71 }
72 }
73
74 try {
75 if(isRunning()){
76
77 try {
78 for(MetaDataPropertyName metaDataPropertyName : MetaDataPropertyName.values()){
79 Object result = dataSource.getSingleValue(metaDataPropertyName.getSqlQuery());
80 if(result != null){
81 metaDataMap.put(metaDataPropertyName, (String) result);
82 }
83 }
84 dataSource.closeOpenConnections();
85 } catch (SQLException e) {
86 StoreUtil.warn(this.getClass(), "SQLException when trying to access: " + dataSource.getName() + ". " + e.getMessage());
87 }
88 }
89 } catch (Exception e) {
90 StoreUtil.error(this.getClass(), e);
91 }
92 }
93
94 private Map<MetaDataPropertyName, String> getDefaultMetaDataMap(){
95 Map<MetaDataPropertyName, String> resultMap = new HashMap<MetaDataPropertyName, String>(MetaDataPropertyName.values().length);
96
97 for(MetaDataPropertyName metaDataPropertyName : MetaDataPropertyName.values()){
98 resultMap.put(metaDataPropertyName, null);
99 }
100
101 return resultMap;
102 }
103
104 /**
105 * <p>Getter for the field <code>metaDataMap</code>.</p>
106 *
107 * @return the metaDataMap
108 */
109 public Map<MetaDataPropertyName, String> getMetaDataMap() {
110 return metaDataMap;
111 }
112
113 /**
114 * <p>isDataSourceCompatible</p>
115 *
116 * @return a boolean.
117 */
118 public boolean isDataSourceCompatible(){
119 String version = metaDataMap.get(MetaDataPropertyName.DB_SCHEMA_VERSION);
120 return version == null || version.equals(DEFAULT_ENTRY) ? false : CdmMetaData.isDbSchemaVersionCompatible(version);
121 }
122
123 /**
124 * <p>getMetaData</p>
125 *
126 * @param metaDataPropertyName a {@link eu.etaxonomy.cdm.model.common.CdmMetaData.MetaDataPropertyName} object.
127 * @return a {@link java.lang.String} object.
128 */
129 public String getMetaData(MetaDataPropertyName metaDataPropertyName){
130 String property = metaDataMap.get(metaDataPropertyName);
131 return property != null ? property : DEFAULT_ENTRY;
132 }
133
134 /**
135 * <p>isRunning</p>
136 *
137 * @return the running
138 */
139 public boolean isRunning() {
140 return running;
141 }
142
143 /* (non-Javadoc)
144 * @see java.lang.Object#toString()
145 */
146 /** {@inheritDoc} */
147 @Override
148 public String toString() {
149 return dataSource.getName();
150 }
151 }