Browse Source

rest client update

pull/501/head
Dima Landiak 9 years ago
parent
commit
f67a192e2b
  1. 16
      application/src/main/java/org/thingsboard/server/controller/CustomerController.java
  2. 8
      dao/src/main/java/org/thingsboard/server/dao/customer/CustomerService.java
  3. 8
      dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java
  4. 19
      tools/src/main/java/org/thingsboard/client/tools/RestClient.java

16
application/src/main/java/org/thingsboard/server/controller/CustomerController.java

@ -82,7 +82,7 @@ public class CustomerController extends BaseController {
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer", method = RequestMethod.POST)
@ResponseBody
@ResponseBody
public Customer saveCustomer(@RequestBody Customer customer) throws ThingsboardException {
try {
customer.setTenantId(getCurrentUser().getTenantId());
@ -107,7 +107,7 @@ public class CustomerController extends BaseController {
}
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customers", params = { "limit" }, method = RequestMethod.GET)
@RequestMapping(value = "/customers", params = {"limit"}, method = RequestMethod.GET)
@ResponseBody
public TextPageData<Customer> getCustomers(@RequestParam int limit,
@RequestParam(required = false) String textSearch,
@ -122,4 +122,16 @@ public class CustomerController extends BaseController {
}
}
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/tenant/customers", params = {"customerTitle"}, method = RequestMethod.GET)
@ResponseBody
public Customer getTenantCustomer(
@RequestParam String customerTitle) throws ThingsboardException {
try {
TenantId tenantId = getCurrentUser().getTenantId();
return checkNotNull(customerService.findCustomerByTenantIdAndTitle(tenantId, customerTitle));
} catch (Exception e) {
throw handleException(e);
}
}
}

8
dao/src/main/java/org/thingsboard/server/dao/customer/CustomerService.java

@ -22,20 +22,24 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.TextPageData;
import org.thingsboard.server.common.data.page.TextPageLink;
import java.util.Optional;
public interface CustomerService {
Customer findCustomerById(CustomerId customerId);
Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title);
ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId);
Customer saveCustomer(Customer customer);
void deleteCustomer(CustomerId customerId);
Customer findOrCreatePublicCustomer(TenantId tenantId);
TextPageData<Customer> findCustomersByTenantId(TenantId tenantId, TextPageLink pageLink);
void deleteCustomersByTenantId(TenantId tenantId);
}

8
dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java

@ -52,6 +52,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
private static final String PUBLIC_CUSTOMER_TITLE = "Public";
public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId ";
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
@Autowired
private CustomerDao customerDao;
@ -78,6 +79,13 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
return customerDao.findById(customerId.getId());
}
@Override
public Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title) {
log.trace("Executing findCustomerByTenantIdAndTitle [{}] [{}]", tenantId, title);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
return customerDao.findCustomersByTenantIdAndTitle(tenantId.getId(), title);
}
@Override
public ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId) {
log.trace("Executing findCustomerByIdAsync [{}]", customerId);

19
tools/src/main/java/org/thingsboard/client/tools/RestClient.java

@ -77,6 +77,21 @@ public class RestClient implements ClientHttpRequestInterceptor {
}
}
public Optional<Asset> findAsset(String name) {
Map<String, String> params = new HashMap<String, String>();
params.put("assetName", name);
try {
ResponseEntity<Asset> assetEntity = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, params);
return Optional.of(assetEntity.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Customer createCustomer(String title) {
Customer customer = new Customer();
customer.setTitle(title);
@ -123,6 +138,10 @@ public class RestClient implements ClientHttpRequestInterceptor {
return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
}
public Customer getCustomerByTitle(String title) {
return restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle=" + title, Customer.class).getBody();
}
public RestTemplate getRestTemplate() {
return restTemplate;
}

Loading…
Cancel
Save