mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Merge branch 'master' of https://github.com/wso2/carbon-device-mgt
This commit is contained in:
commit
4e4dc2cc28
@ -17,8 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.wso2.carbon.device.mgt.common;
|
package org.wso2.carbon.device.mgt.common;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@ -78,4 +78,16 @@ public final class DeviceManagementConstants {
|
|||||||
}
|
}
|
||||||
public static final String NOTIFICATION_CONFIG_FILE = "notification-messages.xml";
|
public static final String NOTIFICATION_CONFIG_FILE = "notification-messages.xml";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class DataBaseTypes {
|
||||||
|
private DataBaseTypes() {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
public static final String DB_TYPE_MYSQL = "MySQL";
|
||||||
|
public static final String DB_TYPE_ORACLE = "Oracle";
|
||||||
|
public static final String DB_TYPE_MSSQL = "MSSQL";
|
||||||
|
public static final String DB_TYPE_DB2 = "DB2";
|
||||||
|
public static final String DB_TYPE_H2 = "H2";
|
||||||
|
public static final String DB_TYPE_POSTGRESQL = "PostgreSQL";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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 java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds necessary data to represent a paginated result.
|
||||||
|
*/
|
||||||
|
public class PaginationResult implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1998101711L;
|
||||||
|
private int recordsTotal;
|
||||||
|
private int recordsFiltered;
|
||||||
|
private int draw;
|
||||||
|
private List<?> data;
|
||||||
|
|
||||||
|
public int getRecordsTotal() {
|
||||||
|
return recordsTotal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRecordsFiltered() {
|
||||||
|
return recordsFiltered;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecordsFiltered(int recordsFiltered) {
|
||||||
|
this.recordsFiltered = recordsFiltered;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecordsTotal(int recordsTotal) {
|
||||||
|
this.recordsTotal = recordsTotal;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<?> getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(List<?> data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDraw() {
|
||||||
|
return draw;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDraw(int draw) {
|
||||||
|
this.draw = draw;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -31,6 +31,16 @@ import java.util.List;
|
|||||||
public class PermissionConfiguration {
|
public class PermissionConfiguration {
|
||||||
|
|
||||||
private List<Permission> permissions;
|
private List<Permission> permissions;
|
||||||
|
private String apiVersion;
|
||||||
|
|
||||||
|
public String getApiVersion() {
|
||||||
|
return apiVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement (name = "APIVersion", required = true)
|
||||||
|
public void setApiVersion(String apiVersion) {
|
||||||
|
this.apiVersion = apiVersion;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Permission> getPermissions() {
|
public List<Permission> getPermissions() {
|
||||||
return permissions;
|
return permissions;
|
||||||
|
|||||||
@ -24,9 +24,11 @@ import org.apache.catalina.LifecycleListener;
|
|||||||
import org.apache.catalina.core.StandardContext;
|
import org.apache.catalina.core.StandardContext;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.permission.mgt.Permission;
|
||||||
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException;
|
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException;
|
||||||
import org.wso2.carbon.device.mgt.core.config.permission.PermissionConfiguration;
|
import org.wso2.carbon.device.mgt.core.config.permission.PermissionConfiguration;
|
||||||
import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceImpl;
|
import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceImpl;
|
||||||
|
import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
@ -34,6 +36,7 @@ import javax.xml.bind.JAXBException;
|
|||||||
import javax.xml.bind.Unmarshaller;
|
import javax.xml.bind.Unmarshaller;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This listener class will initiate the permission addition of permissions defined in
|
* This listener class will initiate the permission addition of permissions defined in
|
||||||
@ -50,6 +53,7 @@ public class WebAppDeploymentLifecycleListener implements LifecycleListener {
|
|||||||
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
|
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
|
||||||
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
|
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
|
||||||
ServletContext servletContext = context.getServletContext();
|
ServletContext servletContext = context.getServletContext();
|
||||||
|
String contextPath = context.getServletContext().getContextPath();
|
||||||
try {
|
try {
|
||||||
InputStream permissionStream = servletContext.getResourceAsStream(PERMISSION_CONFIG_PATH);
|
InputStream permissionStream = servletContext.getResourceAsStream(PERMISSION_CONFIG_PATH);
|
||||||
if (permissionStream != null) {
|
if (permissionStream != null) {
|
||||||
@ -58,16 +62,22 @@ public class WebAppDeploymentLifecycleListener implements LifecycleListener {
|
|||||||
Unmarshaller unmarshaller = cdmContext.createUnmarshaller();
|
Unmarshaller unmarshaller = cdmContext.createUnmarshaller();
|
||||||
PermissionConfiguration permissionConfiguration = (PermissionConfiguration)
|
PermissionConfiguration permissionConfiguration = (PermissionConfiguration)
|
||||||
unmarshaller.unmarshal(permissionStream);
|
unmarshaller.unmarshal(permissionStream);
|
||||||
if (permissionConfiguration != null &&
|
List<Permission> permissions = permissionConfiguration.getPermissions();
|
||||||
permissionConfiguration.getPermissions() != null) {
|
String apiVersion = permissionConfiguration.getApiVersion();
|
||||||
PermissionManagerServiceImpl.getInstance().addPermissions(
|
if (permissionConfiguration != null && permissions != null) {
|
||||||
permissionConfiguration.getPermissions());
|
for (Permission permission : permissions) {
|
||||||
|
// update the permission path to absolute permission path
|
||||||
|
permission.setPath(PermissionUtils.getAbsolutePermissionPath(permission.getPath()));
|
||||||
|
permission.setUrl(PermissionUtils.getAbsoluteContextPathOfAPI(contextPath, apiVersion,
|
||||||
|
permission.getUrl()));
|
||||||
|
PermissionManagerServiceImpl.getInstance().addPermission(permission);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (JAXBException e) {
|
} catch (JAXBException e) {
|
||||||
log.error(
|
log.error(
|
||||||
"Exception occurred while parsing the permission configuration of webapp : "
|
"Exception occurred while parsing the permission configuration of webapp : "
|
||||||
+ servletContext.getContextPath(), e);
|
+ context.getServletContext().getContextPath(), e);
|
||||||
} catch (PermissionManagementException e) {
|
} catch (PermissionManagementException e) {
|
||||||
log.error("Exception occurred while adding the permissions from webapp : "
|
log.error("Exception occurred while adding the permissions from webapp : "
|
||||||
+ servletContext.getContextPath(), e);
|
+ servletContext.getContextPath(), e);
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.Device;
|
|||||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -31,6 +32,16 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface DeviceDAO {
|
public interface DeviceDAO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is used to add a device.
|
||||||
|
*
|
||||||
|
* @param type device type.
|
||||||
|
* @param tenantId tenant id.
|
||||||
|
* @return returns the device count of given type.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is used to add a device.
|
* This method is used to add a device.
|
||||||
*
|
*
|
||||||
@ -100,6 +111,29 @@ public interface DeviceDAO {
|
|||||||
*/
|
*/
|
||||||
List<Device> getDevices(int tenantId) throws DeviceManagementDAOException;
|
List<Device> getDevices(int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is used to retrieve the devices of a given tenant as a paginated result.
|
||||||
|
*
|
||||||
|
* @param index start index of result set.
|
||||||
|
* @param limit number of records to be returned.
|
||||||
|
* @param tenantId tenant id.
|
||||||
|
* @return returns a PaginationResult including the requested data.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
PaginationResult getDevices(int index, int limit, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is used to retrieve the devices of a given tenant and type as a paginated result.
|
||||||
|
*
|
||||||
|
* @param type device type.
|
||||||
|
* @param index start index of result set.
|
||||||
|
* @param limit number of records to be returned.
|
||||||
|
* @param tenantId tenant id.
|
||||||
|
* @return returns a PaginationResult including the requested data.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
PaginationResult getDevices(String type, int index, int limit, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is used to retrieve all the devices of a given tenant and device type.
|
* This method is used to retrieve all the devices of a given tenant and device type.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao;
|
|||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||||
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
|
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
|
||||||
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
||||||
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
||||||
@ -82,11 +83,27 @@ import java.util.List;
|
|||||||
public class DeviceManagementDAOFactory {
|
public class DeviceManagementDAOFactory {
|
||||||
|
|
||||||
private static DataSource dataSource;
|
private static DataSource dataSource;
|
||||||
|
private static String databaseEngine;
|
||||||
private static final Log log = LogFactory.getLog(DeviceManagementDAOFactory.class);
|
private static final Log log = LogFactory.getLog(DeviceManagementDAOFactory.class);
|
||||||
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
|
||||||
public static DeviceDAO getDeviceDAO() {
|
public static DeviceDAO getDeviceDAO() {
|
||||||
return new DeviceDAOImpl();
|
if(databaseEngine != null) {
|
||||||
|
switch (databaseEngine) {
|
||||||
|
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
|
||||||
|
return new OracleDeviceDAOImpl();
|
||||||
|
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
|
||||||
|
return new SQLServerDeviceDAOImpl();
|
||||||
|
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
|
||||||
|
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
|
||||||
|
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
|
||||||
|
default:
|
||||||
|
return new GenericDeviceDAOImpl();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return new GenericDeviceDAOImpl();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DeviceTypeDAO getDeviceTypeDAO() {
|
public static DeviceTypeDAO getDeviceTypeDAO() {
|
||||||
@ -107,10 +124,20 @@ public class DeviceManagementDAOFactory {
|
|||||||
|
|
||||||
public static void init(DataSourceConfig config) {
|
public static void init(DataSourceConfig config) {
|
||||||
dataSource = resolveDataSource(config);
|
dataSource = resolveDataSource(config);
|
||||||
|
try {
|
||||||
|
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while retrieving config.datasource connection", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void init(DataSource dtSource) {
|
public static void init(DataSource dtSource) {
|
||||||
dataSource = dtSource;
|
dataSource = dtSource;
|
||||||
|
try {
|
||||||
|
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while retrieving config.datasource connection", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void beginTransaction() throws TransactionManagementException {
|
public static void beginTransaction() throws TransactionManagementException {
|
||||||
|
|||||||
@ -34,7 +34,7 @@ import java.util.Iterator;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class DeviceDAOImpl implements DeviceDAO {
|
public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException {
|
public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException {
|
||||||
@ -122,7 +122,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
stmt.setInt(4, tenantId);
|
stmt.setInt(4, tenantId);
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
device = this.loadDevice(rs);
|
device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
|
||||||
@ -152,7 +152,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
stmt.setString(2, deviceIdentifier.getId());
|
stmt.setString(2, deviceIdentifier.getId());
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
device = this.loadDevice(rs);
|
device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
deviceHashMap.put(rs.getInt("TENANT_ID"), device);
|
deviceHashMap.put(rs.getInt("TENANT_ID"), device);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -184,7 +184,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
stmt.setInt(3, tenantId);
|
stmt.setInt(3, tenantId);
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
device = this.loadDevice(rs);
|
device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DeviceManagementDAOException("Error occurred while retrieving device for id " +
|
throw new DeviceManagementDAOException("Error occurred while retrieving device for id " +
|
||||||
@ -215,7 +215,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
devices = new ArrayList<>();
|
devices = new ArrayList<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Device device = this.loadDevice(rs);
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
devices.add(device);
|
devices.add(device);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -248,7 +248,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
devices = new ArrayList<>();
|
devices = new ArrayList<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Device device = this.loadDevice(rs);
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
devices.add(device);
|
devices.add(device);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -279,7 +279,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Device device = this.loadDevice(rs);
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
devices.add(device);
|
devices.add(device);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -324,6 +324,31 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
return deviceCount;
|
return deviceCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int deviceCount = 0;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t " +
|
||||||
|
"WHERE t.NAME = ?) d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
if (rs.next()) {
|
||||||
|
deviceCount = rs.getInt("DEVICE_COUNT");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while getting the device count", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
return deviceCount;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of devices that matches with the given device name.
|
* Get the list of devices that matches with the given device name.
|
||||||
*
|
*
|
||||||
@ -352,7 +377,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Device device = this.loadDevice(rs);
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
devices.add(device);
|
devices.add(device);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -475,7 +500,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
stmt.setInt(5, tenantId);
|
stmt.setInt(5, tenantId);
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
enrolmentInfo = this.loadEnrolment(rs);
|
enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs);
|
||||||
}
|
}
|
||||||
return enrolmentInfo;
|
return enrolmentInfo;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -551,7 +576,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
stmt.setInt(index, tenantId);
|
stmt.setInt(index, tenantId);
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
enrolments.add(this.loadEnrolment(rs));
|
enrolments.add(DeviceManagementDAOUtil.loadEnrolment(rs));
|
||||||
}
|
}
|
||||||
return enrolments;
|
return enrolments;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -562,28 +587,6 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Device loadDevice(ResultSet rs) throws SQLException {
|
|
||||||
Device device = new Device();
|
|
||||||
device.setId(rs.getInt("DEVICE_ID"));
|
|
||||||
device.setName(rs.getString("DEVICE_NAME"));
|
|
||||||
device.setDescription(rs.getString("DESCRIPTION"));
|
|
||||||
device.setType(rs.getString("DEVICE_TYPE"));
|
|
||||||
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
|
|
||||||
device.setEnrolmentInfo(this.loadEnrolment(rs));
|
|
||||||
return device;
|
|
||||||
}
|
|
||||||
|
|
||||||
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
|
|
||||||
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
|
||||||
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
|
|
||||||
enrolmentInfo.setOwner(rs.getString("OWNER"));
|
|
||||||
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
|
|
||||||
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
|
|
||||||
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
|
|
||||||
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
|
|
||||||
return enrolmentInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId)
|
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId)
|
||||||
throws DeviceManagementDAOException {
|
throws DeviceManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
@ -604,7 +607,7 @@ public class DeviceDAOImpl implements DeviceDAO {
|
|||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Device device = this.loadDevice(rs);
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
devices.add(device);
|
devices.add(device);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
|
||||||
|
// String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
|
||||||
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
|
||||||
|
// String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" +
|
||||||
|
// " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,? ROW_NUMBER <= ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
|
||||||
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? LIMIT ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
|
||||||
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? LIMIT ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
|
||||||
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -20,6 +20,8 @@ package org.wso2.carbon.device.mgt.core.dao.util;
|
|||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.context.CarbonContext;
|
import org.wso2.carbon.context.CarbonContext;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||||
import org.wso2.carbon.user.api.UserStoreException;
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
@ -119,5 +121,36 @@ public final class DeviceManagementDAOUtil {
|
|||||||
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
|
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
public static Device loadDevice(ResultSet rs) throws SQLException {
|
||||||
|
Device device = new Device();
|
||||||
|
device.setId(rs.getInt("ID"));
|
||||||
|
device.setName(rs.getString("NAME"));
|
||||||
|
device.setDescription(rs.getString("DESCRIPTION"));
|
||||||
|
device.setType(rs.getString("DEVICE_TYPE_ID"));
|
||||||
|
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
|
||||||
|
return device;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public static EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
|
||||||
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||||
|
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
|
||||||
|
enrolmentInfo.setOwner(rs.getString("OWNER"));
|
||||||
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
|
||||||
|
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
|
||||||
|
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
|
||||||
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
|
||||||
|
return enrolmentInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Device loadDevice(ResultSet rs) throws SQLException {
|
||||||
|
Device device = new Device();
|
||||||
|
device.setId(rs.getInt("DEVICE_ID"));
|
||||||
|
device.setName(rs.getString("DEVICE_NAME"));
|
||||||
|
device.setDescription(rs.getString("DESCRIPTION"));
|
||||||
|
device.setType(rs.getString("DEVICE_TYPE"));
|
||||||
|
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
|
||||||
|
device.setEnrolmentInfo(loadEnrolment(rs));
|
||||||
|
return device;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,18 @@ import org.apache.commons.logging.Log;
|
|||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.osgi.framework.BundleContext;
|
import org.osgi.framework.BundleContext;
|
||||||
import org.osgi.service.component.ComponentContext;
|
import org.osgi.service.component.ComponentContext;
|
||||||
|
import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService;
|
||||||
import org.wso2.carbon.device.mgt.core.scep.SCEPManager;
|
import org.wso2.carbon.device.mgt.core.scep.SCEPManager;
|
||||||
import org.wso2.carbon.device.mgt.core.scep.SCEPManagerImpl;
|
import org.wso2.carbon.device.mgt.core.scep.SCEPManagerImpl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @scr.component name="org.wso2.carbon.device.mgt.core.scep" immediate="true"
|
* @scr.component name="org.wso2.carbon.device.mgt.core.scep" immediate="true"
|
||||||
|
* @scr.reference name="app.mgt.service"
|
||||||
|
* interface="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setApplicationManagementProviderService"
|
||||||
|
* unbind="unsetApplicationManagementProviderService"
|
||||||
*/
|
*/
|
||||||
public class SCEPManagerServiceComponent {
|
public class SCEPManagerServiceComponent {
|
||||||
|
|
||||||
@ -40,4 +47,14 @@ public class SCEPManagerServiceComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void unsetApplicationManagementProviderService(ApplicationManagementProviderService
|
||||||
|
applicationManagementProviderService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setApplicationManagementProviderService(ApplicationManagementProviderService
|
||||||
|
applicationManagementProviderService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,17 +51,8 @@ public class PermissionManagerServiceImpl implements PermissionManagerService {
|
|||||||
return registryBasedPermissionManager;
|
return registryBasedPermissionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addPermissions(List<Permission> permissions) throws PermissionManagementException {
|
|
||||||
for (Permission permission : permissions) {
|
|
||||||
this.addPermission(permission);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addPermission(Permission permission) throws PermissionManagementException {
|
public boolean addPermission(Permission permission) throws PermissionManagementException {
|
||||||
// update the permission path to absolute permission path
|
|
||||||
permission.setPath(PermissionUtils.getAbsolutePermissionPath(permission.getPath()));
|
|
||||||
// adding a permission to the tree
|
// adding a permission to the tree
|
||||||
permissionTree.addPermission(permission);
|
permissionTree.addPermission(permission);
|
||||||
return PermissionUtils.putPermission(permission);
|
return PermissionUtils.putPermission(permission);
|
||||||
|
|||||||
@ -59,6 +59,13 @@ public class PermissionUtils {
|
|||||||
return PermissionUtils.ADMIN_PERMISSION_REGISTRY_PATH + permissionPath;
|
return PermissionUtils.ADMIN_PERMISSION_REGISTRY_PATH + permissionPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getAbsoluteContextPathOfAPI(String contextPath, String version, String url) {
|
||||||
|
if((version != null) && !version.isEmpty()) {
|
||||||
|
return contextPath + "/" + version + url;
|
||||||
|
}
|
||||||
|
return contextPath + url;
|
||||||
|
}
|
||||||
|
|
||||||
public static Permission getPermission(String path) throws PermissionManagementException {
|
public static Permission getPermission(String path) throws PermissionManagementException {
|
||||||
try {
|
try {
|
||||||
Resource resource = PermissionUtils.getGovernanceRegistry().get(path);
|
Resource resource = PermissionUtils.getGovernanceRegistry().get(path);
|
||||||
|
|||||||
@ -36,6 +36,10 @@ public interface DeviceManagementProviderService extends OperationManager {
|
|||||||
|
|
||||||
List<Device> getAllDevices() throws DeviceManagementException;
|
List<Device> getAllDevices() throws DeviceManagementException;
|
||||||
|
|
||||||
|
PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException;
|
||||||
|
|
||||||
|
PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException;
|
||||||
|
|
||||||
void sendEnrolmentInvitation(EmailMessageProperties config) throws DeviceManagementException;
|
void sendEnrolmentInvitation(EmailMessageProperties config) throws DeviceManagementException;
|
||||||
|
|
||||||
void sendRegistrationEmail(EmailMessageProperties config) throws DeviceManagementException;
|
void sendRegistrationEmail(EmailMessageProperties config) throws DeviceManagementException;
|
||||||
|
|||||||
@ -348,6 +348,84 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
return devices;
|
return devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException {
|
||||||
|
PaginationResult paginationResult;
|
||||||
|
List<Device> devices = new ArrayList<>();
|
||||||
|
List<Device> allDevices;
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
paginationResult = deviceDAO.getDevices(deviceType, index, limit, this.getTenantId());
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
|
||||||
|
"the current tenant", e);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
allDevices = (List<Device>) paginationResult.getData();
|
||||||
|
for (Device device : allDevices) {
|
||||||
|
DeviceManager deviceManager = this.getDeviceManager(device.getType());
|
||||||
|
if (deviceManager == null) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
|
||||||
|
"Therefore, not attempting method 'isEnrolled'");
|
||||||
|
}
|
||||||
|
devices.add(device);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Device dmsDevice =
|
||||||
|
deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
|
||||||
|
if (dmsDevice != null) {
|
||||||
|
device.setFeatures(dmsDevice.getFeatures());
|
||||||
|
device.setProperties(dmsDevice.getProperties());
|
||||||
|
}
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
paginationResult.setData(devices);
|
||||||
|
return paginationResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException {
|
||||||
|
PaginationResult paginationResult;
|
||||||
|
List<Device> devices = new ArrayList<>();
|
||||||
|
List<Device> allDevices;
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
paginationResult = deviceDAO.getDevices(index, limit, this.getTenantId());
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
|
||||||
|
"the current tenant", e);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
allDevices = (List<Device>) paginationResult.getData();
|
||||||
|
for (Device device : allDevices) {
|
||||||
|
DeviceManager deviceManager = this.getDeviceManager(device.getType());
|
||||||
|
if (deviceManager == null) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
|
||||||
|
"Therefore, not attempting method 'isEnrolled'");
|
||||||
|
}
|
||||||
|
devices.add(device);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Device dmsDevice =
|
||||||
|
deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
|
||||||
|
if (dmsDevice != null) {
|
||||||
|
device.setFeatures(dmsDevice.getFeatures());
|
||||||
|
device.setProperties(dmsDevice.getProperties());
|
||||||
|
}
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
paginationResult.setData(devices);
|
||||||
|
return paginationResult;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Device> getAllDevices(String deviceType) throws DeviceManagementException {
|
public List<Device> getAllDevices(String deviceType) throws DeviceManagementException {
|
||||||
List<Device> devices = new ArrayList<>();
|
List<Device> devices = new ArrayList<>();
|
||||||
|
|||||||
@ -120,10 +120,9 @@ public class DynamicClientWebAppRegistrationManager {
|
|||||||
String requiredDynamicClientRegistration, webAppName;
|
String requiredDynamicClientRegistration, webAppName;
|
||||||
ServletContext servletContext;
|
ServletContext servletContext;
|
||||||
RegistrationProfile registrationProfile;
|
RegistrationProfile registrationProfile;
|
||||||
OAuthAppDetails oAuthAppDetails = null;
|
OAuthAppDetails oAuthAppDetails;
|
||||||
DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager =
|
DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager =
|
||||||
DynamicClientWebAppRegistrationManager.getInstance();
|
DynamicClientWebAppRegistrationManager.getInstance();
|
||||||
//todo move enumeration to while loop
|
|
||||||
Enumeration enumeration = new IteratorEnumeration(DynamicClientWebAppRegistrationManager.
|
Enumeration enumeration = new IteratorEnumeration(DynamicClientWebAppRegistrationManager.
|
||||||
webAppContexts.keySet().iterator());
|
webAppContexts.keySet().iterator());
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
|
|||||||
@ -17,7 +17,8 @@
|
|||||||
~ under the License.
|
~ under the License.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
@ -72,10 +73,25 @@
|
|||||||
<Private-Package>org.wso2.carbon.device.mgt.oauth.extensions.internal</Private-Package>
|
<Private-Package>org.wso2.carbon.device.mgt.oauth.extensions.internal</Private-Package>
|
||||||
<Export-Package>
|
<Export-Package>
|
||||||
!org.wso2.carbon.device.mgt.oauth.extensions.internal,
|
!org.wso2.carbon.device.mgt.oauth.extensions.internal,
|
||||||
org.wso2.carbon.device.mgt.oauth.extensions.handlers.*,
|
org.wso2.carbon.device.mgt.oauth.extensions.*
|
||||||
org.wso2.carbon.device.mgt.oauth.extensions.validators.*
|
|
||||||
</Export-Package>
|
</Export-Package>
|
||||||
<DynamicImport-Package>*</DynamicImport-Package>
|
<Import-Package>
|
||||||
|
javax.security.auth.*,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.wso2.carbon.device.mgt.common.permission.mgt,
|
||||||
|
org.wso2.carbon.device.mgt.oauth.extensions.*,
|
||||||
|
org.wso2.carbon.device.mgt.*
|
||||||
|
org.wso2.carbon.identity.application.common.model,
|
||||||
|
org.wso2.carbon.identity.oauth.callback,
|
||||||
|
org.wso2.carbon.identity.oauth2,
|
||||||
|
org.wso2.carbon.identity.oauth2.model,
|
||||||
|
org.wso2.carbon.identity.oauth2.validators,
|
||||||
|
org.wso2.carbon.user.api,
|
||||||
|
org.wso2.carbon.user.core.service,
|
||||||
|
org.wso2.carbon.identity.application.common.model,
|
||||||
|
org.wso2.carbon.user.core.tenant
|
||||||
|
</Import-Package>
|
||||||
</instructions>
|
</instructions>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.oauth.extensions;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.oauth.extensions.internal.OAuthExtensionsDataHolder;
|
||||||
|
import org.wso2.carbon.user.api.TenantManager;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds util methods used by OAuth extension bundle.
|
||||||
|
*/
|
||||||
|
public class OAuthExtUtils {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(OAuthExtUtils.class);
|
||||||
|
|
||||||
|
public static int getTenantId(String tenantDomain) {
|
||||||
|
int tenantId = 0;
|
||||||
|
if (tenantDomain != null) {
|
||||||
|
try {
|
||||||
|
TenantManager tenantManager = OAuthExtensionsDataHolder.getInstance().getRealmService().getTenantManager();
|
||||||
|
tenantId = tenantManager.getTenantId(tenantDomain);
|
||||||
|
} catch (UserStoreException e) {
|
||||||
|
String errorMsg = "Error when getting the tenant id from the tenant domain : " +
|
||||||
|
tenantDomain;
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tenantId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -20,11 +20,12 @@ package org.wso2.carbon.device.mgt.oauth.extensions.validators;
|
|||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.context.CarbonContext;
|
|
||||||
import org.wso2.carbon.device.mgt.common.permission.mgt.Permission;
|
import org.wso2.carbon.device.mgt.common.permission.mgt.Permission;
|
||||||
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException;
|
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException;
|
||||||
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService;
|
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService;
|
||||||
|
import org.wso2.carbon.device.mgt.oauth.extensions.OAuthExtUtils;
|
||||||
import org.wso2.carbon.device.mgt.oauth.extensions.internal.OAuthExtensionsDataHolder;
|
import org.wso2.carbon.device.mgt.oauth.extensions.internal.OAuthExtensionsDataHolder;
|
||||||
|
import org.wso2.carbon.identity.application.common.model.User;
|
||||||
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
|
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
|
||||||
import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
|
import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
|
||||||
import org.wso2.carbon.identity.oauth2.validators.OAuth2ScopeValidator;
|
import org.wso2.carbon.identity.oauth2.validators.OAuth2ScopeValidator;
|
||||||
@ -51,6 +52,7 @@ public class PermissionBasedScopeValidator extends OAuth2ScopeValidator {
|
|||||||
public static final String WRITE = "write";
|
public static final String WRITE = "write";
|
||||||
public static final String DELETE = "delete";
|
public static final String DELETE = "delete";
|
||||||
public static final String ACTION = "action";
|
public static final String ACTION = "action";
|
||||||
|
public static final String UI_EXECUTE = "ui.execute";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(PermissionBasedScopeValidator.class);
|
private static final Log log = LogFactory.getLog(PermissionBasedScopeValidator.class);
|
||||||
@ -71,13 +73,15 @@ public class PermissionBasedScopeValidator extends OAuth2ScopeValidator {
|
|||||||
getPermissionManagerService();
|
getPermissionManagerService();
|
||||||
try {
|
try {
|
||||||
Permission permission = permissionManagerService.getPermission(properties);
|
Permission permission = permissionManagerService.getPermission(properties);
|
||||||
if ((permission != null) && (accessTokenDO.getAuthzUser() != null)) {
|
User authzUser = accessTokenDO.getAuthzUser();
|
||||||
String username = accessTokenDO.getAuthzUser().getUserName();
|
if ((permission != null) && (authzUser != null)) {
|
||||||
UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
|
String username = authzUser.getUserName();
|
||||||
|
int tenantId = OAuthExtUtils.getTenantId(authzUser.getTenantDomain());
|
||||||
|
UserRealm userRealm = OAuthExtensionsDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId);
|
||||||
if (userRealm != null && userRealm.getAuthorizationManager() != null) {
|
if (userRealm != null && userRealm.getAuthorizationManager() != null) {
|
||||||
status = userRealm.getAuthorizationManager()
|
status = userRealm.getAuthorizationManager()
|
||||||
.isUserAuthorized(username, permission.getPath(),
|
.isUserAuthorized(username, permission.getPath(),
|
||||||
PermissionMethod.READ);
|
PermissionMethod.UI_EXECUTE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (PermissionManagementException e) {
|
} catch (PermissionManagementException e) {
|
||||||
|
|||||||
@ -106,7 +106,7 @@ public class PolicyManagerImpl implements PolicyManager {
|
|||||||
policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias());
|
policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(policy.isActive()){
|
if (policy.isActive()) {
|
||||||
policyDAO.activatePolicy(policy.getId());
|
policyDAO.activatePolicy(policy.getId());
|
||||||
}
|
}
|
||||||
PolicyManagementDAOFactory.commitTransaction();
|
PolicyManagementDAOFactory.commitTransaction();
|
||||||
@ -135,18 +135,52 @@ public class PolicyManagerImpl implements PolicyManager {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Previous policy needs to be obtained before begining the transaction
|
// Previous policy needs to be obtained before begining the transaction
|
||||||
Policy previousPolicy = getPolicy(policy.getId());
|
Policy previousPolicy = this.getPolicy(policy.getId());
|
||||||
|
|
||||||
PolicyManagementDAOFactory.beginTransaction();
|
PolicyManagementDAOFactory.beginTransaction();
|
||||||
// This will keep track of the policies updated.
|
// This will keep track of the policies updated.
|
||||||
policyDAO.recordUpdatedPolicy(policy);
|
policyDAO.recordUpdatedPolicy(policy);
|
||||||
|
|
||||||
|
|
||||||
|
List<ProfileFeature> existingFeaturesList = new ArrayList<>();
|
||||||
|
List<ProfileFeature> newFeaturesList = new ArrayList<>();
|
||||||
|
List<String> temp = new ArrayList<>();
|
||||||
|
|
||||||
|
List<ProfileFeature> updatedFeatureList = policy.getProfile().getProfileFeaturesList();
|
||||||
|
|
||||||
|
List<ProfileFeature> existingProfileFeaturesList = previousPolicy.getProfile().getProfileFeaturesList();
|
||||||
|
|
||||||
|
// Checks for the existing features
|
||||||
|
for (ProfileFeature feature : updatedFeatureList) {
|
||||||
|
for (ProfileFeature fe : existingProfileFeaturesList) {
|
||||||
|
if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) {
|
||||||
|
existingFeaturesList.add(feature);
|
||||||
|
temp.add(feature.getFeatureCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks for the new features
|
||||||
|
for (ProfileFeature feature : updatedFeatureList) {
|
||||||
|
if (!temp.contains(feature.getFeatureCode())) {
|
||||||
|
newFeaturesList.add(feature);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int profileId = previousPolicy.getProfile().getProfileId();
|
||||||
|
policy.getProfile().setProfileId(profileId);
|
||||||
|
policy.setProfileId(profileId);
|
||||||
|
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
|
||||||
|
policy.getProfile().setUpdatedDate(currentTimestamp);
|
||||||
|
|
||||||
policyDAO.updatePolicy(policy);
|
policyDAO.updatePolicy(policy);
|
||||||
profileDAO.updateProfile(policy.getProfile());
|
profileDAO.updateProfile(policy.getProfile());
|
||||||
featureDAO.updateProfileFeatures(policy.getProfile().getProfileFeaturesList(), policy.getProfile()
|
|
||||||
.getProfileId());
|
|
||||||
policyDAO.deleteAllPolicyRelatedConfigs(policy.getId());
|
|
||||||
|
|
||||||
|
featureDAO.updateProfileFeatures(existingFeaturesList, profileId);
|
||||||
|
if (!newFeaturesList.isEmpty()) {
|
||||||
|
featureDAO.addProfileFeatures(newFeaturesList, profileId);
|
||||||
|
}
|
||||||
|
policyDAO.deleteAllPolicyRelatedConfigs(policy.getId());
|
||||||
|
|
||||||
|
|
||||||
if (policy.getUsers() != null) {
|
if (policy.getUsers() != null) {
|
||||||
@ -478,20 +512,24 @@ public class PolicyManagerImpl implements PolicyManager {
|
|||||||
|
|
||||||
roleNames = policyDAO.getPolicyAppliedRoles(policyId);
|
roleNames = policyDAO.getPolicyAppliedRoles(policyId);
|
||||||
userNames = policyDAO.getPolicyAppliedUsers(policyId);
|
userNames = policyDAO.getPolicyAppliedUsers(policyId);
|
||||||
Profile profile = profileDAO.getProfile(policy.getProfileId());
|
|
||||||
|
|
||||||
policy.setProfile(profile);
|
//Profile profile = profileDAO.getProfile(policy.getProfileId());
|
||||||
|
|
||||||
|
|
||||||
policy.setRoles(roleNames);
|
policy.setRoles(roleNames);
|
||||||
policy.setUsers(userNames);
|
policy.setUsers(userNames);
|
||||||
|
|
||||||
} catch (PolicyManagerDAOException e) {
|
} catch (PolicyManagerDAOException e) {
|
||||||
throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" +
|
throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" +
|
||||||
policyId + ")", e);
|
policyId + ")", e);
|
||||||
} catch (ProfileManagerDAOException e) {
|
// } catch (ProfileManagerDAOException e) {
|
||||||
throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" +
|
// throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" +
|
||||||
policyId + ")", e);
|
// policyId + ")", e);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
|
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
|
||||||
|
// } catch (ProfileManagementException e) {
|
||||||
|
// throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" +
|
||||||
|
// policyId + ")", e);
|
||||||
} finally {
|
} finally {
|
||||||
PolicyManagementDAOFactory.closeConnection();
|
PolicyManagementDAOFactory.closeConnection();
|
||||||
}
|
}
|
||||||
@ -499,6 +537,20 @@ public class PolicyManagerImpl implements PolicyManager {
|
|||||||
// This is done because connection close in below method too.
|
// This is done because connection close in below method too.
|
||||||
deviceList = this.getPolicyAppliedDevicesIds(policyId);
|
deviceList = this.getPolicyAppliedDevicesIds(policyId);
|
||||||
policy.setDevices(deviceList);
|
policy.setDevices(deviceList);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// PolicyManagementDAOFactory.openConnection();
|
||||||
|
Profile profile = profileManager.getProfile(policy.getProfileId());
|
||||||
|
policy.setProfile(profile);
|
||||||
|
} catch (ProfileManagementException e) {
|
||||||
|
throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" +
|
||||||
|
policyId + ")", e);
|
||||||
|
// } catch (SQLException e) {
|
||||||
|
// throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
|
||||||
|
// } finally {
|
||||||
|
// PolicyManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
return policy;
|
return policy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -314,6 +314,11 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
|
|||||||
users.add("Udara");
|
users.add("Udara");
|
||||||
users.add("Dileesha");
|
users.add("Dileesha");
|
||||||
policy.setUsers(users);
|
policy.setUsers(users);
|
||||||
|
|
||||||
|
Profile profile2 = ProfileCreator.getProfile3(FeatureCreator.getFeatureList4());
|
||||||
|
|
||||||
|
Profile pf = new Profile();
|
||||||
|
|
||||||
pap.updatePolicy(policy);
|
pap.updatePolicy(policy);
|
||||||
pap.activatePolicy(policy.getId());
|
pap.activatePolicy(policy.getId());
|
||||||
}
|
}
|
||||||
|
|||||||
5
pom.xml
5
pom.xml
@ -417,12 +417,12 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.osgi</groupId>
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
<artifactId>org.eclipse.osgi.services</artifactId>
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
<version>3.3.100.v20120522-1822</version>
|
<version>${eclipse.equinox.services.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.osgi.ut</groupId>
|
<groupId>org.osgi.ut</groupId>
|
||||||
<artifactId>org.eclipse.osgi</artifactId>
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
<version>3.3.100.v20120522-1822</version>
|
<version>${eclipse.equinox.services.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- End of OSGi dependencies -->
|
<!-- End of OSGi dependencies -->
|
||||||
|
|
||||||
@ -1454,6 +1454,7 @@
|
|||||||
|
|
||||||
<!--Eclipse OSGi-->
|
<!--Eclipse OSGi-->
|
||||||
<eclipse.equinox.common.version>3.6.100.v20120522-1841</eclipse.equinox.common.version>
|
<eclipse.equinox.common.version>3.6.100.v20120522-1841</eclipse.equinox.common.version>
|
||||||
|
<eclipse.equinox.services.version>3.3.100.v20120522-1822</eclipse.equinox.services.version>
|
||||||
<eclipse.osgi.version>3.8.1.v20120830-144521</eclipse.osgi.version>
|
<eclipse.osgi.version>3.8.1.v20120830-144521</eclipse.osgi.version>
|
||||||
|
|
||||||
<orbit.version.h2.engine>1.2.140.wso2v3</orbit.version.h2.engine>
|
<orbit.version.h2.engine>1.2.140.wso2v3</orbit.version.h2.engine>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user