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