Commit f67a192e2bdb5eb99a91cb4425b4c301653cb71c

Authored by Dima Landiak
1 parent 69145679

rest client update

... ... @@ -82,7 +82,7 @@ public class CustomerController extends BaseController {
82 82
83 83 @PreAuthorize("hasAuthority('TENANT_ADMIN')")
84 84 @RequestMapping(value = "/customer", method = RequestMethod.POST)
85   - @ResponseBody
  85 + @ResponseBody
86 86 public Customer saveCustomer(@RequestBody Customer customer) throws ThingsboardException {
87 87 try {
88 88 customer.setTenantId(getCurrentUser().getTenantId());
... ... @@ -107,7 +107,7 @@ public class CustomerController extends BaseController {
107 107 }
108 108
109 109 @PreAuthorize("hasAuthority('TENANT_ADMIN')")
110   - @RequestMapping(value = "/customers", params = { "limit" }, method = RequestMethod.GET)
  110 + @RequestMapping(value = "/customers", params = {"limit"}, method = RequestMethod.GET)
111 111 @ResponseBody
112 112 public TextPageData<Customer> getCustomers(@RequestParam int limit,
113 113 @RequestParam(required = false) String textSearch,
... ... @@ -122,4 +122,16 @@ public class CustomerController extends BaseController {
122 122 }
123 123 }
124 124
  125 + @PreAuthorize("hasAuthority('TENANT_ADMIN')")
  126 + @RequestMapping(value = "/tenant/customers", params = {"customerTitle"}, method = RequestMethod.GET)
  127 + @ResponseBody
  128 + public Customer getTenantCustomer(
  129 + @RequestParam String customerTitle) throws ThingsboardException {
  130 + try {
  131 + TenantId tenantId = getCurrentUser().getTenantId();
  132 + return checkNotNull(customerService.findCustomerByTenantIdAndTitle(tenantId, customerTitle));
  133 + } catch (Exception e) {
  134 + throw handleException(e);
  135 + }
  136 + }
125 137 }
... ...
... ... @@ -22,20 +22,24 @@ import org.thingsboard.server.common.data.id.TenantId;
22 22 import org.thingsboard.server.common.data.page.TextPageData;
23 23 import org.thingsboard.server.common.data.page.TextPageLink;
24 24
  25 +import java.util.Optional;
  26 +
25 27 public interface CustomerService {
26 28
27 29 Customer findCustomerById(CustomerId customerId);
28 30
  31 + Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title);
  32 +
29 33 ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId);
30 34
31 35 Customer saveCustomer(Customer customer);
32   -
  36 +
33 37 void deleteCustomer(CustomerId customerId);
34 38
35 39 Customer findOrCreatePublicCustomer(TenantId tenantId);
36 40
37 41 TextPageData<Customer> findCustomersByTenantId(TenantId tenantId, TextPageLink pageLink);
38   -
  42 +
39 43 void deleteCustomersByTenantId(TenantId tenantId);
40 44
41 45 }
... ...
... ... @@ -52,6 +52,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
52 52
53 53 private static final String PUBLIC_CUSTOMER_TITLE = "Public";
54 54 public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId ";
  55 + public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
55 56
56 57 @Autowired
57 58 private CustomerDao customerDao;
... ... @@ -79,6 +80,13 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
79 80 }
80 81
81 82 @Override
  83 + public Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title) {
  84 + log.trace("Executing findCustomerByTenantIdAndTitle [{}] [{}]", tenantId, title);
  85 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  86 + return customerDao.findCustomersByTenantIdAndTitle(tenantId.getId(), title);
  87 + }
  88 +
  89 + @Override
82 90 public ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId) {
83 91 log.trace("Executing findCustomerByIdAsync [{}]", customerId);
84 92 validateId(customerId, INCORRECT_CUSTOMER_ID + customerId);
... ...
... ... @@ -77,6 +77,21 @@ public class RestClient implements ClientHttpRequestInterceptor {
77 77 }
78 78 }
79 79
  80 + public Optional<Asset> findAsset(String name) {
  81 + Map<String, String> params = new HashMap<String, String>();
  82 + params.put("assetName", name);
  83 + try {
  84 + ResponseEntity<Asset> assetEntity = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, params);
  85 + return Optional.of(assetEntity.getBody());
  86 + } catch (HttpClientErrorException exception) {
  87 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  88 + return Optional.empty();
  89 + } else {
  90 + throw exception;
  91 + }
  92 + }
  93 + }
  94 +
80 95 public Customer createCustomer(String title) {
81 96 Customer customer = new Customer();
82 97 customer.setTitle(title);
... ... @@ -123,6 +138,10 @@ public class RestClient implements ClientHttpRequestInterceptor {
123 138 return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
124 139 }
125 140
  141 + public Customer getCustomerByTitle(String title) {
  142 + return restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle=" + title, Customer.class).getBody();
  143 + }
  144 +
126 145 public RestTemplate getRestTemplate() {
127 146 return restTemplate;
128 147 }
... ...