revert-70aa11f8
hasuniea 9 years ago
commit a251b5d84e

@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.common.operation.mgt;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import java.util.List;
@ -41,12 +42,24 @@ public interface OperationManager {
/**
* Method to retrieve the list of all operations to a device.
*
* @param deviceId DeviceIdentifier of the device
* @param deviceId
* @throws OperationManagementException If some unusual behaviour is observed while fetching the
* operation list.
*/
List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException;
/**
* Method to retrieve all the operations applied to a device with pagination support.
*
* @param deviceId DeviceIdentifier of the device
* @param index Starting row number
* @param limit No of rows to fetch
* @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws OperationManagementException If some unusual behaviour is observed while fetching the
* operation list.
*/
PaginationResult getOperations(DeviceIdentifier deviceId, int index, int limit) throws OperationManagementException;
/**
* Method to retrieve the list of available operations to a device.
*

@ -26,6 +26,10 @@ 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.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.impl.*;
import org.wso2.carbon.device.mgt.core.dao.impl.device.GenericDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.OracleDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.PostgreSQLDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.SQLServerDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import javax.sql.DataSource;
@ -96,6 +100,7 @@ public class DeviceManagementDAOFactory {
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
return new SQLServerDeviceDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
return new PostgreSQLDeviceDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
default:

@ -26,6 +26,7 @@ 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.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.sql.*;
import java.util.ArrayList;
@ -652,4 +653,29 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return devices;
}
@Override
public List<DeviceType> getDeviceTypes()
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<DeviceType> deviceTypes;
try {
conn = this.getConnection();
String sql = "SELECT t.ID, t.NAME FROM DM_DEVICE_TYPE t";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
deviceTypes = new ArrayList<>();
while (rs.next()) {
DeviceType deviceType = DeviceManagementDAOUtil.loadDeviceType(rs);
deviceTypes.add(deviceType);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing device types.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceTypes;
}
}

@ -16,13 +16,14 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.core.dao.impl;
package org.wso2.carbon.device.mgt.core.dao.impl.device;
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.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -121,32 +122,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
return result;
}
@Override
public List<DeviceType> getDeviceTypes()
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<DeviceType> deviceTypes;
try {
conn = this.getConnection();
String sql = "SELECT t.ID, t.NAME " +
"FROM DM_DEVICE_TYPE t";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
deviceTypes = new ArrayList<>();
while (rs.next()) {
DeviceType deviceType = DeviceManagementDAOUtil.loadDeviceType(rs);
deviceTypes.add(deviceType);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing device types.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceTypes;
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}

@ -16,12 +16,13 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.core.dao.impl;
package org.wso2.carbon.device.mgt.core.dao.impl.device;
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.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -52,7 +53,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
"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 <= ?";
"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);
@ -92,7 +93,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
"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 ?,?";
"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);
@ -117,30 +118,6 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
return result;
}
@Override public List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<DeviceType> deviceTypes;
try {
conn = this.getConnection();
String sql = "SELECT t.ID, t.NAME " +
"FROM DM_DEVICE_TYPE t";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
deviceTypes = new ArrayList<>();
while (rs.next()) {
DeviceType deviceType = DeviceManagementDAOUtil.loadDeviceType(rs);
deviceTypes.add(deviceType);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing device types.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceTypes;
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}

@ -16,12 +16,13 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.core.dao.impl;
package org.wso2.carbon.device.mgt.core.dao.impl.device;
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.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -52,12 +53,12 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
"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 ?";
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId);
stmt.setInt(3, index);
stmt.setInt(4, limit);
stmt.setInt(3, limit);
stmt.setInt(4, index);
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
@ -92,13 +93,13 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
"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 ?";
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type);
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, index);
stmt.setInt(5, limit);
stmt.setInt(4, limit);
stmt.setInt(5, index);
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
@ -117,30 +118,6 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
return result;
}
@Override public List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<DeviceType> deviceTypes;
try {
conn = this.getConnection();
String sql = "SELECT t.ID, t.NAME " +
"FROM DM_DEVICE_TYPE t";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
deviceTypes = new ArrayList<>();
while (rs.next()) {
DeviceType deviceType = DeviceManagementDAOUtil.loadDeviceType(rs);
deviceTypes.add(deviceType);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing device types.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceTypes;
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}

@ -16,12 +16,13 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.core.dao.impl;
package org.wso2.carbon.device.mgt.core.dao.impl.device;
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.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -117,31 +118,6 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
return result;
}
@Override
public List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<DeviceType> deviceTypes;
try {
conn = this.getConnection();
String sql = "SELECT t.ID, t.NAME " +
"FROM DM_DEVICE_TYPE t";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
deviceTypes = new ArrayList<>();
while (rs.next()) {
DeviceType deviceType = DeviceManagementDAOUtil.loadDeviceType(rs);
deviceTypes.add(deviceType);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing device types.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceTypes;
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}

@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
@ -188,6 +189,67 @@ public class OperationManagerImpl implements OperationManager {
return operations;
}
@Override
public PaginationResult getOperations(DeviceIdentifier deviceId, int index, int limit)
throws OperationManagementException {
PaginationResult paginationResult = null;
int enrolmentId;
List<Operation> operations = new ArrayList<>();
try {
boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService().
isUserAuthorized(deviceId);
if (isUserAuthorized) {
try {
try {
DeviceManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
OperationManagementDAOFactory.openConnection();
if (enrolmentId < 0) {
throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type" +
deviceId.getType());
}
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList =
operationDAO.getOperationsForDevice(enrolmentId, index, limit);
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) {
Operation operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation);
}
paginationResult = new PaginationResult();
int count = operationDAO.getOperationCountForDevice(enrolmentId);
paginationResult.setData(operations);
paginationResult.setRecordsTotal(count);
paginationResult.setRecordsFiltered(operations.size());
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
} catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving metadata of '" +
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "'");
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
} else {
log.info("User : " + getUser() + " is not authorized to fetch operations on device : " + deviceId.getId());
}
} catch (DeviceAccessAuthorizationException e) {
throw new OperationManagementException("Error occurred while authorizing access to the devices for user : " +
this.getUser(), e);
}
return paginationResult;
}
@Override
public List<? extends Operation> getPendingOperations(DeviceIdentifier deviceId) throws
OperationManagementException {

@ -37,8 +37,15 @@ public interface OperationDAO {
List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status)
throws OperationManagementDAOException;
List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit, Operation.Status status)
throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException;
int getOperationCountForDevice(int enrolmentId) throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit) throws OperationManagementDAOException;
Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException;
void updateOperationStatus(int enrolmentId, int operationId,Operation.Status status)

@ -20,12 +20,16 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.apache.commons.logging.Log;
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.TransactionManagementException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.*;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.OracleOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.PostgreSQLOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.SQLServerOperationDAOImpl;
import javax.sql.DataSource;
import java.sql.Connection;
@ -37,6 +41,7 @@ public class OperationManagementDAOFactory {
private static final Log log = LogFactory.getLog(OperationManagementDAOFactory.class);
private static DataSource dataSource;
private static String databaseEngine;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public static OperationDAO getCommandOperationDAO() {
@ -60,15 +65,40 @@ public class OperationManagementDAOFactory {
}
public static OperationDAO getOperationDAO() {
return new OperationDAOImpl();
if(databaseEngine != null) {
switch (databaseEngine) {
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
return new OracleOperationDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
return new SQLServerOperationDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
return new PostgreSQLOperationDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
default:
return new GenericOperationDAOImpl();
}
} else {
return new GenericOperationDAOImpl();
}
}
public static void init(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 init(DataSourceConfig 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 beginTransaction() throws TransactionManagementException {

@ -31,7 +31,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CommandOperationDAOImpl extends OperationDAOImpl {
public class CommandOperationDAOImpl extends GenericOperationDAOImpl {
@Override
public int addOperation(Operation operation) throws OperationManagementDAOException {

@ -35,7 +35,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ConfigOperationDAOImpl extends OperationDAOImpl {
public class ConfigOperationDAOImpl extends GenericOperationDAOImpl {
private static final Log log = LogFactory.getLog(ConfigOperationDAOImpl.class);

@ -4,7 +4,7 @@
* 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
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@ -14,7 +14,6 @@
* 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.operation.mgt.dao.impl;
@ -34,9 +33,12 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class OperationDAOImpl implements OperationDAO {
/**
* This class holds the generic implementation of OperationDAO which can be used to support ANSI db syntax.
*/
public class GenericOperationDAOImpl implements OperationDAO {
private static final Log log = LogFactory.getLog(OperationDAOImpl.class);
private static final Log log = LogFactory.getLog(GenericOperationDAOImpl.class);
public int addOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
@ -273,6 +275,51 @@ public class OperationDAOImpl implements OperationDAO {
return operations;
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit,
Operation.Status status)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
stmt.setInt(3, index);
stmt.setInt(4, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(status);
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
@Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
@ -312,6 +359,72 @@ public class OperationDAOImpl implements OperationDAO {
return operations;
}
@Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setInt(2, index);
stmt.setInt(3, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
@Override
public int getOperationCountForDevice(int enrolmentId) throws OperationManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int operationCount = 0;
try {
conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(ID) AS OPERATION_COUNT FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
rs = stmt.executeQuery();
if (rs.next()) {
operationCount = rs.getInt("OPERATION_COUNT");
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while getting the operations count for enrolment : "
+ enrolmentId, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operationCount;
}
@Override
public Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException {
PreparedStatement stmt = null;

@ -31,7 +31,7 @@ import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class PolicyOperationDAOImpl extends OperationDAOImpl {
public class PolicyOperationDAOImpl extends GenericOperationDAOImpl {
private static final Log log = LogFactory.getLog(PolicyOperationDAOImpl.class);

@ -31,7 +31,7 @@ import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ProfileOperationDAOImpl extends OperationDAOImpl {
public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
private static final Log log = LogFactory.getLog(ProfileOperationDAOImpl.class);

@ -0,0 +1,127 @@
/*
* 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.operation.mgt.dao.impl.operation;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl;
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 implementation of OperationDAO which can be used to support Oracle db syntax.
*/
public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
@Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC " +
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setInt(2, index);
stmt.setInt(3, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit,
Operation.Status status)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
stmt.setInt(3, index);
stmt.setInt(4, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(status);
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
}

@ -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.operation.mgt.dao.impl.operation;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl;
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 implementation of OperationDAO which can be used to support PostgreSQL db syntax.
*/
public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
@Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setInt(2, limit);
stmt.setInt(3, index);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit,
Operation.Status status)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
stmt.setInt(3, limit);
stmt.setInt(4, index);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(status);
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
}

@ -0,0 +1,127 @@
/*
* 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.operation.mgt.dao.impl.operation;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl;
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 implementation of OperationDAO which can be used to support SQLServer db syntax.
*/
public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
@Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC " +
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setInt(2, index);
stmt.setInt(3, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit,
Operation.Status status)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operations = new ArrayList<Operation>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
stmt.setInt(3, index);
stmt.setInt(4, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(status);
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
}

@ -861,6 +861,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return DeviceManagementDataHolder.getInstance().getOperationManager().getOperations(deviceId);
}
@Override
public PaginationResult getOperations(DeviceIdentifier deviceId, int index, int limit)
throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getOperationManager().getOperations(deviceId, index, limit);
}
@Override
public List<? extends Operation> getPendingOperations(DeviceIdentifier deviceId)
throws OperationManagementException {

@ -123,6 +123,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
if (log.isDebugEnabled()) {
log.debug("Compliance status primary key " + complianceData.getId());
}
monitoringDAO.deleteNoneComplianceData(complianceData.getId());
monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(),
complianceFeatures);

Loading…
Cancel
Save