Refractor Plugin DAO factories

revert-dabc3590
manoj 10 years ago
parent c75a32a36e
commit fe72fc5b97

@ -20,14 +20,16 @@ package org.wso2.carbon.device.mgt.mobile.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.DataSourceNotAvailableException;
import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.dao.impl.*;
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
@ -36,21 +38,16 @@ import java.util.Map;
/**
* Factory class used to create MobileDeviceManagement related DAO objects.
*/
public class MobileDeviceManagementDAOFactory {
public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface {
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class);
private static Map<String, MobileDataSourceConfig> mobileDataSourceConfigMap;
private static Map<String, DataSource> dataSourceMap;
private String pluginProvider;
private DataSource dataSource;
private static boolean isInitialized;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
protected static DataSource dataSource;
public MobileDeviceManagementDAOFactory(String pluginProvider) {
this.pluginProvider = pluginProvider;
this.dataSource = dataSourceMap.get(pluginProvider);
}
public static void init() throws DeviceManagementException {
public static void init() throws MobileDeviceMgtPluginException {
dataSourceMap = new HashMap<String, DataSource>();
DataSource dataSource;
@ -68,8 +65,7 @@ public class MobileDeviceManagementDAOFactory {
* @param config Mobile data source configuration
* @return data source resolved from the data source definition
*/
private static DataSource resolveDataSource(MobileDataSourceConfig config)
throws DeviceManagementException {
protected static DataSource resolveDataSource(MobileDataSourceConfig config) {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException("Device Management Repository data source configuration " +
@ -99,34 +95,6 @@ public class MobileDeviceManagementDAOFactory {
return dataSource;
}
public MobileDeviceDAO getMobileDeviceDAO() {
return new MobileDeviceDAOImpl(dataSource);
}
public MobileOperationDAO getMobileOperationDAO() {
return new MobileOperationDAOImpl(dataSource);
}
public MobileOperationPropertyDAO getMobileOperationPropertyDAO() {
return new MobileOperationPropertyDAOImpl(dataSource);
}
public MobileDeviceOperationMappingDAO getMobileDeviceOperationDAO() {
return new MobileDeviceOperationMappingDAOImpl(dataSource);
}
public MobileFeatureDAO getFeatureDAO() {
return new MobileFeatureDAOImpl(dataSource);
}
public MobileFeaturePropertyDAO getFeaturePropertyDAO() {
return new MobileFeaturePropertyDAOImpl(dataSource);
}
public MobileDataSourceConfig getMobileDeviceManagementConfig(String pluginType) {
return mobileDataSourceConfigMap.get(pluginType);
}
public static Map<String, MobileDataSourceConfig> getMobileDataSourceConfigMap() {
return mobileDataSourceConfigMap;
}
@ -135,14 +103,10 @@ public class MobileDeviceManagementDAOFactory {
MobileDeviceManagementDAOFactory.mobileDataSourceConfigMap = mobileDataSourceConfigMap;
}
public DataSource getDataSource(String type) {
public static DataSource getDataSource(String type) {
return dataSourceMap.get(type);
}
public DataSource getDataSource() {
return dataSource;
}
public static Map<String, DataSource> getDataSourceMap() {
return dataSourceMap;
}
@ -153,4 +117,51 @@ public class MobileDeviceManagementDAOFactory {
"is not initialized");
}
}
public static void beginTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
}
}
public static Connection getConnection() {
return currentConnection.get();
}
public static void commitTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
}
}
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while rollbacking the transaction", e);
}
}
}

@ -35,7 +35,7 @@ public interface MobileFeatureDAO {
* @return The id of inserted MobileFeature.
* @throws MobileDeviceManagementDAOException
*/
int addMobileFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException;
boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException;
/**
* Updates a MobileFeature in Mobile-Feature table.
@ -44,7 +44,7 @@ public interface MobileFeatureDAO {
* @return The status of the operation.
* @throws MobileDeviceManagementDAOException
*/
boolean updateMobileFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException;
boolean updateFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException;
/**
* Deletes a MobileFeature from Mobile-Feature table when the feature id is given.
@ -53,7 +53,7 @@ public interface MobileFeatureDAO {
* @return The status of the operation.
* @throws MobileDeviceManagementDAOException
*/
boolean deleteMobileFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException;
boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException;
/**
* Deletes a MobileFeature from Mobile-Feature table when the feature code is given.
@ -62,7 +62,7 @@ public interface MobileFeatureDAO {
* @return The status of the operation.
* @throws MobileDeviceManagementDAOException
*/
boolean deleteMobileFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException;
boolean deleteFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException;
/**
* Retrieves a given MobileFeature from Mobile-Feature table when the feature id is given.
@ -71,7 +71,7 @@ public interface MobileFeatureDAO {
* @return MobileFeature object that holds data of the feature represented by featureId.
* @throws MobileDeviceManagementDAOException
*/
MobileFeature getMobileFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException;
MobileFeature getFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException;
/**
* Retrieves a given MobileFeature from Mobile-Feature table when the feature code is given.
@ -80,7 +80,7 @@ public interface MobileFeatureDAO {
* @return MobileFeature object that holds data of the feature represented by featureCode.
* @throws MobileDeviceManagementDAOException
*/
MobileFeature getMobileFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException;
MobileFeature getFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException;
/**
* Retrieves all MobileFeatures of a MobileDevice type from Mobile-Feature table.
@ -89,7 +89,7 @@ public interface MobileFeatureDAO {
* @return MobileFeature object list.
* @throws MobileDeviceManagementDAOException
*/
List<MobileFeature> getMobileFeatureByDeviceType(String deviceType) throws MobileDeviceManagementDAOException;
List<MobileFeature> getFeatureByDeviceType(String deviceType) throws MobileDeviceManagementDAOException;
/**
* Retrieve all the MobileFeatures from Mobile-Feature table.
@ -97,5 +97,5 @@ public interface MobileFeatureDAO {
* @return MobileFeature object list.
* @throws MobileDeviceManagementDAOException
*/
List<MobileFeature> getAllMobileFeatures() throws MobileDeviceManagementDAOException;
List<MobileFeature> getAllFeatures() throws MobileDeviceManagementDAOException;
}

@ -46,9 +46,9 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public int addMobileFeature(MobileFeature mobileFeature)
public boolean addFeature(MobileFeature mobileFeature)
throws MobileDeviceManagementDAOException {
int status = 0;
boolean status = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
@ -63,15 +63,13 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
stmt.setString(4, mobileFeature.getDeviceType());
int rows = stmt.executeUpdate();
if (rows > 0) {
ResultSet rs = stmt.getGeneratedKeys();
if (rs != null && rs.next()) {
status = rs.getInt(1);
}
if (log.isDebugEnabled()) {
log.debug("Added a new MobileFeature " + mobileFeature.getCode() + " to the" +
" MDM database.");
}
status = true;
}
} catch (SQLException e) {
String msg = "Error occurred while adding feature code - '" +
mobileFeature.getCode() + "' to feature table";
@ -84,7 +82,7 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public boolean updateMobileFeature(MobileFeature mobileFeature)
public boolean updateFeature(MobileFeature mobileFeature)
throws MobileDeviceManagementDAOException {
boolean status = false;
Connection conn = null;
@ -119,7 +117,7 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public boolean deleteMobileFeatureByCode(String mblFeatureCode)
public boolean deleteFeatureByCode(String mblFeatureCode)
throws MobileDeviceManagementDAOException {
boolean status = false;
Connection conn = null;
@ -149,7 +147,7 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public boolean deleteMobileFeatureById(int mblFeatureId)
public boolean deleteFeatureById(int mblFeatureId)
throws MobileDeviceManagementDAOException {
boolean status = false;
Connection conn = null;
@ -179,7 +177,7 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public MobileFeature getMobileFeatureByCode(String mblFeatureCode)
public MobileFeature getFeatureByCode(String mblFeatureCode)
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
@ -216,7 +214,7 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public MobileFeature getMobileFeatureById(int mblFeatureId)
public MobileFeature getFeatureById(int mblFeatureId)
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
@ -253,7 +251,7 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public List<MobileFeature> getAllMobileFeatures() throws MobileDeviceManagementDAOException {
public List<MobileFeature> getAllFeatures() throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
MobileFeature mobileFeature;
@ -287,7 +285,7 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
}
@Override
public List<MobileFeature> getMobileFeatureByDeviceType(String deviceType) throws
public List<MobileFeature> getFeatureByDeviceType(String deviceType) throws
MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;

@ -25,7 +25,7 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.FeatureManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import java.util.ArrayList;
@ -36,180 +36,177 @@ import java.util.List;
*/
public class AndroidDeviceManager implements DeviceManager {
private MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory;
private static final Log log = LogFactory.getLog(AndroidDeviceManager.class);
public AndroidDeviceManager() {
mobileDeviceManagementDAOFactory = new MobileDeviceManagementDAOFactory(
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
FeatureManagementDAOFactory
.init(mobileDeviceManagementDAOFactory.getDataSource(this.getProviderType()));
}
@Override
public String getProviderType() {
return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID;
}
@Override
public FeatureManager getFeatureManager() {
return new AndroidFeatureManager();
}
@Override
public boolean enrollDevice(Device device) throws DeviceManagementException {
boolean status;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
if (log.isDebugEnabled()) {
log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice(
mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while enrolling the Android device : " +
device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
boolean status;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
if (log.isDebugEnabled()) {
log.debug("Modifying the Android device enrollment data");
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while updating the enrollment of the Android device : " +
device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
boolean status;
try {
if (log.isDebugEnabled()) {
log.debug("Dis-enrolling Android device : " + deviceId);
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.deleteMobileDevice(deviceId.getId());
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while removing the Android device : " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
boolean isEnrolled = false;
try {
if (log.isDebugEnabled()) {
log.debug("Checking the enrollment of Android device : " + deviceId.getId());
}
MobileDevice mobileDevice =
mobileDeviceManagementDAOFactory.getMobileDeviceDAO().getMobileDevice(
deviceId.getId());
if (mobileDevice != null) {
isEnrolled = true;
}
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while checking the enrollment status of Android device : " +
deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return isEnrolled;
}
@Override
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
return true;
}
@Override
public boolean setActive(DeviceIdentifier deviceId, boolean status)
throws DeviceManagementException {
return true;
}
@Override
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
Device device;
try {
if (log.isDebugEnabled()) {
log.debug("Getting the details of Android device : " + deviceId.getId());
}
MobileDevice mobileDevice = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().
getMobileDevice(deviceId.getId());
device = MobileDeviceManagementUtil.convertToDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while fetching the Android device : " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return device;
}
@Override
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
throws DeviceManagementException {
return true;
}
@Override
public boolean updateDeviceInfo(Device device) throws DeviceManagementException {
boolean status;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
if (log.isDebugEnabled()) {
log.debug(
"updating the details of Android device : " + device.getDeviceIdentifier());
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg =
"Error while updating the Android device : " + device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public List<Device> getAllDevices() throws DeviceManagementException {
List<Device> devices = null;
try {
if (log.isDebugEnabled()) {
log.debug("Fetching the details of all Android devices");
}
List<MobileDevice> mobileDevices =
mobileDeviceManagementDAOFactory.getMobileDeviceDAO().
getAllMobileDevices();
if (mobileDevices != null) {
devices = new ArrayList<Device>();
for (MobileDevice mobileDevice : mobileDevices) {
devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevice));
}
}
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while fetching all Android devices.";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return devices;
}
private MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory;
private static final Log log = LogFactory.getLog(AndroidDeviceManager.class);
public AndroidDeviceManager() {
mobileDeviceManagementDAOFactory = new AndroidDAOFactory();
}
@Override
public String getProviderType() {
return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID;
}
@Override
public FeatureManager getFeatureManager() {
return new AndroidFeatureManager();
}
@Override
public boolean enrollDevice(Device device) throws DeviceManagementException {
boolean status;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
if (log.isDebugEnabled()) {
log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice(
mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while enrolling the Android device : " +
device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
boolean status;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
if (log.isDebugEnabled()) {
log.debug("Modifying the Android device enrollment data");
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while updating the enrollment of the Android device : " +
device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
boolean status;
try {
if (log.isDebugEnabled()) {
log.debug("Dis-enrolling Android device : " + deviceId);
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.deleteMobileDevice(deviceId.getId());
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while removing the Android device : " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
boolean isEnrolled = false;
try {
if (log.isDebugEnabled()) {
log.debug("Checking the enrollment of Android device : " + deviceId.getId());
}
MobileDevice mobileDevice =
mobileDeviceManagementDAOFactory.getMobileDeviceDAO().getMobileDevice(
deviceId.getId());
if (mobileDevice != null) {
isEnrolled = true;
}
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while checking the enrollment status of Android device : " +
deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return isEnrolled;
}
@Override
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
return true;
}
@Override
public boolean setActive(DeviceIdentifier deviceId, boolean status)
throws DeviceManagementException {
return true;
}
@Override
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
Device device;
try {
if (log.isDebugEnabled()) {
log.debug("Getting the details of Android device : " + deviceId.getId());
}
MobileDevice mobileDevice = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().
getMobileDevice(deviceId.getId());
device = MobileDeviceManagementUtil.convertToDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while fetching the Android device : " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return device;
}
@Override
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
throws DeviceManagementException {
return true;
}
@Override
public boolean updateDeviceInfo(Device device) throws DeviceManagementException {
boolean status;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
if (log.isDebugEnabled()) {
log.debug(
"updating the details of Android device : " + device.getDeviceIdentifier());
}
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg =
"Error while updating the Android device : " + device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
}
@Override
public List<Device> getAllDevices() throws DeviceManagementException {
List<Device> devices = null;
try {
if (log.isDebugEnabled()) {
log.debug("Fetching the details of all Android devices");
}
List<MobileDevice> mobileDevices =
mobileDeviceManagementDAOFactory.getMobileDeviceDAO().
getAllMobileDevices();
if (mobileDevices != null) {
devices = new ArrayList<Device>();
for (MobileDevice mobileDevice : mobileDevices) {
devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevice));
}
}
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while fetching all Android devices.";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return devices;
}
}

@ -22,34 +22,44 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.common.FeatureManagementException;
import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.FeatureDAO;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.FeatureManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.FeatureManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import java.util.ArrayList;
import java.util.List;
public class AndroidFeatureManager implements FeatureManager {
private FeatureDAO featureDAO;
private MobileFeatureDAO featureDAO;
private static final Log log = LogFactory.getLog(AndroidFeatureManager.class);
private MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory;
public AndroidFeatureManager() {
this.featureDAO = FeatureManagementDAOFactory.getFeatureDAO();
mobileDeviceManagementDAOFactory = new AndroidDAOFactory();
this.featureDAO = mobileDeviceManagementDAOFactory.getMobileFeatureDao();
}
@Override
public boolean addFeature(Feature feature) throws DeviceManagementException {
try {
FeatureManagementDAOFactory.beginTransaction();
featureDAO.addFeature(feature);
FeatureManagementDAOFactory.commitTransaction();
mobileDeviceManagementDAOFactory.beginTransaction();
MobileFeature mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature);
featureDAO.addFeature(mobileFeature);
mobileDeviceManagementDAOFactory.commitTransaction();
return true;
} catch (FeatureManagementDAOException e) {
} catch (MobileDeviceManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
mobileDeviceManagementDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
throw new DeviceManagementException("Error occurred while adding the feature", e);
@ -59,14 +69,13 @@ public class AndroidFeatureManager implements FeatureManager {
@Override
public Feature getFeature(String name) throws DeviceManagementException {
try {
FeatureManagementDAOFactory.beginTransaction();
Feature feature = featureDAO.getFeature(name);
FeatureManagementDAOFactory.commitTransaction();
MobileFeature mobileFeature = featureDAO.getFeatureByCode(name);
Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature);
return feature;
} catch (FeatureManagementDAOException e) {
} catch (MobileDeviceManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
mobileDeviceManagementDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
throw new DeviceManagementException("Error occurred while retrieving the feature", e);
@ -75,15 +84,18 @@ public class AndroidFeatureManager implements FeatureManager {
@Override
public List<Feature> getFeatures() throws DeviceManagementException {
List<Feature> featureList = new ArrayList<Feature>();
try {
FeatureManagementDAOFactory.beginTransaction();
List<Feature> features = featureDAO.getFeatures();
FeatureManagementDAOFactory.commitTransaction();
return features;
} catch (FeatureManagementDAOException e) {
List<MobileFeature> mobileFeatures = featureDAO.getAllFeatures();
for (MobileFeature mobileFeature : mobileFeatures) {
featureList.add(MobileDeviceManagementUtil.convertToFeature(mobileFeature));
}
return featureList;
} catch (MobileDeviceManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
mobileDeviceManagementDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
throw new DeviceManagementException("Error occurred while retrieving the list of features registered " +
@ -92,20 +104,22 @@ public class AndroidFeatureManager implements FeatureManager {
}
@Override
public boolean removeFeature(String name) throws DeviceManagementException {
public boolean removeFeature(String code) throws DeviceManagementException {
boolean status = false;
try {
FeatureManagementDAOFactory.beginTransaction();
featureDAO.removeFeature(name);
FeatureManagementDAOFactory.commitTransaction();
return true;
} catch (FeatureManagementDAOException e) {
mobileDeviceManagementDAOFactory.beginTransaction();
featureDAO.deleteFeatureByCode(code);
mobileDeviceManagementDAOFactory.commitTransaction();
status = true;
} catch (MobileDeviceManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
mobileDeviceManagementDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
throw new DeviceManagementException("Error occurred while removing the feature", e);
}
return status;
}
}

@ -20,24 +20,48 @@ package org.wso2.carbon.device.mgt.mobile.impl.android.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.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.dao.*;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.impl.FeatureDAOImpl;
import javax.sql.DataSource;
public class AndroidDAOFactory {
public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory
implements MobileDeviceManagementDAOFactoryInterface {
private static final Log log = LogFactory.getLog(AndroidDAOFactory.class);
private static DataSource dataSource;
private static boolean isInitialized;
private static MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory;
public static void init() {
mobileDeviceManagementDAOFactory = new MobileDeviceManagementDAOFactory(DeviceManagementConstants
.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
public static void init(MobileDataSourceConfig config) {
dataSource = resolveDataSource(config);
}
public static DataSource getDataSource() {
return mobileDeviceManagementDAOFactory.getDataSource();
@Override
public MobileDeviceDAO getMobileDeviceDAO() {
return null;
}
@Override
public MobileOperationDAO getMobileOperationDAO() {
return null;
}
@Override
public MobileOperationPropertyDAO getMobileOperationPropertyDAO() {
return null;
}
@Override
public MobileDeviceOperationMappingDAO getMobileDeviceOperationDAO() {
return null;
}
@Override public MobileFeatureDAO getMobileFeatureDao() {
return new FeatureDAOImpl();
}
public MobileFeaturePropertyDAO getFeaturePropertyDAO() {
return null;
}
}

@ -19,17 +19,18 @@
package org.wso2.carbon.device.mgt.mobile.impl.android.dao;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import java.util.List;
public interface FeatureDAO {
void addFeature(Feature feature) throws FeatureManagementDAOException;
void addFeature(MobileFeature feature) throws FeatureManagementDAOException;
void removeFeature(String name) throws FeatureManagementDAOException;
Feature getFeature(String name) throws FeatureManagementDAOException;
MobileFeature getFeature(String name) throws FeatureManagementDAOException;
List<Feature> getFeatures() throws FeatureManagementDAOException;
List<MobileFeature> getFeatures() throws FeatureManagementDAOException;
}

@ -18,7 +18,9 @@
*/
package org.wso2.carbon.device.mgt.mobile.impl.android.dao;
public class FeatureManagementDAOException extends Exception {
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
public class FeatureManagementDAOException extends MobileDeviceManagementDAOException {
private String message;
private static final long serialVersionUID = 2021891706072918865L;

@ -20,10 +20,12 @@ package org.wso2.carbon.device.mgt.mobile.impl.android.dao.impl;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.FeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.FeatureManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.FeatureManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -32,53 +34,77 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class FeatureDAOImpl implements FeatureDAO {
public class FeatureDAOImpl implements MobileFeatureDAO {
@Override
public void addFeature(Feature feature) throws FeatureManagementDAOException {
public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null;
boolean status = false;
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, feature.getCode());
stmt.setString(2, feature.getName());
stmt.setString(3, feature.getDescription());
stmt.setString(1, mobileFeature.getCode());
stmt.setString(2, mobileFeature.getName());
stmt.setString(3, mobileFeature.getDescription());
stmt.executeUpdate();
status = true;
status = true;
} catch (SQLException e) {
throw new FeatureManagementDAOException("Error occurred while adding feature '" +
feature.getName() + "' into the metadata repository", e);
throw new org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOException(
"Error occurred while adding feature '" +
mobileFeature.getName() + "' into the metadata repository", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
@Override
public void removeFeature(String code) throws FeatureManagementDAOException {
public boolean updateFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException {
return false;
}
@Override
public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException {
return false;
}
@Override
public boolean deleteFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null;
boolean status = false;
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, code);
stmt.setString(1, mblFeatureCode);
stmt.execute();
status = true;
} catch (SQLException e) {
throw new FeatureManagementDAOException("Error occurred while adding feature '" +
code + "' into the metadata repository", e);
throw new org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOException(
"Error occurred while adding feature '" +
mblFeatureCode + "' into the metadata repository", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
@Override
public MobileFeature getFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException {
return null;
}
@Override
public Feature getFeature(String code) throws FeatureManagementDAOException {
public MobileFeature getFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, code);
stmt.setString(1, mblFeatureCode);
rs = stmt.executeQuery();
Feature feature = null;
@ -89,25 +115,35 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setName(rs.getString("NAME"));
feature.setDescription(rs.getString("DESCRIPTION"));
}
return feature;
MobileFeature mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature);
return mobileFeature;
} catch (SQLException e) {
throw new FeatureManagementDAOException("Error occurred while retrieving feature metadata '" +
code + "' from the feature metadata repository", e);
throw new org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOException(
"Error occurred while retrieving feature metadata '" +
mblFeatureCode + "' from the feature metadata repository", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<Feature> getFeatures() throws FeatureManagementDAOException {
public List<MobileFeature> getFeatureByDeviceType(String deviceType)
throws MobileDeviceManagementDAOException {
return null;
}
@Override
public List<MobileFeature> getAllFeatures() throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Feature> features = new ArrayList<Feature>();
List<MobileFeature> features = new ArrayList<MobileFeature>();
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
MobileFeature mobileFeature;
while (rs.next()) {
Feature feature = new Feature();
@ -115,6 +151,8 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setCode(rs.getString("CODE"));
feature.setName(rs.getString("NAME"));
feature.setDescription(rs.getString("DESCRIPTION"));
mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature);
features.add(mobileFeature);
}
return features;
} catch (SQLException e) {
@ -124,5 +162,4 @@ public class FeatureDAOImpl implements FeatureDAO {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
}

@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.IOSDAOFactory;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import java.util.List;
@ -44,8 +45,7 @@ public class IOSDeviceManager implements DeviceManager {
}
public IOSDeviceManager() {
mobileDeviceManagementDAOFactory = new MobileDeviceManagementDAOFactory(DeviceManagementConstants
.MobileDeviceTypes.MOBILE_DEVICE_TYPE_IOS);
mobileDeviceManagementDAOFactory = new IOSDAOFactory();
iosFeatureManager = new IOSFeatureManager();
}

@ -23,69 +23,75 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureDAO;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.IOSDAOFactory;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import java.util.ArrayList;
import java.util.List;
public class IOSFeatureManager implements FeatureManager {
private static final Log log = LogFactory.getLog(IOSFeatureManager.class);
private MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory;
private FeatureDAO featureDAO;
private MobileFeatureDAO featureDAO;
public IOSFeatureManager() {
this.featureDAO = FeatureManagementDAOFactory.getFeatureDAO();
mobileDeviceManagementDAOFactory = new IOSDAOFactory();
this.featureDAO = mobileDeviceManagementDAOFactory.getMobileFeatureDao();
}
@Override
public boolean addFeature(Feature feature) throws DeviceManagementException {
try {
FeatureManagementDAOFactory.beginTransaction();
featureDAO.addFeature(feature);
FeatureManagementDAOFactory.commitTransaction();
mobileDeviceManagementDAOFactory.beginTransaction();
MobileFeature mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature);
try {
featureDAO.addFeature(mobileFeature);
} catch (MobileDeviceManagementDAOException e) {
log.error("error in feature add ", e);
throw new DeviceManagementException("error in feature add", e);
}
mobileDeviceManagementDAOFactory.commitTransaction();
return true;
} catch (FeatureManagementDAOException e) {
} catch (MobileDeviceManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
mobileDeviceManagementDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
throw new DeviceManagementException("Error occurred while adding the feature", e);
throw new DeviceManagementException("DB transaction error occurred while add the feature", e);
}
}
@Override
public Feature getFeature(String name) throws DeviceManagementException {
public Feature getFeature(String code) throws DeviceManagementException {
try {
FeatureManagementDAOFactory.beginTransaction();
Feature feature = featureDAO.getFeature(name);
FeatureManagementDAOFactory.commitTransaction();
MobileFeature mobileFeature = featureDAO.getFeatureByCode(code);
Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature);
return feature;
} catch (FeatureManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
} catch (MobileDeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving the feature", e);
}
}
@Override
public List<Feature> getFeatures() throws DeviceManagementException {
List<Feature> featureList = new ArrayList<Feature>();
try {
FeatureManagementDAOFactory.beginTransaction();
List<Feature> features = featureDAO.getFeatures();
FeatureManagementDAOFactory.commitTransaction();
return features;
} catch (FeatureManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
List<MobileFeature> mobileFeatures = featureDAO.getAllFeatures();
for (MobileFeature mobileFeature : mobileFeatures) {
featureList.add(MobileDeviceManagementUtil.convertToFeature(mobileFeature));
}
return featureList;
} catch (MobileDeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving the list of features registered " +
"for Android platform", e);
}
@ -94,14 +100,14 @@ public class IOSFeatureManager implements FeatureManager {
@Override
public boolean removeFeature(String name) throws DeviceManagementException {
try {
FeatureManagementDAOFactory.beginTransaction();
featureDAO.removeFeature(name);
FeatureManagementDAOFactory.commitTransaction();
mobileDeviceManagementDAOFactory.beginTransaction();
featureDAO.deleteFeatureByCode(name);
mobileDeviceManagementDAOFactory.commitTransaction();
return true;
} catch (FeatureManagementDAOException e) {
} catch (MobileDeviceManagementDAOException e) {
try {
FeatureManagementDAOFactory.rollbackTransaction();
} catch (FeatureManagementDAOException e1) {
mobileDeviceManagementDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
throw new DeviceManagementException("Error occurred while removing the feature", e);

@ -18,7 +18,9 @@
*/
package org.wso2.carbon.device.mgt.mobile.impl.ios.dao;
public class FeatureManagementDAOException extends Exception {
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
public class FeatureManagementDAOException extends MobileDeviceManagementDAOException {
private String message;
private static final long serialVersionUID = 2021891706072918865L;

@ -19,24 +19,43 @@ package org.wso2.carbon.device.mgt.mobile.impl.ios.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.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.dao.*;
import javax.sql.DataSource;
public class IOSDAOFactory {
public class IOSDAOFactory extends MobileDeviceManagementDAOFactory {
private static final Log log = LogFactory.getLog(IOSDAOFactory.class);
private static DataSource dataSource;
private static boolean isInitialized;
private static MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory;
public static void init() {
mobileDeviceManagementDAOFactory = new MobileDeviceManagementDAOFactory(DeviceManagementConstants
.MobileDeviceTypes.MOBILE_DEVICE_TYPE_IOS);
public static void init(MobileDataSourceConfig config) {
dataSource = resolveDataSource(config);
}
@Override
public MobileDeviceDAO getMobileDeviceDAO() {
return null;
}
@Override
public MobileOperationDAO getMobileOperationDAO() {
return null;
}
@Override
public MobileOperationPropertyDAO getMobileOperationPropertyDAO() {
return null;
}
@Override
public MobileDeviceOperationMappingDAO getMobileDeviceOperationDAO() {
return null;
}
@Override
public MobileFeatureDAO getMobileFeatureDao() {
return null;
}
public static DataSource getDataSource() {
return mobileDeviceManagementDAOFactory.getDataSource();
public MobileFeaturePropertyDAO getFeaturePropertyDAO() {
return null;
}
}

@ -18,12 +18,12 @@
*/
package org.wso2.carbon.device.mgt.mobile.impl.ios.dao.impl;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -31,58 +31,79 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class FeatureDAOImpl implements FeatureDAO {
public class FeatureDAOImpl implements MobileFeatureDAO {
@Override
public void addFeature(Feature feature) throws FeatureManagementDAOException {
public boolean addFeature(MobileFeature feature) throws FeatureManagementDAOException {
PreparedStatement stmt = null;
boolean status = false;
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "INSERT INTO IOS_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, feature.getCode());
stmt.setString(2, feature.getName());
stmt.setString(3, feature.getDescription());
stmt.executeUpdate();
status = true;
} catch (SQLException e) {
throw new FeatureManagementDAOException("Error occurred while adding feature '" +
feature.getName() + "' into the metadata repository", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
@Override
public boolean updateFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException {
return false;
}
@Override
public void removeFeature(String code) throws FeatureManagementDAOException {
public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException {
return false;
}
@Override
public boolean deleteFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null;
boolean status = false;
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "DELETE FROM IOS_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, code);
stmt.setString(1, mblFeatureCode);
stmt.execute();
status = true;
} catch (SQLException e) {
throw new FeatureManagementDAOException("Error occurred while adding feature '" +
code + "' into the metadata repository", e);
mblFeatureCode + "' into the metadata repository", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
@Override
public MobileFeature getFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException {
return null;
}
@Override
public Feature getFeature(String code) throws FeatureManagementDAOException {
public MobileFeature getFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM IOS_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, code);
stmt.setString(1, mblFeatureCode);
rs = stmt.executeQuery();
Feature feature = null;
MobileFeature feature = null;
if (rs.next()) {
feature = new Feature();
feature = new MobileFeature();
feature.setId(rs.getInt("ID"));
feature.setCode(rs.getString("CODE"));
feature.setName(rs.getString("NAME"));
@ -91,25 +112,30 @@ public class FeatureDAOImpl implements FeatureDAO {
return feature;
} catch (SQLException e) {
throw new FeatureManagementDAOException("Error occurred while retrieving feature metadata '" +
code + "' from the feature metadata repository", e);
mblFeatureCode + "' from the feature metadata repository", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<Feature> getFeatures() throws FeatureManagementDAOException {
public List<MobileFeature> getFeatureByDeviceType(String deviceType) throws MobileDeviceManagementDAOException {
return null;
}
@Override
public List<MobileFeature> getAllFeatures() throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Feature> features = new ArrayList<Feature>();
List<MobileFeature> features = new ArrayList<MobileFeature>();
try {
Connection conn = FeatureManagementDAOFactory.getConnection();
Connection conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM IOS_FEATURE";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Feature feature = new Feature();
MobileFeature feature = new MobileFeature();
feature.setId(rs.getInt("ID"));
feature.setCode(rs.getString("CODE"));
feature.setName(rs.getString("NAME"));

@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import java.util.List;
@ -37,8 +38,7 @@ public class WindowsDeviceManager implements DeviceManager {
private MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory;
public WindowsDeviceManager() {
mobileDeviceManagementDAOFactory = new MobileDeviceManagementDAOFactory(DeviceManagementConstants
.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS);
mobileDeviceManagementDAOFactory = new WindowsDAOFactory();
}
private static final Log log = LogFactory.getLog(WindowsDeviceManager.class);

@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfi
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.IOSDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManager;
@ -77,8 +78,9 @@ public class MobileDeviceManagementServiceComponent {
Map<String, MobileDataSourceConfig> dsConfigMap =
config.getMobileDeviceMgtRepository().getMobileDataSourceConfigMap();
MobileDeviceManagementDAOFactory.setMobileDataSourceConfigMap(dsConfigMap);
MobileDeviceManagementDAOFactory.init();
IOSDAOFactory.init();
IOSDAOFactory.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_IOS));
AndroidDAOFactory
.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID));
String setupOption = System.getProperty("setup");
if (setupOption != null) {

@ -23,11 +23,9 @@ import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDeviceOperationMapping;
import org.wso2.carbon.device.mgt.mobile.dto.MobileOperation;
import org.wso2.carbon.device.mgt.mobile.dto.MobileOperationProperty;
import org.wso2.carbon.device.mgt.mobile.dto.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -164,4 +162,24 @@ public class MobileDeviceManagementUtil {
operation.setProperties(properties);
return operation;
}
public static MobileFeature convertToMobileFeature(Feature feature) {
MobileFeature mobileFeature = new MobileFeature();
mobileFeature.setName(feature.getName());
mobileFeature.setCode(feature.getCode());
mobileFeature.setDescription(feature.getDescription());
mobileFeature.setDeviceType(feature.getDeviceType());
return mobileFeature;
}
public static Feature convertToFeature(MobileFeature mobileFeature) {
Feature feature = new Feature();
feature.setDescription(mobileFeature.getDescription());
feature.setDeviceType(mobileFeature.getDeviceType());
feature.setCode(mobileFeature.getCode());
feature.setName(mobileFeature.getName());
return feature;
}
}

@ -87,7 +87,8 @@ public class MobileFeatureDAOTestSuite {
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setDeviceType(MBL_FEATURE_DEVICE_TYPE);
int id = mblFeatureDAO.addMobileFeature(mobileFeature);
mblFeatureDAO.addFeature(mobileFeature);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
@ -110,7 +111,7 @@ public class MobileFeatureDAOTestSuite {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
mblFeatureId = testMblFeature.getId();
Assert.assertTrue(id > 0, "MobileFeature has added ");
Assert.assertTrue(mblFeatureId > 0, "MobileFeature has added ");
Assert.assertEquals(MBL_FEATURE_CODE, testMblFeature.getCode(),
"MobileFeature code has persisted ");
Assert.assertEquals(MBL_FEATURE_NAME, testMblFeature.getName(),
@ -125,7 +126,7 @@ public class MobileFeatureDAOTestSuite {
public void getMobileFeatureByCodeTest()
throws MobileDeviceManagementDAOException {
MobileFeature mobileFeature = mblFeatureDAO.getMobileFeatureByCode(MBL_FEATURE_CODE);
MobileFeature mobileFeature = mblFeatureDAO.getFeatureByCode(MBL_FEATURE_CODE);
Assert.assertEquals(MBL_FEATURE_CODE, mobileFeature.getCode(),
"MobileFeature code has retrieved ");
Assert.assertEquals(MBL_FEATURE_NAME, mobileFeature.getName(),
@ -138,7 +139,7 @@ public class MobileFeatureDAOTestSuite {
public void getMobileFeatureByIdTest()
throws MobileDeviceManagementDAOException {
MobileFeature mobileFeature = mblFeatureDAO.getMobileFeatureById(mblFeatureId);
MobileFeature mobileFeature = mblFeatureDAO.getFeatureById(mblFeatureId);
Assert.assertEquals(MBL_FEATURE_CODE, mobileFeature.getCode(),
"MobileFeature code has retrieved ");
Assert.assertEquals(MBL_FEATURE_NAME, mobileFeature.getName(),
@ -151,7 +152,7 @@ public class MobileFeatureDAOTestSuite {
public void getAllMobileFeaturesTest()
throws MobileDeviceManagementDAOException {
List<MobileFeature> mobileFeatures = mblFeatureDAO.getAllMobileFeatures();
List<MobileFeature> mobileFeatures = mblFeatureDAO.getAllFeatures();
Assert.assertNotNull(mobileFeatures, "MobileFeature list is not null");
Assert.assertTrue(mobileFeatures.size() > 0, "MobileFeature list has 1 MobileFeature");
}
@ -170,7 +171,7 @@ public class MobileFeatureDAOTestSuite {
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setId(mblFeatureId);
boolean updated = mblFeatureDAO.updateMobileFeature(mobileFeature);
boolean updated = mblFeatureDAO.updateFeature(mobileFeature);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
@ -204,7 +205,7 @@ public class MobileFeatureDAOTestSuite {
Connection conn = null;
PreparedStatement stmt = null;
boolean status = mblFeatureDAO.deleteMobileFeatureById(mblFeatureId);
boolean status = mblFeatureDAO.deleteFeatureById(mblFeatureId);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query = "SELECT FEATURE_ID, CODE FROM AD_FEATURE WHERE FEATURE_ID = ?";
@ -236,8 +237,8 @@ public class MobileFeatureDAOTestSuite {
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setDeviceType(MBL_FEATURE_DEVICE_TYPE);
mblFeatureDAO.addMobileFeature(mobileFeature);
boolean status = mblFeatureDAO.deleteMobileFeatureByCode(MBL_FEATURE_CODE);
mblFeatureDAO.addFeature(mobileFeature);
boolean status = mblFeatureDAO.deleteFeatureByCode(MBL_FEATURE_CODE);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query = "SELECT FEATURE_ID, CODE FROM AD_FEATURE WHERE CODE = ?";

@ -45,194 +45,197 @@ import java.util.List;
*
*/
public class MobileFeaturePropertyDAOTestSuite {
private static final Log log = LogFactory.getLog(MobileFeaturePropertyDAOTestSuite.class);
public static final String MBL_FEATURE_NAME = "WIFI";
private static final String MBL_FEATURE_CODE = "500A";
public static final String MBL_FEATURE_DESCRIPTION = "Wifi config";
public static final String MBL_FEATURE_DEVICE_TYPE = "Android";
public static final String MBL_FEATURE_PROP_1 = "SSID";
public static final String MBL_FEATURE_PROP_2 = "PASSWORD";
private TestDBConfiguration testDBConfiguration;
private MobileFeatureDAOImpl mblFeatureDAO;
private MobileFeaturePropertyDAOImpl mobileFeaturePropertyDAO;
private int mblFeatureId;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mblFeatureDAO = new MobileFeatureDAOImpl(testDataSource);
mobileFeaturePropertyDAO = new MobileFeaturePropertyDAOImpl(testDataSource);
default:
}
}
@Test
public void addMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
List<MobileFeatureProperty> propertyList = new ArrayList<MobileFeatureProperty>();
//Add a new MobileFeature to the database
MobileFeature mobileFeature = new MobileFeature();
mobileFeature.setCode(MBL_FEATURE_CODE);
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setDeviceType(MBL_FEATURE_DEVICE_TYPE);
mblFeatureId = mblFeatureDAO.addMobileFeature(mobileFeature);
//Add 1st property to the feature
MobileFeatureProperty mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(mblFeatureId);
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_1);
boolean status1 = mobileFeaturePropertyDAO.addMobileFeatureProperty(mobileFeatureProperty);
//Add 2nd property to the feature
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_2);
boolean status2 = mobileFeaturePropertyDAO.addMobileFeatureProperty(mobileFeatureProperty);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT FEATURE_ID, PROPERTY FROM AD_FEATURE_PROPERTY WHERE FEATURE_ID = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, mblFeatureId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(resultSet.getInt(1));
mobileFeatureProperty.setProperty(resultSet.getString(2));
propertyList.add(mobileFeatureProperty);
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Feature data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status1, "MobileFeatureProperty1 has added ");
Assert.assertTrue(status2, "MobileFeatureProperty2 has added ");
Assert.assertTrue(propertyList.size() == 2, "MobileFeatureProperties have retrieved ");
for (MobileFeatureProperty mblFeatureProperty : propertyList) {
Assert.assertNotNull(mblFeatureProperty.getProperty(),
"MobileFeatureProperty property has persisted ");
Assert.assertNotNull(mblFeatureProperty.getFeatureID(),
"MobileFeatureProperty feature-id has persisted ");
}
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest" })
public void getMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
MobileFeatureProperty mobileFeatureProperty =
mobileFeaturePropertyDAO.getMobileFeatureProperty(MBL_FEATURE_PROP_1);
Assert.assertNotNull(mobileFeatureProperty, "MobileFeatureProperty has retrieved ");
Assert.assertEquals(MBL_FEATURE_PROP_1, mobileFeatureProperty.getProperty(),
"MobileFeatureProperty property has retrieved ");
Assert.assertTrue(mblFeatureId == mobileFeatureProperty.getFeatureID(),
"MobileFeatureProperty featureId has retrieved ");
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest" })
public void getFeaturePropertyOfFeatureTest()
throws MobileDeviceManagementDAOException {
List<MobileFeatureProperty> mobileFeatureProperties =
mobileFeaturePropertyDAO.getFeaturePropertiesOfFeature(mblFeatureId);
Assert.assertNotNull(mobileFeatureProperties, "MobileFeatureProperty list has retrieved ");
Assert.assertTrue(mobileFeatureProperties.size() == 2,
"MobileFeatureProperties have fetched ");
for (MobileFeatureProperty mblFeatureProperty : mobileFeatureProperties) {
Assert.assertNotNull(mblFeatureProperty.getProperty(),
"MobileFeatureProperty property has fetched ");
Assert.assertNotNull(mblFeatureProperty.getFeatureID(),
"MobileFeatureProperty feature-id has fetched ");
}
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" }, expectedExceptions = MobileDeviceManagementDAOException.class)
public void updateMobileFeaturePropertyTest() throws MobileDeviceManagementDAOException {
//Update 1st property to a non-exist feature
MobileFeatureProperty mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(2);
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_1);
mobileFeaturePropertyDAO.updateMobileFeatureProperty(mobileFeatureProperty);
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" })
public void deleteMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mobileFeaturePropertyDAO.deleteMobileFeatureProperty(MBL_FEATURE_PROP_2);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT PROPERTY, FEATURE_ID FROM AD_FEATURE_PROPERTY WHERE PROPERTY = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, MBL_FEATURE_PROP_2);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileFeatureProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileFeatureProperty has deleted ");
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" , "updateMobileFeaturePropertyTest",
"deleteMobileFeaturePropertyTest"})
public void deleteMobileFeaturePropertiesOfFeatureTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mobileFeaturePropertyDAO.deleteMobileFeaturePropertiesOfFeature(mblFeatureId);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT PROPERTY, FEATURE_ID FROM AD_FEATURE_PROPERTY WHERE FEATURE_ID = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, mblFeatureId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileFeatureProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileFeatureProperties has deleted ");
}
private static final Log log = LogFactory.getLog(MobileFeaturePropertyDAOTestSuite.class);
public static final String MBL_FEATURE_NAME = "WIFI";
private static final String MBL_FEATURE_CODE = "500A";
public static final String MBL_FEATURE_DESCRIPTION = "Wifi config";
public static final String MBL_FEATURE_DEVICE_TYPE = "Android";
public static final String MBL_FEATURE_PROP_1 = "SSID";
public static final String MBL_FEATURE_PROP_2 = "PASSWORD";
private TestDBConfiguration testDBConfiguration;
private MobileFeatureDAOImpl mblFeatureDAO;
private MobileFeaturePropertyDAOImpl mobileFeaturePropertyDAO;
private int mblFeatureId;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mblFeatureDAO = new MobileFeatureDAOImpl(testDataSource);
mobileFeaturePropertyDAO = new MobileFeaturePropertyDAOImpl(testDataSource);
default:
}
}
@Test
public void addMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
List<MobileFeatureProperty> propertyList = new ArrayList<MobileFeatureProperty>();
//Add a new MobileFeature to the database
MobileFeature mobileFeature = new MobileFeature();
mobileFeature.setCode(MBL_FEATURE_CODE);
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setDeviceType(MBL_FEATURE_DEVICE_TYPE);
mblFeatureDAO.addFeature(mobileFeature);
MobileFeature persistMblFeature = mblFeatureDAO.getFeatureByCode(MBL_FEATURE_CODE);
mblFeatureId = persistMblFeature.getId();
//Add 1st property to the feature
MobileFeatureProperty mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(mblFeatureId);
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_1);
boolean status1 = mobileFeaturePropertyDAO.addMobileFeatureProperty(mobileFeatureProperty);
//Add 2nd property to the feature
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_2);
boolean status2 = mobileFeaturePropertyDAO.addMobileFeatureProperty(mobileFeatureProperty);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT FEATURE_ID, PROPERTY FROM AD_FEATURE_PROPERTY WHERE FEATURE_ID = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, mblFeatureId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(resultSet.getInt(1));
mobileFeatureProperty.setProperty(resultSet.getString(2));
propertyList.add(mobileFeatureProperty);
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Feature data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status1, "MobileFeatureProperty1 has added ");
Assert.assertTrue(status2, "MobileFeatureProperty2 has added ");
Assert.assertTrue(propertyList.size() == 2, "MobileFeatureProperties have retrieved ");
for (MobileFeatureProperty mblFeatureProperty : propertyList) {
Assert.assertNotNull(mblFeatureProperty.getProperty(),
"MobileFeatureProperty property has persisted ");
Assert.assertNotNull(mblFeatureProperty.getFeatureID(),
"MobileFeatureProperty feature-id has persisted ");
}
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest" })
public void getMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
MobileFeatureProperty mobileFeatureProperty =
mobileFeaturePropertyDAO.getMobileFeatureProperty(MBL_FEATURE_PROP_1);
Assert.assertNotNull(mobileFeatureProperty, "MobileFeatureProperty has retrieved ");
Assert.assertEquals(MBL_FEATURE_PROP_1, mobileFeatureProperty.getProperty(),
"MobileFeatureProperty property has retrieved ");
Assert.assertTrue(mblFeatureId == mobileFeatureProperty.getFeatureID(),
"MobileFeatureProperty featureId has retrieved ");
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest" })
public void getFeaturePropertyOfFeatureTest()
throws MobileDeviceManagementDAOException {
List<MobileFeatureProperty> mobileFeatureProperties =
mobileFeaturePropertyDAO.getFeaturePropertiesOfFeature(mblFeatureId);
Assert.assertNotNull(mobileFeatureProperties, "MobileFeatureProperty list has retrieved ");
Assert.assertTrue(mobileFeatureProperties.size() == 2,
"MobileFeatureProperties have fetched ");
for (MobileFeatureProperty mblFeatureProperty : mobileFeatureProperties) {
Assert.assertNotNull(mblFeatureProperty.getProperty(),
"MobileFeatureProperty property has fetched ");
Assert.assertNotNull(mblFeatureProperty.getFeatureID(),
"MobileFeatureProperty feature-id has fetched ");
}
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" }, expectedExceptions = MobileDeviceManagementDAOException.class)
public void updateMobileFeaturePropertyTest() throws MobileDeviceManagementDAOException {
//Update 1st property to a non-exist feature
MobileFeatureProperty mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(2);
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_1);
mobileFeaturePropertyDAO.updateMobileFeatureProperty(mobileFeatureProperty);
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" })
public void deleteMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mobileFeaturePropertyDAO.deleteMobileFeatureProperty(MBL_FEATURE_PROP_2);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT PROPERTY, FEATURE_ID FROM AD_FEATURE_PROPERTY WHERE PROPERTY = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, MBL_FEATURE_PROP_2);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileFeatureProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileFeatureProperty has deleted ");
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest", "updateMobileFeaturePropertyTest",
"deleteMobileFeaturePropertyTest" })
public void deleteMobileFeaturePropertiesOfFeatureTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mobileFeaturePropertyDAO.deleteMobileFeaturePropertiesOfFeature(mblFeatureId);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT PROPERTY, FEATURE_ID FROM AD_FEATURE_PROPERTY WHERE FEATURE_ID = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, mblFeatureId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileFeatureProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileFeatureProperties has deleted ");
}
}

Loading…
Cancel
Save