|
| 1 | +/* |
| 2 | + * Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved. |
| 3 | + */ |
| 4 | +package com.gooddata.connectors; |
| 5 | + |
| 6 | +import com.gooddata.AbstractService; |
| 7 | +import com.gooddata.FutureResult; |
| 8 | +import com.gooddata.GoodDataException; |
| 9 | +import com.gooddata.GoodDataRestException; |
| 10 | +import com.gooddata.SimplePollHandler; |
| 11 | +import com.gooddata.gdc.UriResponse; |
| 12 | +import com.gooddata.project.Project; |
| 13 | +import com.gooddata.project.ProjectService; |
| 14 | +import com.gooddata.project.ProjectTemplate; |
| 15 | +import org.springframework.http.client.ClientHttpResponse; |
| 16 | +import org.springframework.web.client.RestClientException; |
| 17 | +import org.springframework.web.client.RestTemplate; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Collection; |
| 21 | + |
| 22 | +import static com.gooddata.Validate.notNull; |
| 23 | + |
| 24 | +/** |
| 25 | + * Connector Service |
| 26 | + */ |
| 27 | +public class ConnectorService extends AbstractService { |
| 28 | + |
| 29 | + private final ProjectService projectService; |
| 30 | + |
| 31 | + public ConnectorService(final RestTemplate restTemplate, final ProjectService projectService) { |
| 32 | + super(restTemplate); |
| 33 | + this.projectService = notNull(projectService, "projectService"); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Create connector integration with given settings |
| 38 | + * @param project project |
| 39 | + * @param settings integration settings |
| 40 | + * @return created integration |
| 41 | + * @throws ConnectorException if integration can't be created |
| 42 | + */ |
| 43 | + public Integration createIntegration(final Project project, final Settings settings) { |
| 44 | + notNull(project, "project"); |
| 45 | + notNull(settings, "settings"); |
| 46 | + final Collection<ProjectTemplate> projectTemplates = projectService.getProjectTemplates(project); |
| 47 | + if (projectTemplates == null || projectTemplates.isEmpty()) { |
| 48 | + throw new GoodDataException("Project " + project.getId() + " doesn't contain a template reference"); |
| 49 | + } |
| 50 | + final ProjectTemplate template = notNull(projectTemplates.iterator().next(), "project template"); |
| 51 | + final Integration integration = createIntegration(project, settings.getConnector(), new Integration(template.getUrl())); |
| 52 | + updateSettings(project, settings); |
| 53 | + return integration; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create connector integration |
| 58 | + * @param project project |
| 59 | + * @param connector connector |
| 60 | + * @param integration integration |
| 61 | + * @return created integration |
| 62 | + * @throws ConnectorException if integration can't be created |
| 63 | + */ |
| 64 | + public Integration createIntegration(final Project project, final Connector connector, final Integration integration) { |
| 65 | + notNull(project, "project"); |
| 66 | + notNull(connector, "connector"); |
| 67 | + notNull(integration, "integration"); |
| 68 | + try { |
| 69 | + return restTemplate.postForObject(Integration.URL, integration, Integration.class, project.getId(), connector.getName()); |
| 70 | + } catch (GoodDataRestException | RestClientException e) { |
| 71 | + throw new ConnectorException("Unable to create " + connector + " integration", e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Update integration settings |
| 77 | + * @param project project |
| 78 | + * @param settings integration settings |
| 79 | + * @throws ConnectorException if settings can't be updated |
| 80 | + */ |
| 81 | + public <T extends Settings> void updateSettings(final Project project, final T settings) { |
| 82 | + notNull(settings, "settings"); |
| 83 | + notNull(project, "project"); |
| 84 | + try { |
| 85 | + restTemplate.put(Settings.URL, settings, project.getId(), settings.getConnector().getName()); |
| 86 | + } catch (GoodDataRestException | RestClientException e) { |
| 87 | + throw new ConnectorException("Unable to set " + settings.getConnector() + " settings", e); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Execute connector process |
| 93 | + * @param project project |
| 94 | + * @param execution process execution |
| 95 | + * @return executed process |
| 96 | + * @throws ConnectorException if process execution fails |
| 97 | + */ |
| 98 | + public FutureResult<Process> executeProcess(final Project project, final ProcessExecution execution) { |
| 99 | + notNull(project, "project"); |
| 100 | + notNull(execution, "execution"); |
| 101 | + final String connectorName = execution.getConnector().getName(); |
| 102 | + try { |
| 103 | + final UriResponse response = restTemplate.postForObject(Process.URL, execution, UriResponse.class, project.getId(), connectorName); |
| 104 | + return new FutureResult<>(this, new SimplePollHandler<Process>(response.getUri(), Process.class) { |
| 105 | + @Override |
| 106 | + public boolean isFinished(final ClientHttpResponse response) throws IOException { |
| 107 | + final Process process = extractData(response, Process.class); |
| 108 | + return process.isFinished(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void handlePollResult(final Process pollResult) { |
| 113 | + super.handlePollResult(pollResult); |
| 114 | + if (pollResult.isFailed()) { |
| 115 | + throw new ConnectorException("Unable to execute " + connectorName + " process: " + |
| 116 | + pollResult.getStatus().getDescription()); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + } catch (GoodDataRestException | RestClientException e) { |
| 121 | + throw new ConnectorException("Unable to execute " + connectorName + " process", e); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments