Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / source / 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.taxeditor.remoting.source;
10
11 import java.util.Map;
12
13 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
14
15 import eu.etaxonomy.cdm.api.service.IUserService;
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 IUserService userService;
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(IUserService.class);
80 // FIXME:Remoting need to fix this hardcoded url and make it configurable somehow
81 proxy.setServiceUrl(baseUrl + "/remoting-public/user.service");
82 proxy.afterPropertiesSet();
83 userService = (IUserService) proxy.getObject();
84
85 }
86
87 /* (non-Javadoc)
88 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getBaseUrl()
89 */
90 @Override
91 public String getBaseUrl() {
92 return baseUrl;
93 }
94
95 /**
96 * Sets the base url for the http-invoker services as listed in
97 * httpInvokerServicesClients.xml.
98 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
99 * base url would be 'http://127.0.0.1:8080/col'
100 *
101 * @param baseUrl
102 */
103 public void setBaseUrl(String baseUrl) {
104 this.baseUrl = baseUrl;
105 }
106
107 /* (non-Javadoc)
108 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getContextPath()
109 */
110 @Override
111 public String getContextPath() {
112 return contextPath;
113 }
114
115 /**
116 * Sets the context path.
117 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
118 * context path would be 'col'
119 *
120 * @param contextPath
121 */
122 public void setContextPath(String contextPath) {
123 this.contextPath = contextPath;
124 }
125
126 /* (non-Javadoc)
127 * @see eu.etaxonomy.cdm.config.CdmSource#getDbSchemaVersion()
128 */
129 @Override
130 public String getDbSchemaVersion() throws CdmSourceException {
131 return userService.getDbSchemaVersion();
132
133 }
134
135 /* (non-Javadoc)
136 * @see eu.etaxonomy.cdm.config.CdmSource#isDbEmpty()
137 */
138 @Override
139 public boolean isDbEmpty() throws CdmSourceException {
140 return userService.isDbEmpty();
141
142 }
143
144 /* (non-Javadoc)
145 * @see eu.etaxonomy.cdm.config.CdmSource#checkConnection()
146 */
147 @Override
148 public boolean checkConnection() throws CdmSourceException {
149 // assuming that database service works implies
150 // the connection is up
151 // if no exception is thrown then we assume that the
152 // connection is up
153 // FIXME:Remoting is this really correct?
154 userService.getDbSchemaVersion();
155
156 return true;
157 }
158
159 /* (non-Javadoc)
160 * @see eu.etaxonomy.cdm.config.CdmSource#getConnectionMessage()
161 */
162 @Override
163 public String getConnectionMessage() {
164 return "Connecting to Remote CDM Instance " + getName() + ":" + getPort() + "/" + getContextPath();
165 }
166
167
168 @Override
169 public Map<MetaDataPropertyName, String> getMetaDataMap() throws CdmSourceException {
170 return userService.getCdmMetadataMap();
171 }
172
173
174 }