From ad10426c110ddde266893d2e07dfdfa3e9bcc21f Mon Sep 17 00:00:00 2001 From: harshanL Date: Wed, 17 Dec 2014 15:43:31 +0530 Subject: [PATCH 1/6] Added mobile device management service with db schema --- .../android/AndroidDeviceManagerService.java | 86 +++++++- .../MobileDeviceConfigurationManager.java | 4 +- .../mgt/mobile/impl/dao/MobileDeviceDAO.java | 6 +- .../impl/dao/MobileDeviceDAOFactory.java | 57 ----- .../dao/MobileDeviceManagementDAOFactory.java | 82 +++---- .../mobile/impl/dao/MobileDeviceModelDAO.java | 34 --- .../impl/dao/MobileDeviceVendorDAO.java | 35 --- .../mobile/impl/dao/MobileOSVersionDAO.java | 34 --- .../impl/dao/impl/MobileDeviceDAOImpl.java | 131 ++++++++++- .../dao/impl/MobileDeviceModelDAOImpl.java | 62 ------ .../dao/impl/MobileDeviceVendorDAOImpl.java | 62 ------ .../impl/dao/impl/MobileOSVersionDAOImpl.java | 58 ----- .../util/MobileDeviceManagementDAOUtil.java | 128 +++++++++-- .../mgt/mobile/impl/dto/MobileDevice.java | 30 +-- .../mobile/impl/dto/MobileDeviceModel.java | 44 ---- .../mobile/impl/dto/MobileDeviceVendor.java | 44 ---- .../mgt/mobile/impl/dto/MobileOSVersion.java | 44 ---- ...MobileDeviceManagementBundleActivator.java | 206 ++++++++++-------- ...bileDeviceManagementSchemaInitializer.java | 8 +- .../impl/util/MobileDeviceManagementUtil.java | 109 +++++++++ .../impl/util/MobileDeviceManagerUtil.java | 91 -------- .../cdm/api/android/common/ErrorHandler.java | 2 +- .../repository/dbscripts/cdm/plugins/h2.sql | 35 +-- .../dbscripts/cdm/plugins/mysql.sql | 56 +---- 24 files changed, 587 insertions(+), 861 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java index 805345e743..78791808eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java @@ -16,11 +16,17 @@ package org.wso2.carbon.device.mgt.mobile.impl.android; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; +import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementUtil; import java.util.List; @@ -29,6 +35,8 @@ import java.util.List; */ public class AndroidDeviceManagerService implements DeviceManagerService { + private static final Log log = LogFactory.getLog(AndroidDeviceManagerService.class); + @Override public String getProviderType() { return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID; @@ -36,22 +44,64 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - return true; + boolean status = false; + MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); + try { + status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().addDevice(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 { - return true; + boolean status = false; + MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); + try { + status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().updateDevice(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 { - return true; + boolean status = false; + try { + status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().deleteDevice(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 { - return true; + boolean isEnrolled = false; + try { + MobileDevice mobileDevice = + MobileDeviceManagementDAOFactory.getMobileDeviceDAO().getDevice( + 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 @@ -72,7 +122,17 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return null; + Device device = null; + try { + MobileDevice mobileDevice = MobileDeviceManagementDAOFactory.getMobileDeviceDAO(). + getDevice(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 @@ -83,9 +143,15 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - return true; + boolean status = false; + MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); + try { + status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().updateDevice(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; } - - //should implement equals and hashcode in all service bundles - -} +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java index 1a24811996..3b69a71e6e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java @@ -18,7 +18,7 @@ package org.wso2.carbon.device.mgt.mobile.impl.config; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagerUtil; +import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementUtil; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.utils.CarbonUtils; @@ -53,7 +53,7 @@ public class MobileDeviceConfigurationManager { public synchronized void initConfig() throws DeviceManagementException { try { File mobileDeviceMgtConfig = new File(mobileDeviceMgtConfigXMLPath); - Document doc = MobileDeviceManagerUtil.convertToDocument(mobileDeviceMgtConfig); + Document doc = MobileDeviceManagementUtil.convertToDocument(mobileDeviceMgtConfig); JAXBContext mobileDeviceMgmtContext = JAXBContext.newInstance(MobileDeviceManagementConfig.class); Unmarshaller unmarshaller = mobileDeviceMgmtContext.createUnmarshaller(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java index 16330df10c..8cba1f8a07 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java @@ -26,10 +26,10 @@ public interface MobileDeviceDAO { MobileDevice getDevice(String deviceId) throws MobileDeviceManagementDAOException; - void addDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException; + boolean addDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException; - void updateDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException; + boolean updateDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException; - void deleteDevice(String deviceId) throws MobileDeviceManagementDAOException; + boolean deleteDevice(String deviceId) throws MobileDeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java deleted file mode 100644 index fabb81d8d3..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dao; - -import org.wso2.carbon.device.mgt.mobile.impl.DataSourceListener; -import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceDAOImpl; -import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceModelDAOImpl; -import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceVendorDAOImpl; -import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileOSVersionDAOImpl; -import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil; -import org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementBundleActivator; - -import javax.sql.DataSource; - -public class MobileDeviceDAOFactory implements DataSourceListener { - - private static DataSource dataSource; - - public MobileDeviceDAOFactory() { - MobileDeviceManagementBundleActivator.registerDataSourceListener(this); - } - - @Override - public void notifyObserver() { - dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(); - } - - public static MobileDeviceDAO getMobileDeviceDAO() { - return new MobileDeviceDAOImpl(dataSource); - } - - public static MobileDeviceModelDAO getMobileDeviceModelDAO() { - return new MobileDeviceModelDAOImpl(dataSource); - } - - public static MobileDeviceVendorDAO getMobileDeviceVendorDAO() { - return new MobileDeviceVendorDAOImpl(dataSource); - } - - public static MobileOSVersionDAO getMobileOSVersionDAO() { - return new MobileOSVersionDAOImpl(dataSource); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java index 65750447d5..c7bc7922a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java @@ -18,87 +18,59 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.mobile.impl.DataSourceListener; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; -import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceDAOImpl; -import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceModelDAOImpl; -import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceVendorDAOImpl; -import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileOSVersionDAOImpl; import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementBundleActivator; import javax.sql.DataSource; -import java.util.Hashtable; -import java.util.List; /** * Factory class used to create MobileDeviceManagement related DAO objects. */ -public class MobileDeviceManagementDAOFactory { +public class MobileDeviceManagementDAOFactory implements DataSourceListener { private static DataSource dataSource; + private static MobileDataSourceConfig mobileDataSourceConfig; private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); - public static MobileDeviceDAO getMobileDeviceDAO() { - return new MobileDeviceDAOImpl(dataSource); - } + public MobileDeviceManagementDAOFactory() { - public static MobileDeviceModelDAO getMobileDeviceModelDAO() { - return new MobileDeviceModelDAOImpl(dataSource); } - public static MobileDeviceVendorDAO getMobileDeviceVendorDAO() { - return new MobileDeviceVendorDAOImpl(dataSource); + public void init(){ + dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); + if(dataSource!=null){ + MobileDeviceManagementDAOUtil.createDataSource(dataSource); + }else{ + MobileDeviceManagementBundleActivator.registerDataSourceListener(this); + } } - public static MobileOSVersionDAO getMobileOSVersionDAO() { - return new MobileOSVersionDAOImpl(dataSource); + public static MobileDeviceDAO getMobileDeviceDAO() { + return new MobileDeviceDAOImpl(dataSource); } - public static void init(MobileDataSourceConfig config) { - dataSource = resolveDataSource(config); + public static MobileDataSourceConfig getMobileDeviceManagementConfig() { + return mobileDataSourceConfig; } - public static void init(DataSource dtSource) { - dataSource = dtSource; + public static void setMobileDataSourceConfig( + MobileDataSourceConfig mobileDataSourceConfig) { + MobileDeviceManagementDAOFactory.mobileDataSourceConfig = + mobileDataSourceConfig; } - /** - * Resolve data source from the data source definition - * - * @param config data source configuration - * @return data source resolved from the data source definition - */ - private static DataSource resolveDataSource(MobileDataSourceConfig config) { - DataSource dataSource = null; - if (config == null) { - throw new RuntimeException("Device Management Repository data source configuration " + - "is null and thus, is not initialized"); - } - JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion(); - if (jndiConfig != null) { - if (log.isDebugEnabled()) { - log.debug("Initializing Device Management Repository data source using the JNDI " + - "Lookup Definition"); - } - List jndiPropertyList = - jndiConfig.getJndiProperties(); - if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); - for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { - jndiProperties.put(prop.getName(), prop.getValue()); - } - dataSource = - MobileDeviceManagementDAOUtil - .lookupDataSource(jndiConfig.getJndiName(), jndiProperties); - } else { - dataSource = MobileDeviceManagementDAOUtil - .lookupDataSource(jndiConfig.getJndiName(), null); - } - } + public static DataSource getDataSource() { return dataSource; } - public static DataSource getDataSource() { - return dataSource; + @Override + public void notifyObserver() { + dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); + if(dataSource!=null){ + MobileDeviceManagementDAOUtil.createDataSource(dataSource); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java deleted file mode 100644 index 768f297231..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dao; - -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceModel; - -/** - * This class represents the key operations associated with persisting mobile-device model related - * information. - */ -public interface MobileDeviceModelDAO { - - MobileDeviceModel getDeviceModel(String modelId) throws MobileDeviceManagementDAOException; - - void addDeviceModel(MobileDeviceModel deviceModel) throws MobileDeviceManagementDAOException; - - void updateDeviceModel(MobileDeviceModel deviceModel) throws MobileDeviceManagementDAOException; - - void deleteDeviceModel(String modelId) throws MobileDeviceManagementDAOException; -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java deleted file mode 100644 index 4a4d24bf7e..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dao; - -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceVendor; - -/** - * This class represents the key operations associated with persisting mobile-device vendor - * related information. - */ -public interface MobileDeviceVendorDAO { - - MobileDeviceVendor getDeviceModel(String vendorId) throws MobileDeviceManagementDAOException; - - void addDeviceVendor(MobileDeviceVendor deviceVendor) throws MobileDeviceManagementDAOException; - - void updateDeviceVendor(MobileDeviceVendor deviceVendor) - throws MobileDeviceManagementDAOException; - - void deleteDeviceVendor(String vendorId) throws MobileDeviceManagementDAOException; -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java deleted file mode 100644 index dcffc528b6..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dao; - -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileOSVersion; - -/** - * This class represents the key operations associated with persisting mobile-device OS version - * related information. - */ -public interface MobileOSVersionDAO { - - MobileOSVersion getMobileOSVersion(String versionId) throws MobileDeviceManagementDAOException; - - void addMobileOSVersion(MobileOSVersion osVersion) throws MobileDeviceManagementDAOException; - - void updateMobileOSVersion(MobileOSVersion osVersion) throws MobileDeviceManagementDAOException; - - void deleteMobileOSVersion(String versionId) throws MobileDeviceManagementDAOException; -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java index d0ab883d69..42164e2c3d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java @@ -20,9 +20,14 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAO; import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; /** * Implementation of MobileDeviceDAO. @@ -38,23 +43,139 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { @Override public MobileDevice getDevice(String deviceId) throws MobileDeviceManagementDAOException { - return null; + Connection conn = null; + PreparedStatement stmt = null; + MobileDevice mobileDevice = null; + try { + conn = this.getConnection(); + String createDBQuery = + "SELECT * FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, deviceId); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + mobileDevice.setMobileDeviceId(resultSet.getString(0)); + mobileDevice.setRegId(resultSet.getString(1)); + mobileDevice.setImei(resultSet.getString(2)); + mobileDevice.setImsi(resultSet.getString(3)); + mobileDevice.setOsVersion(resultSet.getString(4)); + mobileDevice.setModel(resultSet.getString(5)); + mobileDevice.setVendor(resultSet.getString(6)); + break; + } + } catch (SQLException e) { + String msg = "Error occurred while fetching mobile device '" + + deviceId + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return mobileDevice; } @Override - public void addDevice(MobileDevice mobileDevice) + public boolean addDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "INSERT INTO MBL_DEVICE(MOBILE_DEVICE_ID, REG_ID, IMEI, IMSI, OS_VERSION," + + "DEVICE_MODEL, VENDOR) VALUES (?, ?, ?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, mobileDevice.getMobileDeviceId()); + stmt.setString(2, mobileDevice.getRegId()); + stmt.setString(3, mobileDevice.getImei()); + stmt.setString(4, mobileDevice.getImsi()); + stmt.setString(5, mobileDevice.getOsVersion()); + stmt.setString(6, mobileDevice.getModel()); + stmt.setString(7, mobileDevice.getVendor()); + int rows = stmt.executeUpdate(); + if(rows>0){ + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while enrolling mobile device '" + + mobileDevice.getMobileDeviceId() + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; } @Override - public void updateDevice(MobileDevice mobileDevice) + public boolean updateDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException { - + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "UPDATE MBL_DEVICE SET REG_ID = ?, IMEI = ?, IMSI = ?, OS_VERSION = ?," + + "DEVICE_MODEL = ?, VENDOR = ? WHERE MOBILE_DEVICE_ID = ?"; + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, mobileDevice.getRegId()); + stmt.setString(2, mobileDevice.getImei()); + stmt.setString(3, mobileDevice.getImsi()); + stmt.setString(4, mobileDevice.getOsVersion()); + stmt.setString(5, mobileDevice.getModel()); + stmt.setString(6, mobileDevice.getVendor()); + stmt.setString(7, mobileDevice.getMobileDeviceId()); + int rows = stmt.executeUpdate(); + if(rows>0){ + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while updating the mobile device '" + + mobileDevice.getMobileDeviceId() + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; } @Override - public void deleteDevice(String deviceId) throws MobileDeviceManagementDAOException { + public boolean deleteDevice(String deviceId) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1,deviceId); + int rows = stmt.executeUpdate(); + if(rows>0){ + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while deleting mobile device " + deviceId; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + private Connection getConnection() throws MobileDeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + String msg = "Error occurred while obtaining a connection from the mobile device " + + "management metadata repository datasource"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java deleted file mode 100644 index 04dbd9c654..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dao.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceModelDAO; -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceModel; - -import javax.sql.DataSource; - -/** - * Implementation of MobileDeviceModel. - */ -public class MobileDeviceModelDAOImpl implements MobileDeviceModelDAO { - - private DataSource dataSource; - private static final Log log = LogFactory.getLog(MobileDeviceModelDAOImpl.class); - - public MobileDeviceModelDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; - } - - @Override - public MobileDeviceModel getDeviceModel(String modelId) - throws MobileDeviceManagementDAOException { - return null; - } - - @Override - public void addDeviceModel(MobileDeviceModel deviceModel) - throws MobileDeviceManagementDAOException { - - } - - @Override - public void updateDeviceModel(MobileDeviceModel deviceModel) - throws MobileDeviceManagementDAOException { - - } - - @Override - public void deleteDeviceModel(String modelId) - throws MobileDeviceManagementDAOException { - - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java deleted file mode 100644 index 3908ef3ba0..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dao.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceVendorDAO; -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceVendor; - -import javax.sql.DataSource; - -/** - * Implementation of MobileDeviceVendorDAO. - */ -public class MobileDeviceVendorDAOImpl implements MobileDeviceVendorDAO { - - private DataSource dataSource; - private static final Log log = LogFactory.getLog(MobileDeviceVendorDAOImpl.class); - - public MobileDeviceVendorDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; - } - - @Override - public MobileDeviceVendor getDeviceModel(String vendorId) - throws MobileDeviceManagementDAOException { - return null; - } - - @Override - public void addDeviceVendor(MobileDeviceVendor deviceVendor) - throws MobileDeviceManagementDAOException { - - } - - @Override - public void updateDeviceVendor(MobileDeviceVendor deviceVendor) - throws MobileDeviceManagementDAOException { - - } - - @Override - public void deleteDeviceVendor(String vendorId) - throws MobileDeviceManagementDAOException { - - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java deleted file mode 100644 index 4296ec974a..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dao.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileOSVersionDAO; -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileOSVersion; - -import javax.sql.DataSource; - -/** - * Implementation of MobileOSVersionDAO. - */ -public class MobileOSVersionDAOImpl implements MobileOSVersionDAO { - - private DataSource dataSource; - private static final Log log = LogFactory.getLog(MobileOSVersionDAOImpl.class); - - public MobileOSVersionDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; - } - - @Override public MobileOSVersion getMobileOSVersion(String versionId) - throws MobileDeviceManagementDAOException { - return null; - } - - @Override public void addMobileOSVersion(MobileOSVersion osVersion) - throws MobileDeviceManagementDAOException { - - } - - @Override public void updateMobileOSVersion(MobileOSVersion osVersion) - throws MobileDeviceManagementDAOException { - - } - - @Override public void deleteMobileOSVersion(String versionId) - throws MobileDeviceManagementDAOException { - - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java index bc210f8ffa..104de0fd72 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java @@ -16,14 +16,12 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao.util; - -import javax.sql.DataSource; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.JNDILookupDefinition; +import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; +import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementSchemaInitializer; import javax.naming.InitialContext; import javax.sql.DataSource; @@ -32,18 +30,79 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Hashtable; +import java.util.List; /** * Utility method required by MobileDeviceManagement DAO classes. */ public class MobileDeviceManagementDAOUtil { - public static DataSource resolveDataSource() { - return null; - } - private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOUtil.class); + /** + * Resolve data source from the data source definition + * + * @param config Mobile data source configuration + * @return data source resolved from the data source definition + */ + public static DataSource resolveDataSource(MobileDataSourceConfig config) { + DataSource dataSource = null; + if (config == null) { + throw new RuntimeException("Device Management Repository data source configuration " + + "is null and thus, is not initialized"); + } + JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion(); + if (jndiConfig != null) { + if (log.isDebugEnabled()) { + log.debug("Initializing Device Management Repository data source using the JNDI " + + "Lookup Definition"); + } + List jndiPropertyList = + jndiConfig.getJndiProperties(); + if (jndiPropertyList != null) { + Hashtable jndiProperties = new Hashtable(); + for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { + jndiProperties.put(prop.getName(), prop.getValue()); + } + try { + dataSource = MobileDeviceManagementDAOUtil.lookupDataSource( + jndiConfig.getJndiName(), jndiProperties); + } catch (DeviceManagementException e) { + if (log.isDebugEnabled()) { + log.debug("Error in looking up data source: " + e.getMessage()); + } + log.error(e); + } + } else { + try { + dataSource = MobileDeviceManagementDAOUtil.lookupDataSource( + jndiConfig.getJndiName(), null); + } catch (DeviceManagementException e) { + if (log.isDebugEnabled()) { + log.debug("Error in looking up data source: " + e.getMessage()); + } + log.error(e); + } + } + } + return dataSource; + } + + public static DataSource lookupDataSource(String dataSourceName, + final Hashtable jndiProperties) + throws DeviceManagementException { + try { + if (jndiProperties == null || jndiProperties.isEmpty()) { + return (DataSource) InitialContext.doLookup(dataSourceName); + } + final InitialContext context = new InitialContext(jndiProperties); + return (DataSource) context.doLookup(dataSourceName); + } catch (Exception e) { + throw new DeviceManagementException( + "Error in looking up data source: " + e.getMessage(), e); + } + } + public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { if (rs != null) { try { @@ -68,23 +127,46 @@ public class MobileDeviceManagementDAOUtil { } } - public static DataSource lookupDataSource(String dataSourceName, - final Hashtable jndiProperties) { - try { - if (jndiProperties == null || jndiProperties.isEmpty()) { - return (DataSource) InitialContext.doLookup(dataSourceName); + /** + * Initializes the creation of mobile device management schema if -Dsetup has provided + * + * @param dataSource Mobile data source + */ + public static void createDataSource(DataSource dataSource) { + String setupOption = System.getProperty("setup"); + if (setupOption != null) { + if (log.isDebugEnabled()) { + log.debug( + "-Dsetup is enabled. Mobile Device management repository schema initialization is about " + + "to begin"); + } + try { + MobileDeviceManagementDAOUtil.setupMobileDeviceManagementSchema(dataSource); + } catch (DeviceManagementException e) { + log.error( + "Exception occurred while initializing mobile device management database schema", + e); } - final InitialContext context = new InitialContext(jndiProperties); - return (DataSource) context.doLookup(dataSourceName); - } catch (Exception e) { - throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); } } - public static MobileDevice convertToMobileDevice(Device device) - throws MobileDeviceManagementDAOException { - MobileDevice mobileDeviceBO = new MobileDevice(); - return mobileDeviceBO; + /** + * Creates the mobile device management schema + * + * @param dataSource Mobile data source + */ + public static void setupMobileDeviceManagementSchema(DataSource dataSource) throws + DeviceManagementException { + MobileDeviceManagementSchemaInitializer initializer = + new MobileDeviceManagementSchemaInitializer(dataSource); + log.info("Initializing mobile device management repository database schema"); + try { + initializer.createRegistryDatabase(); + } catch (Exception e) { + throw new DeviceManagementException( + "Error occurred while initializing Mobile Device Management " + + "database schema", e); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java index f7b1d14b1f..f054b2bf5f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java @@ -27,9 +27,9 @@ public class MobileDevice implements Serializable { private String regId; private String imei; private String imsi; - private int osVersionId; - private int modelId; - private int vendorId; + private String osVersion; + private String model; + private String vendor; public String getMobileDeviceId() { return mobileDeviceId; @@ -63,27 +63,27 @@ public class MobileDevice implements Serializable { this.imsi = imsi; } - public int getOsVersionId() { - return osVersionId; + public String getOsVersion() { + return osVersion; } - public void setOsVersionId(int osVersionId) { - this.osVersionId = osVersionId; + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; } - public int getModelId() { - return modelId; + public String getModel() { + return model; } - public void setModelId(int modelId) { - this.modelId = modelId; + public void setModel(String model) { + this.model = model; } - public int getVendorId() { - return vendorId; + public String getVendor() { + return vendor; } - public void setVendorId(int vendorId) { - this.vendorId = vendorId; + public void setVendor(String vendor) { + this.vendor = vendor; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java deleted file mode 100644 index b5d5e5ebbb..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dto; - -import java.io.Serializable; - -/** - * DTO of MobileDeviceModel. - */ -public class MobileDeviceModel implements Serializable { - - private int modelId; - private String model; - - public int getModelId() { - return modelId; - } - - public void setModelId(int modelId) { - this.modelId = modelId; - } - - public String getModel() { - return model; - } - - public void setModel(String model) { - this.model = model; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java deleted file mode 100644 index 2d48af52a0..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dto; - -import java.io.Serializable; - -/** - * DTO of MobileVendor. - */ -public class MobileDeviceVendor implements Serializable { - - private int vendorId; - private String vendor; - - public int getVendorId() { - return vendorId; - } - - public void setVendorId(int vendorId) { - this.vendorId = vendorId; - } - - public String getVendor() { - return vendor; - } - - public void setVendor(String vendor) { - this.vendor = vendor; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java deleted file mode 100644 index 32a56b6fcc..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.dto; - -import java.io.Serializable; - -/** - * DTO of MobileOSVersion. - */ -public class MobileOSVersion implements Serializable { - - private int osVersionId; - private String osVersion; - - public int getOsVersionId() { - return osVersionId; - } - - public void setOsVersionId(int osVersionId) { - this.osVersionId = osVersionId; - } - - public String getOsVersion() { - return osVersion; - } - - public void setOsVersion(String osVersion) { - this.osVersion = osVersion; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java index 9d3e08cadd..d544e2fa65 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java @@ -19,14 +19,14 @@ package org.wso2.carbon.device.mgt.mobile.impl.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.*; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; import org.wso2.carbon.device.mgt.mobile.impl.DataSourceListener; import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManagerService; +import org.wso2.carbon.device.mgt.mobile.impl.config.MobileDeviceConfigurationManager; +import org.wso2.carbon.device.mgt.mobile.impl.config.MobileDeviceManagementConfig; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAOFactory; +import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManagerService; -import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementSchemaInitializer; import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagerService; import java.util.ArrayList; @@ -34,97 +34,113 @@ import java.util.List; public class MobileDeviceManagementBundleActivator implements BundleActivator, BundleListener { - private ServiceRegistration androidServiceRegRef; - private ServiceRegistration iOSServiceRegRef; - private ServiceRegistration windowsServiceRegRef; - private static List dataSourceListeners = new ArrayList(); - - private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class); - private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT = "org.wso2.carbon.ndatasource.core"; - - @Override - public void start(BundleContext bundleContext) throws Exception { - try { - if (log.isDebugEnabled()) { - log.debug("Activating Mobile Device Management Service bundle"); - } - bundleContext.addBundleListener(this); - - /* If -Dsetup option enabled then create device management database schema */ - String setupOption = System.getProperty("setup"); - if (setupOption != null) { - if (log.isDebugEnabled()) { - log.debug( - "-Dsetup is enabled. Mobile Device management repository schema initialization is about " + - "to begin"); - } - setupMobileDeviceManagementSchema(null); - } - - MobileDeviceDAOFactory daoFactory = new MobileDeviceDAOFactory(); - //TODO Register this dao to an appropriate config file - - androidServiceRegRef = - bundleContext.registerService(DeviceManagerService.class.getName(), - new AndroidDeviceManagerService(), null); - iOSServiceRegRef = - bundleContext.registerService(DeviceManagerService.class.getName(), - new IOSDeviceManagerService(), null); - windowsServiceRegRef = - bundleContext.registerService(DeviceManagerService.class.getName(), - new WindowsDeviceManagerService(), null); - if (log.isDebugEnabled()) { - log.debug("Mobile Device Management Service bundle is activated"); - } - } catch (Throwable e) { - log.error("Error occurred while activating Mobile Device Management Service Component", e); - } - } - - @Override - public void stop(BundleContext bundleContext) throws Exception { - if (log.isDebugEnabled()) { - log.debug("Deactivating Mobile Device Management Service"); - } - androidServiceRegRef.unregister(); - iOSServiceRegRef.unregister(); - windowsServiceRegRef.unregister(); - - bundleContext.removeBundleListener(this); - } - - @Override - public void bundleChanged(BundleEvent bundleEvent) { - int eventType = bundleEvent.getType(); - String bundleSymbolicName = bundleEvent.getBundle().getSymbolicName(); - if (SYMBOLIC_NAME_DATA_SOURCE_COMPONENT.equals(bundleSymbolicName) && eventType == BundleEvent.STARTED) { - for (DataSourceListener listener : this.getDataSourceListeners()) { - listener.notifyObserver(); - } - } - } - - public static void registerDataSourceListener(DataSourceListener listener) { - dataSourceListeners.add(listener); - } - - private List getDataSourceListeners() { - return dataSourceListeners; - } - - private void setupMobileDeviceManagementSchema(MobileDataSourceConfig config) throws - DeviceManagementException { - MobileDeviceManagementSchemaInitializer initializer = - new MobileDeviceManagementSchemaInitializer(config); - log.info("Initializing mobile device management repository database schema"); - try { - //initializer.createRegistryDatabase(); - } catch (Exception e) { - throw new DeviceManagementException( - "Error occurred while initializing Mobile Device Management " + - "database schema", e); - } - } - + private ServiceRegistration androidServiceRegRef; + private ServiceRegistration iOSServiceRegRef; + private ServiceRegistration windowsServiceRegRef; + private static List dataSourceListeners = + new ArrayList(); + + private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class); + private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT = + "org.eclipse.osgi"; + + @Override + public void start(BundleContext bundleContext) throws Exception { + try { + if (log.isDebugEnabled()) { + log.debug("Activating Mobile Device Management Service bundle"); + } + bundleContext.addBundleListener(this); + /* Initialize the datasource configuration */ + MobileDeviceConfigurationManager.getInstance().initConfig(); + MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() + .getMobileDeviceManagementConfig(); + MobileDataSourceConfig dsConfig = + config.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); + + MobileDeviceManagementDAOFactory daoFactory = new MobileDeviceManagementDAOFactory(); + daoFactory.setMobileDataSourceConfig(dsConfig); + daoFactory.init(); + + androidServiceRegRef = + bundleContext.registerService(DeviceManagerService.class.getName(), + new AndroidDeviceManagerService(), null); + iOSServiceRegRef = + bundleContext.registerService(DeviceManagerService.class.getName(), + new IOSDeviceManagerService(), null); + windowsServiceRegRef = + bundleContext.registerService(DeviceManagerService.class.getName(), + new WindowsDeviceManagerService(), null); + if (log.isDebugEnabled()) { + log.debug("Mobile Device Management Service bundle is activated"); + } + } catch (Throwable e) { + log.error("Error occurred while activating Mobile Device Management Service Component", + e); + } + } + + @Override + public void stop(BundleContext bundleContext) throws Exception { + if (log.isDebugEnabled()) { + log.debug("Deactivating Mobile Device Management Service"); + } + androidServiceRegRef.unregister(); + iOSServiceRegRef.unregister(); + windowsServiceRegRef.unregister(); + bundleContext.removeBundleListener(this); + } + + @Override + public void bundleChanged(BundleEvent bundleEvent) { + int eventType = bundleEvent.getType(); + String bundleSymbolicName = bundleEvent.getBundle().getSymbolicName(); +// log.info("Received Event bundleSymbolicName " + bundleSymbolicName); +// log.info("Received Event :::::::::::::::: " + typeAsString(bundleEvent)); + + if (SYMBOLIC_NAME_DATA_SOURCE_COMPONENT.equals(bundleSymbolicName) && + eventType == BundleEvent.STARTED) { + for (DataSourceListener listener : this.getDataSourceListeners()) { + listener.notifyObserver(); + } + } + } + + public static void registerDataSourceListener(DataSourceListener listener) { + dataSourceListeners.add(listener); + } + + private List getDataSourceListeners() { + return dataSourceListeners; + } + + private static String typeAsString(BundleEvent event) { + if (event == null) { + return "null"; + } + int type = event.getType(); + switch (type) { + case BundleEvent.INSTALLED: + return "INSTALLED"; + case BundleEvent.LAZY_ACTIVATION: + return "LAZY_ACTIVATION"; + case BundleEvent.RESOLVED: + return "RESOLVED"; + case BundleEvent.STARTED: + return "STARTED"; + case BundleEvent.STARTING: + return "Starting"; + case BundleEvent.STOPPED: + return "STOPPED"; + case BundleEvent.UNINSTALLED: + return "UNINSTALLED"; + case BundleEvent.UNRESOLVED: + return "UNRESOLVED"; + case BundleEvent.UPDATED: + return "UPDATED"; + default: + return "unknown event type: " + type; + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java index f62b09cc10..24e5b57bc3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java @@ -18,10 +18,10 @@ package org.wso2.carbon.device.mgt.mobile.impl.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.dbcreator.DatabaseCreator; +import javax.sql.DataSource; import java.io.File; public final class MobileDeviceManagementSchemaInitializer extends DatabaseCreator { @@ -29,10 +29,10 @@ public final class MobileDeviceManagementSchemaInitializer extends DatabaseCreat private static final Log log = LogFactory.getLog(MobileDeviceManagementSchemaInitializer.class); private static final String setupSQLScriptBaseLocation = CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" + - File.separator + "plugins"; + File.separator + "plugins" + File.separator ; - public MobileDeviceManagementSchemaInitializer(MobileDataSourceConfig config) { - super(MobileDeviceManagerUtil.resolveDataSource(config)); + public MobileDeviceManagementSchemaInitializer(DataSource dataSource) { + super(dataSource); } protected String getDbScriptLocation(String databaseType) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java new file mode 100644 index 0000000000..3cc4af6619 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.mobile.impl.util; + +import org.apache.commons.logging.Log; +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.mobile.impl.dto.MobileDevice; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * Provides utility methods required by the mobile device management bundle. + */ +public class MobileDeviceManagementUtil { + + private static final Log log = LogFactory.getLog(MobileDeviceManagementUtil.class); + private static final String MOBILE_DEVICE_IMEI = "imei"; + private static final String MOBILE_DEVICE_IMSI = "imsi"; + private static final String MOBILE_DEVICE_REG_ID = "regId"; + private static final String MOBILE_DEVICE_VENDOR = "vendor"; + private static final String MOBILE_DEVICE_OS_VERSION = "osVersion"; + private static final String MOBILE_DEVICE_MODEL = "model"; + + public static Document convertToDocument(File file) throws DeviceManagementException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new DeviceManagementException( + "Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document : " + e.getMessage(), e); + } + } + + private static String getPropertyValue(Device device, String property) { + for (Device.Property prop : device.getProperties()) { + if (property.equals(prop.getName())) { + return prop.getValue(); + } + } + return null; + } + + private static Device.Property getProperty(String property, String value) { + Device.Property prop = null; + if(property != null){ + prop = new Device.Property(); + prop.setName(property); + prop.setValue(value); + return prop; + } + return prop; + } + + public static MobileDevice convertToMobileDevice(Device device) { + MobileDevice mobileDevice = null; + if (device != null) { + mobileDevice = new MobileDevice(); + mobileDevice.setMobileDeviceId(device.getDeviceIdentifier()); + mobileDevice.setImei(getPropertyValue(device, MOBILE_DEVICE_IMEI)); + mobileDevice.setImsi(getPropertyValue(device, MOBILE_DEVICE_IMSI)); + mobileDevice.setRegId(getPropertyValue(device, MOBILE_DEVICE_REG_ID)); + mobileDevice.setModel(getPropertyValue(device, MOBILE_DEVICE_MODEL)); + mobileDevice.setOsVersion(getPropertyValue(device, MOBILE_DEVICE_OS_VERSION)); + mobileDevice.setVendor(getPropertyValue(device, MOBILE_DEVICE_VENDOR)); + } + return mobileDevice; + } + + public static Device convertToDevice(MobileDevice mobileDevice) { + Device device = null; + if(mobileDevice!=null){ + device = new Device(); + List propertyList = new ArrayList(); + propertyList.add(getProperty(MOBILE_DEVICE_IMEI,mobileDevice.getImei())); + propertyList.add(getProperty(MOBILE_DEVICE_IMSI,mobileDevice.getImsi())); + propertyList.add(getProperty(MOBILE_DEVICE_REG_ID,mobileDevice.getRegId())); + propertyList.add(getProperty(MOBILE_DEVICE_MODEL,mobileDevice.getModel())); + propertyList.add(getProperty(MOBILE_DEVICE_OS_VERSION,mobileDevice.getOsVersion())); + propertyList.add(getProperty(MOBILE_DEVICE_VENDOR,mobileDevice.getVendor())); + device.setProperties(propertyList); + device.setDeviceIdentifier(mobileDevice.getMobileDeviceId()); + } + return device; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java deleted file mode 100644 index c5b9940fb6..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.mobile.impl.util; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.w3c.dom.Document; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.JNDILookupDefinition; -import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; -import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil; - -import javax.sql.DataSource; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import java.io.File; -import java.util.Hashtable; -import java.util.List; - -/** - * Created by harshan on 12/15/14. - */ -public class MobileDeviceManagerUtil { - - private static final Log log = LogFactory.getLog(MobileDeviceManagerUtil.class); - - public static Document convertToDocument(File file) throws DeviceManagementException { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - try { - DocumentBuilder docBuilder = factory.newDocumentBuilder(); - return docBuilder.parse(file); - } catch (Exception e) { - throw new DeviceManagementException( - "Error occurred while parsing file, while converting " + - "to a org.w3c.dom.Document : " + e.getMessage(), e); - } - } - - /** - * Resolve data source from the data source definition - * - * @param config data source configuration - * @return data source resolved from the data source definition - */ - public static DataSource resolveDataSource(MobileDataSourceConfig config) { - DataSource dataSource = null; - if (config == null) { - throw new RuntimeException( - "Mobile Device Management Repository data source configuration " + - "is null and thus, is not initialized"); - } - JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion(); - if (jndiConfig != null) { - if (log.isDebugEnabled()) { - log.debug( - "Initializing Mobile Device Management Repository data source using the JNDI " + - "Lookup Definition"); - } - List jndiPropertyList = - jndiConfig.getJndiProperties(); - if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); - for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { - jndiProperties.put(prop.getName(), prop.getValue()); - } - dataSource = - MobileDeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), - jndiProperties); - } else { - dataSource = MobileDeviceManagementDAOUtil - .lookupDataSource(jndiConfig.getJndiName(), null); - } - } - return dataSource; - } -} diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java index 14603fd1b5..6fa07dba1f 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java @@ -23,7 +23,7 @@ import javax.ws.rs.ext.ExceptionMapper; public class ErrorHandler implements ExceptionMapper { - @Override public Response toResponse(Throwable throwable) { + public Response toResponse(Throwable throwable) { Response.Status status; status = Response.Status.INTERNAL_SERVER_ERROR; // return Response.status(status).header("exception", exception.getMessage()).build(); diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql index f36a034bd5..e426421494 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql @@ -1,38 +1,11 @@ -CREATE TABLE IF NOT EXISTS MBL_OS_VERSION -( - VERSION_ID INT NOT NULL AUTO_INCREMENT, - NAME VARCHAR(45) NULL DEFAULT NULL, - PRIMARY KEY (VERSION_ID) -); - -CREATE TABLE IF NOT EXISTS MBL_DEVICE_MODEL -( - MODEL_ID INT NOT NULL AUTO_INCREMENT, - MODEL VARCHAR(45) NULL DEFAULT NULL, - PRIMARY KEY (MODEL_ID) -); - - CREATE TABLE IF NOT EXISTS MBL_VENDOR -( - VENDOR_ID INT NOT NULL AUTO_INCREMENT, - VENDOR VARCHAR(45) NULL DEFAULT NULL, - PRIMARY KEY (VENDOR_ID) -); - CREATE TABLE IF NOT EXISTS MBL_DEVICE ( MOBILE_DEVICE_ID VARCHAR(45) NOT NULL, REG_ID VARCHAR(45) NOT NULL, IMEI VARCHAR(45) NOT NULL, IMSI VARCHAR(45) NOT NULL, - OS_VERSION_ID INT NOT NULL, - DEVICE_MODEL_ID INT NOT NULL, - VENDOR_ID INT NOT NULL, - PRIMARY KEY (MOBILE_DEVICE_ID), - CONSTRAINT fk_DEVICE_OS_VERSION1 FOREIGN KEY (OS_VERSION_ID ) - REFERENCES MBL_OS_VERSION (VERSION_ID ) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_DEVICE_DEVICE_MODEL2 FOREIGN KEY (DEVICE_MODEL_ID ) - REFERENCES MBL_DEVICE_MODEL (MODEL_ID ) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_DEVICE_VENDOR1 FOREIGN KEY (VENDOR_ID ) - REFERENCES MBL_VENDOR (VENDOR_ID ) ON DELETE NO ACTION ON UPDATE NO ACTION + OS_VERSION VARCHAR(45) NOT NULL, + DEVICE_MODEL VARCHAR(45) NOT NULL, + VENDOR VARCHAR(45) NOT NULL, + PRIMARY KEY (MOBILE_DEVICE_ID) ); \ No newline at end of file diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql index 9c57710484..afc4f32d9e 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql @@ -1,33 +1,3 @@ --- ----------------------------------------------------- --- Table `MBL_OS_VERSION` --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `MBL_OS_VERSION` ( - `VERSION_ID` INT NOT NULL AUTO_INCREMENT, - `VERSION` VARCHAR(45) NULL, - PRIMARY KEY (`VERSION_ID`)) -ENGINE = InnoDB; - - --- ----------------------------------------------------- --- Table `MBL_DEVICE_MODEL` --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `MBL_DEVICE_MODEL` ( - `MODEL_ID` INT NOT NULL AUTO_INCREMENT, - `MODEL` VARCHAR(45) NULL, - PRIMARY KEY (`MODEL_ID`)) -ENGINE = InnoDB; - - --- ----------------------------------------------------- --- Table `MBL_VENDOR` --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `MBL_VENDOR` ( - `VENDOR_ID` INT NOT NULL AUTO_INCREMENT, - `VENDOR` VARCHAR(45) NULL, - PRIMARY KEY (`VENDOR_ID`)) -ENGINE = InnoDB; - - -- ----------------------------------------------------- -- Table `MBL_DEVICE` -- ----------------------------------------------------- @@ -36,28 +6,10 @@ CREATE TABLE IF NOT EXISTS `MBL_DEVICE` ( `REG_ID` VARCHAR(45) NULL, `IMEI` VARCHAR(45) NULL, `IMSI` VARCHAR(45) NULL, - `OS_VERSION_ID` INT NOT NULL, - `DEVICE_MODEL_ID` INT NOT NULL, - `VENDOR_ID` INT NOT NULL, - PRIMARY KEY (`MOBILE_DEVICE_ID`), - INDEX `fk_DEVICE_OS_VERSION1_idx` (`OS_VERSION_ID` ASC), - INDEX `fk_DEVICE_DEVICE_MODEL2_idx` (`DEVICE_MODEL_ID` ASC), - INDEX `fk_DEVICE_VENDOR1_idx` (`VENDOR_ID` ASC), - CONSTRAINT `fk_DEVICE_OS_VERSION1` - FOREIGN KEY (`OS_VERSION_ID`) - REFERENCES `MBL_OS_VERSION` (`VERSION_ID`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_DEVICE_DEVICE_MODEL2` - FOREIGN KEY (`DEVICE_MODEL_ID`) - REFERENCES `MBL_DEVICE_MODEL` (`MODEL_ID`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_DEVICE_VENDOR1` - FOREIGN KEY (`VENDOR_ID`) - REFERENCES `MBL_VENDOR` (`VENDOR_ID`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + `OS_VERSION` VARCHAR(45) NULL, + `DEVICE_MODEL` VARCHAR(45) NULL, + `VENDOR` VARCHAR(45) NULL, + PRIMARY KEY (`MOBILE_DEVICE_ID`)) ENGINE = InnoDB; From 29247f7b5069f782d47f495d31a399d1fe29c63c Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Wed, 17 Dec 2014 15:55:02 +0530 Subject: [PATCH 2/6] Removing the unnessacary files from distribution target folder and changing the policy --- .../java/org/wso2/carbon/policy/mgt/common/Policy.java | 9 +++++++++ product/modules/distribution/pom.xml | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index 96d687365b..a52bd487e6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -24,6 +24,15 @@ public class Policy { private int id; private String policyName; private List featuresList; + private boolean generic; + + public boolean isGeneric() { + return generic; + } + + public void setGeneric(boolean generic) { + this.generic = generic; + } public int getId() { return id; diff --git a/product/modules/distribution/pom.xml b/product/modules/distribution/pom.xml index ab110a0ff2..b859527b8c 100644 --- a/product/modules/distribution/pom.xml +++ b/product/modules/distribution/pom.xml @@ -182,7 +182,8 @@ - + + From 624d6eb6eff3a6e90708effa11b7a412a0b39bb9 Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Wed, 17 Dec 2014 17:34:58 +0530 Subject: [PATCH 3/6] adding dbconsole ui to the product - this should be removed when releasing the product --- product/modules/p2-profile-gen/pom.xml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/product/modules/p2-profile-gen/pom.xml b/product/modules/p2-profile-gen/pom.xml index e2e880ee2e..bccccd97af 100644 --- a/product/modules/p2-profile-gen/pom.xml +++ b/product/modules/p2-profile-gen/pom.xml @@ -137,6 +137,9 @@ org.wso2.carbon:org.wso2.carbon.as.runtimes.cxf.feature:${carbon.platform.version} + + org.wso2.carbon:org.wso2.carbon.dbconsole.ui.feature:${carbon.platform.version} + @@ -205,7 +208,10 @@ org.wso2.carbon.as.runtimes.cxf.feature.group ${carbon.platform.version} - + + org.wso2.carbon.dbconsole.ui.feature.group + ${carbon.platform.version} + From 9ba2c0198c7f308d57aa0de91a8136f27fa09803 Mon Sep 17 00:00:00 2001 From: harshanL Date: Wed, 17 Dec 2014 18:33:22 +0530 Subject: [PATCH 4/6] Fixed issues in enrollment process --- .../carbon/device/mgt/core/DeviceManager.java | 8 ++++- .../carbon/device/mgt/core/dao/DeviceDAO.java | 1 + .../mgt/core/dao/impl/DeviceDAOImpl.java | 33 ++++++++++++++++++ .../dao/util/DeviceManagementDAOUtil.java | 2 +- product/modules/agents/android/jax-rs/pom.xml | 2 +- .../java/cdm/api/android/Authentication.java | 11 ++---- .../main/java/cdm/api/android/Enrollment.java | 10 ++++++ .../src/main/java/cdm/api/android/Test.java | 34 ------------------- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 3 -- .../src/repository/dbscripts/cdm/h2.sql | 6 ++-- 10 files changed, 60 insertions(+), 50 deletions(-) delete mode 100644 product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Test.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java index a7c6ef8154..ffa935461f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java @@ -54,7 +54,13 @@ public class DeviceManager implements DeviceManagerService { boolean status = dms.enrollDevice(device); try { this.getDeviceTypeDAO().getDeviceType(); - this.getDeviceDAO().addDevice(DeviceManagementDAOUtil.convertDevice(device)); + org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice( + device); + + Integer deviceTypeId = this.getDeviceDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); + deviceDto.setDeviceType(deviceTypeId); + this.getDeviceDAO().addDevice(deviceDto); + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + "'", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 2c9cb0d585..ea9c148c81 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -37,4 +37,5 @@ public interface DeviceDAO { List getDevices() throws DeviceManagementDAOException; + Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index ec48c4c10f..dd85092825 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.dto.Status; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import java.util.List; @@ -101,6 +102,38 @@ public class DeviceDAOImpl implements DeviceDAO { return null; } + @Override + public Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + Integer deviceTypeId = null; + + try { + conn = this.getConnection(); + String createDBQuery = + "SELECT * From DM_DEVICE_TYPE DT WHERE DT.NAME=?"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, type); + resultSet = stmt.executeQuery(); + + while(resultSet.next()){ + deviceTypeId = resultSet.getInt(1); + } + + } catch (SQLException e) { + String msg = "Error occurred while fetch device type id for device type '" + type + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + + return deviceTypeId; + } + private Connection getConnection() throws DeviceManagementDAOException { try { return dataSource.getConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 5f81287390..60ec9785f2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -118,7 +118,7 @@ public final class DeviceManagementDAOUtil { deviceBO.setOwnerId(device.getOwner()); deviceBO.setOwnerShip(device.getOwnership()); deviceBO.setTenantId(DeviceManagementDAOUtil.getTenantId()); - //deviceBO.setDeviceType(Integer.parseInt(device.getType())); + deviceBO.setDeviceIdentificationId(device.getDeviceIdentifier()); return deviceBO; } diff --git a/product/modules/agents/android/jax-rs/pom.xml b/product/modules/agents/android/jax-rs/pom.xml index 49ccbaf328..ff4e139e64 100644 --- a/product/modules/agents/android/jax-rs/pom.xml +++ b/product/modules/agents/android/jax-rs/pom.xml @@ -169,7 +169,7 @@ org.codehaus.jackson jackson-jaxrs - 1.1.1 + 1.9.0 diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java index cff7c08fcc..b5687928b1 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java @@ -18,8 +18,6 @@ package cdm.api.android; import javax.ws.rs.*; -@Produces({ "application/json", "application/xml" }) -@Consumes({ "application/json", "application/xml" }) @Path("/authenticate/") public class Authentication { @@ -27,16 +25,13 @@ public class Authentication { @Path("/device/") public String authenticateDevice(@FormParam("username") String username, @FormParam("password") String password) { -/* JsonObject result = new JsonObject(); - result.addProperty("senderId", "jwwfowrjwqporqwrpqworpq");*/ - return ""; + return "jwwfowrjwqporqwrpqworpq"; } @POST @Path("/device/license") + @Produces ("text/plain") public String getLicense() { -/* JsonObject result = new JsonObject(); - result.addProperty("licenseText", "License Agreement");*/ - return ""; + return "License Agreement"; } } diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java index cd68364cce..fca33567e4 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; @@ -40,6 +41,14 @@ public class Enrollment { private static Log log = LogFactory.getLog(Enrollment.class); + /* + * Request Format : {"deviceIdentifier":"macid","description":"ww","ownership":"ww", + * "properties":[{"name":"username","value":"ww"},{"name":"device","value":"ww"}, + * {"name":"imei","value":"imei"},{"name":"imsi","value":"imsi"},{"name":"model","value":"mi3"}, + * {"name":"regId","value":"regid"},{"name":"vendor","value":"vendor"}, + * {"name":"osVersion","value":"Lolipop"}]} + * + **/ @POST public Message enrollDevice(Device device) { @@ -57,6 +66,7 @@ public class Enrollment { try { if (dmService != null) { + device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); result = dmService.enrollDevice(device); Response.status(HttpStatus.SC_CREATED); responseMsg.setResponseMessage("Device enrollment has succeeded"); diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Test.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Test.java deleted file mode 100644 index 2c7e6fd25c..0000000000 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package cdm.api.android; - -import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.Device; - -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Response; -import java.util.ArrayList; -import java.util.List; - - -@Produces({"application/json", "application/xml"}) -@Consumes({"application/json", "application/xml"}) -public class Test { - - @GET - public List getAllDevices() { - - Device dev = new Device(); - dev.setName("test1"); - dev.setDateOfEnrolment(11111111L); - dev.setDateOfLastUpdate(992093209L); - dev.setDescription("sassasaas"); - - ArrayList listdevices = new ArrayList(); - listdevices.add(dev); - - return listdevices; - } - -} diff --git a/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml b/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml index bab3022d98..3f42f416b0 100644 --- a/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -51,9 +51,6 @@ - - - diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql b/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql index 29f5b3075f..dadcbd080c 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE CREATE TABLE IF NOT EXISTS DM_DEVICE ( - ID VARCHAR(20) NOT NULL, + ID INT auto_increment NOT NULL, DESCRIPTION TEXT NULL DEFAULT NULL, NAME VARCHAR(100) NULL DEFAULT NULL, DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL, @@ -21,4 +21,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION ON UPDATE NO ACTION -); +); +-- TO:DO - Remove this INSERT sql statement. +Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android'); From 0ad9d1bd16d7eea5acec2d337c836ad02ea1f117 Mon Sep 17 00:00:00 2001 From: harshanL Date: Thu, 18 Dec 2014 10:52:51 +0530 Subject: [PATCH 5/6] Added device type insert query to h2 database script --- .../java/org/wso2/carbon/device/mgt/core/DeviceManager.java | 1 - .../distribution/src/repository/dbscripts/cdm/plugins/h2.sql | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java index ffa935461f..6f62ea8d0e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java @@ -60,7 +60,6 @@ public class DeviceManager implements DeviceManagerService { Integer deviceTypeId = this.getDeviceDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); deviceDto.setDeviceType(deviceTypeId); this.getDeviceDAO().addDevice(deviceDto); - } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + "'", e); diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql index e426421494..ed495ac190 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql @@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS MBL_DEVICE MOBILE_DEVICE_ID VARCHAR(45) NOT NULL, REG_ID VARCHAR(45) NOT NULL, IMEI VARCHAR(45) NOT NULL, - IMSI VARCHAR(45) NOT NULL, + IMSI VARCHAR(45), OS_VERSION VARCHAR(45) NOT NULL, DEVICE_MODEL VARCHAR(45) NOT NULL, VENDOR VARCHAR(45) NOT NULL, From e60e7b6d7f358fbb08564dce8af05413235089c0 Mon Sep 17 00:00:00 2001 From: harshanL Date: Thu, 18 Dec 2014 17:07:10 +0530 Subject: [PATCH 6/6] Fixed issues in Android JAX-RS app --- .../impl/dao/impl/MobileDeviceDAOImpl.java | 27 +- .../src/main/java/cdm/api/android/Device.java | 206 +++++----- .../main/java/cdm/api/android/Enrollment.java | 353 +++++++++--------- 3 files changed, 299 insertions(+), 287 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java index 42164e2c3d..890c7c8da1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java @@ -48,19 +48,20 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { MobileDevice mobileDevice = null; try { conn = this.getConnection(); - String createDBQuery = + String selectDBQuery = "SELECT * FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; - stmt = conn.prepareStatement(createDBQuery); + stmt = conn.prepareStatement(selectDBQuery); stmt.setString(1, deviceId); ResultSet resultSet = stmt.executeQuery(); while (resultSet.next()) { - mobileDevice.setMobileDeviceId(resultSet.getString(0)); - mobileDevice.setRegId(resultSet.getString(1)); - mobileDevice.setImei(resultSet.getString(2)); - mobileDevice.setImsi(resultSet.getString(3)); - mobileDevice.setOsVersion(resultSet.getString(4)); - mobileDevice.setModel(resultSet.getString(5)); - mobileDevice.setVendor(resultSet.getString(6)); + mobileDevice = new MobileDevice(); + mobileDevice.setMobileDeviceId(resultSet.getString(1)); + mobileDevice.setRegId(resultSet.getString(2)); + mobileDevice.setImei(resultSet.getString(3)); + mobileDevice.setImsi(resultSet.getString(4)); + mobileDevice.setOsVersion(resultSet.getString(5)); + mobileDevice.setModel(resultSet.getString(6)); + mobileDevice.setVendor(resultSet.getString(7)); break; } } catch (SQLException e) { @@ -117,10 +118,10 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String createDBQuery = + String updateDBQuery = "UPDATE MBL_DEVICE SET REG_ID = ?, IMEI = ?, IMSI = ?, OS_VERSION = ?," + "DEVICE_MODEL = ?, VENDOR = ? WHERE MOBILE_DEVICE_ID = ?"; - stmt = conn.prepareStatement(createDBQuery); + stmt = conn.prepareStatement(updateDBQuery); stmt.setString(1, mobileDevice.getRegId()); stmt.setString(2, mobileDevice.getImei()); stmt.setString(3, mobileDevice.getImsi()); @@ -150,9 +151,9 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String createDBQuery = + String deleteDBQuery = "DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; - stmt = conn.prepareStatement(createDBQuery); + stmt = conn.prepareStatement(deleteDBQuery); stmt.setString(1,deviceId); int rows = stmt.executeUpdate(); if(rows>0){ diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java index 0b1cdf35e9..cc75b3a89e 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java @@ -30,7 +30,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import javax.ws.rs.*; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; /** @@ -40,106 +39,107 @@ import java.util.List; @Consumes({ "application/json", "application/xml" }) public class Device { - private static Log log = LogFactory.getLog(Device.class); - - @GET - public List getAllDevices() { - - List devices = null; - String msg = ""; - DeviceManagementService dmService; - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - try { - if (dmService != null) { - devices = dmService.getAllDevices(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); - Response.status(HttpStatus.SC_OK); - } else { - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - } catch (DeviceManagementException e) { - msg = "Error occurred while fetching the device list"; - log.error(msg, e); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - return devices; - } - - @GET - @Path("{id}") - public org.wso2.carbon.device.mgt.common.Device getDevice(@PathParam("id") String id) { - - String msg = ""; - DeviceManagementService dmService; - org.wso2.carbon.device.mgt.common.Device device = new org.wso2.carbon.device.mgt.common.Device(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); - try { - if (dmService != null) { - device = dmService.getDevice(deviceIdentifier); - if (device == null) { - Response.status(HttpStatus.SC_NOT_FOUND); - } - - } else { - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - - } catch (DeviceManagementException e) { - msg = "Error occurred while fetching the device information"; - log.error(msg, e); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - return device; - } - - @PUT - @Path("{id}") - public Message updateDevice(@PathParam("id") String id, org.wso2.carbon.device.mgt.common.Device device) { - - boolean result = false; - String msg = ""; - DeviceManagementService dmService; - Message responseMessage = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - try { - if (dmService != null) { - result = dmService.updateDeviceInfo(device); - if (result) { - Response.status(HttpStatus.SC_OK); - responseMessage.setResponseMessage("Device has modified"); - } else { - Response.status(HttpStatus.SC_NOT_MODIFIED); - responseMessage.setResponseMessage("Update device has failed"); - } - } else { - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; - responseMessage.setResponseMessage(msg); - } - - } catch (DeviceManagementException e) { - msg = "Error occurred while modifying the device information"; - log.error(msg, e); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - responseMessage.setResponseMessage(msg); - - } - - return responseMessage; - } + private static Log log = LogFactory.getLog(Device.class); + + @GET + public List getAllDevices() { + List devices = null; + String msg = ""; + DeviceManagementService dmService; + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + try { + if (dmService != null) { + devices = dmService.getAllDevices( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + Response.status(HttpStatus.SC_OK); + } else { + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + } catch (DeviceManagementException e) { + msg = "Error occurred while fetching the device list."; + log.error(msg, e); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + return devices; + } + + @GET + @Path("{id}") + public org.wso2.carbon.device.mgt.common.Device getDevice(@PathParam("id") String id) { + String msg = ""; + DeviceManagementService dmService; + org.wso2.carbon.device.mgt.common.Device device = + new org.wso2.carbon.device.mgt.common.Device(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); + try { + if (dmService != null) { + device = dmService.getDevice(deviceIdentifier); + if (device == null) { + Response.status(HttpStatus.SC_NOT_FOUND); + } + + } else { + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + + } catch (DeviceManagementException e) { + msg = "Error occurred while fetching the device information."; + log.error(msg, e); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + return device; + } + + @PUT + @Path("{id}") + public Message updateDevice(@PathParam("id") String id, + org.wso2.carbon.device.mgt.common.Device device) { + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMessage = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + try { + if (dmService != null) { + device.setType( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + result = dmService.updateDeviceInfo(device); + if (result) { + Response.status(HttpStatus.SC_OK); + responseMessage.setResponseMessage("Device information has modified successfully."); + } else { + Response.status(HttpStatus.SC_NOT_MODIFIED); + responseMessage.setResponseMessage("Update device has failed."); + } + } else { + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; + responseMessage.setResponseMessage(msg); + } + + } catch (DeviceManagementException e) { + msg = "Error occurred while modifying the device information."; + log.error(msg, e); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + responseMessage.setResponseMessage(msg); + + } + return responseMessage; + } } diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java index fca33567e4..f2eb449d75 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java @@ -39,175 +39,186 @@ import javax.ws.rs.core.Response; @Consumes({ "application/json", "application/xml" }) public class Enrollment { - private static Log log = LogFactory.getLog(Enrollment.class); - - /* - * Request Format : {"deviceIdentifier":"macid","description":"ww","ownership":"ww", - * "properties":[{"name":"username","value":"ww"},{"name":"device","value":"ww"}, - * {"name":"imei","value":"imei"},{"name":"imsi","value":"imsi"},{"name":"model","value":"mi3"}, - * {"name":"regId","value":"regid"},{"name":"vendor","value":"vendor"}, - * {"name":"osVersion","value":"Lolipop"}]} - * - **/ - @POST - public Message enrollDevice(Device device) { - - boolean result = false; - int status = 0; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - - try { - if (dmService != null) { - device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); - result = dmService.enrollDevice(device); - Response.status(HttpStatus.SC_CREATED); - responseMsg.setResponseMessage("Device enrollment has succeeded"); - return responseMsg; - - } else { - responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - return responseMsg; - } - } catch (DeviceManagementException e) { - log.error(msg, e); - responseMsg.setResponseMessage("Error occurred while enrolling the device"); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - return responseMsg; - } - - } - - @GET - @Path("{id}") - public Message isEnrolled(@PathParam("id") String id) { - - boolean result = false; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); - try { - if (dmService != null) { - result = dmService.isEnrolled(deviceIdentifier); - if (result) { - Response.status(HttpStatus.SC_OK); - responseMsg.setResponseMessage("Device already enroll"); - } else { - - Response.status(HttpStatus.SC_NOT_FOUND); - responseMsg.setResponseMessage("Device not enroll"); - } - return responseMsg; - } else { - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); - return responseMsg; - } - } catch (DeviceManagementException e) { - msg = "Error occurred while checking enrollment of the device"; - log.error(msg, e); - responseMsg.setResponseMessage(msg); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - return responseMsg; - } - - } - - @PUT - @Path("{id}") - public Message modifyEnrollment(@PathParam("id") String id, Device device) { - boolean result = false; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - - try { - if (dmService != null) { - result = dmService.modifyEnrollment(device); - - if (result) { - responseMsg.setResponseMessage("update device"); - Response.status(HttpStatus.SC_OK); - }else{ - responseMsg.setResponseMessage("Update enrollment has failed"); - Response.status(HttpStatus.SC_NOT_MODIFIED); - } - } else { - msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; - responseMsg.setResponseMessage(msg); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - return responseMsg; - } catch (DeviceManagementException e) { - msg = "Error occurred while modifying enrollment of the device"; - log.error(msg, e); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - responseMsg.setResponseMessage(msg); - return responseMsg; - } - - } - - @DELETE - @Path("{id}") - public Message disenrollDevice(@PathParam("id") String id) { - - boolean result = false; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); - try { - if (dmService != null) { - result = dmService.disenrollDevice(deviceIdentifier); - if (result) { - responseMsg.setResponseMessage("Dis enrolled device"); - Response.status(HttpStatus.SC_OK); - }else{ - responseMsg.setResponseMessage("Device not found"); - Response.status(HttpStatus.SC_NOT_FOUND); - } - } else { - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; - responseMsg.setResponseMessage(msg); - } - - return responseMsg; - } catch (DeviceManagementException e) { - msg = "Error occurred while disenrolling the device"; - log.error(msg, e); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - responseMsg.setResponseMessage(msg); - return responseMsg; - } - } + private static Log log = LogFactory.getLog(Enrollment.class); + + /* + * Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD", + * "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"}, + * {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"}, + * {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"}, + * {"name":"osVersion","value":"5.0.0"}]} + * + **/ + @POST + public Message enrollDevice(Device device) { + + boolean result = false; + int status = 0; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + + try { + if (dmService != null) { + device.setType( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + result = dmService.enrollDevice(device); + Response.status(HttpStatus.SC_CREATED); + responseMsg.setResponseMessage("Device enrollment has succeeded"); + return responseMsg; + + } else { + responseMsg.setResponseMessage( + AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + return responseMsg; + } + } catch (DeviceManagementException e) { + log.error(msg, e); + responseMsg.setResponseMessage("Error occurred while enrolling the device"); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + return responseMsg; + } + + } + + @GET + @Path("{id}") + public Message isEnrolled(@PathParam("id") String id) { + + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); + try { + if (dmService != null) { + result = dmService.isEnrolled(deviceIdentifier); + if (result) { + Response.status(HttpStatus.SC_OK); + responseMsg.setResponseMessage("Device has already enrolled"); + } else { + Response.status(HttpStatus.SC_NOT_FOUND); + responseMsg.setResponseMessage("Device has not enrolled"); + } + return responseMsg; + } else { + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + responseMsg.setResponseMessage( + AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); + return responseMsg; + } + } catch (DeviceManagementException e) { + msg = "Error occurred while checking the enrollment of the device."; + log.error(msg, e); + responseMsg.setResponseMessage(msg); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + return responseMsg; + } + + } + + /* + * Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD", + * "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"}, + * {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"}, + * {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"}, + * {"name":"osVersion","value":"5.0.0"}]} + * + **/ + @PUT + @Path("{id}") + public Message modifyEnrollment(@PathParam("id") String id, Device device) { + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + + try { + if (dmService != null) { + device.setType( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + result = dmService.modifyEnrollment(device); + + if (result) { + responseMsg.setResponseMessage("Device enrollment has updated successfully"); + Response.status(HttpStatus.SC_OK); + } else { + responseMsg.setResponseMessage("Update enrollment has failed"); + Response.status(HttpStatus.SC_NOT_MODIFIED); + } + } else { + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; + responseMsg.setResponseMessage(msg); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + return responseMsg; + } catch (DeviceManagementException e) { + msg = "Error occurred while modifying enrollment of the device"; + log.error(msg, e); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + responseMsg.setResponseMessage(msg); + return responseMsg; + } + + } + + @DELETE + @Path("{id}") + public Message disenrollDevice(@PathParam("id") String id) { + + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); + try { + if (dmService != null) { + result = dmService.disenrollDevice(deviceIdentifier); + if (result) { + responseMsg.setResponseMessage("Device has disenrolled successfully"); + Response.status(HttpStatus.SC_OK); + } else { + responseMsg.setResponseMessage("Device not found"); + Response.status(HttpStatus.SC_NOT_FOUND); + } + } else { + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; + responseMsg.setResponseMessage(msg); + } + + return responseMsg; + } catch (DeviceManagementException e) { + msg = "Error occurred while disenrolling the device"; + log.error(msg, e); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + responseMsg.setResponseMessage(msg); + return responseMsg; + } + } }