- extended DnaQuality details view
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / main / java / eu / etaxonomy / cdm / remote / CdmRemoteSourceBase.java
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 package eu.etaxonomy.cdm.remote;
10
11 import java.util.Map;
12
13 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
14
15 import eu.etaxonomy.cdm.api.service.IDatabaseService;
16 import eu.etaxonomy.cdm.config.CdmSource;
17 import eu.etaxonomy.cdm.config.CdmSourceException;
18 import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
19 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
20
21 /**
22 * Base class representing a CDM remote source.
23 * This class handles all the configuration relating to the remoting aspect of
24 * a CDM source.
25 *
26 */
27 public class CdmRemoteSourceBase extends CdmSource implements ICdmRemoteSource {
28
29 protected static final String DEFAULT_NAME = "default";
30 protected static final String DEFAULT_SERVER = "127.0.0.1";
31 protected static final int DEFAULT_PORT = 8080;
32 protected static final String DEFAULT_CONTEXT_PATH = "";
33 protected static final NomenclaturalCode DEFAULT_NOMENCLATURAL_CODE = NomenclaturalCode.ICNAFP;
34 private String contextPath;
35 private String baseUrl;
36
37 private IDatabaseService databaseService;
38
39 /**
40 * Constructs a CdmRemoteSourceBase object with default values.
41 *
42 */
43 protected CdmRemoteSourceBase() {
44 setName(DEFAULT_NAME);
45 setServer(DEFAULT_SERVER);
46 setPort(DEFAULT_PORT);
47 setContextPath(DEFAULT_CONTEXT_PATH);
48 setNomenclaturalCode(DEFAULT_NOMENCLATURAL_CODE);
49 }
50
51 /**
52 * Constructs a CdmRemoteSourceBase
53 *
54 * @param name
55 * @param server
56 * @param port
57 * @param contextPath
58 * @param nomenclaturalCode
59 */
60 public CdmRemoteSourceBase(String name, String server, int port, String contextPath, NomenclaturalCode nomenclaturalCode) {
61 setName(name);
62 setServer(server);
63 setPort(port);
64 setContextPath(contextPath);
65 setNomenclaturalCode(nomenclaturalCode);
66 initDatabaseConnection();
67 }
68
69 protected void initDatabaseConnection() {
70 if(getContextPath() == null || getContextPath().equals("")) {
71 setBaseUrl("http://" + getServer() + ":" + String.valueOf(getPort()));
72 } else {
73 setBaseUrl("http://" + getServer() + ":" + String.valueOf(getPort()) + "/" + getContextPath());
74 }
75 // the database service needs to be initialised (before the spring
76 // application context initialsation) since it is required to
77 // to make queries related to the source database
78 HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
79 proxy.setServiceInterface(IDatabaseService.class);
80 proxy.setServiceUrl(baseUrl + "/remoting/database.service");
81 proxy.afterPropertiesSet();
82 databaseService = (IDatabaseService) proxy.getObject();
83
84 }
85
86 /* (non-Javadoc)
87 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getBaseUrl()
88 */
89 @Override
90 public String getBaseUrl() {
91 return baseUrl;
92 }
93
94 /**
95 * Sets the base url for the http-invoker services as listed in
96 * httpInvokerServicesClients.xml.
97 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
98 * base url would be 'http://127.0.0.1:8080/col'
99 *
100 * @param baseUrl
101 */
102 public void setBaseUrl(String baseUrl) {
103 this.baseUrl = baseUrl;
104 }
105
106 /* (non-Javadoc)
107 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getContextPath()
108 */
109 @Override
110 public String getContextPath() {
111 return contextPath;
112 }
113
114 /**
115 * Sets the context path.
116 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
117 * context path would be 'col'
118 *
119 * @param contextPath
120 */
121 public void setContextPath(String contextPath) {
122 this.contextPath = contextPath;
123 }
124
125 /* (non-Javadoc)
126 * @see eu.etaxonomy.cdm.config.CdmSource#getDbSchemaVersion()
127 */
128 @Override
129 public String getDbSchemaVersion() throws CdmSourceException {
130 return databaseService.getDbSchemaVersion();
131
132 }
133
134 /* (non-Javadoc)
135 * @see eu.etaxonomy.cdm.config.CdmSource#isDbEmpty()
136 */
137 @Override
138 public boolean isDbEmpty() throws CdmSourceException {
139 return databaseService.isDbEmpty();
140
141 }
142
143 /* (non-Javadoc)
144 * @see eu.etaxonomy.cdm.config.CdmSource#checkConnection()
145 */
146 @Override
147 public boolean checkConnection() throws CdmSourceException {
148 // assuming that database service works implies
149 // the connection is up
150 // if no exception is thrown then we assume that the
151 // connection is up
152 // FIXME:Remoting is this really correct?
153 databaseService.getDbSchemaVersion();
154
155 return true;
156 }
157
158 /* (non-Javadoc)
159 * @see eu.etaxonomy.cdm.config.CdmSource#getConnectionMessage()
160 */
161 @Override
162 public String getConnectionMessage() {
163 return "Conncting to Remote CDM Server " + getName();
164 }
165
166
167 @Override
168 public Map<MetaDataPropertyName, String> getMetaDataMap() throws CdmSourceException {
169 return databaseService.getCdmMetadataMap();
170 }
171
172
173 }