Search in sources :

Example 21 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class RestClient method createCustomer.

public Customer createCustomer(String title) {
    Customer customer = new Customer();
    customer.setTitle(title);
    return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
}
Also used : Customer(org.thingsboard.server.common.data.Customer)

Example 22 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class AssetController method assignAssetToPublicCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/asset/{assetId}", method = RequestMethod.POST)
@ResponseBody
public Asset assignAssetToPublicCustomer(@PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter(ASSET_ID, strAssetId);
    try {
        AssetId assetId = new AssetId(toUUID(strAssetId));
        Asset asset = checkAssetId(assetId);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(asset.getTenantId());
        Asset savedAsset = checkNotNull(assetService.assignAssetToCustomer(assetId, publicCustomer.getId()));
        logEntityAction(assetId, savedAsset, savedAsset.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strAssetId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strAssetId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 23 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class AssetController method unassignAssetFromCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/asset/{assetId}", method = RequestMethod.DELETE)
@ResponseBody
public Asset unassignAssetFromCustomer(@PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter(ASSET_ID, strAssetId);
    try {
        AssetId assetId = new AssetId(toUUID(strAssetId));
        Asset asset = checkAssetId(assetId);
        if (asset.getCustomerId() == null || asset.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            throw new IncorrectParameterException("Asset isn't assigned to any customer!");
        }
        Customer customer = checkCustomerId(asset.getCustomerId());
        Asset savedAsset = checkNotNull(assetService.unassignAssetFromCustomer(assetId));
        logEntityAction(assetId, asset, asset.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, strAssetId, customer.getId().toString(), customer.getName());
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.UNASSIGNED_FROM_CUSTOMER, e, strAssetId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 24 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class CustomerController method saveCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer", method = RequestMethod.POST)
@ResponseBody
public Customer saveCustomer(@RequestBody Customer customer) throws ThingsboardException {
    try {
        customer.setTenantId(getCurrentUser().getTenantId());
        Customer savedCustomer = checkNotNull(customerService.saveCustomer(customer));
        logEntityAction(savedCustomer.getId(), savedCustomer, savedCustomer.getId(), customer.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
        return savedCustomer;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.CUSTOMER), customer, null, customer.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 25 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class DeviceController method assignDeviceToPublicCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/device/{deviceId}", method = RequestMethod.POST)
@ResponseBody
public Device assignDeviceToPublicCustomer(@PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        Device device = checkDeviceId(deviceId);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(device.getTenantId());
        Device savedDevice = checkNotNull(deviceService.assignDeviceToCustomer(deviceId, publicCustomer.getId()));
        logEntityAction(deviceId, savedDevice, savedDevice.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strDeviceId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedDevice;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strDeviceId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

Customer (org.thingsboard.server.common.data.Customer)59 Test (org.junit.Test)37 CustomerId (org.thingsboard.server.common.data.id.CustomerId)24 Tenant (org.thingsboard.server.common.data.Tenant)21 ArrayList (java.util.ArrayList)18 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)18 User (org.thingsboard.server.common.data.User)16 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Asset (org.thingsboard.server.common.data.asset.Asset)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)10 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)8 TypeReference (com.fasterxml.jackson.core.type.TypeReference)7 Device (org.thingsboard.server.common.data.Device)7 Matchers.containsString (org.hamcrest.Matchers.containsString)4 AssetId (org.thingsboard.server.common.data.id.AssetId)3 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 UUID (java.util.UUID)2 Before (org.junit.Before)2