/corelib-sugarcrm/client/src/main/java/eu/europeana/uim/sugarcrmclient/ws/SugarWsClientImpl.java

https://gitlab.com/Aaeinstein54/corelib · Java · 471 lines · 223 code · 74 blank · 174 comment · 11 complexity · c83ec039fb9686ec07ddd9f5326cd79d MD5 · raw file

  1. /*
  2. * Copyright 2007 EDL FOUNDATION
  3. *
  4. * Licensed under the EUPL, Version 1.1 or - as soon they
  5. * will be approved by the European Commission - subsequent
  6. * versions of the EUPL (the "Licence");
  7. * you may not use this work except in compliance with the
  8. * Licence.
  9. * You may obtain a copy of the Licence at:
  10. *
  11. * http://ec.europa.eu/idabc/eupl
  12. *
  13. * Unless required by applicable law or agreed to in
  14. * writing, software distributed under the Licence is
  15. * distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  17. * express or implied.
  18. * See the Licence for the specific language governing
  19. * permissions and limitations under the Licence.
  20. */
  21. package eu.europeana.uim.sugarcrmclient.ws;
  22. import org.springframework.ws.client.core.WebServiceTemplate;
  23. import eu.europeana.uim.sugarcrmclient.jibxbindings.IsUserAdmin;
  24. import eu.europeana.uim.sugarcrmclient.jibxbindings.IsUserAdminResponse;
  25. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetEntryList;
  26. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetEntryListResponse;
  27. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetEntry;
  28. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetEntryResponse;
  29. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetEntries;
  30. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetEntriesResponse;
  31. import eu.europeana.uim.sugarcrmclient.jibxbindings.SetEntry;
  32. import eu.europeana.uim.sugarcrmclient.jibxbindings.SetEntryResponse;
  33. import eu.europeana.uim.sugarcrmclient.jibxbindings.Logout;
  34. import eu.europeana.uim.sugarcrmclient.jibxbindings.LogoutResponse;
  35. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetModuleFields;
  36. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetModuleFieldsResponse;
  37. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetAvailableModules;
  38. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetAvailableModulesResponse;
  39. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetUserId;
  40. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetUserIdResponse;
  41. import eu.europeana.uim.sugarcrmclient.jibxbindings.Login;
  42. import eu.europeana.uim.sugarcrmclient.jibxbindings.LoginResponse;
  43. import eu.europeana.uim.sugarcrmclient.jibxbindings.SetNoteAttachment;
  44. import eu.europeana.uim.sugarcrmclient.jibxbindings.SetNoteAttachmentResponse;
  45. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetNoteAttachment;
  46. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetNoteAttachmentResponse;
  47. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetRelationships;
  48. import eu.europeana.uim.sugarcrmclient.jibxbindings.GetRelationshipsResponse;
  49. import eu.europeana.uim.sugarcrmclient.ws.exceptions.*;
  50. /**
  51. * The core class for performing SOAP based sugarCRM operations
  52. *
  53. * @author Georgios Markakis
  54. */
  55. public class SugarWsClientImpl implements SugarWsClient {
  56. private WebServiceTemplate webServiceTemplate;
  57. private String username;
  58. private String password;
  59. private String sessionID;
  60. /**
  61. * Default Constructor
  62. */
  63. public SugarWsClientImpl(){
  64. }
  65. /**
  66. * Used for unit/integration tests
  67. * @param username
  68. * @param password
  69. */
  70. public SugarWsClientImpl(String username,String password){
  71. this.username = username;
  72. this.password = password;
  73. }
  74. /**
  75. * Generic auxiliary method for marshalling and unmarshalling requests and
  76. * responses via Spring-WS
  77. *
  78. * @param <T>
  79. * Class of the request Object
  80. * @param <S>
  81. * Class of the response object
  82. * @param wsOperation
  83. * the instance of the request operation
  84. * @return the unmarshalled response object
  85. */
  86. private <T, S> S invokeWSTemplate(T wsOperation, Class<S> responseClass) {
  87. @SuppressWarnings("unchecked")
  88. S wsResponse = (S) webServiceTemplate.marshalSendAndReceive(wsOperation);
  89. return wsResponse;
  90. }
  91. /**
  92. * Public method for performing Login operations (see Junit test for usage
  93. * example)
  94. *
  95. * @param login
  96. * the Login object
  97. * @return a String containing the current Session id
  98. * @throws JIXBLoginFailureException
  99. * when login credentials are incorrect
  100. * @throws GenericSugarCrmException
  101. */
  102. @Override
  103. public String login(Login login) throws JIXBLoginFailureException {
  104. LoginResponse response = invokeWSTemplate(login, LoginResponse.class);
  105. String sessionID = response.getReturn().getId();
  106. this.sessionID = sessionID;
  107. if ("-1".equals(sessionID)) {
  108. throw new JIXBLoginFailureException(response.getReturn().getError());
  109. }
  110. return sessionID;
  111. }
  112. /**
  113. * Public method for performing Login operations (see JUnit test for usage
  114. * example)
  115. *
  116. * @param login
  117. * @return a LoginResponse object
  118. * @throws JIXBLoginFailureException
  119. * when login credentials are incorrect
  120. * @throws GenericSugarCrmException
  121. */
  122. @Override
  123. public LoginResponse login2(Login login) throws JIXBLoginFailureException {
  124. LoginResponse response = invokeWSTemplate(login, LoginResponse.class);
  125. if ("-1".equals(response.getReturn().getId())) {
  126. throw new JIXBLoginFailureException(response.getReturn().getError());
  127. }
  128. return response;
  129. }
  130. /**
  131. * Public method for performing Logout operations (see Junit test for usage
  132. * example)
  133. *
  134. * @param a logout request object
  135. * @return a LogoutResponse object
  136. * @throws JIXBLogoutFailureException
  137. * when logout fails
  138. * @throws GenericSugarCrmException
  139. */
  140. @Override
  141. public LogoutResponse logout(Logout request)
  142. throws JIXBLogoutFailureException {
  143. LogoutResponse response = invokeWSTemplate(request,
  144. LogoutResponse.class);
  145. String returnvalue = response.getReturn().getNumber();
  146. if (!"0".equals(returnvalue)) {
  147. throw new JIXBLogoutFailureException(response.getReturn()
  148. .getDescription());
  149. }
  150. return response;
  151. }
  152. /**
  153. * This method returns an object indicating that the current user has admin
  154. * privileges or not.
  155. *
  156. * @param request
  157. * @return
  158. * @throws GenericSugarCrmException
  159. */
  160. @Override
  161. public IsUserAdminResponse isUserAdmin(IsUserAdmin request)
  162. throws Exception {
  163. try {
  164. IsUserAdminResponse response = invokeWSTemplate(request,
  165. IsUserAdminResponse.class);
  166. return response;
  167. } catch (Exception e) {
  168. throw new Exception();
  169. }
  170. }
  171. /**
  172. * This method gives the user name of the user who "owns" the specific
  173. * session
  174. *
  175. * @param request
  176. * @return
  177. * @throws GenericSugarCrmException
  178. */
  179. @Override
  180. public GetUserIdResponse getUserId(GetUserId request)
  181. throws Exception {
  182. try {
  183. GetUserIdResponse response = invokeWSTemplate(request,
  184. GetUserIdResponse.class);
  185. return response;
  186. } catch (Exception e) {
  187. throw new Exception();
  188. }
  189. }
  190. /**
  191. * Shows all the available module names in SugarCRM
  192. *
  193. * @param request
  194. * @return
  195. * @throws JIXBQueryResultException
  196. * @throws GenericSugarCrmException
  197. */
  198. @Override
  199. public GetAvailableModulesResponse getAvailableModules(
  200. GetAvailableModules request) throws JIXBQueryResultException {
  201. GetAvailableModulesResponse response = invokeWSTemplate(request,
  202. GetAvailableModulesResponse.class);
  203. if (!"0".equals(response.getReturn().getError().getNumber())) {
  204. throw new JIXBQueryResultException(response.getReturn().getError());
  205. }
  206. return response;
  207. }
  208. /**
  209. * Get the fields for a specific module
  210. *
  211. * @param request
  212. * @return a GetModuleFieldsResponse containing a list of module fields
  213. * @throws JIXBQueryResultException
  214. */
  215. @Override
  216. public GetModuleFieldsResponse getModuleFields(GetModuleFields request)
  217. throws JIXBQueryResultException {
  218. GetModuleFieldsResponse response = invokeWSTemplate(request,
  219. GetModuleFieldsResponse.class);
  220. /*
  221. * if(!"0".equals(response.getReturn().getError().getNumber())){ throw
  222. * new JIXBQueryResultException(response.getReturn().getError()); }
  223. */
  224. return response;
  225. }
  226. /**
  227. * Performs a query on the records contained in sugarCRM
  228. *
  229. * @param request
  230. * @return
  231. * @throws JIXBQueryResultException
  232. */
  233. @Override
  234. public GetEntryListResponse getEntryList(GetEntryList request)
  235. throws JIXBQueryResultException {
  236. GetEntryListResponse response = invokeWSTemplate(request,
  237. GetEntryListResponse.class);
  238. if (!"0".equals(response.getReturn().getError().getNumber())) {
  239. throw new JIXBQueryResultException(response.getReturn().getError());
  240. }
  241. return response;
  242. }
  243. /**
  244. * Gets a specific entry
  245. *
  246. * @param request
  247. * @return
  248. * @throws JIXBQueryResultException
  249. */
  250. @Override
  251. public GetEntryResponse getEntry(GetEntry request)
  252. throws JIXBQueryResultException {
  253. GetEntryResponse response = invokeWSTemplate(request,
  254. GetEntryResponse.class);
  255. if (!"0".equals(response.getReturn().getError().getNumber())) {
  256. throw new JIXBQueryResultException(response.getReturn().getError());
  257. }
  258. return response;
  259. }
  260. /**
  261. * Creates/Updates an entry in SugarCRM
  262. *
  263. * @param request
  264. * @return
  265. * @throws JIXBQueryResultException
  266. */
  267. @Override
  268. public SetEntryResponse setEntry(SetEntry request)
  269. throws JIXBQueryResultException {
  270. SetEntryResponse response = invokeWSTemplate(request,
  271. SetEntryResponse.class);
  272. if (!"0".equals(response.getReturn().getError().getNumber())) {
  273. throw new JIXBQueryResultException(response.getReturn().getError());
  274. }
  275. return response;
  276. }
  277. /**
  278. * Gets the entries for a request
  279. *
  280. * @param request
  281. * @return
  282. * @throws JIXBQueryResultException
  283. */
  284. @Override
  285. public GetEntriesResponse getEntries(GetEntries request)
  286. throws JIXBQueryResultException {
  287. GetEntriesResponse response = invokeWSTemplate(request,
  288. GetEntriesResponse.class);
  289. if (!"0".equals(response.getReturn().getError().getNumber())) {
  290. throw new JIXBQueryResultException(response.getReturn().getError());
  291. }
  292. return response;
  293. }
  294. /**
  295. * Sets a note attachment to a record
  296. *
  297. * @param request
  298. * @return
  299. */
  300. @Override
  301. public SetNoteAttachmentResponse setNoteAttachment(SetNoteAttachment request)
  302. throws JIXBFileAttachmentException {
  303. SetNoteAttachmentResponse response = invokeWSTemplate(request,
  304. SetNoteAttachmentResponse.class);
  305. if (!"0".equals(response.getReturn().getError().getNumber())) {
  306. throw new JIXBFileAttachmentException(response.getReturn()
  307. .getError());
  308. }
  309. return response;
  310. }
  311. /**
  312. * Gets a note attachment from a record
  313. *
  314. * @param request
  315. * @return
  316. */
  317. @Override
  318. public GetNoteAttachmentResponse getNoteAttachment(GetNoteAttachment request)
  319. throws JIXBFileAttachmentException {
  320. GetNoteAttachmentResponse response = invokeWSTemplate(request,
  321. GetNoteAttachmentResponse.class);
  322. if (!"0".equals(response.getReturn().getError().getNumber())) {
  323. throw new JIXBFileAttachmentException(response.getReturn()
  324. .getError());
  325. }
  326. return response;
  327. }
  328. /**
  329. * Gets the Relationships for a specific module
  330. *
  331. * @param request
  332. * @return
  333. * @throws JIXBQueryResultException
  334. */
  335. @Override
  336. public GetRelationshipsResponse getRelationships(GetRelationships request)
  337. throws JIXBQueryResultException {
  338. GetRelationshipsResponse response = invokeWSTemplate(request,
  339. GetRelationshipsResponse.class);
  340. if (!"0".equals(response.getReturn().getError().getNumber())) {
  341. throw new JIXBQueryResultException(response.getReturn().getError());
  342. }
  343. return response;
  344. }
  345. /**
  346. * @return
  347. */
  348. @Override
  349. public String getSessionID() {
  350. return sessionID;
  351. }
  352. @Override
  353. public String getUsername() {
  354. return username;
  355. }
  356. @Override
  357. public void setUsername(String username) {
  358. this.username = username;
  359. }
  360. @Override
  361. public String getPassword() {
  362. return password;
  363. }
  364. @Override
  365. public void setPassword(String password) {
  366. this.password = password;
  367. }
  368. /* Getters & Setters */
  369. /**
  370. * @return
  371. */
  372. public WebServiceTemplate getWebServiceTemplate() {
  373. return this.webServiceTemplate;
  374. }
  375. /**
  376. * @param webServiceTemplate
  377. */
  378. public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
  379. this.webServiceTemplate = webServiceTemplate;
  380. }
  381. /**
  382. * @param defaultUri
  383. */
  384. public void setDefaultUri(String defaultUri) {
  385. webServiceTemplate.setDefaultUri(defaultUri);
  386. }
  387. /**
  388. * @return
  389. */
  390. public String getDefaultUri() {
  391. return webServiceTemplate.getDefaultUri();
  392. }
  393. @Override
  394. public void setSessionID(String sessionID) {
  395. this.sessionID = sessionID;
  396. }
  397. }