Project

General

Profile

Download (5.06 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 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.config;
11

    
12
import java.util.ArrayList;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Properties;
16

    
17
import org.apache.log4j.Logger;
18
import org.jdom.Attribute;
19
import org.jdom.Element;
20

    
21
import eu.etaxonomy.cdm.common.XmlHelp;
22

    
23
/**
24
 * Cdm Source class which represents a persisted CDM source object
25
 * as configured in the CDM sources config file. 
26
 * 
27
 * @author cmathew
28
 *
29
 */
30
public class CdmPersistentXMLSource { 
31
	private static final Logger logger = Logger.getLogger(CdmPersistentXMLSource.class);
32
	
33
	
34
	/**
35
	 * CDM sources config file name
36
	 */
37
	public final static String CDMSOURCE_FILE_NAME = "cdm.datasources.xml";
38
	
39
	/**
40
	 * Directory path of the template CDM sources config file - this file is used
41
	 * in the case of the very first call to persist sources see {@link eu.etaxonomy.cdm.api.application.CdmApplicationUtils#getWritableResourceDir()}
42
	 */
43
	public final static String CDMSOURCE_PATH = "/eu/etaxonomy/cdm/";
44
	
45
	/**
46
	 * jDom Element represeting the CDM source as stored in the source config file 
47
	 */
48
	private Element bean;
49
	
50
	
51
	/**
52
	 * Post fix represeting the type of source (data / remote)
53
	 */
54
	private String postFix;
55
	
56
	/**
57
	 * Enumeration containg all possible properties of all types of
58
	 * CDM Sources
59
	 * 
60
	 *
61
	 */
62
	public enum CdmSourceProperties {		
63
		URL,
64
		SERVER, 		
65
		PORT,		
66
		FILEPATH,
67
		CONTEXTPATH,
68
		USERNAME,
69
		PASSWORD, 
70
		NOMENCLATURAL_CODE,
71
		DRIVER_CLASS,
72
		DATABASE,
73
		MODE;
74

    
75
		@Override
76
		public String toString(){
77
			switch (this){
78
				case URL:
79
					return "url";
80
				case SERVER:
81
					return "server";
82
				case PORT:
83
					return "port";
84
				case FILEPATH:
85
					return "filePath";
86
				case CONTEXTPATH:
87
					return "contextPath";
88
				case USERNAME:
89
					return "username";
90
				case PASSWORD:
91
					return "password";
92
				case NOMENCLATURAL_CODE:
93
					return "nomenclaturalCode";
94
				case DRIVER_CLASS:
95
					return "driverClassName";
96
				case DATABASE:
97
					return "database";
98
				case MODE:
99
					return "mode";
100
				default: 
101
					throw new IllegalArgumentException( "Unknown enumeration type" );
102
			}
103
		}
104
	}
105
	
106
	private String cdmSourceName;
107
	
108
	/**
109
	 * Constructor which uses the given CDM source name and post fix to initialze the 
110
	 * jDom element from the source config file.
111
	 * 
112
	 * @param cdmSourceName
113
	 * @param postFix
114
	 */
115
	private CdmPersistentXMLSource(String cdmSourceName, String postFix) {
116
		this.cdmSourceName = cdmSourceName;
117
		this.postFix = postFix;
118
		bean = CdmPersistentSourceUtils.getCdmSourceBeanXml(cdmSourceName, postFix);
119
	}
120
	
121
	/**
122
	 * Constructor which uses the given CDM source name and post fix to initialze the 
123
	 * jDom element from the source config file.
124
	 * 
125
	 * @param strDataSourceName
126
	 * @param postFix
127
	 * @return new CdmPersistentXMLSource object
128
	 */
129
	public final static CdmPersistentXMLSource NewInstance(String strDataSourceName, String postFix) {
130
		return new CdmPersistentXMLSource(strDataSourceName, postFix);		
131
	}
132
	
133

    
134
	/**
135
	 * Returns the loaded jDom element representing this object
136
	 * 
137
	 * @return
138
	 */
139
	public Element getElement() {
140
		return bean;
141
	}
142
	
143
	/**
144
	 * Returns the name of the bean Element in the cdm source xml config file.
145
	 * @return bean name
146
	 */
147
	public String getBeanName(){
148
		return CdmPersistentSourceUtils.getBeanName(cdmSourceName, postFix);
149
	}
150
	
151
	/**
152
	 * Returns the list of attributes of this CDM source 
153
	 * that are defined in the cdm source xml config file.    
154
	 * @return 
155
	 */
156
	@SuppressWarnings("unchecked")
157
	public List<Attribute> getCdmSourceAttributes(){
158
		List<Attribute> result = new ArrayList<Attribute>();		
159
		if (bean == null){
160
			return null;
161
		}else{
162
			result = bean.getAttributes();
163
		}
164
		return result;
165
	}	
166
	
167
	/**
168
	 * Returns a defined property of the cdm source xml config file.    
169
	 * @return the property of the cdm source or null if the datasource bean or the property does not exist.
170
	 */
171
	public String getCdmSourceProperty(String property){
172
		
173
		if (bean == null){
174
			return null;
175
		}else{
176
			Element elProperty = XmlHelp.getFirstAttributedChild(bean, "property", "name", property);
177
			if (elProperty == null){
178
				logger.warn("Unknown property: " + property);
179
		    	return null;
180
			}else{
181
				String strValue = elProperty.getAttributeValue("value");
182
				return strValue;
183
			}
184
		}
185
	}
186
	
187
	/**
188
	 * Returns the list of properties that are defined in the 
189
	 * cdm source xml config file.
190
	 *  
191
	 * @return 
192
	 */
193
	public Properties getCdmSourceProperties(){
194
		Properties result = new Properties();		
195
		if (bean == null){
196
			return null;
197
		}else{
198
			List<Element> elProperties = XmlHelp.getAttributedChildList(bean, "property", "name");
199
			Iterator<Element> iterator = elProperties.iterator();
200
			while(iterator.hasNext()){
201
				Element next = iterator.next();
202
				String strName = next.getAttributeValue("name");
203
				String strValue = next.getAttributeValue("value");
204
				result.put(strName, strValue);
205
			}
206
		}
207
		return result;
208
	}
209

    
210
}
(2-2/7)