Merge branch 'master' into android-feature

master
Pahansith Gunathilake 2 years ago
commit 0f8c3e22e6

@ -2,4 +2,4 @@
<a href='https://opensource.org/licenses/Apache-2.0'><img src='https://img.shields.io/badge/License-Apache%202.0-blue.svg'></a><br/>
<a href='#'><img src="https://storage.googleapis.com/cdn-entgra/build-status/device-mgt-core/icon.svg"></a>
<a href='#'><img src="https://builder.entgra.net/buildStatus/icon?job=device-mgt-core"></a>

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>grafana-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>grafana-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>grafana-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>analytics-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -3,7 +3,7 @@
<parent>
<artifactId>carbon-devicemgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>apimgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -21,7 +21,7 @@
<parent>
<artifactId>apimgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>apimgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -3,7 +3,7 @@
<parent>
<artifactId>apimgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

@ -40,11 +40,11 @@ public interface KeyManagerService {
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/token")
Response generateAccessToken(@HeaderParam("Authorization") String basicAuthHeader,
@FormParam("client_id") String clientId,
@FormParam("client_secret") String clientSecret,
@FormParam("refresh_token") String refreshToken,
@FormParam("scope") String scope,
@FormParam("grant_type") String grantType,
@FormParam("assertion") String assertion,
@FormParam("admin_access_token") String admin_access_token);
@FormParam("admin_access_token") String admin_access_token,
@FormParam("username") String username,
@FormParam("password") String password);
}

@ -63,13 +63,13 @@ public class KeyManagerServiceImpl implements KeyManagerService {
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/token")
public Response generateAccessToken(@HeaderParam("Authorization") String basicAuthHeader,
@FormParam("client_id") String clientId,
@FormParam("client_secret") String clientSecret,
@FormParam("refresh_token") String refreshToken,
@FormParam("scope") String scope,
@FormParam("grant_type") String grantType,
@FormParam("assertion") String assertion,
@FormParam("admin_access_token") String admin_access_token) {
@FormParam("admin_access_token") String admin_access_token,
@FormParam("username") String username,
@FormParam("password") String password) {
try {
if (basicAuthHeader == null) {
String msg = "Invalid credentials. Make sure your API call is invoked with a Basic Authorization header.";
@ -80,7 +80,7 @@ public class KeyManagerServiceImpl implements KeyManagerService {
TokenResponse resp = keyMgtService.generateAccessToken(
new TokenRequest(encodedClientCredentials.split(":")[0],
encodedClientCredentials.split(":")[1], refreshToken, scope,
grantType, assertion,admin_access_token));
grantType, assertion, admin_access_token, username, password));
return Response.status(Response.Status.OK).entity(gson.toJson(resp)).build();
} catch (KeyMgtException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();

@ -3,7 +3,7 @@
<parent>
<artifactId>apimgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -26,9 +26,11 @@ public class TokenRequest {
private String grantType;
private String assertion;
private String admin_access_token;
private String username;
private String password;
public TokenRequest(String clientId, String clientSecret, String refreshToken, String scope, String grantType,
String assertion, String admin_access_token) {
String assertion, String admin_access_token, String username, String password) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.refreshToken = refreshToken;
@ -36,6 +38,8 @@ public class TokenRequest {
this.grantType = grantType;
this.assertion = assertion;
this.admin_access_token = admin_access_token;
this.username = username;
this.password = password;
}
public String getClientId() {
@ -93,4 +97,20 @@ public class TokenRequest {
public void setAdminAccessToken(String admin_access_token) {
this.admin_access_token = admin_access_token;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -33,6 +33,13 @@ public class TokenResponse {
this.expires_in = expires_in;
}
public TokenResponse(String access_token, String scope, String token_type, int expires_in) {
this.access_token = access_token;
this.scope = scope;
this.token_type = token_type;
this.expires_in = expires_in;
}
public String getAccessToken() {
return access_token;
}

@ -159,49 +159,34 @@ public class KeyMgtServiceImpl implements KeyMgtService {
}
String tenantDomain = MultitenantUtils.getTenantDomain(application.getOwner());
String username, password;
if (KeyMgtConstants.SUPER_TENANT.equals(tenantDomain)) {
kmConfig = getKeyManagerConfig();
username = kmConfig.getAdminUsername();
password = kmConfig.getAdminUsername();
} else {
try {
username = getRealmService()
.getTenantUserRealm(-1234).getRealmConfiguration()
.getRealmProperty("reserved_tenant_user_username") + "@" + tenantDomain;
password = getRealmService()
.getTenantUserRealm(-1234).getRealmConfiguration()
.getRealmProperty("reserved_tenant_user_password");
} catch (UserStoreException e) {
msg = "Error while loading user realm configuration";
log.error(msg);
throw new KeyMgtException(msg);
}
}
kmConfig = getKeyManagerConfig();
String appTokenEndpoint = kmConfig.getServerUrl() + KeyMgtConstants.OAUTH2_TOKEN_ENDPOINT;
RequestBody appTokenPayload;
switch (tokenRequest.getGrantType()) {
case "client_credentials":
appTokenPayload = new FormBody.Builder()
.add("grant_type", "client_credentials")
.add("scope", tokenRequest.getScope()).build();
break;
case "password":
appTokenPayload = new FormBody.Builder()
.add("grant_type", "password")
.add("username", username)
.add("password", password)
.add("username", tokenRequest.getUsername())
.add("password", tokenRequest.getPassword())
.add("scope", tokenRequest.getScope()).build();
break;
case "refresh_token":
appTokenPayload = new FormBody.Builder()
.add("grant_type", "refresh_token")
.add("refresh_token", tokenRequest.getRefreshToken())
.add("scope", tokenRequest.getScope()).build();
.add("refresh_token", tokenRequest.getRefreshToken()).build();
break;
case "urn:ietf:params:oauth:grant-type:jwt-bearer":
appTokenPayload = new FormBody.Builder()
.add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer")
.add("assertion", tokenRequest.getAssertion())
.add("scope", tokenRequest.getScope()).build();
appTokenEndpoint += "?tenantDomain=carbon.super";
break;
case "access_token":
appTokenPayload = new FormBody.Builder()
@ -216,8 +201,6 @@ public class KeyMgtServiceImpl implements KeyMgtService {
break;
}
kmConfig = getKeyManagerConfig();
String appTokenEndpoint = kmConfig.getServerUrl() + KeyMgtConstants.OAUTH2_TOKEN_ENDPOINT;
Request request = new Request.Builder()
.url(appTokenEndpoint)
.addHeader(KeyMgtConstants.AUTHORIZATION_HEADER, Credentials.basic(tokenRequest.getClientId(), tokenRequest.getClientSecret()))
@ -239,12 +222,19 @@ public class KeyMgtServiceImpl implements KeyMgtService {
.getTenantManager().getTenantId(tenantDomain);
accessToken = tenantId + "_" + responseObj.getString("access_token");
}
return new TokenResponse(accessToken,
responseObj.getString("refresh_token"),
responseObj.getString("scope"),
responseObj.getString("token_type"),
responseObj.getInt("expires_in"));
if (tokenRequest.getGrantType().equals("client_credentials")) {
return new TokenResponse(accessToken,
responseObj.getString("scope"),
responseObj.getString("token_type"),
responseObj.getInt("expires_in"));
} else {
return new TokenResponse(accessToken,
responseObj.getString("refresh_token"),
responseObj.getString("scope"),
responseObj.getString("token_type"),
responseObj.getInt("expires_in"));
}
} catch (APIManagementException e) {
msg = "Error occurred while retrieving application";
log.error(msg);

@ -22,7 +22,7 @@
<parent>
<artifactId>apimgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>carbon-devicemgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

@ -20,7 +20,7 @@
<parent>
<artifactId>application-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>application-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>application-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>application-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -31,6 +31,7 @@ import io.entgra.application.mgt.core.util.DAOUtil;
import io.entgra.application.mgt.core.dao.impl.AbstractDAOImpl;
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
import io.entgra.application.mgt.core.exception.UnexpectedServerErrorException;
import io.entgra.application.mgt.core.util.Constants;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -149,7 +150,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
sql += "WHERE AP_APP.TENANT_ID = ? ";
if (StringUtils.isNotEmpty(filter.getAppType())) {
if (StringUtils.isNotEmpty(filter.getAppType()) && !Constants.ALL.equalsIgnoreCase(filter.getAppType())) {
sql += "AND AP_APP.TYPE = ? ";
}
if (StringUtils.isNotEmpty(filter.getAppName())) {
@ -204,7 +205,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
stmt.setInt(paramIndex++, tenantId);
if (StringUtils.isNotEmpty(filter.getAppType())) {
if (StringUtils.isNotEmpty(filter.getAppType()) && !Constants.ALL.equalsIgnoreCase(filter.getAppType())) {
stmt.setString(paramIndex++, filter.getAppType());
}
if (StringUtils.isNotEmpty(filter.getAppName())) {

@ -26,6 +26,7 @@ import io.entgra.application.mgt.common.dto.ApplicationDTO;
import io.entgra.application.mgt.common.exception.DBConnectionException;
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
import io.entgra.application.mgt.core.util.DAOUtil;
import io.entgra.application.mgt.core.util.Constants;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -94,7 +95,7 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
|| StringUtils.isNotEmpty(filter.getAppReleaseType())) {
sql += "LEFT JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID ";
}
if (StringUtils.isNotEmpty(filter.getAppType())) {
if (StringUtils.isNotEmpty(filter.getAppType()) && !Constants.ALL.equalsIgnoreCase(filter.getAppType())) {
sql += "AND AP_APP.TYPE = ? ";
}
if (StringUtils.isNotEmpty(filter.getAppName())) {
@ -145,7 +146,7 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
if (StringUtils.isNotEmpty(filter.getAppType())) {
if (StringUtils.isNotEmpty(filter.getAppType()) && !Constants.ALL.equalsIgnoreCase(filter.getAppType())) {
stmt.setString(paramIndex++, filter.getAppType());
}
if (StringUtils.isNotEmpty(filter.getAppName())) {

@ -25,6 +25,7 @@ import io.entgra.application.mgt.common.dto.ApplicationDTO;
import io.entgra.application.mgt.common.exception.DBConnectionException;
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
import io.entgra.application.mgt.core.util.DAOUtil;
import io.entgra.application.mgt.core.util.Constants;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -93,7 +94,7 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl {
|| StringUtils.isNotEmpty(filter.getAppReleaseType())) {
sql += "LEFT JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID ";
}
if (StringUtils.isNotEmpty(filter.getAppType())) {
if (StringUtils.isNotEmpty(filter.getAppType()) && !Constants.ALL.equalsIgnoreCase(filter.getAppType())) {
sql += "AND AP_APP.TYPE = ? ";
}
if (StringUtils.isNotEmpty(filter.getAppName())) {
@ -144,7 +145,7 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
if (StringUtils.isNotEmpty(filter.getAppType())) {
if (StringUtils.isNotEmpty(filter.getAppType()) && !Constants.ALL.equalsIgnoreCase(filter.getAppType())) {
stmt.setString(paramIndex++, filter.getAppType());
}
if (StringUtils.isNotEmpty(filter.getAppName())) {

@ -2932,7 +2932,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (!StringUtils.isEmpty(appType)) {
boolean isValidAppType = false;
for (ApplicationType applicationType : ApplicationType.values()) {
if (applicationType.toString().equalsIgnoreCase(appType)) {
if (applicationType.toString().equalsIgnoreCase(appType) || Constants.ALL.equalsIgnoreCase(appType)) {
isValidAppType = true;
break;
}
@ -3434,6 +3434,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int deviceTypeId = -1;
String appName;
int appNameLength = 20;
List<String> appCategories;
List<String> unrestrictedRoles;
@ -3445,6 +3446,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg);
throw new BadRequestException(msg);
}
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = applicationWrapper.getCategories();
if (appCategories == null) {
String msg = "Application category can't be null.";
@ -3477,6 +3483,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg);
throw new BadRequestException(msg);
}
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = webAppWrapper.getCategories();
if (appCategories == null) {
String msg = "Web Clip category can't be null.";
@ -3510,6 +3521,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg);
throw new BadRequestException(msg);
}
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = publicAppWrapper.getCategories();
if (appCategories == null) {
String msg = "Application category can't be null.";
@ -3542,6 +3558,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg);
throw new BadRequestException(msg);
}
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = customAppWrapper.getCategories();
if (appCategories == null) {
String msg = "Application category can't be null.";

@ -22,7 +22,7 @@
<parent>
<artifactId>application-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -322,7 +322,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished);
@QueryParam("is-published") boolean isPublished);
@POST
@Path("/web-app")
@ -366,7 +366,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished
@QueryParam("is-published") boolean isPublished
);
@POST
@ -411,7 +411,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished
@QueryParam("is-published") boolean isPublished
);
@POST
@ -457,7 +457,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished
@QueryParam("is-published") boolean isPublished
);
@POST
@ -512,7 +512,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished
@QueryParam("is-published") boolean isPublished
);
@POST
@ -567,7 +567,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished
@QueryParam("is-published") boolean isPublished
);
@POST
@ -617,7 +617,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished
@QueryParam("is-published") boolean isPublished
);
@POST
@ -672,7 +672,7 @@ public interface ApplicationManagementPublisherAPI {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished
@QueryParam("is-published") boolean isPublished
);
@PUT

@ -318,7 +318,7 @@ public interface SPApplicationService {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished);
@QueryParam("is-published") boolean isPublished);
/**
* This method is used to register an APIM application for tenant domain.
@ -345,7 +345,7 @@ public interface SPApplicationService {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished);
@QueryParam("is-published") boolean isPublished);
@Path("/{identity-server-id}/service-provider/{service-provider-id}/create/web-app")
@POST
@ -369,7 +369,7 @@ public interface SPApplicationService {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished);
@QueryParam("is-published") boolean isPublished);
@Path("/{identity-server-id}/service-provider/{service-provider-id}/create/custom-app")
@POST
@ -392,5 +392,5 @@ public interface SPApplicationService {
name = "isPublished",
value = "Published state of the application"
)
@QueryParam("isPublished") boolean isPublished);
@QueryParam("is-published") boolean isPublished);
}

@ -157,12 +157,12 @@ public interface ApplicationManagementPublisherAdminAPI {
@PathParam("appId") int applicatioId);
@DELETE
@Path("/tags/{tagName}")
@Path("/tags")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
httpMethod = "DELETE",
value = "Delete application tag",
notes = "This will delete application tag",
tags = "Application Management",
@ -185,10 +185,10 @@ public interface ApplicationManagementPublisherAdminAPI {
})
Response deleteTag(
@ApiParam(
name = "tagName",
name = "tag-name",
value = "Tag Name",
required = true)
@PathParam("tagName") String tagName
@QueryParam("tag-name") String tagName
);
@POST
@ -273,7 +273,7 @@ public interface ApplicationManagementPublisherAdminAPI {
);
@DELETE
@Path("/categories/{categoryName}")
@Path("/categories")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
@ -301,10 +301,10 @@ public interface ApplicationManagementPublisherAdminAPI {
})
Response deleteCategory(
@ApiParam(
name = "categoryName",
name = "category-name",
value = "Category Name",
required = true)
@PathParam("categoryName") String categoryName
@QueryParam("category-name") String categoryName
);
@PUT

@ -208,7 +208,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/public-app")
@ -260,7 +260,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
@PathParam("deviceType") String deviceTypeName,
@PathParam("appId") int appId,
EntAppReleaseWrapper entAppReleaseWrapper,
@QueryParam("isPublished") boolean isPublished) {
@QueryParam("is-published") boolean isPublished) {
try {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
applicationManager.validateEntAppReleaseCreatingRequest(entAppReleaseWrapper, deviceTypeName);
@ -284,7 +284,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
public Response createPubAppRelease(
@PathParam("deviceType") String deviceTypeName,
@PathParam("appId") int appId,
PublicAppReleaseWrapper publicAppReleaseWrapper, @QueryParam("isPublished") boolean isPublished) {
PublicAppReleaseWrapper publicAppReleaseWrapper, @QueryParam("is-published") boolean isPublished) {
try {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
@ -312,7 +312,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
@Override
public Response createWebAppRelease(
@PathParam("appId") int appId,
WebAppReleaseWrapper webAppReleaseWrapper, @QueryParam("isPublished") boolean isPublished) {
WebAppReleaseWrapper webAppReleaseWrapper, @QueryParam("is-published") boolean isPublished) {
try {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
applicationManager.validateWebAppReleaseCreatingRequest(webAppReleaseWrapper);
@ -340,7 +340,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
public Response createCustomAppRelease(
@PathParam("deviceType") String deviceTypeName,
@PathParam("appId") int appId,
CustomAppReleaseWrapper customAppReleaseWrapper, @QueryParam("isPublished") boolean isPublished) {
CustomAppReleaseWrapper customAppReleaseWrapper, @QueryParam("is-published") boolean isPublished) {
try {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
applicationManager.validateCustomAppReleaseCreatingRequest(customAppReleaseWrapper, deviceTypeName);
@ -371,19 +371,28 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
if (appName == null) {
String msg = "Invalid app name, appName query param cannot be empty/null.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).build();
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
if (appName.length() > 20) {
String msg = "Invalid app name, maximum length of the application name should be 20 characters.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
ApplicationManager applicationManager = APIUtil.getApplicationManager();
if (applicationManager.isExistingAppName(appName, deviceType)) {
return Response.status(Response.Status.CONFLICT).build();
String msg = "Invalid app name, app name already exists.";
log.error(msg);
return Response.status(Response.Status.CONFLICT).entity(msg).build();
}
return Response.status(Response.Status.OK).build();
} catch (BadRequestException e) {
log.error("Found invalid device type to check application existence.", e);
return Response.status(Response.Status.BAD_REQUEST).build();
String msg = "Found invalid device type to check application existence.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (ApplicationManagementException e) {
log.error("Internal Error occurred while checking the application existence.", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
String msg = "Internal Error occurred while checking the application existence.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}

@ -295,7 +295,7 @@ public class SPApplicationServiceImpl implements SPApplicationService {
@Override
public Response createEntApp(@PathParam("identity-server-id") int identityServerId,
@PathParam("service-provider-id") String serviceProviderId, ApplicationWrapper app,
@QueryParam("isPublished") boolean isPublished) {
@QueryParam("is-published") boolean isPublished) {
return createSPApplication(identityServerId, serviceProviderId, app, isPublished);
}
@ -304,7 +304,7 @@ public class SPApplicationServiceImpl implements SPApplicationService {
@Override
public Response createPubApp(@PathParam("identity-server-id") int identityServerId,
@PathParam("service-provider-id") String serviceProviderId, PublicAppWrapper app,
@QueryParam("isPublished") boolean isPublished) {
@QueryParam("is-published") boolean isPublished) {
return createSPApplication(identityServerId, serviceProviderId, app, isPublished);
}
@ -313,7 +313,7 @@ public class SPApplicationServiceImpl implements SPApplicationService {
@Override
public Response createWebApp(@PathParam("identity-server-id") int identityServerId,
@PathParam("service-provider-id") String serviceProviderId, WebAppWrapper app,
@QueryParam("isPublished") boolean isPublished) {
@QueryParam("is-published") boolean isPublished) {
return createSPApplication(identityServerId, serviceProviderId, app, isPublished);
}
@ -322,7 +322,7 @@ public class SPApplicationServiceImpl implements SPApplicationService {
@Override
public Response createCustomApp(@PathParam("identity-server-id") int identityServerId,
@PathParam("service-provider-id") String serviceProviderId, CustomAppWrapper app,
@QueryParam("isPublished") boolean isPublished) {
@QueryParam("is-published") boolean isPublished) {
return createSPApplication(identityServerId, serviceProviderId, app, isPublished);
}

@ -104,9 +104,9 @@ public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationMa
@DELETE
@Override
@Consumes(MediaType.WILDCARD)
@Path("/tags/{tagName}")
@Path("/tags")
public Response deleteTag(
@PathParam("tagName") String tagName) {
@QueryParam("tag-name") String tagName) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
applicationManager.deleteTag(tagName);
@ -169,9 +169,9 @@ public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationMa
@DELETE
@Override
@Consumes(MediaType.WILDCARD)
@Path("/categories/{categoryName}")
@Path("/categories")
public Response deleteCategory(
@PathParam("categoryName") String categoryName) {
@QueryParam("category-name") String categoryName) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
applicationManager.deleteCategory(categoryName);

@ -22,7 +22,7 @@
<parent>
<artifactId>application-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -50,11 +50,22 @@ public class RequestValidationUtil {
case "REMOVED":
case "BLOCKED":
case "CREATED":
case "CONFIGURED":
case "READY_TO_CONNECT":
case "RETURN_PENDING":
case "RETURNED":
case "DEFECTIVE":
case "WARRANTY_PENDING":
case "WARRANTY_SENT":
case "WARRANTY_REPLACED":
case "ASSIGNED":
break;
default:
String msg = "Invalid enrollment status type: " + status + ". \nValid status types " +
"are ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " +
"DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED";
"DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED | CONFIGURED | READY_TO_CONNECT | " +
"RETURN_PENDING | RETURNED | DEFECTIVE | WARRANTY_PENDING | WARRANTY_SENT | " +
"WARRANTY_REPLACED | ASSIGNED |";
log.error(msg);
throw new BadRequestException(msg);
}

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>carbon-devicemgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>certificate-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>certificate-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -38,7 +38,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>certificate-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>carbon-devicemgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

@ -23,7 +23,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
@ -51,7 +51,7 @@
<Bundle-Description>Entgra Logger Bundle</Bundle-Description>
<Import-Package>
io.entgra.device.mgt.extensions.logger,
org.apache.commons.logging;version="[1.2,2)
org.apache.commons.logging
</Import-Package>
<Export-Package>
io.entgra.device.mgt.extensions.logger.*

@ -24,57 +24,29 @@ import org.apache.commons.logging.Log;
public interface EntgraLogger extends Log {
void info(String message);
void info(Object object, LogContext logContext);
void info(Object object, Throwable t, LogContext logContext);
void info(String message, Throwable t);
void debug(Object object, LogContext logContext);
void info(String message, LogContext logContext);
void debug(Object object, Throwable t, LogContext logContext);
void debug(String message);
void error(Object object, LogContext logContext);
void debug(String message, Throwable t);
void error(Object object, Throwable t, LogContext logContext);
void debug(String message, LogContext logContext);
void fatal(Object object, LogContext logContext);
void error(String message);
void fatal(Object object, Throwable t, LogContext logContext);
void error(String message, Throwable t);
void trace(Object object, LogContext logContext);
void error(String message, LogContext logContext);
void trace(Object object, Throwable t, LogContext logContext);
void error(String message, Throwable t, LogContext logContext);
void warn(Object object, LogContext logContext);
void warn(String message);
void warn(String message, Throwable t);
void warn(String message, LogContext logContext);
void warn(String message, Throwable t, LogContext logContext);
void trace(String message);
void trace(String message, Throwable t);
void trace(String message, LogContext logContext);
void fatal(String message);
void fatal(String message, Throwable t);
void fatal(String message, LogContext logContext);
boolean isDebugEnabled();
boolean isErrorEnabled();
boolean isFatalEnabled();
boolean isInfoEnabled();
boolean isTraceEnabled();
boolean isWarnEnabled();
void warn(Object object, Throwable t, LogContext logContext);
void clearLogContext();

@ -23,7 +23,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>carbon-devicemgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -35,8 +35,11 @@ public class DeviceList extends BasePaginatedResult {
@ApiModelProperty(name = "message", value = "Send information text to the billing UI", required = false)
private String message;
@ApiModelProperty(name = "billedDateIsValid", value = "Check if user entered date is valid", required = false)
private boolean billedDateIsValid;
@ApiModelProperty(name = "deviceCount", value = "Total count of all devices per tenant", required = false)
private double deviceCount;
@ApiModelProperty(name = "billPeriod", value = "Billed period", required = false)
private String billPeriod;
@ApiModelProperty(value = "List of devices returned")
@JsonProperty("devices")
@ -48,12 +51,20 @@ public class DeviceList extends BasePaginatedResult {
this.devices = devices;
}
public boolean isBilledDateIsValid() {
return billedDateIsValid;
public String getBillPeriod() {
return billPeriod;
}
public void setBillPeriod(String billPeriod) {
this.billPeriod = billPeriod;
}
public double getDeviceCount() {
return deviceCount;
}
public void setBilledDateIsValid(boolean billedDateIsValid) {
this.billedDateIsValid = billedDateIsValid;
public void setDeviceCount(double deviceCount) {
this.deviceCount = deviceCount;
}
public String getMessage() {

@ -344,179 +344,6 @@ public interface DeviceManagementService {
@QueryParam("limit")
int limit);
@GET
@Path("/billing")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting Cost details of devices in a tenant",
notes = "Provides individual cost and total cost of all devices per tenant.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of devices.",
response = DeviceList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client already has the latest version of " +
"the requested resource.\n"),
@ApiResponse(
code = 400,
message = "The incoming request has more than one selection criteria defined via the query parameters.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "The search criteria did not match any device registered with the server.",
response = ErrorResponse.class),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the device list.",
response = ErrorResponse.class)
})
Response getDevicesBilling(
@ApiParam(
name = "tenantDomain",
value = "The tenant domain.",
required = false)
@QueryParam("tenantDomain")
String tenantDomain,
@ApiParam(
name = "startDate",
value = "The start date.",
required = false)
@QueryParam("startDate")
Timestamp startDate,
@ApiParam(
name = "endDate",
value = "The end date.",
required = false)
@QueryParam("endDate")
Timestamp endDate,
@ApiParam(
name = "generateBill",
value = "The generate bill boolean.",
required = false)
@QueryParam("generateBill")
boolean generateBill,
@ApiParam(
name = "offset",
value = "The starting pagination index for the complete list of qualified items.",
required = false,
defaultValue = "0")
@QueryParam("offset")
int offset,
@ApiParam(
name = "limit",
value = "Provide how many device details you require from the starting pagination index/offset.",
required = false,
defaultValue = "10")
@QueryParam("limit")
int limit);
@GET
@Path("/billing/file")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting Cost details of devices in a tenant",
notes = "Provides individual cost and total cost of all devices per tenant.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of devices.",
response = DeviceList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "Content-Disposition",
description = "The content disposition of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client already has the latest version of " +
"the requested resource.\n"),
@ApiResponse(
code = 400,
message = "The incoming request has more than one selection criteria defined via the query parameters.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "The search criteria did not match any device registered with the server.",
response = ErrorResponse.class),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the device list.",
response = ErrorResponse.class)
})
Response exportBilling(
@ApiParam(
name = "tenantDomain",
value = "The tenant domain.",
required = false)
@QueryParam("tenantDomain")
String tenantDomain,
@ApiParam(
name = "startDate",
value = "The start date.",
required = false)
@QueryParam("startDate")
Timestamp startDate,
@ApiParam(
name = "endDate",
value = "The end date.",
required = false)
@QueryParam("endDate")
Timestamp endDate,
@ApiParam(
name = "generateBill",
value = "The generate bill boolean.",
required = false)
@QueryParam("generateBill")
boolean generateBill);
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(

@ -65,6 +65,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.util.List;
@SwaggerDefinition(
@ -110,7 +111,14 @@ import java.util.List;
key = "perm:devices:permanent-delete",
roles = {"Internal/devicemgt-admin"},
permissions = {"/device-mgt/admin/devices/permanent-delete"}
)
),
@Scope(
name = "Get Usage of Devices",
description = "Get Usage of Devices",
key = "perm:admin:usage:view",
roles = {"Internal/devicemgt-admin"},
permissions = {"/device-mgt/admin/devices/usage/view"}
),
}
)
public interface DeviceManagementAdminService {
@ -423,4 +431,78 @@ public interface DeviceManagementAdminService {
List<String> actions
);
@GET
@Path("/billing")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting Cost details of devices in a tenant",
notes = "Provides individual cost and total cost of all devices per tenant.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:usage:view")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of devices.",
response = DeviceList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "Content-Disposition",
description = "The content disposition of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client already has the latest version of " +
"the requested resource.\n"),
@ApiResponse(
code = 400,
message = "The incoming request has more than one selection criteria defined via the query parameters.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "The search criteria did not match any device registered with the server.",
response = ErrorResponse.class),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the device list.",
response = ErrorResponse.class)
})
Response getBilling(
@ApiParam(
name = "tenantDomain",
value = "The tenant domain.",
required = false)
@QueryParam("tenantDomain")
String tenantDomain,
@ApiParam(
name = "startDate",
value = "The start date.",
required = false)
@QueryParam("startDate")
Timestamp startDate,
@ApiParam(
name = "endDate",
value = "The end date.",
required = false)
@QueryParam("endDate")
Timestamp endDate);
}

@ -355,105 +355,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
}
@GET
@Override
@Path("/billing")
public Response getDevicesBilling(
@QueryParam("tenantDomain") String tenantDomain,
@QueryParam("startDate") Timestamp startDate,
@QueryParam("endDate") Timestamp endDate,
@QueryParam("generateBill") boolean generateBill,
@QueryParam("offset") int offset,
@DefaultValue("10")
@QueryParam("limit") int limit) {
try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
PaginationRequest request = new PaginationRequest(offset, limit);
PaginationResult result;
DeviceList devices = new DeviceList();
int tenantId = 0;
RealmService realmService = DeviceMgtAPIUtils.getRealmService();
if (!tenantDomain.isEmpty()) {
tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
} else {
tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
}
try {
result = dms.getAllDevicesBillings(request, tenantId, tenantDomain, startDate, endDate, generateBill);
} catch (Exception exception) {
String msg = "Error occurred when trying to retrieve billing data";
log.error(msg, exception);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
int resultCount = result.getRecordsTotal();
if (resultCount == 0) {
Response.status(Response.Status.OK).entity(devices).build();
}
devices.setList((List<Device>) result.getData());
devices.setBilledDateIsValid(result.isBilledDateIsValid());
devices.setMessage(result.getMessage());
devices.setCount(result.getRecordsTotal());
devices.setTotalCost(result.getTotalCost());
return Response.status(Response.Status.OK).entity(devices).build();
} catch (Exception e) {
String msg = "Error occurred while retrieving billing data";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@GET
@Override
@Path("/billing/file")
public Response exportBilling(
@QueryParam("tenantDomain") String tenantDomain,
@QueryParam("startDate") Timestamp startDate,
@QueryParam("endDate") Timestamp endDate,
@QueryParam("generateBill") boolean generateBill) {
try {
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
PaginationResult result;
int tenantId = 0;
RealmService realmService = DeviceMgtAPIUtils.getRealmService();
DeviceList devices = new DeviceList();
if (!tenantDomain.isEmpty()) {
tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
} else {
tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
}
try {
result = dms.createBillingFile(tenantId, tenantDomain, startDate, endDate, generateBill);
} catch (Exception exception) {
String msg = "Error occurred when trying to retrieve billing data without pagination";
log.error(msg, exception);
return null;
}
devices.setList((List<Device>) result.getData());
devices.setBilledDateIsValid(result.isBilledDateIsValid());
devices.setMessage(result.getMessage());
devices.setCount(result.getRecordsTotal());
devices.setTotalCost(result.getTotalCost());
return Response.status(Response.Status.OK).entity(devices).build();
} catch (Exception e) {
String msg = "Error occurred while retrieving billing data without pagination";
log.error(msg, e);
return null;
}
}
@GET
@Override
@Path("/user-devices")

@ -54,6 +54,7 @@ import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.resource.services.utils.ChangeRolePermissionsUtil;
import org.wso2.carbon.user.api.*;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.constants.UserCoreErrorConstants.ErrorMessages;
import org.wso2.carbon.user.mgt.UserRealmProxy;
import org.wso2.carbon.user.mgt.common.UIPermissionNode;
import org.wso2.carbon.user.mgt.common.UserAdminException;
@ -316,21 +317,31 @@ public class RoleManagementServiceImpl implements RoleManagementService {
entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been"
+ " added").build();
} catch (UserStoreException e) {
String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
String errorCode = "";
String errorMessage = e.getMessage();
if (errorMessage != null && !errorMessage.isEmpty() &&
errorMessage.contains(ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS.getCode())) {
errorCode = e.getMessage().split("-")[0].trim();
}
if (ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS.getCode().equals(errorCode)) {
String roleName = roleInfo.getRoleName().split("/")[1];
String msg = "Role already exists with name " + roleName + ".";
log.warn(msg);
return Response.status(Response.Status.CONFLICT).entity(msg).build();
} else {
String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} catch (URISyntaxException e) {
String msg = "Error occurred while composing the URI at which the information of the newly created role " +
"can be retrieved";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (UnsupportedEncodingException e) {
String msg = "Error occurred while encoding role name";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}

@ -52,12 +52,15 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.exceptions.UserNotFoundException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import javax.validation.constraints.Size;
import javax.ws.rs.Consumes;
@ -71,6 +74,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.util.List;
@Path("/admin/devices")
@ -147,7 +151,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
@Path("/device-owner")
public Response updateEnrollOwner(
@QueryParam("owner") String owner,
List<String> deviceIdentifiers){
List<String> deviceIdentifiers) {
try {
if (DeviceMgtAPIUtils.getDeviceManagementService().updateEnrollment(owner, true, deviceIdentifiers)) {
String msg = "Device owner is updated successfully.";
@ -190,8 +194,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
catch (InvalidDeviceException e) {
} catch (InvalidDeviceException e) {
String msg = "Found Invalid devices";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(
@ -205,7 +208,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
public Response triggerCorrectiveActions(
@PathParam("deviceId") String deviceIdentifier,
@PathParam("featureCode") String featureCode,
List<String> actions){
List<String> actions) {
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
PlatformConfigurationManagementService platformConfigurationManagementService = DeviceMgtAPIUtils
.getPlatformConfigurationManagementService();
@ -233,4 +236,51 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
}
return Response.status(Response.Status.OK).entity("Triggered action successfully").build();
}
@GET
@Override
@Path("/billing")
public Response getBilling(
@QueryParam("tenantDomain") String tenantDomain,
@QueryParam("startDate") Timestamp startDate,
@QueryParam("endDate") Timestamp endDate) {
try {
PaginationResult result;
int tenantId = 0;
RealmService realmService = DeviceMgtAPIUtils.getRealmService();
int tenantIdContext = CarbonContext.getThreadLocalCarbonContext().getTenantId();
if (!tenantDomain.isEmpty()) {
tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
}
if (tenantIdContext != MultitenantConstants.SUPER_TENANT_ID && tenantIdContext != tenantId) {
String msg = "Current logged in user is not authorized to access billing details of other tenants";
log.error(msg);
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
} else {
DeviceList devices = new DeviceList();
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
result = dms.createBillingFile(tenantId, tenantDomain, startDate, endDate);
devices.setList((List<Device>) result.getData());
devices.setDeviceCount(result.getTotalDeviceCount());
devices.setMessage(result.getMessage());
devices.setTotalCost(result.getTotalCost());
devices.setBillPeriod(startDate.toString() + " - " + endDate.toString());
return Response.status(Response.Status.OK).entity(devices).build();
}
} catch (BadRequestException e) {
String msg = "Bad request, can't proceed. Hence verify the request and re-try";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving billing data";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (UserStoreException e) {
String msg = "Error occurred while processing tenant configuration.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
}

@ -153,11 +153,21 @@ public class RequestValidationUtil {
case "REMOVED":
case "BLOCKED":
case "CREATED":
case "CONFIGURED":
case "READY_TO_CONNECT":
case "RETURN_PENDING":
case "RETURNED":
case "DEFECTIVE":
case "WARRANTY_PENDING":
case "WARRANTY_SENT":
case "WARRANTY_REPLACED":
case "ASSIGNED":
break;
default:
String msg = "Invalid enrollment status type: " + status + ". \nValid status types are " +
"ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " +
"DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED";
"DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED | CONFIGURED | READY_TO_CONNECT | " +
"RETURN_PENDING | RETURNED | DEFECTIVE | WARRANTY_PENDING | WARRANTY_SENT | WARRANTY_REPLACED | ASSIGNED ";
log.error(msg);
throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder()
.setCode(HttpStatus.SC_BAD_REQUEST)
@ -175,12 +185,18 @@ public class RequestValidationUtil {
switch (ownership) {
case "BYOD":
case "COPE":
case "WORK_PROFILE":
case "GOOGLE_ENTERPRISE":
case "COSU":
case "FULLY_MANAGED":
case "DEDICATED_DEVICE":
return;
default:
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(
"Invalid ownership type received. " +
"Valid ownership types are BYOD | COPE").build());
"Valid ownership types are BYOD | COPE " +
"WORK_PROFILE | GOOGLE_ENTERPRISE | COSU | FULLY_MANAGED | DEDICATED_DEVICE").build());
}
}

@ -21,7 +21,7 @@
<parent>
<artifactId>device-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -0,0 +1,123 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.common;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.List;
@ApiModel(value = "BillingResponse", description = "This class carries all information related to a billing response.")
public class BillingResponse implements Serializable {
private static final long serialVersionUID = 1998101711L;
@ApiModelProperty(name = "year", value = "Year of the billed period",
required = false)
private String year;
@ApiModelProperty(name = "totalCostPerYear", value = "Bill for a period of year", required = false)
private double totalCostPerYear;
@ApiModelProperty(name = "devices", value = "Billed list of devices per year", required = false)
private List<Device> device;
@ApiModelProperty(name = "billPeriod", value = "Billed period", required = false)
private String billPeriod;
@ApiModelProperty(name = "startDate", value = "Start Date of period", required = false)
private String startDate;
@ApiModelProperty(name = "endDate", value = "End Date of period", required = false)
private String endDate;
@ApiModelProperty(name = "deviceCount", value = "Device count for a billing period",
required = false)
private int deviceCount;
public BillingResponse() {
}
public BillingResponse(String year, double totalCostPerYear, List<Device> device, String billPeriod, String startDate, String endDate, int deviceCount) {
this.year = year;
this.totalCostPerYear = totalCostPerYear;
this.device = device;
this.billPeriod = billPeriod;
this.startDate = startDate;
this.endDate = endDate;
this.deviceCount = deviceCount;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public double getTotalCostPerYear() {
return totalCostPerYear;
}
public void setTotalCostPerYear(double totalCostPerYear) {
this.totalCostPerYear = totalCostPerYear;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public List<Device> getDevice() {
return device;
}
public void setDevice(List<Device> device) {
this.device = device;
}
public String getBillPeriod() {
return billPeriod;
}
public void setBillPeriod(String billPeriod) {
this.billPeriod = billPeriod;
}
public int getDeviceCount() {
return deviceCount;
}
public void setDeviceCount(int deviceCount) {
this.deviceCount = deviceCount;
}
}

@ -47,18 +47,29 @@ public class PaginationResult implements Serializable {
@ApiModelProperty(name = "totalCost", value = "Total cost of all devices per tenant", required = false)
private double totalCost;
@ApiModelProperty(name = "billedDateIsValid", value = "Check if user entered date is valid", required = false)
private boolean billedDateIsValid;
@ApiModelProperty(name = "totalDeviceCount", value = "Total count of all devices per tenant", required = false)
private double totalDeviceCount;
@ApiModelProperty(name = "billPeriod", value = "Billed period", required = false)
private String billPeriod;
@ApiModelProperty(name = "message", value = "Send information text to the billing UI", required = false)
private String message;
public boolean isBilledDateIsValid() {
return billedDateIsValid;
public String getBillPeriod() {
return billPeriod;
}
public void setBillPeriod(String billPeriod) {
this.billPeriod = billPeriod;
}
public double getTotalDeviceCount() {
return totalDeviceCount;
}
public void setBilledDateIsValid(boolean billedDateIsValid) {
this.billedDateIsValid = billedDateIsValid;
public void setTotalDeviceCount(double totalDeviceCount) {
this.totalDeviceCount = totalDeviceCount;
}
public String getMessage() {

@ -0,0 +1,49 @@
/* Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.common.otp.mgt.wrapper;
public class DownloadURLDetails {
private String firstName;
private String URL;
private String email;
public String getURL() {
return URL;
}
public void setURL(String URL) {
this.URL = URL;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.OTPManagementException;
import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.DownloadURLDetails;
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.OTPWrapper;
import java.util.Map;
@ -34,7 +35,7 @@ public interface OTPManagementService {
* @throws OTPManagementException if error occurs while creating OTP token and storing tenant details.
* @throws BadRequestException if found and incompatible payload to create OTP token.
*/
void sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException;
String sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException;
/**
* Check the validity of the OTP
@ -62,4 +63,13 @@ public interface OTPManagementService {
*/
void sendDeviceEnrollmentInvitationMail(DeviceEnrollmentInvitation deviceEnrollmentInvitation)
throws OTPManagementException;
}
/**
* Send an e-mail to the requesting e-mail address with a product download URL
* @param downloadURLDetails Contains the details to send product download e-mail
* @throws OTPManagementException if request payload doesn't contains required details to send the product
* download mail.
*/
void shareProductDownloadUrl(DownloadURLDetails downloadURLDetails) throws OTPManagementException;
}

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>device-mgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -44,6 +44,7 @@ public final class DeviceManagementConstants {
public static final String DEVICE_CACHE = "DEVICE_CACHE";
public static final String API_RESOURCE_PERMISSION_CACHE = "API_RESOURCE_CACHE_CACHE";
public static final String GEOFENCE_CACHE = "GEOFENCE_CACHE";
public static final String BILLING_CACHE = "BILLING_CACHE";
public static final String META_KEY = "PER_DEVICE_COST";
public static final String ACTIVE_STATUS = "ACTIVE";
public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification";
@ -131,6 +132,7 @@ public final class DeviceManagementConstants {
public static final String USER_REGISTRATION_TEMPLATE = "user-registration";
public static final String USER_ENROLLMENT_TEMPLATE = "user-enrollment";
public static final String USER_VERIFY_TEMPLATE = "user-verify";
public static final String PRODUCT_DOWNLOAD_LINK_SHARING_TEMPLATE = "share-product-download-url";
public static final String POLICY_VIOLATE_TEMPLATE = "policy-violating-notifier";
public static final String USER_WELCOME_TEMPLATE = "user-welcome";
public static final String DEFAULT_ENROLLMENT_TEMPLATE = "default-enrollment-invitation";

@ -0,0 +1,79 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.cache;
import java.sql.Timestamp;
import java.util.Objects;
public class BillingCacheKey {
private String tenantDomain;
private Timestamp startDate;
private Timestamp endDate;
private volatile int hashCode;
public String getTenantDomain() {
return tenantDomain;
}
public void setTenantDomain(String tenantDomain) {
this.tenantDomain = tenantDomain;
}
public Timestamp getStartDate() {
return startDate;
}
public void setStartDate(Timestamp startDate) {
this.startDate = startDate;
}
public Timestamp getEndDate() {
return endDate;
}
public void setEndDate(Timestamp endDate) {
this.endDate = endDate;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!BillingCacheKey.class.isAssignableFrom(obj.getClass())) {
return false;
}
final BillingCacheKey other = (BillingCacheKey) obj;
String thisId = this.tenantDomain + "_" + this.startDate + "_" + this.endDate;
String otherId = other.tenantDomain + "_" + other.startDate + "_" + this.endDate;
if (!thisId.equals(otherId)) {
return false;
}
return true;
}
@Override
public int hashCode() {
if (hashCode == 0) {
hashCode = Objects.hash(tenantDomain, startDate, endDate);
}
return hashCode;
}
}

@ -0,0 +1,73 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.cache;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import java.sql.Timestamp;
import java.util.List;
public interface BillingCacheManager {
/**
* Adds a given billing object to the billing-cache.
* @param startDate - startDate of the billing period.
* @param endDate - endDate of the billing period.
* @param paginationResult - PaginationResult object to be added.
* @param tenantDomain - Owning tenant of the billing.
*
*/
void addBillingToCache(PaginationResult paginationResult, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException;
/**
* Removes a billing object from billing-cache.
* @param startDate - startDate of the billing period.
* @param endDate - endDate of the billing period.
* @param tenantDomain - Owning tenant of the billing.
*
*/
void removeBillingFromCache(String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException;
/**
* Removes a list of devices from billing-cache.
* @param billingList - List of Cache-Keys of the billing objects to be removed.
*
*/
void removeBillingsFromCache(List<BillingCacheKey> billingList) throws DeviceManagementException;
/**
* Updates a given billing object in the billing-cache.
* @param startDate - startDate of the billing period.
* @param endDate - endDate of the billing period.
* @param paginationResult - PaginationResult object to be updated.
* @param tenantDomain - Owning tenant of the billing.
*
*/
void updateBillingInCache(PaginationResult paginationResult, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException;
/**
* Fetches a billing object from billing-cache.
* @param startDate - startDate of the billing period.
* @param endDate - endDate of the billing period.
* @param tenantDomain - Owning tenant of the billing.
* @return Device object
*
*/
PaginationResult getBillingFromCache(String tenantDomain, Timestamp startDate, Timestamp endDate);
}

@ -0,0 +1,135 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.cache.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.cache.BillingCacheKey;
import org.wso2.carbon.device.mgt.core.cache.BillingCacheManager;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import javax.cache.Cache;
import java.sql.Timestamp;
import java.util.List;
/**
* Implementation of BillingCacheManager.
*/
public class BillingCacheManagerImpl implements BillingCacheManager {
private static final Log log = LogFactory.getLog(BillingCacheManagerImpl.class);
private static BillingCacheManagerImpl billingCacheManager;
private BillingCacheManagerImpl() {
}
public static BillingCacheManagerImpl getInstance() {
if (billingCacheManager == null) {
synchronized (BillingCacheManagerImpl.class) {
if (billingCacheManager == null) {
billingCacheManager = new BillingCacheManagerImpl();
}
}
}
return billingCacheManager;
}
@Override
public void addBillingToCache(PaginationResult paginationResult, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException {
Cache<BillingCacheKey, PaginationResult> lCache = DeviceManagerUtil.getBillingCache();
if (lCache != null) {
BillingCacheKey cacheKey = getCacheKey(tenantDomain, startDate, endDate);
if (lCache.containsKey(cacheKey)) {
this.updateBillingInCache(paginationResult, tenantDomain, startDate, endDate);
} else {
lCache.put(cacheKey, paginationResult);
}
}
}
@Override
public void removeBillingFromCache(String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException {
Cache<BillingCacheKey, PaginationResult> lCache = DeviceManagerUtil.getBillingCache();
if (lCache != null) {
BillingCacheKey cacheKey = getCacheKey(tenantDomain, startDate, endDate);
if (lCache.containsKey(cacheKey)) {
lCache.remove(cacheKey);
}
} else {
String msg = "Failed to remove selected billing from cache";
log.error(msg);
throw new DeviceManagementException(msg);
}
}
@Override
public void removeBillingsFromCache(List<BillingCacheKey> billingList) throws DeviceManagementException {
Cache<BillingCacheKey, PaginationResult> lCache = DeviceManagerUtil.getBillingCache();
if (lCache != null) {
for (BillingCacheKey cacheKey : billingList) {
if (lCache.containsKey(cacheKey)) {
lCache.remove(cacheKey);
}
}
} else {
String msg = "Failed to remove billing from cache";
log.error(msg);
throw new DeviceManagementException(msg);
}
}
@Override
public void updateBillingInCache(PaginationResult paginationResult, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException {
Cache<BillingCacheKey, PaginationResult> lCache = DeviceManagerUtil.getBillingCache();
if (lCache != null) {
BillingCacheKey cacheKey = getCacheKey(tenantDomain, startDate, endDate);
if (lCache.containsKey(cacheKey)) {
lCache.replace(cacheKey, paginationResult);
}
} else {
String msg = "Failed to update billing cache";
log.error(msg);
throw new DeviceManagementException(msg);
}
}
// TODO remove null check from here and do cache enable check in the methods calling this
@Override
public PaginationResult getBillingFromCache(String tenantDomain, Timestamp startDate, Timestamp endDate) {
Cache<BillingCacheKey, PaginationResult> lCache = DeviceManagerUtil.getBillingCache();
if (lCache != null) {
return lCache.get(getCacheKey(tenantDomain, startDate, endDate));
}
return null;
}
/**
* This method generates the billing CacheKey and returns it.
*/
private BillingCacheKey getCacheKey(String tenantDomain, Timestamp startDate, Timestamp endDate) {
BillingCacheKey billingCacheKey = new BillingCacheKey();
billingCacheKey.setTenantDomain(tenantDomain);
billingCacheKey.setStartDate(startDate);
billingCacheKey.setEndDate(endDate);
return billingCacheKey;
}
}

@ -21,6 +21,7 @@ import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotif
import org.wso2.carbon.device.mgt.common.roles.config.DefaultRoles;
import org.wso2.carbon.device.mgt.core.config.analytics.OperationAnalyticsConfiguration;
import org.wso2.carbon.device.mgt.core.config.archival.ArchivalConfiguration;
import org.wso2.carbon.device.mgt.core.config.cache.BillingCacheConfiguration;
import org.wso2.carbon.device.mgt.core.config.cache.CertificateCacheConfiguration;
import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration;
import org.wso2.carbon.device.mgt.core.config.cache.GeoFenceCacheConfiguration;
@ -58,6 +59,7 @@ public final class DeviceManagementConfig {
private DeviceStatusTaskConfig deviceStatusTaskConfig;
private DeviceCacheConfiguration deviceCacheConfiguration;
private GeoFenceCacheConfiguration geoFenceCacheConfiguration;
private BillingCacheConfiguration billingCacheConfiguration;
private EventOperationTaskConfiguration eventOperationTaskConfiguration;
private CertificateCacheConfiguration certificateCacheConfiguration;
private OperationAnalyticsConfiguration operationAnalyticsConfiguration;
@ -169,6 +171,15 @@ public final class DeviceManagementConfig {
this.geoFenceCacheConfiguration = geoFenceCacheConfiguration;
}
@XmlElement(name = "BillingCacheConfiguration", required = true)
public BillingCacheConfiguration getBillingCacheConfiguration() {
return billingCacheConfiguration;
}
public void setBillingCacheConfiguration(BillingCacheConfiguration billingCacheConfiguration) {
this.billingCacheConfiguration = billingCacheConfiguration;
}
@XmlElement(name = "EventOperationTaskConfiguration", required = true)
public EventOperationTaskConfiguration getEventOperationTaskConfiguration() {
return eventOperationTaskConfiguration;

@ -0,0 +1,56 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.config.cache;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "BillingCacheConfiguration")
public class BillingCacheConfiguration {
private boolean isEnabled;
private int expiryTime;
private long capacity;
@XmlElement(name = "Enable", required = true)
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
@XmlElement(name = "ExpiryTime", required = true)
public int getExpiryTime() {
return expiryTime;
}
public void setExpiryTime(int expiryTime) {
this.expiryTime = expiryTime;
}
@XmlElement(name = "Capacity", required = true)
public long getCapacity() {
return capacity;
}
public void setCapacity(long capacity) {
this.capacity = capacity;
}
}

@ -1,13 +0,0 @@
package org.wso2.carbon.device.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.Billing;
import java.sql.Timestamp;
import java.util.List;
public interface BillingDAO {
void addBilling(int deviceId, int tenantId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException;
List<Billing> getBilling(int deviceId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException;
}

@ -293,6 +293,50 @@ public interface DeviceDAO {
*/
List<Device> getDevices(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the not removed device list in a year of a given tenant without pagination.
*
* @param tenantId tenant id.
* @param startDate start date of usage period.
* @param endDate end date of usage period.
* @return returns a list of not removed devices enrolled in that year.
* @throws DeviceManagementDAOException
*/
List<Device> getNonRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the removed device list in a year of a given tenant without pagination.
*
* @param tenantId tenant id.
* @param startDate start date of usage period.
* @param endDate end date of usage period.
* @return returns a list of removed devices enrolled in that year.
* @throws DeviceManagementDAOException
*/
List<Device> getRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the not removed device list in the prior year of a given tenant without pagination.
*
* @param tenantId tenant id.
* @param startDate start date of usage period.
* @param endDate end date of usage period.
* @return returns a list of not removed devices enrolled prior to that year.
* @throws DeviceManagementDAOException
*/
List<Device> getNonRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the removed device list in the prior year of a given tenant without pagination.
*
* @param tenantId tenant id.
* @param startDate start date of usage period.
* @param endDate end date of usage period.
* @return returns a list of removed devices enrolled prior to that year.
* @throws DeviceManagementDAOException
*/
List<Device> getRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the devices of a given tenant without pagination.
* @param tenantId tenant id.

@ -125,11 +125,6 @@ public class DeviceManagementDAOFactory {
return new EnrollmentDAOImpl();
}
public static BillingDAO getBillingDAO() {
return new BillingDAOImpl();
}
public static TrackerDAO getTrackerDAO() {
if (databaseEngine != null) {
switch (databaseEngine) {

@ -33,8 +33,6 @@ public interface EnrollmentDAO {
boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException;
boolean updateEnrollmentLastBilledDate(EnrolmentInfo enrolmentInfos, Timestamp lastBilledDate, int tenantId) throws DeviceManagementDAOException;
int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
@Deprecated

@ -1,71 +0,0 @@
package org.wso2.carbon.device.mgt.core.dao.impl;
import org.wso2.carbon.device.mgt.common.Billing;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.dao.BillingDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BillingDAOImpl implements BillingDAO {
@Override
public void addBilling(int deviceId, int tenantId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "INSERT INTO DM_BILLING(DEVICE_ID, TENANT_ID, BILLING_START, BILLING_END) VALUES(?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, new String[] {"invoice_id"});
stmt.setInt(1, deviceId);
stmt.setInt(2,tenantId);
stmt.setTimestamp(3, billingStart);
stmt.setTimestamp(4, billingEnd);
stmt.execute();
} catch (SQLException e) {
e.printStackTrace();
throw new DeviceManagementDAOException("Error occurred while adding billing period", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<Billing> getBilling(int deviceId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException {
List<Billing> billings = new ArrayList<>();
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo.Status status = null;
try {
conn = this.getConnection();
String sql;
sql = "SELECT * FROM DM_BILLING WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
rs = stmt.executeQuery();
while (rs.next()) {
Billing bill = new Billing(rs.getInt("INVOICE_ID"), rs.getInt("DEVICE_ID"),rs.getInt("TENANT_ID"),
(rs.getTimestamp("BILLING_START").getTime()), (rs.getTimestamp("BILLING_END").getTime()));
billings.add(bill);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred getting billing periods", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return billings;
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}
}

@ -25,13 +25,7 @@ public class DeviceStatusDAOImpl implements DeviceStatusDAO {
conn = this.getConnection();
// either we list all status values for the device using the device id or only get status values for the given enrolment id
String idType = isDeviceId ? "DEVICE_ID" : "ENROLMENT_ID";
String sql;
if (billingStatus) {
sql = "SELECT ENROLMENT_ID, DEVICE_ID, UPDATE_TIME, STATUS, CHANGED_BY FROM DM_DEVICE_STATUS WHERE STATUS IN ('ACTIVE','REMOVED') AND " + idType + " = ?";
} else {
sql = "SELECT ENROLMENT_ID, DEVICE_ID, UPDATE_TIME, STATUS, CHANGED_BY FROM DM_DEVICE_STATUS WHERE " + idType + " = ?";
}
String sql = "SELECT ENROLMENT_ID, DEVICE_ID, UPDATE_TIME, STATUS, CHANGED_BY FROM DM_DEVICE_STATUS WHERE " + idType + " = ?";
// filter the data based on a date range if specified
if (fromDate != null){
@ -41,6 +35,10 @@ public class DeviceStatusDAOImpl implements DeviceStatusDAO {
sql += " AND UPDATE_TIME <= ?";
}
if (billingStatus) {
sql += " ORDER BY UPDATE_TIME DESC";
}
stmt = conn.prepareStatement(sql);
int i = 1;

@ -144,28 +144,6 @@ public class EnrollmentDAOImpl implements EnrollmentDAO {
return status;
}
@Override
public boolean updateEnrollmentLastBilledDate(EnrolmentInfo enrolmentInfo, Timestamp lastBilledDate, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET LAST_BILLED_DATE = ? WHERE ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, lastBilledDate.getTime());
stmt.setInt(2, enrolmentInfo.getId());
stmt.setInt(3, tenantId);
stmt.executeUpdate();
return true;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment last billed date.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public int removeEnrollment(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {

@ -84,7 +84,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"e.IS_TRANSFERRED, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, " +
"e.LAST_BILLED_DATE, " +
"e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, " +
"(SELECT d.ID, " +
@ -188,6 +187,169 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
@Override
public List<Device> getNonRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
List<Device> devices = new ArrayList<>();
try {
Connection conn = getConnection();
String sql = "SELECT d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " +
"NAME, " +
"DATE_OF_ENROLMENT, " +
"STATUS, " +
"DATE_OF_LAST_UPDATE, " +
"TIMESTAMPDIFF(DAY, ?, DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED " +
"FROM DM_DEVICE d, DM_ENROLMENT e " +
"WHERE " +
"e.TENANT_ID=? AND " +
"d.ID=e.DEVICE_ID AND " +
"STATUS !='REMOVED' AND " +
"(" +
"DATE_OF_ENROLMENT BETWEEN ? AND ? " +
")";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, String.valueOf(endDate));
stmt.setInt(2, tenantId);
stmt.setString(3, String.valueOf(startDate));
stmt.setString(4, String.valueOf(endDate));
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
devices.add(DeviceManagementDAOUtil.loadDeviceBilling(rs));
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of NonRemovedYearly device billing ", e);
}
return devices;
}
@Override
public List<Device> getRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
Connection conn;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "select d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " +
"NAME, " +
"DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, " +
"STATUS, " +
"TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, DATE_OF_ENROLMENT) AS DAYS_USED " +
"from DM_DEVICE d, DM_ENROLMENT e " +
"where " +
"e.TENANT_ID=? and d.ID=e.DEVICE_ID and " +
"STATUS ='REMOVED' and " +
"(" +
"DATE_OF_ENROLMENT between ? and ? " +
") and " +
"(" +
"DATE_OF_LAST_UPDATE >= ? " +
")";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
stmt.setString(2, String.valueOf(startDate));
stmt.setString(3, String.valueOf(endDate));
stmt.setString(4, String.valueOf(startDate));
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
devices.add(DeviceManagementDAOUtil.loadDeviceBilling(rs));
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of RemovedYearly device billing ", e);
}
return devices;
}
@Override
public List<Device> getNonRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
Connection conn;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "select d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " +
"NAME, " +
"DATE_OF_ENROLMENT, " +
"STATUS, " +
"DATE_OF_LAST_UPDATE, " +
"TIMESTAMPDIFF(DAY, ?, ?) as DAYS_SINCE_ENROLLED " +
"from DM_DEVICE d, DM_ENROLMENT e " +
"where " +
"e.TENANT_ID=? and " +
"d.ID=e.DEVICE_ID and " +
"STATUS !='REMOVED' and " +
"(" +
"DATE_OF_ENROLMENT < ? " +
")";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, String.valueOf(endDate));
stmt.setString(2, String.valueOf(startDate));
stmt.setInt(3, tenantId);
stmt.setString(4, String.valueOf(startDate));
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
devices.add(DeviceManagementDAOUtil.loadDeviceBilling(rs));
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of NonRemovedPriorYears device billing ", e);
}
return devices;
}
@Override
public List<Device> getRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
Connection conn;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "select d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " +
"NAME, " +
"DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, " +
"STATUS, " +
"TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, ?) AS DAYS_USED " +
"from DM_DEVICE d, DM_ENROLMENT e " +
"where " +
"e.TENANT_ID=? and d.ID=e.DEVICE_ID and " +
"STATUS ='REMOVED' and " +
"(" +
"DATE_OF_ENROLMENT < ? " +
") and " +
"(" +
"DATE_OF_LAST_UPDATE >= ? " +
")";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, String.valueOf(startDate));
stmt.setInt(2, tenantId);
stmt.setString(3, String.valueOf(startDate));
stmt.setString(4, String.valueOf(startDate));
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
devices.add(DeviceManagementDAOUtil.loadDeviceBilling(rs));
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of RemovedPriorYears device billing ", e);
}
return devices;
}
//Return only not removed id list
@Override
public List<Device> getDeviceListWithoutPagination(int tenantId)
@ -197,10 +359,26 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT DM_DEVICE.ID AS DEVICE_ID, DEVICE_IDENTIFICATION, DESCRIPTION, DM_DEVICE.NAME AS DEVICE_NAME, DM_DEVICE_TYPE.NAME AS DEVICE_TYPE,\n" +
"DM_ENROLMENT.ID AS ENROLMENT_ID, DATE_OF_ENROLMENT,OWNER, OWNERSHIP,IS_TRANSFERRED, STATUS, DATE_OF_LAST_UPDATE, LAST_BILLED_DATE,\n" +
"TIMESTAMPDIFF(DAY, DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED FROM DM_DEVICE JOIN DM_ENROLMENT\n" +
"ON (DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID) JOIN DM_DEVICE_TYPE ON (DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID) WHERE DM_ENROLMENT.TENANT_ID=?";
String sql = "SELECT " +
"DM_DEVICE.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " +
"DM_DEVICE.NAME AS DEVICE_NAME, " +
"DM_DEVICE_TYPE.NAME AS DEVICE_TYPE, " +
"DM_ENROLMENT.ID AS ENROLMENT_ID, " +
"DATE_OF_ENROLMENT, " +
"OWNER, " +
"OWNERSHIP, " +
"IS_TRANSFERRED, " +
"STATUS, " +
"DATE_OF_LAST_UPDATE, " +
"TIMESTAMPDIFF(DAY, DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED " +
"FROM " +
"DM_DEVICE " +
"JOIN DM_ENROLMENT ON (DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID) " +
"JOIN DM_DEVICE_TYPE ON (DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID) " +
"WHERE " +
"DM_ENROLMENT.TENANT_ID = ? ";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
ResultSet rs = stmt.executeQuery();

@ -263,6 +263,34 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
// TODO - add Oracle support for below billing method
@Override
public List<Device> getNonRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add Oracle support for below billing method
@Override
public List<Device> getRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add Oracle support for below billing method
@Override
public List<Device> getNonRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add Oracle support for below billing method
@Override
public List<Device> getRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
@Override
public List<Device> getDeviceListWithoutPagination(int tenantId) throws DeviceManagementDAOException {
return null;

@ -179,6 +179,35 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
// TODO - add PostgreSQL support for below billing method
@Override
public List<Device> getNonRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add PostgreSQL support for below billing method
@Override
public List<Device> getRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add PostgreSQL support for below billing method
@Override
public List<Device> getNonRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add PostgreSQL support for below billing method
@Override
public List<Device> getRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
//Return only not removed id list
@Override
public List<Device> getDevicesIds(PaginationRequest request, int tenantId)

@ -189,6 +189,35 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
// TODO - add SQL support for below billing method
@Override
public List<Device> getNonRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add SQL support for below billing method
@Override
public List<Device> getRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add SQL support for below billing method
@Override
public List<Device> getNonRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
// TODO - add SQL support for below billing method
@Override
public List<Device> getRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate)
throws DeviceManagementDAOException {
return null;
}
//Return only not removed id list
@Override
public List<Device> getDevicesIds(PaginationRequest request, int tenantId)

@ -26,6 +26,7 @@ import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.el.lang.ELSupport;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceBilling;
@ -143,14 +144,6 @@ public final class DeviceManagementDAOUtil {
public static EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
String columnName = "LAST_BILLED_DATE";
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
for (int x = 1; x <= columns; x++) {
if (columnName.equals(rsmd.getColumnName(x))) {
enrolmentInfo.setLastBilledDate(rs.getLong("LAST_BILLED_DATE"));
}
}
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
enrolmentInfo.setOwner(rs.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
@ -161,15 +154,12 @@ public final class DeviceManagementDAOUtil {
return enrolmentInfo;
}
/* This is used to set the enrollment data of the billing query */
public static EnrolmentInfo loadEnrolmentBilling(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
enrolmentInfo.setLastBilledDate(rs.getLong("LAST_BILLED_DATE"));
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
if (EnrolmentInfo.Status.valueOf(rs.getString("STATUS")).equals("REMOVED")) {
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
}
return enrolmentInfo;
}
@ -211,6 +201,24 @@ public final class DeviceManagementDAOUtil {
return enrolmentInfos.get(EnrolmentInfo.Status.SUSPENDED);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.BLOCKED)) {
return enrolmentInfos.get(EnrolmentInfo.Status.BLOCKED);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.CONFIGURED)) {
return enrolmentInfos.get(EnrolmentInfo.Status.CONFIGURED);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.READY_TO_CONNECT)) {
return enrolmentInfos.get(EnrolmentInfo.Status.READY_TO_CONNECT);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.RETURN_PENDING)) {
return enrolmentInfos.get(EnrolmentInfo.Status.RETURN_PENDING);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.RETURNED)) {
return enrolmentInfos.get(EnrolmentInfo.Status.RETURNED);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.DEFECTIVE)) {
return enrolmentInfos.get(EnrolmentInfo.Status.DEFECTIVE);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.WARRANTY_PENDING)) {
return enrolmentInfos.get(EnrolmentInfo.Status.WARRANTY_PENDING);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.WARRANTY_SENT)) {
return enrolmentInfos.get(EnrolmentInfo.Status.WARRANTY_SENT);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.WARRANTY_REPLACED)) {
return enrolmentInfos.get(EnrolmentInfo.Status.WARRANTY_REPLACED);
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.ASSIGNED)) {
return enrolmentInfos.get(EnrolmentInfo.Status.ASSIGNED);
}
return enrolmentInfo;
}
@ -226,29 +234,23 @@ public final class DeviceManagementDAOUtil {
return device;
}
public static Device loadDeviceIds(ResultSet rs) throws SQLException {
/* This is used to set the device data of the billing query */
public static Device loadDeviceBilling(ResultSet rs) throws SQLException {
Device device = new Device();
device.setId(rs.getInt("DEVICE_ID"));
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
device.setEnrolmentInfo(loadEnrolmentStatus(rs));
device.setName(rs.getString("DESCRIPTION"));
device.setDescription(rs.getString("NAME"));
device.setEnrolmentInfo(loadEnrolmentBilling(rs));
return device;
}
public static DeviceBilling loadDeviceBilling(ResultSet rs) throws SQLException {
DeviceBilling device = new DeviceBilling();
device.setId(rs.getInt("ID"));
device.setName(rs.getString("DEVICE_NAME"));
device.setDescription(rs.getString("DESCRIPTION"));
public static Device loadDeviceIds(ResultSet rs) throws SQLException {
Device device = new Device();
device.setId(rs.getInt("DEVICE_ID"));
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
device.setDaysUsed((int) rs.getLong("DAYS_SINCE_ENROLLED"));
device.setEnrolmentInfo(loadEnrolmentBilling(rs));
device.setEnrolmentInfo(loadEnrolmentStatus(rs));
return device;
// if (removedDevices) {
// device.setDaysUsed((int) rs.getLong("DAYS_USED"));
// } else {
// device.setDaysSinceEnrolled((int) rs.getLong("DAYS_SINCE_ENROLLED"));
// }
}
public static DeviceMonitoringData loadDevice(ResultSet rs, String deviceTypeName) throws SQLException {

@ -35,6 +35,7 @@ import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentType;
import org.wso2.carbon.device.mgt.common.metadata.mgt.Metadata;
import org.wso2.carbon.device.mgt.common.otp.mgt.OTPEmailTypes;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.DownloadURLDetails;
import org.wso2.carbon.device.mgt.common.spi.OTPManagementService;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
@ -78,18 +79,19 @@ public class OTPManagementServiceImpl implements OTPManagementService {
}
@Override
public void sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException {
public String sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException {
Tenant tenant = validateTenantCreatingDetails(otpWrapper);
OneTimePinDTO oneTimePinDTO = createOneTimePin(otpWrapper.getEmail(), otpWrapper.getEmailType(),
otpWrapper.getUsername(), tenant, -1234);
try {
ConnectionManagerUtil.beginDBTransaction();
this.otpManagementDAO.addOTPData(Collections.singletonList(oneTimePinDTO));
Properties props = new Properties();
props.setProperty("first-name", tenant.getAdminFirstName());
props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
sendMail(props, tenant.getEmail(), DeviceManagementConstants.EmailAttributes.USER_VERIFY_TEMPLATE);
// Properties props = new Properties();
// props.setProperty("first-name", tenant.getAdminFirstName());
// props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
// sendMail(props, tenant.getEmail(), DeviceManagementConstants.EmailAttributes.USER_VERIFY_TEMPLATE);
ConnectionManagerUtil.commitDBTransaction();
return oneTimePinDTO.getOtpToken();
} catch (TransactionManagementException e) {
String msg = "Error occurred while disabling AutoCommit.";
log.error(msg, e);
@ -108,6 +110,31 @@ public class OTPManagementServiceImpl implements OTPManagementService {
}
}
@Override
public void shareProductDownloadUrl(DownloadURLDetails downloadURLDetails) throws OTPManagementException {
if (StringUtils.isBlank(downloadURLDetails.getURL())) {
String msg = "Couldn't find the download URL with the request.";
log.error(msg);
throw new OTPManagementException(msg);
}
if (StringUtils.isBlank(downloadURLDetails.getFirstName())) {
String msg = "Couldn't find the First Name with the request.";
log.error(msg);
throw new OTPManagementException(msg);
}
if (StringUtils.isBlank(downloadURLDetails.getEmail())) {
String msg = "Couldn't find the e-mail address with the request.";
log.error(msg);
throw new OTPManagementException(msg);
}
Properties props = new Properties();
props.setProperty("first-name", downloadURLDetails.getFirstName());
props.setProperty("download-url", downloadURLDetails.getURL());
sendMail(props, downloadURLDetails.getEmail(),
DeviceManagementConstants.EmailAttributes.PRODUCT_DOWNLOAD_LINK_SHARING_TEMPLATE);
}
@Override
public OneTimePinDTO isValidOTP(String oneTimeToken) throws OTPManagementException, BadRequestException {
if (StringUtils.isBlank(oneTimeToken)){

@ -208,16 +208,6 @@ public interface DeviceManagementProviderService {
*/
PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException;
/**
* Method to retrieve all the devices with pagination support.
*
* @param request PaginationRequest object holding the data for pagination
* @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching billing of
* devices.
*/
PaginationResult getAllDevicesBillings(PaginationRequest request, int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException;
/**
* Method to retrieve all the devices with pagination support.
*
@ -225,9 +215,7 @@ public interface DeviceManagementProviderService {
* @throws DeviceManagementException If some unusual behaviour is observed while fetching billing of
* devices.
*/
PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException;
PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException;
/**
* Method to retrieve all the devices with pagination support.

@ -69,8 +69,10 @@ import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.StartupOperationConfig;
import org.wso2.carbon.device.mgt.common.BillingResponse;
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.app.mgt.MobileAppTypes;
import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfigurationException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
@ -119,12 +121,12 @@ import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformVersion;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey;
import org.wso2.carbon.device.mgt.core.cache.impl.BillingCacheManagerImpl;
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl;
import org.wso2.carbon.device.mgt.core.common.Constants;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO;
import org.wso2.carbon.device.mgt.core.dao.BillingDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -142,6 +144,7 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataDAO;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
@ -166,8 +169,7 @@ import java.io.StringWriter;
import java.lang.reflect.Type;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
@ -195,7 +197,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private final EnrollmentDAO enrollmentDAO;
private final ApplicationDAO applicationDAO;
private MetadataDAO metadataDAO;
private final BillingDAO billingDAO;
private final DeviceStatusDAO deviceStatusDAO;
public DeviceManagementProviderServiceImpl() {
@ -205,7 +206,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
this.billingDAO = DeviceManagementDAOFactory.getBillingDAO();
this.deviceStatusDAO = DeviceManagementDAOFactory.getDeviceStatusDAO();
/* Registering a listener to retrieve events when some device management service plugin is installed after
@ -529,6 +529,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection();
}
extractDeviceLocationToUpdate(device);
//enroll Traccar device
if (HttpReportingUtil.isTrackerEnabled()) {
try {
int tenantId = this.getTenantId();
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService().modifyDevice(device, tenantId);
} catch (ExecutionException e) {
log.error("ExecutionException : " + e);
//throw new RuntimeException(e);
//Exception was not thrown due to being conflicted with non-traccar features
} catch (InterruptedException e) {
log.error("InterruptedException : " + e);
//throw new RuntimeException(e);
//Exception was not thrown due to being conflicted with non-traccar features
}
} else {
log.info("Traccar is disabled");
}
//enroll Traccar device
return status;
}
@ -1005,236 +1023,232 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return this.getAllDevices(request, true);
}
public PaginationResult calculateCost(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill, List<Device> allDevices) throws DeviceManagementException {
/**
* Calculate cost of a tenants Device List.
* Once the full device list of a tenant is sent here devices are looped for cost calculation
* Cost per tenant is retrieved from the Meta Table
* When looping the devices the most recent device status of that device is retrieved from status table
* If device is enrolled prior to start date now in removed state --> time is calculated from - startDate to last updated time
* If device is enrolled prior to start date now in not removed --> time is calculated from - startDate to endDate
* If device is enrolled after the start date now in removed state --> time is calculated from - dateOfEnrollment to last updated time
* If device is enrolled after the start date now in not removed --> time is calculated from - dateOfEnrollment to endDate
* Once time is calculated cost is set for each device
*
* @param tenantDomain Tenant domain cost id calculated for.
* @param startDate start date of usage period.
* @param endDate end date of usage period.
* @param allDevices device list of the tenant for the selected time-period.
* @return Whether status is changed or not
* @throws DeviceManagementException on errors while trying to calculate Cost
*/
public BillingResponse calculateCost(String tenantDomain, Timestamp startDate, Timestamp endDate, List<Device> allDevices) throws MetadataManagementDAOException, DeviceManagementException {
PaginationResult paginationResult = new PaginationResult();
BillingResponse billingResponse = new BillingResponse();
List<Device> deviceStatusNotAvailable = new ArrayList<>();
double totalCost = 0.0;
boolean allDevicesBilledDateIsValid = true;
ArrayList<String> lastBilledDatesList = new ArrayList<>();
List<Billing> invalidDevices = new ArrayList<>();
List<Device> removeBillingPeriodInvalidDevices = new ArrayList<>() ;
List<Device> removeStatusUpdateInvalidDevices = new ArrayList<>() ;
try {
MetadataManagementDAOFactory.beginTransaction();
MetadataManagementDAOFactory.openConnection();
Metadata metadata = metadataDAO.getMetadata(MultitenantConstants.SUPER_TENANT_ID, DeviceManagementConstants.META_KEY);
Gson g = new Gson();
Collection<Cost> costData = null;
Type collectionType = new TypeToken<Collection<Cost>>() {}.getType();
Type collectionType = new TypeToken<Collection<Cost>>() {
}.getType();
if (metadata != null) {
costData = g.fromJson(metadata.getMetaValue(), collectionType);
for (Cost tenantCost : costData) {
if (tenantCost.getTenantDomain().equals(tenantDomain)) {
for (Device device : allDevices) {
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, null, true));
long dateDiff = 0;
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true));
List<DeviceStatus> deviceStatus = device.getDeviceStatusInfo();
boolean firstDateBilled = false;
boolean deviceStatusIsValid = false;
List<Billing> deviceBilling = billingDAO.getBilling(device.getId(), startDate, endDate);
boolean billDateIsInvalid = false;
if (deviceBilling.isEmpty()) {
billDateIsInvalid = false;
}
if (generateBill) {
for (Billing bill : deviceBilling) {
if ((bill.getBillingStart() <= startDate.getTime() && startDate.getTime() <= bill.getBillingEnd()) ||
(bill.getBillingStart() <= endDate.getTime() && endDate.getTime() <= bill.getBillingEnd())) {
billDateIsInvalid = true;
invalidDevices.add(bill);
if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) {
if (!deviceStatus.isEmpty() && deviceStatus.get(0).getStatus().equals("REMOVED")) {
if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) {
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - startDate.getTime();
}
} else if (!deviceStatus.isEmpty() && !deviceStatus.get(0).getStatus().equals("REMOVED")) {
dateDiff = endDate.getTime() - startDate.getTime();
}
}
if (!billDateIsInvalid) {
for (int i = 0; i < deviceStatus.size(); i++) {
if (DeviceManagementConstants.ACTIVE_STATUS.equals(deviceStatus.get(i).getStatus().toString())) {
if (deviceStatus.size() > i + 1) {
if (!firstDateBilled) {
firstDateBilled = true;
if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
} else {
if (deviceStatus.get(i + 1).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else {
if (deviceStatus.get(i).getUpdateTime().getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
}
} else {
// If only one status row is retrieved this block is executed
if (!firstDateBilled) {
firstDateBilled = true;
// Is executed if there is no lastBilled date and if the updates time is before the end date
if (device.getEnrolmentInfo().getLastBilledDate() == 0) {
if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
} else {
if (endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
} else {
if (device.getEnrolmentInfo().getLastBilledDate() <= deviceStatus.get(i).getUpdateTime().getTime()
&& endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - deviceStatus.get(i).getUpdateTime().getTime());
}
if (device.getEnrolmentInfo().getLastBilledDate() >= deviceStatus.get(i).getUpdateTime().getTime()
&& endDate.getTime() >= device.getEnrolmentInfo().getLastBilledDate()) {
deviceStatusIsValid = true;
dateDiff = dateDiff + (endDate.getTime() - device.getEnrolmentInfo().getLastBilledDate());
}
}
}
}
}
long dateInDays = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);
double cost = (tenantCost.getCost() / 365) * dateInDays;
totalCost = cost + totalCost;
device.setCost(Math.round(cost * 100.0) / 100.0);
device.setDaysUsed((int) dateInDays);
if (generateBill) {
enrollmentDAO.updateEnrollmentLastBilledDate(device.getEnrolmentInfo(), endDate, tenantId);
billingDAO.addBilling(device.getId(), tenantId, startDate, endDate);
DeviceManagementDAOFactory.commitTransaction();
}
} else {
for (int i = 0; i < deviceStatus.size(); i++) {
if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()
&& startDate.getTime() <= deviceStatus.get(i).getUpdateTime().getTime()) {
deviceStatusIsValid = true;
}
}
if (device.getEnrolmentInfo().getLastBilledDate() != 0) {
Date date = new Date(device.getEnrolmentInfo().getLastBilledDate());
Format format = new SimpleDateFormat("yyyy MM dd");
for (String lastBillDate : lastBilledDatesList) {
if (!lastBillDate.equals(format.format(date))) {
lastBilledDatesList.add(format.format(date));
}
if (!deviceStatus.isEmpty() && deviceStatus.get(0).getStatus().equals("REMOVED")) {
if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) {
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
}
} else if (!deviceStatus.isEmpty() && !deviceStatus.get(0).getStatus().equals("REMOVED")) {
dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
}
removeBillingPeriodInvalidDevices.add(device);
}
if (!deviceStatusIsValid) {
removeStatusUpdateInvalidDevices.add(device);
long dateInDays = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);
double cost = (tenantCost.getCost() / 365) * dateInDays;
totalCost += cost;
device.setCost(Math.round(cost * 100.0) / 100.0);
long totalDays = dateInDays + device.getDaysUsed();
device.setDaysUsed((int) totalDays);
if (deviceStatus.isEmpty()) {
deviceStatusNotAvailable.add(device);
}
}
}
}
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device list pertaining to the current tenant";
} catch (DeviceManagementException e) {
String msg = "Error occurred calculating cost of devices";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getAllDevices";
} catch (SQLException e) {
String msg = "Error when retrieving data";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (MetadataManagementDAOException e) {
String msg = "Error when retrieving metadata of billing feature";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
MetadataManagementDAOFactory.closeConnection();
}
if(!removeBillingPeriodInvalidDevices.isEmpty() && removeBillingPeriodInvalidDevices.size() == allDevices.size()) {
allDevicesBilledDateIsValid = false;
paginationResult.setMessage("Invalid bill period.");
if (!deviceStatusNotAvailable.isEmpty()) {
allDevices.removeAll(deviceStatusNotAvailable);
}
if(!removeStatusUpdateInvalidDevices.isEmpty() && removeStatusUpdateInvalidDevices.size() == allDevices.size()) {
allDevicesBilledDateIsValid = false;
if (paginationResult.getMessage() != null){
paginationResult.setMessage(paginationResult.getMessage() + " and no device updates within entered bill period.");
} else {
paginationResult.setMessage("Devices have not been updated within the given period or entered end date comes before the " +
"last billed date.");
}
}
allDevices.removeAll(removeBillingPeriodInvalidDevices);
allDevices.removeAll(removeStatusUpdateInvalidDevices);
Calendar calStart = Calendar.getInstance();
Calendar calEnd = Calendar.getInstance();
calStart.setTimeInMillis(startDate.getTime());
calEnd.setTimeInMillis(endDate.getTime());
paginationResult.setBilledDateIsValid(allDevicesBilledDateIsValid);
paginationResult.setData(allDevices);
paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0);
return paginationResult;
billingResponse.setDevice(allDevices);
billingResponse.setYear(String.valueOf(calStart.get(Calendar.YEAR)));
billingResponse.setStartDate(startDate.toString());
billingResponse.setEndDate(endDate.toString());
billingResponse.setBillPeriod(calStart.get(Calendar.YEAR) + " - " + calEnd.get(Calendar.YEAR));
billingResponse.setTotalCostPerYear(Math.round(totalCost * 100.0) / 100.0);
billingResponse.setDeviceCount(allDevices.size());
return billingResponse;
}
@Override
public PaginationResult getAllDevicesBillings(PaginationRequest request, int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException {
if (request == null) {
String msg = "Received incomplete pagination request for method getAllDeviceBillings";
log.error(msg);
throw new DeviceManagementException(msg);
}
public PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException {
DeviceManagerUtil.validateDeviceListPageSize(request);
PaginationResult paginationResult = new PaginationResult();
List<Device> allDevices;
int count = 0;
List<Device> allDevices = new ArrayList<>();
List<BillingResponse> billingResponseList = new ArrayList<>();
double totalCost = 0.0;
int deviceCount = 0;
Timestamp initialStartDate = startDate;
boolean remainingDaysConsidered = false;
try {
DeviceManagementDAOFactory.beginTransaction();
allDevices = deviceDAO.getDevices(request, tenantId);
count = deviceDAO.getDeviceCount(request, tenantId);
paginationResult = calculateCost(tenantId, tenantDomain, startDate, endDate, generateBill, allDevices);
DeviceManagementDAOFactory.openConnection();
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device bill list pertaining to the current tenant";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getAllDevicesBillings";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult;
}
// TODO Do the check of cache enabling here
paginationResult = BillingCacheManagerImpl.getInstance().getBillingFromCache(tenantDomain, startDate, endDate);
if (paginationResult == null) {
paginationResult = new PaginationResult();
long difference_In_Time = endDate.getTime() - startDate.getTime();
long difference_In_Years = (difference_In_Time / (1000L * 60 * 60 * 24 * 365));
long difference_In_Days = (difference_In_Time / (1000 * 60 * 60 * 24)) % 365;
for (int i = 1; i <= difference_In_Years; i++) {
List<Device> allDevicesPerYear = new ArrayList<>();
LocalDateTime oneYearAfterStart = startDate.toLocalDateTime().plusYears(1);
Timestamp newStartDate;
Timestamp newEndDate;
if (i == difference_In_Years) {
if (difference_In_Days == 0 || Timestamp.valueOf(oneYearAfterStart).getTime() >= endDate.getTime()) {
remainingDaysConsidered = true;
oneYearAfterStart = startDate.toLocalDateTime();
newEndDate = endDate;
} else if (Timestamp.valueOf(oneYearAfterStart).getTime() >= endDate.getTime()) {
newEndDate = Timestamp.valueOf(oneYearAfterStart);
} else {
oneYearAfterStart = startDate.toLocalDateTime().plusYears(1);
newEndDate = Timestamp.valueOf(oneYearAfterStart);
}
} else {
oneYearAfterStart = startDate.toLocalDateTime().plusYears(1);
newEndDate = Timestamp.valueOf(oneYearAfterStart);
}
@Override
public PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate, boolean generateBill) throws DeviceManagementException {
PaginationResult paginationResult = new PaginationResult();
List<Device> allDevices;
try {
DeviceManagementDAOFactory.beginTransaction();
allDevices = deviceDAO.getDeviceListWithoutPagination(tenantId);
paginationResult = calculateCost(tenantId, tenantDomain, startDate, endDate, generateBill, allDevices);
newStartDate = startDate;
// The query returns devices which are enrolled in this year now in not removed state
allDevicesPerYear.addAll(deviceDAO.getNonRemovedYearlyDeviceList(tenantId, newStartDate, newEndDate));
// The query returns devices which are enrolled in this year now in removed state
allDevicesPerYear.addAll(deviceDAO.getRemovedYearlyDeviceList(tenantId, newStartDate, newEndDate));
// The query returns devices which are enrolled prior this year now in not removed state
allDevicesPerYear.addAll(deviceDAO.getNonRemovedPriorYearsDeviceList(tenantId, newStartDate, newEndDate));
// The query returns devices which are enrolled prior this year now in removed state
allDevicesPerYear.addAll(deviceDAO.getRemovedPriorYearsDeviceList(tenantId, newStartDate, newEndDate));
BillingResponse billingResponse = calculateCost(tenantDomain, newStartDate, newEndDate, allDevicesPerYear);
billingResponseList.add(billingResponse);
allDevices.addAll(billingResponse.getDevice());
totalCost = totalCost + billingResponse.getTotalCostPerYear();
deviceCount = deviceCount + billingResponse.getDeviceCount();
LocalDateTime nextStartDate = oneYearAfterStart.plusDays(1);
startDate = Timestamp.valueOf(nextStartDate);
}
if (difference_In_Days != 0 && !remainingDaysConsidered) {
List<Device> allDevicesPerRemainingDays = new ArrayList<>();
// The query returns devices which are enrolled in this year now in not removed state
allDevicesPerRemainingDays.addAll(deviceDAO.getNonRemovedYearlyDeviceList(tenantId, startDate, endDate));
// The query returns devices which are enrolled in this year now in removed state
allDevicesPerRemainingDays.addAll(deviceDAO.getRemovedYearlyDeviceList(tenantId, startDate, endDate));
// The query returns devices which are enrolled prior this year now in not removed state
allDevicesPerRemainingDays.addAll(deviceDAO.getNonRemovedPriorYearsDeviceList(tenantId, startDate, endDate));
// The query returns devices which are enrolled prior this year now in removed state
allDevicesPerRemainingDays.addAll(deviceDAO.getRemovedPriorYearsDeviceList(tenantId, startDate, endDate));
BillingResponse billingResponse = calculateCost(tenantDomain, startDate, endDate, allDevicesPerRemainingDays);
billingResponseList.add(billingResponse);
allDevices.addAll(billingResponse.getDevice());
totalCost = totalCost + billingResponse.getTotalCostPerYear();
deviceCount = deviceCount + billingResponse.getDeviceCount();
}
Calendar calStart = Calendar.getInstance();
Calendar calEnd = Calendar.getInstance();
calStart.setTimeInMillis(initialStartDate.getTime());
calEnd.setTimeInMillis(endDate.getTime());
BillingResponse billingResponse = new BillingResponse("all", Math.round(totalCost * 100.0) / 100.0, allDevices, calStart.get(Calendar.YEAR) + " - " + calEnd.get(Calendar.YEAR), initialStartDate.toString(), endDate.toString(), allDevices.size());
billingResponseList.add(billingResponse);
paginationResult.setData(billingResponseList);
paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0);
paginationResult.setTotalDeviceCount(deviceCount);
BillingCacheManagerImpl.getInstance().addBillingToCache(paginationResult, tenantDomain, initialStartDate, endDate);
return paginationResult;
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device bill list without pagination pertaining to the current tenant";
String msg = "Error occurred while retrieving device bill list related to the current tenant";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in createBillingFile";
} catch (MetadataManagementDAOException e) {
String msg = "Error when retrieving metadata of billing feature";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return paginationResult;
}

@ -47,6 +47,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
@ -54,12 +55,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException;
import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GroupDAO;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.*;
import org.wso2.carbon.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor;
import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
@ -76,6 +72,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
@ -144,12 +141,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (HttpReportingUtil.isTrackerEnabled()){
existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId);
int groupId = existingGroup.getGroupId();
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.addGroup(deviceGroup, groupId, tenantId);
try {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.addGroup(deviceGroup, groupId, tenantId);
} catch (TrackerAlreadyExistException e) {
throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName());
}
} else {
throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName());
}
// add a group if not exist in traccar starts
throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName());
}
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
@ -194,8 +194,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.beginTransaction();
DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId);
if (existingGroup != null) {
boolean existingGroupName = this.groupDAO.getGroup(deviceGroup.getName(), tenantId) != null;
if (existingGroupName) {
DeviceGroup existingGroupByName = this.groupDAO.getGroup(deviceGroup.getName(), tenantId);
if (existingGroupByName != null && existingGroupByName.getGroupId() != groupId) {
throw new GroupAlreadyExistException("Group already exists with name '" + deviceGroup.getName() + "'.");
}
List<DeviceGroup> groupsToUpdate = new ArrayList<>();
@ -299,8 +299,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
//procees to delete a group from traccar starts
if (HttpReportingUtil.isTrackerEnabled()) {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.deleteGroup(groupId, tenantId);
try {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.deleteGroup(groupId, tenantId);
} catch (TrackerManagementDAOException e) {
String msg = "Failed while deleting traccar group " + groupId;
log.error(msg, e);
} catch (ExecutionException | InterruptedException e) {
String msg = "Failed while deleting traccar group "+groupId+" due to concurrent execution failure";
log.error(msg, e);
}
}
//procees to delete a group from traccar ends

@ -50,6 +50,14 @@ public interface DeviceAPIClientService {
*/
void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException;
/**
* Updates device Traccar configuration records (like device name)
*
* @params device to be modifies
* @throws TrackerManagementDAOException errors thrown while modifing a traccar device
*/
void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException;
/**
* Delete a device Traccar configuration records
*

@ -469,6 +469,54 @@ public class TraccarClientFactory {
}
}
/**
* Modify the Device
*
* @param traccarDevice with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId
* Model, Contact, Category, fenceIds
* @throws TrackerManagementDAOException Failed while add Traccar Device the operation
*/
public void modifyDevice(TraccarDevice traccarDevice, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException {
TrackerDeviceInfo trackerDeviceInfo = null;
try {
TrackerManagementDAOFactory.openConnection();
trackerDeviceInfo = trackerDAO.getTrackerDevice(traccarDevice.getId(), tenantId);
} catch (TrackerManagementDAOException e) {
String msg = "Error occurred while mapping with deviceId .";
log.error(msg, e);
throw new TrackerManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred establishing the DB connection .";
log.error(msg, e);
throw new TrackerManagementDAOException(msg, e);
} finally {
TrackerManagementDAOFactory.closeConnection();
}
if (trackerDeviceInfo != null) {
log.info("Preparing to rename device (" + traccarDevice.getId() + " to taccar server");
String url = defaultPort + "/api/devices/" + trackerDeviceInfo.getTraccarDeviceId();
JSONObject payload = TraccarUtil.TraccarDevicePayload(traccarDevice, trackerDeviceInfo.getTraccarDeviceId());
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get();
log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar.");
if (res.isDone() && result.charAt(0) == '{') {
log.info("Succesfully renamed Traccar Device - " + traccarDevice.getId() + "in server");
} else {
log.info("Failed to rename Traccar Device" + traccarDevice.getId() + "in server");
}
} else {
// forward to add device
try {
addDevice(traccarDevice, tenantId);
} catch (TrackerAlreadyExistException e) {
String msg = "The device already exist";
log.error(msg, e);
}
}
}
/**
* Add Device GPS Location operation.
*
@ -600,7 +648,7 @@ public class TraccarClientFactory {
TrackerManagementDAOFactory.openConnection();
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
if (trackerGroupInfo != null) {
String msg = "The group already exit";
String msg = "The group already exists in Traccar.";
log.error(msg);
throw new TrackerAlreadyExistException(msg);
}
@ -626,7 +674,9 @@ public class TraccarClientFactory {
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get();
log.info("Group " + trackerGroupInfo.getGroupId() + " has been added to Traccar.");
if (null != trackerGroupInfo) {
log.info("Group " + trackerGroupInfo.getGroupId() + " has been added to Traccar.");
}
if (res.isDone() && result.charAt(0) == '{') {
JSONObject obj = new JSONObject(result);
if (obj.has("id")) {

@ -555,7 +555,7 @@ public class TraccarClientImpl implements TraccarClient {
TrackerManagementDAOFactory.openConnection();
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
if (trackerGroupInfo != null) {
String msg = "The group already exit";
String msg = "The group already exists in Traccar.";
log.error(msg);
throw new TrackerAlreadyExistException(msg);
}

@ -62,6 +62,17 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
}
}
@Override
public void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException {
TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getDeviceIdentifier(), device.getName());
try {
client.modifyDevice(traccarDevice, tenantId);
} catch (TrackerManagementDAOException e) {
String msg = "Error occurred while mapping with deviceId";
log.error(msg, e);
}
}
@Override
public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException {
TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(),

@ -55,6 +55,12 @@ public class TraccarDevice {
this.category =category;
}
public TraccarDevice(int id, String uniqueId, String deviceName) {
this.id = id;
this.uniqueId = uniqueId;
this.deviceName = deviceName;
}
public TraccarDevice(){ }
public int getId() { return id; }

@ -66,4 +66,12 @@ public class TraccarUtil {
payload.put("attributes", new JSONObject());
return payload;
}
public static JSONObject TraccarDevicePayload(TraccarDevice deviceInfo, int id) {
JSONObject payload = new JSONObject();
payload.put("id", id);
payload.put("name", deviceInfo.getDeviceName());
payload.put("uniqueId", deviceInfo.getUniqueId());
return payload;
}
}

@ -58,6 +58,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.EnrollmentConfiguration;
@ -76,6 +77,7 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagement
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.cache.BillingCacheKey;
import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey;
import org.wso2.carbon.device.mgt.core.cache.GeoCacheKey;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
@ -137,6 +139,7 @@ public final class DeviceManagerUtil {
public static final String GENERAL_CONFIG_RESOURCE_PATH = "general";
private static boolean isDeviceCacheInitialized = false;
private static boolean isBillingCacheInitialized = false;
private static boolean isAPIResourcePermissionCacheInitialized = false;
private static boolean isGeoFenceCacheInitialized = false;
@ -652,6 +655,47 @@ public final class DeviceManagerUtil {
}
}
/**
* Enable Billing caching according to the configurations provided by cdm-config.xml
*/
public static void initializeBillingCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
int billingCacheExpiry = config.getBillingCacheConfiguration().getExpiryTime();
long billingCacheCapacity = config.getBillingCacheConfiguration().getCapacity();
CacheManager manager = getCacheManager();
if (config.getBillingCacheConfiguration().isEnabled()) {
if(!isBillingCacheInitialized) {
isBillingCacheInitialized = true;
if (manager != null) {
if (billingCacheExpiry > 0) {
manager.<BillingCacheKey, PaginationResult>createCacheBuilder(DeviceManagementConstants.BILLING_CACHE).
setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS,
billingCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration.
Duration(TimeUnit.SECONDS, billingCacheExpiry)).setStoreByValue(true).build();
if(billingCacheCapacity > 0 ) {
((CacheImpl) manager.<BillingCacheKey, PaginationResult>getCache(DeviceManagementConstants.BILLING_CACHE)).
setCapacity(billingCacheCapacity);
}
} else {
manager.<BillingCacheKey, PaginationResult>getCache(DeviceManagementConstants.BILLING_CACHE);
}
} else {
if (billingCacheExpiry > 0) {
Caching.getCacheManager().
<BillingCacheKey, PaginationResult>createCacheBuilder(DeviceManagementConstants.BILLING_CACHE).
setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS,
billingCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration.
Duration(TimeUnit.SECONDS, billingCacheExpiry)).setStoreByValue(true).build();
((CacheImpl)(manager.<BillingCacheKey, PaginationResult>getCache(DeviceManagementConstants.BILLING_CACHE))).
setCapacity(billingCacheCapacity);
} else {
Caching.getCacheManager().<BillingCacheKey, PaginationResult>getCache(DeviceManagementConstants.BILLING_CACHE);
}
}
}
}
}
/**
* Enable Geofence caching according to the configurations proviced by cdm-config.xml
*/
@ -711,6 +755,28 @@ public final class DeviceManagerUtil {
return deviceCache;
}
/**
* Get billing cache object
* @return {@link Cache<BillingCacheKey, PaginationResult>}
*/
public static Cache<BillingCacheKey, PaginationResult> getBillingCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
CacheManager manager = getCacheManager();
Cache<BillingCacheKey, PaginationResult> billingCache = null;
if (config.getBillingCacheConfiguration().isEnabled()) {
if(!isBillingCacheInitialized) {
initializeBillingCache();
}
if (manager != null) {
billingCache = manager.getCache(DeviceManagementConstants.BILLING_CACHE);
} else {
billingCache = Caching.getCacheManager(DeviceManagementConstants.DM_CACHE_MANAGER)
.getCache(DeviceManagementConstants.BILLING_CACHE);
}
}
return billingCache;
}
/**
* Get geofence cache object
* @return {@link Cache<GeoCacheKey, GeofenceData>}

@ -26,6 +26,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import java.io.IOException;
@ -39,6 +40,7 @@ public class HttpReportingUtil {
private static final String TRACKER_SERVER_URI = "trackerServer";
private static final String TRACKER_PASSWORD = "trackerPassword";
private static final String TRACKER_USER = "trackerUsername";
private static final String TRACKER_CONFIG = "locationPublishing";
public static String getReportingHost() {
return System.getProperty(DeviceManagementConstants.Report.REPORTING_EVENT_HOST);
@ -63,47 +65,49 @@ public class HttpReportingUtil {
public static boolean isPublishingEnabledForTenant() {
Object configuration = DeviceManagerUtil.getConfiguration(IS_EVENT_PUBLISHING_ENABLED);
if (configuration != null) {
return Boolean.valueOf(configuration.toString());
return Boolean.parseBoolean(configuration.toString());
}
return false;
}
public static boolean isLocationPublishing() {
Object configuration = DeviceManagerUtil.getConfiguration(IS_LOCATION_PUBLISHING_ENABLED);
if (configuration != null) {
return Boolean.valueOf(configuration.toString());
}
return false;
return getTrackerBooleanValues(IS_LOCATION_PUBLISHING_ENABLED);
}
public static boolean isTrackerEnabled() {
Object configuration = DeviceManagerUtil.getConfiguration(IS_TRACKER_ENABLED);
if (configuration != null) {
return Boolean.valueOf(configuration.toString());
}
return false;
return getTrackerBooleanValues(IS_TRACKER_ENABLED);
}
public static String trackerServer() {
Object configuration = DeviceManagerUtil.getConfiguration(TRACKER_SERVER_URI);
if (configuration != null) {
return configuration.toString();
}
return null;
return getTrackerStringValues(TRACKER_SERVER_URI);
}
public static String trackerPassword() {
Object configuration = DeviceManagerUtil.getConfiguration(TRACKER_PASSWORD);
return getTrackerStringValues(TRACKER_PASSWORD);
}
public static String trackerUser() {
return getTrackerStringValues(TRACKER_USER);
}
public static boolean getTrackerBooleanValues(String trackerConfigKey) {
Object configuration = DeviceManagerUtil.getConfiguration(TRACKER_CONFIG);
if (configuration != null) {
return configuration.toString();
JSONObject locationConfig = new JSONObject(configuration.toString());
if (locationConfig.has(trackerConfigKey)) {
return Boolean.parseBoolean(locationConfig.get(trackerConfigKey).toString());
}
}
return null;
return false;
}
public static String trackerUser() {
Object configuration = DeviceManagerUtil.getConfiguration(TRACKER_USER);
public static String getTrackerStringValues(String trackerConfigKey) {
Object configuration = DeviceManagerUtil.getConfiguration(TRACKER_CONFIG);
if (configuration != null) {
return configuration.toString();
JSONObject locationConfig = new JSONObject(configuration.toString());
if (locationConfig.has(trackerConfigKey)) {
return locationConfig.get(trackerConfigKey).toString();
}
}
return null;
}

@ -94,7 +94,6 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL,
DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL,
TENANT_ID INT NOT NULL,
LAST_BILLED_DATE BIGINT DEFAULT 0,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
@ -564,16 +563,6 @@ ORDER BY TENANT_ID, DEVICE_ID;
-- END OF DASHBOARD RELATED VIEWS --
CREATE TABLE IF NOT EXISTS DM_BILLING (
INVOICE_ID INTEGER AUTO_INCREMENT NOT NULL,
TENANT_ID INTEGER DEFAULT 0,
DEVICE_ID INTEGER DEFAULT NULL,
BILLING_START TIMESTAMP NOT NULL,
BILLING_END TIMESTAMP NOT NULL,
PRIMARY KEY (INVOICE_ID),
CONSTRAINT fk_DM_BILLING_DM_DEVICE2 FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
-- DM_EXT_GROUP_MAPPING TABLE--
CREATE TABLE IF NOT EXISTS DM_EXT_GROUP_MAPPING (
ID INT NOT NULL AUTO_INCREMENT,

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -23,7 +23,7 @@
<parent>
<artifactId>device-mgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>carbon-devicemgt</artifactId>
<version>5.0.14-SNAPSHOT</version>
<version>5.0.16-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save