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