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.remote;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.common.XmlHelp.getBeansRoot;
|
13
|
import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlBean;
|
14
|
import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlValueProperty;
|
15
|
import static eu.etaxonomy.cdm.common.XmlHelp.saveToXml;
|
16
|
|
17
|
import java.util.Enumeration;
|
18
|
import java.util.List;
|
19
|
import java.util.Properties;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.jdom.Attribute;
|
23
|
import org.jdom.Element;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
26
|
import eu.etaxonomy.cdm.common.XmlHelp;
|
27
|
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
|
28
|
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource;
|
29
|
import eu.etaxonomy.cdm.config.ICdmPersistentSource;
|
30
|
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
|
31
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
32
|
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
|
33
|
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
|
34
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
35
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
36
|
import eu.etaxonomy.cdm.database.types.IDatabaseType;
|
37
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
38
|
|
39
|
/**
|
40
|
* Class representing a CDM remote source which can be persisted to a source config file
|
41
|
*
|
42
|
*/
|
43
|
public class CdmPersistentRemoteSource extends CdmRemoteSourceBase implements ICdmPersistentSource{
|
44
|
private static final Logger logger = Logger.getLogger(CdmPersistentRemoteSource.class);
|
45
|
|
46
|
public static final String REMOTESOURCE_BEAN_POSTFIX = "RemoteSource";
|
47
|
private String beanName;
|
48
|
private Properties cdmSourceProperties;
|
49
|
private List<Attribute> cdmSourceAttributes;
|
50
|
|
51
|
/**
|
52
|
* Creates a new instance of CdmPersistentRemoteSource
|
53
|
*
|
54
|
* @param name
|
55
|
* @param server
|
56
|
* @param port
|
57
|
* @param contextPath
|
58
|
* @param nomenclaturalCode
|
59
|
* @return
|
60
|
* @throws CdmRemoteSourceException
|
61
|
*/
|
62
|
public static CdmPersistentRemoteSource NewInstance(String remoteSource) throws CdmRemoteSourceException {
|
63
|
if(exists(remoteSource)) {
|
64
|
return new CdmPersistentRemoteSource(remoteSource);
|
65
|
} else {
|
66
|
throw new CdmRemoteSourceException("Remote source '" + remoteSource + "' does not exist in sources setting file");
|
67
|
}
|
68
|
|
69
|
}
|
70
|
|
71
|
/**S
|
72
|
* Creates a new CdmPersistentRemoteSource
|
73
|
*
|
74
|
* @param name
|
75
|
* @param server
|
76
|
* @param port
|
77
|
* @param contextPath
|
78
|
* @param nomenclaturalCode
|
79
|
* @throws CdmRemoteSourceException
|
80
|
*/
|
81
|
private CdmPersistentRemoteSource(String remoteSource) throws CdmRemoteSourceException {
|
82
|
setName(remoteSource);
|
83
|
CdmPersistentXMLSource cdmPersistentXMLSource = CdmPersistentXMLSource.NewInstance(remoteSource, REMOTESOURCE_BEAN_POSTFIX);
|
84
|
if(cdmPersistentXMLSource.getElement() != null) {
|
85
|
beanName = cdmPersistentXMLSource.getBeanName();
|
86
|
// properties from the persistent xml file
|
87
|
cdmSourceProperties = cdmPersistentXMLSource.getCdmSourceProperties();
|
88
|
cdmSourceAttributes = cdmPersistentXMLSource.getCdmSourceAttributes();
|
89
|
checkProperties(cdmSourceProperties);
|
90
|
} else {
|
91
|
throw new CdmRemoteSourceException("Remote source '" + remoteSource + "' does not exist in settings file");
|
92
|
}
|
93
|
}
|
94
|
|
95
|
private static void checkProperties(Properties cdmSourceProperties) throws CdmRemoteSourceException {
|
96
|
if(!cdmSourceProperties.containsKey(CdmSourceProperties.SERVER.toString())) {
|
97
|
throw new CdmRemoteSourceException("Could not initialise remote source. Required property '" + CdmSourceProperties.SERVER.toString() + "' is missing");
|
98
|
}
|
99
|
String port = (String)cdmSourceProperties.get(CdmSourceProperties.PORT.toString());
|
100
|
if(port == null || port.isEmpty()) {
|
101
|
throw new CdmRemoteSourceException("Could not initialise remote source. Required property '" + CdmSourceProperties.SERVER.toString() + "' is missing");
|
102
|
}
|
103
|
if(!cdmSourceProperties.containsKey(CdmSourceProperties.CONTEXTPATH.toString())) {
|
104
|
throw new CdmRemoteSourceException("Could not initialise remote source. Required property '" + CdmSourceProperties.SERVER.toString() + "' is missing");
|
105
|
}
|
106
|
// default NomenclaturalCode is always ICNAFP
|
107
|
if(!cdmSourceProperties.containsKey(CdmSourceProperties.NOMENCLATURAL_CODE.toString())) {
|
108
|
cdmSourceProperties.put(CdmSourceProperties.NOMENCLATURAL_CODE.toString(), NomenclaturalCode.ICNAFP.name());
|
109
|
}
|
110
|
}
|
111
|
|
112
|
/* (non-Javadoc)
|
113
|
* @see eu.etaxonomy.cdm.config.ICdmPersistentSource#getBeanName()
|
114
|
*/
|
115
|
@Override
|
116
|
public String getBeanName() {
|
117
|
return beanName;
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public String getServer() {
|
122
|
return cdmSourceProperties.getProperty(CdmSourceProperties.SERVER.toString());
|
123
|
}
|
124
|
|
125
|
@Override
|
126
|
public int getPort() {
|
127
|
String port = CdmUtils.Nz(cdmSourceProperties.getProperty(CdmSourceProperties.PORT.toString()));
|
128
|
if (port == null || "".equals(port)){
|
129
|
return -1;
|
130
|
}else{
|
131
|
//TODO exception if non integer
|
132
|
return Integer.valueOf(port);
|
133
|
}
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
public String getContextPath() {
|
138
|
return cdmSourceProperties.getProperty(CdmSourceProperties.CONTEXTPATH.toString());
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public NomenclaturalCode getNomenclaturalCode() {
|
143
|
return NomenclaturalCode.fromString(cdmSourceProperties.getProperty(CdmSourceProperties.NOMENCLATURAL_CODE.toString()));
|
144
|
}
|
145
|
|
146
|
public static CdmPersistentRemoteSource save(String strRemoteSourceName, ICdmRemoteSource remoteSource) throws CdmRemoteSourceException {
|
147
|
Properties cdmSourceProperties = new Properties();
|
148
|
cdmSourceProperties.put(CdmSourceProperties.SERVER.toString(), remoteSource.getServer());
|
149
|
cdmSourceProperties.put(CdmSourceProperties.PORT.toString(), String.valueOf(remoteSource.getPort()));
|
150
|
cdmSourceProperties.put(CdmSourceProperties.CONTEXTPATH.toString(), remoteSource.getContextPath());
|
151
|
cdmSourceProperties.put(CdmSourceProperties.NOMENCLATURAL_CODE.toString(), remoteSource.getNomenclaturalCode().name());
|
152
|
checkProperties(cdmSourceProperties);
|
153
|
return save(strRemoteSourceName, cdmSourceProperties);
|
154
|
}
|
155
|
|
156
|
private static CdmPersistentRemoteSource save(String strRemoteSourceName,Properties cdmSourceProperties) throws CdmRemoteSourceException {
|
157
|
|
158
|
//root
|
159
|
Element root = getBeansRoot(CdmPersistentSourceUtils.getCdmSourceInputStream());
|
160
|
if (root == null){
|
161
|
return null;
|
162
|
}
|
163
|
//bean
|
164
|
Element bean = XmlHelp.getFirstAttributedChild(root, "bean", "id", CdmPersistentSourceUtils.getBeanName(strRemoteSourceName, REMOTESOURCE_BEAN_POSTFIX));
|
165
|
if (bean != null){
|
166
|
bean.detach(); //delete old version if necessary
|
167
|
}
|
168
|
bean = insertXmlBean(root, CdmPersistentSourceUtils.getBeanName(strRemoteSourceName, REMOTESOURCE_BEAN_POSTFIX), CdmPersistentRemoteSource.class.getName());
|
169
|
|
170
|
|
171
|
Properties props = cdmSourceProperties;
|
172
|
Enumeration e = props.propertyNames();
|
173
|
|
174
|
while (e.hasMoreElements()) {
|
175
|
String key = (String) e.nextElement();
|
176
|
insertXmlValueProperty(bean, key, props.getProperty(key));
|
177
|
}
|
178
|
|
179
|
//save
|
180
|
saveToXml(root.getDocument(),
|
181
|
CdmPersistentSourceUtils.getResourceDirectory(),
|
182
|
CdmPersistentXMLSource.CDMSOURCE_FILE_NAME,
|
183
|
XmlHelp.prettyFormat );
|
184
|
|
185
|
return NewInstance(strRemoteSourceName) ;
|
186
|
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* @param strDataSourceName
|
191
|
* @param dataSource
|
192
|
* @param code
|
193
|
* @return
|
194
|
* the updated dataSource, null if not succesful
|
195
|
*/
|
196
|
public static CdmPersistentRemoteSource update(String strRemoteSourceName,
|
197
|
ICdmRemoteSource remoteSource) throws CdmRemoteSourceException {
|
198
|
CdmPersistentSourceUtils.delete(CdmPersistentSourceUtils.getBeanName(strRemoteSourceName,REMOTESOURCE_BEAN_POSTFIX));
|
199
|
return save(strRemoteSourceName, remoteSource);
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* Tests existing of the datsource in the according config file.
|
204
|
* @return true if a datasource with the given name exists in the according datasource config file.
|
205
|
*/
|
206
|
public static boolean exists(String strRemoteSourceName){
|
207
|
Element bean = CdmPersistentSourceUtils.getCdmSourceBeanXml(strRemoteSourceName, REMOTESOURCE_BEAN_POSTFIX);
|
208
|
return (bean != null);
|
209
|
}
|
210
|
|
211
|
}
|