Search in sources :

Example 16 with Account

use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.

the class Accounts method getAccount.

/**
 * Returns the Account specified by the "id" path parameter.
 *
 * @param accountId The id of the Account requested.
 * @return The requested Account object.
 */
@GET
@Path("{accountId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Account getAccount(@PathParam("accountId") String accountId) {
    Account account = null;
    try {
        KapuaId id = KapuaEid.parseShortId(accountId);
        account = accountService.find(id);
    } catch (Throwable t) {
        handleException(t);
    }
    return returnNotNullEntity(account);
}
Also used : Account(org.eclipse.kapua.service.account.Account) KapuaId(org.eclipse.kapua.model.id.KapuaId) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with Account

use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.

the class Accounts method postAccount.

/**
 * Returns the Service Plan for the Account specified by the "id" path parameter.
 *
 * @param accountId The id of the Account for which the Service Plan is requested.
 * @return The requested AccountServicePlan object.
 */
// @GET
// @Path("{accountId}/servicePlan")
// @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
// public AccountServicePlan getAccountServicePlan(@PathParam("accountId") long accountId) {
// 
// AccountServicePlan servicePlan = null;
// try {
// ServiceLocator sl = ServiceLocator.getInstance();
// AccountService as = sl.getAccountService();
// servicePlan = as.getAccountServicePlan(accountId);
// } catch (Throwable t) {
// handleException(t);
// }
// return returnNotNullEntity(servicePlan);
// }
/**
 * Returns the usage for the Account specified by the "id" path parameter.
 *
 * @param limit Maximum number of entries to be returned.
 * @param offset Starting offset for the entries to be returned.
 * @param startDate Start date of the date range requested. The parameter is expressed as a long counting the number of milliseconds since January 1, 1970, 00:00:00 GMT. The default value of 0
 *            means no start date. Alternatively, the date can be expressed as a string following the ISO 8601 format.
 * @param endDate End date of the date range requested. The parameter is expressed as a long counting the number of milliseconds since January 1, 1970, 00:00:00 GMT. The default value of 0 means
 *            no end date. Alternatively, the date can be expressed as a string following the ISO 8601 format.
 * @param accountId The id of the Account for which the usage is requested.
 * @return The requested UsageResult object.
 */
// @GET
// @Path("{accountId}/usageByHour")
// @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
// public UsageResult getAccountUsageByHour(@PathParam("accountId") long accountId,
// @QueryParam("limit")     @DefaultValue("50") int limit,
// @QueryParam("offset")    @DefaultValue("0")  int offset,
// @QueryParam("startDate") @DefaultValue("0")  EdcTimestamp startDate,
// @QueryParam("endDate")   @DefaultValue("0")  EdcTimestamp endDate) {
// 
// EdcUsageQuery query = new EdcUsageQuery();
// int maxLimit = EdcConfig.getInstance().getDataExportMaxLimit();
// int maxCount = EdcConfig.getInstance().getDataExportMaxCount();
// if (limit > maxLimit)
// limit = maxLimit;
// if (offset > maxCount)
// offset = maxCount;
// query.setLimit(limit);
// query.setIndexOffset(offset);
// query.setTimetype(1); // HOUR
// if (startDate.getTimeInMillis() != 0) {
// query.setStartDate(startDate.getTimeInMillis());
// }
// if (endDate.getTimeInMillis() != 0) {
// query.setEndDate(endDate.getTimeInMillis());
// }
// UsageResult usage = new UsageResult();
// try {
// ServiceLocator sl = ServiceLocator.getInstance();
// AccountService as = sl.getAccountService();
// String accountName = as.getAccountName(accountId);
// AccountUsageService aus = sl.getAccountUsageService();
// EdcListResult<EdcUsageInfo> result = aus.findUsageByAccount(accountName, query);
// usage.setUsage(result);
// } catch (Throwable t) {
// handleException(t);
// }
// return returnNotNullEntity(usage);
// }
/**
 * Returns the daily usage for the Account specified by the "id" path parameter.
 *
 * @param limit Maximum number of entries to be returned.
 * @param offset Starting offset for the entries to be returned.
 * @param startDate Start date of the date range requested. The parameter is expressed as a long counting the number of milliseconds since January 1, 1970, 00:00:00 GMT. The default value of 0
 *            means no start date. Alternatively, the date can be expressed as a string following the ISO 8601 format.
 * @param endDate End date of the date range requested. The parameter is expressed as a long counting the number of milliseconds since January 1, 1970, 00:00:00 GMT. The default value of 0 means
 *            no end date. Alternatively, the date can be expressed as a string following the ISO 8601 format.
 * @param accountId The id of the Account for which the usage is requested.
 * @return The requested UsageResult object.
 */
// @GET
// @Path("{accountId}/usageByDay")
// @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
// public UsageResult getAccountUsageByDay(@PathParam("accountId") long accountId,
// @QueryParam("limit")     @DefaultValue("50") int limit,
// @QueryParam("offset")    @DefaultValue("0")  int offset,
// @QueryParam("startDate") @DefaultValue("0")  EdcTimestamp startDate,
// @QueryParam("endDate")   @DefaultValue("0")  EdcTimestamp endDate) {
// 
// EdcUsageQuery query = new EdcUsageQuery();
// int maxLimit = EdcConfig.getInstance().getDataExportMaxLimit();
// int maxCount = EdcConfig.getInstance().getDataExportMaxCount();
// if (limit > maxLimit)
// limit = maxLimit;
// if (offset > maxCount)
// offset = maxCount;
// query.setLimit(limit);
// query.setIndexOffset(offset);
// query.setTimetype(2); // DAY
// if (startDate.getTimeInMillis() != 0) {
// query.setStartDate(startDate.getTimeInMillis());
// }
// if (endDate.getTimeInMillis() != 0) {
// query.setEndDate(endDate.getTimeInMillis());
// }
// UsageResult usage = new UsageResult();
// try {
// ServiceLocator sl = ServiceLocator.getInstance();
// AccountService as = sl.getAccountService();
// String accountName = as.getAccountName(accountId);
// AccountUsageService aus = sl.getAccountUsageService();
// EdcListResult<EdcUsageInfo> result = aus.findUsageByAccount(accountName, query);
// usage.setUsage(result);
// } catch (Throwable t) {
// handleException(t);
// }
// return returnNotNullEntity(usage);
// }
/**
 * Returns the number of currently connected devices for the specified account.
 *
 * @param accountId The id of the Account.
 * @return The requested DeviceCountResult object.
 */
// @GET
// @Path("{accountId}/devices/count")
// @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
// public CountResult getDeviceCount(@PathParam("accountId") long accountId) {
// 
// CountResult dcr = new CountResult();
// try {
// ServiceLocator sl = ServiceLocator.getInstance();
// DeviceRegistryService ds = sl.getDeviceRegistryService();
// long result = ds.getDeviceCount(accountId);
// dcr.setCount(result);
// } catch (Throwable t) {
// handleException(t);
// }
// return returnNotNullEntity(dcr);
// }
/**
 * Creates a new Account based on the information provided in AccountCreator parameter.
 *
 * @param accountCreator Provides the information for the new Account to be created.
 * @return The newly created Account object.
 */
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Account postAccount(AccountCreator accountCreator) {
    Account account = null;
    try {
        accountCreator.setScopeId(KapuaSecurityUtils.getSession().getScopeId());
        account = accountService.create(accountCreator);
    } catch (Throwable t) {
        handleException(t);
    }
    return returnNotNullEntity(account);
}
Also used : Account(org.eclipse.kapua.service.account.Account) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 18 with Account

use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.

the class GwtAccountServiceImpl method update.

public GwtAccount update(GwtXSRFToken xsrfToken, GwtAccount gwtAccount) throws GwtKapuaException {
    // 
    // Checking validity of the given XSRF Token
    checkXSRFToken(xsrfToken);
    GwtAccount gwtAccountUpdated = null;
    KapuaId scopeId = KapuaEid.parseShortId(gwtAccount.getId());
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        AccountService accountService = locator.getService(AccountService.class);
        Account account = accountService.find(scopeId);
        account.getOrganization().setName(gwtAccount.getGwtOrganization().getName());
        account.getOrganization().setPersonName(gwtAccount.getGwtOrganization().getPersonName());
        account.getOrganization().setEmail(gwtAccount.getGwtOrganization().getEmailAddress());
        account.getOrganization().setPhoneNumber(gwtAccount.getGwtOrganization().getPhoneNumber());
        account.getOrganization().setAddressLine1(gwtAccount.getGwtOrganization().getAddressLine1());
        account.getOrganization().setAddressLine2(gwtAccount.getGwtOrganization().getAddressLine2());
        account.getOrganization().setZipPostCode(gwtAccount.getGwtOrganization().getZipPostCode());
        account.getOrganization().setCity(gwtAccount.getGwtOrganization().getCity());
        account.getOrganization().setStateProvinceCounty(gwtAccount.getGwtOrganization().getStateProvinceCounty());
        account.getOrganization().setCountry(gwtAccount.getGwtOrganization().getCountry());
        account.setOptlock(gwtAccount.getOptlock());
        account = accountService.update(account);
        // convert to GwtAccount and return
        gwtAccountUpdated = KapuaGwtConverter.convert(account);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtAccountUpdated;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) KapuaId(org.eclipse.kapua.model.id.KapuaId) AccountService(org.eclipse.kapua.service.account.AccountService) GwtAccountService(org.eclipse.kapua.app.console.shared.service.GwtAccountService)

Example 19 with Account

use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.

the class GwtAccountServiceImpl method findByAccountName.

@Override
public GwtAccount findByAccountName(String accountName) throws GwtKapuaException {
    GwtAccount gwtAccount = null;
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        AccountService accountService = locator.getService(AccountService.class);
        Account account = accountService.findByName(accountName);
        if (account != null) {
            gwtAccount = KapuaGwtConverter.convert(account);
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtAccount;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) AccountService(org.eclipse.kapua.service.account.AccountService) GwtAccountService(org.eclipse.kapua.app.console.shared.service.GwtAccountService)

Example 20 with Account

use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.

the class GwtAuthorizationServiceImpl method establishSession.

private GwtSession establishSession() throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    // 
    // Get info from session
    KapuaSession kapuaSession = KapuaSecurityUtils.getSession();
    // 
    // Get user info
    UserService userService = locator.getService(UserService.class);
    User user = userService.find(kapuaSession.getScopeId(), kapuaSession.getUserId());
    // 
    // Get permission info
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    boolean hasAccountCreate = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, kapuaSession.getScopeId()));
    boolean hasAccountRead = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.read, kapuaSession.getScopeId()));
    boolean hasAccountUpdate = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, kapuaSession.getScopeId()));
    boolean hasAccountDelete = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.delete, kapuaSession.getScopeId()));
    boolean hasAccountAll = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, null, null));
    boolean hasDeviceCreate = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.write, kapuaSession.getScopeId()));
    boolean hasDeviceRead = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.read, kapuaSession.getScopeId()));
    boolean hasDeviceUpdate = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.write, kapuaSession.getScopeId()));
    boolean hasDeviceDelete = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.delete, kapuaSession.getScopeId()));
    boolean hasDeviceManage = authorizationService.isPermitted(permissionFactory.newPermission(DeviceLifecycleDomain.DEVICE_LIFECYCLE, Actions.write, kapuaSession.getScopeId()));
    boolean hasDataRead = authorizationService.isPermitted(permissionFactory.newPermission("data", Actions.read, kapuaSession.getScopeId()));
    boolean hasUserCreate = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.write, kapuaSession.getScopeId()));
    boolean hasUserRead = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.read, kapuaSession.getScopeId()));
    boolean hasUserUpdate = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.write, kapuaSession.getScopeId()));
    boolean hasUserDelete = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.delete, kapuaSession.getScopeId()));
    // 
    // Get account info
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.find(kapuaSession.getScopeId());
    // 
    // Convert entities
    GwtUser gwtUser = KapuaGwtConverter.convert(user);
    GwtAccount gwtAccount = KapuaGwtConverter.convert(account);
    // 
    // Build the session
    GwtSession gwtSession = new GwtSession();
    // Console info
    SystemSetting commonsConfig = SystemSetting.getInstance();
    gwtSession.setVersion(commonsConfig.getString(SystemSettingKey.VERSION));
    gwtSession.setBuildVersion(commonsConfig.getString(SystemSettingKey.BUILD_VERSION));
    gwtSession.setBuildNumber(commonsConfig.getString(SystemSettingKey.BUILD_NUMBER));
    // User info
    gwtSession.setGwtUser(gwtUser);
    gwtSession.setGwtAccount(gwtAccount);
    gwtSession.setRootAccount(gwtAccount);
    gwtSession.setSelectedAccount(gwtAccount);
    // Permission info
    gwtSession.setAccountCreatePermission(hasAccountCreate);
    gwtSession.setAccountReadPermission(hasAccountRead);
    gwtSession.setAccountUpdatePermission(hasAccountUpdate);
    gwtSession.setAccountDeletePermission(hasAccountDelete);
    gwtSession.setAccountAllPermission(hasAccountAll);
    gwtSession.setDeviceCreatePermission(hasDeviceCreate);
    gwtSession.setDeviceReadPermission(hasDeviceRead);
    gwtSession.setDeviceUpdatePermission(hasDeviceUpdate);
    gwtSession.setDeviceDeletePermission(hasDeviceDelete);
    gwtSession.setDeviceManagePermission(hasDeviceManage);
    gwtSession.setDataReadPermission(hasDataRead);
    gwtSession.setUserCreatePermission(hasUserCreate);
    gwtSession.setUserReadPermission(hasUserRead);
    gwtSession.setUserUpdatePermission(hasUserUpdate);
    gwtSession.setUserDeletePermission(hasUserDelete);
    return gwtSession;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) User(org.eclipse.kapua.service.user.User) UserService(org.eclipse.kapua.service.user.UserService) KapuaSession(org.eclipse.kapua.commons.security.KapuaSession) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) SystemSetting(org.eclipse.kapua.commons.setting.system.SystemSetting) GwtSession(org.eclipse.kapua.app.console.shared.model.GwtSession) GwtAuthorizationService(org.eclipse.kapua.app.console.shared.service.GwtAuthorizationService) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) AccountService(org.eclipse.kapua.service.account.AccountService)

Aggregations

Account (org.eclipse.kapua.service.account.Account)42 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)37 AccountService (org.eclipse.kapua.service.account.AccountService)31 KapuaId (org.eclipse.kapua.model.id.KapuaId)14 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)12 Device (org.eclipse.kapua.service.device.registry.Device)11 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)11 KapuaException (org.eclipse.kapua.KapuaException)10 GwtAccount (org.eclipse.kapua.app.console.shared.model.GwtAccount)10 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)9 GwtAccountService (org.eclipse.kapua.app.console.shared.service.GwtAccountService)9 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)8 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)8 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)7 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)7 Date (java.util.Date)5 AccountCreator (org.eclipse.kapua.service.account.AccountCreator)5 KuraRequestChannel (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestChannel)5 KuraRequestMessage (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestMessage)5 KuraRequestPayload (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestPayload)5