Change methods to get device info if necessary

This change was done in order to fix a issue while reading
IOS operations which uses agent(eg: location).
Now it's possible to get devices with there properties
feature/appm-store/pbac
Yohan Avishke 5 years ago committed by Charitha Goonetilleke
parent ff2691f5bb
commit 2665243aa3

@ -16,6 +16,23 @@
~ specific language governing permissions and limitations ~ specific language governing permissions and limitations
~ under the License. ~ under the License.
--> -->
<!--
~ Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
~
~ Entgra (pvt) Ltd. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
@ -58,6 +75,7 @@
org.osgi.framework, org.osgi.framework,
org.osgi.service.component, org.osgi.service.component,
org.apache.commons.logging, org.apache.commons.logging,
org.apache.commons.collections.map,
javax.security.auth.x500, javax.security.auth.x500,
javax.xml.*, javax.xml.*,
javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional,

@ -15,10 +15,40 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.certificate.mgt.core.scep; package org.wso2.carbon.certificate.mgt.core.scep;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.user.api.UserStoreException;
public interface SCEPManager { public interface SCEPManager {
TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException; /**
* This method is used to retrieve a device of a given identifier wrapped by a
* {@link TenantedDeviceWrapper} with it's tenant info
*
* @param deviceIdentifier device identifier
* @return {@link TenantedDeviceWrapper} with a device's info and tenant info
* @throws SCEPException will be thrown in case device is null or if a
* {@link DeviceManagementException} or a {@link UserStoreException} is thrown
*/
TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier)
throws SCEPException;
} }

@ -15,8 +15,27 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.certificate.mgt.core.scep; package org.wso2.carbon.certificate.mgt.core.scep;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.certificate.mgt.core.internal.CertificateManagementDataHolder; import org.wso2.carbon.certificate.mgt.core.internal.CertificateManagementDataHolder;
@ -29,8 +48,6 @@ import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.util.HashMap;
public class SCEPManagerImpl implements SCEPManager { public class SCEPManagerImpl implements SCEPManager {
private static final Log log = LogFactory.getLog(SCEPManagerImpl.class); private static final Log log = LogFactory.getLog(SCEPManagerImpl.class);
DeviceManagementProviderService dms; DeviceManagementProviderService dms;
@ -40,21 +57,26 @@ public class SCEPManagerImpl implements SCEPManager {
} }
@Override @Override
public TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException { public TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier)
TenantedDeviceWrapper tenantedDeviceWrapper = new TenantedDeviceWrapper(); throws SCEPException {
try { SingletonMap deviceMap;
HashMap<Integer, Device> deviceHashMap = dms.getTenantedDevice(deviceIdentifier);
Object[] keySet = deviceHashMap.keySet().toArray();
if (keySet.length == 0) { try {
throw new SCEPException("Lookup device not found for the device identifier"); deviceMap = dms.getTenantedDevice(deviceIdentifier, false);
if (deviceMap == null) {
String msg = "Lookup device not found for the device identifier " + deviceIdentifier.getId() +
" of device type " + deviceIdentifier.getType();
log.error(msg);
throw new SCEPException(msg);
} }
Integer tenantId = (Integer) keySet[0]; } catch (DeviceManagementException e) {
tenantedDeviceWrapper.setDevice(deviceHashMap.get(tenantId)); String msg = "Error occurred while getting device " + deviceIdentifier;
tenantedDeviceWrapper.setTenantId(tenantId); log.error(msg);
throw new SCEPException(msg, e);
}
try {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
@ -66,16 +88,22 @@ public class SCEPManagerImpl implements SCEPManager {
log.error(msg); log.error(msg);
throw new SCEPException(msg); throw new SCEPException(msg);
} }
TenantedDeviceWrapper tenantedDeviceWrapper = new TenantedDeviceWrapper();
int tenantId = (int) deviceMap.getKey();
String tenantDomain = realmService.getTenantManager().getDomain(tenantId); String tenantDomain = realmService.getTenantManager().getDomain(tenantId);
tenantedDeviceWrapper.setTenantId(tenantId);
tenantedDeviceWrapper.setTenantDomain(tenantDomain); tenantedDeviceWrapper.setTenantDomain(tenantDomain);
tenantedDeviceWrapper.setDevice((Device) deviceMap.getValue());
return tenantedDeviceWrapper;
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new SCEPException("Error occurred while getting the tenant domain.", e); String msg = "Error occurred while getting the tenant domain.";
} catch (DeviceManagementException e) { log.error(msg);
throw new SCEPException("Error occurred while getting device '" + deviceIdentifier + "'.", e); throw new SCEPException(msg, e);
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
return tenantedDeviceWrapper;
} }
} }

@ -32,9 +32,27 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.dao; package org.wso2.carbon.device.mgt.core.dao;
import org.apache.commons.collections.map.SingletonMap;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
@ -50,7 +68,6 @@ import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -258,12 +275,13 @@ public interface DeviceDAO {
throws DeviceManagementDAOException; throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve a device of a given identifier with it's tenant id
* *
* @param deviceIdentifier device id. * @param deviceIdentifier device id.
* @return HashMap * @return {@link SingletonMap} with device and corresponding tenant id
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException will be thrown in case of a {@link SQLException}
*/ */
HashMap<Integer, Device> getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException; SingletonMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve a device of a given tenant id. * This method is used to retrieve a device of a given tenant id.

@ -33,9 +33,27 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.dao.impl; package org.wso2.carbon.device.mgt.core.dao.impl;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -45,7 +63,6 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo; import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceData;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistory;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
@ -64,7 +81,6 @@ import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner; import java.util.StringJoiner;
@ -647,34 +663,58 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
} }
@Override @Override
public HashMap<Integer, Device> getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException { public SingletonMap getDevice(DeviceIdentifier deviceIdentifier)
Connection conn; throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Device device;
HashMap<Integer, Device> deviceHashMap = new HashMap<>();
try { try {
conn = this.getConnection(); Connection conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, e.TENANT_ID, " + String sql = "SELECT d1.ID AS DEVICE_ID, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "d1.DESCRIPTION, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "d1.NAME AS DEVICE_NAME, " +
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + "d1.DEVICE_TYPE, " +
"t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? ) d1 WHERE d1.ID = e.DEVICE_ID ORDER BY e.DATE_OF_LAST_UPDATE DESC"; "e.TENANT_ID, " +
stmt = conn.prepareStatement(sql); "d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, " +
"e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, " +
"(SELECT d.ID, " +
"d.DESCRIPTION, " +
"d.NAME, " +
"t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t " +
"WHERE t.NAME = ? " +
"AND t.ID = d.DEVICE_TYPE_ID " +
"AND d.DEVICE_IDENTIFICATION = ?) d1 " +
"WHERE d1.ID = e.DEVICE_ID " +
"ORDER BY e.DATE_OF_LAST_UPDATE DESC";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, deviceIdentifier.getType()); stmt.setString(1, deviceIdentifier.getType());
stmt.setString(2, deviceIdentifier.getId()); stmt.setString(2, deviceIdentifier.getId());
rs = stmt.executeQuery();
if (rs.next()) { try (ResultSet rs = stmt.executeQuery()) {
device = DeviceManagementDAOUtil.loadDevice(rs); SingletonMap deviceMap = null;
deviceHashMap.put(rs.getInt("TENANT_ID"), device); if (rs.next()) {
deviceMap = new SingletonMap(
rs.getInt("TENANT_ID"),
DeviceManagementDAOUtil.loadDevice(rs)
);
}
return deviceMap;
}
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type " + String msg = "Error occurred while listing devices (with tenant id) for type " +
"'" + deviceIdentifier.getType() + "'", e); deviceIdentifier.getType();
} finally { log.error(msg);
DeviceManagementDAOUtil.cleanupResources(stmt, rs); throw new DeviceManagementDAOException(msg, e);
} }
return deviceHashMap;
} }
@Override @Override

@ -32,9 +32,27 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.collections.map.SingletonMap;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.FeatureManager;
@ -67,13 +85,14 @@ import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecu
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.common.ui.policy.mgt.PolicyConfigurationManager; import org.wso2.carbon.device.mgt.common.ui.policy.mgt.PolicyConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -564,7 +583,18 @@ public interface DeviceManagementProviderService {
*/ */
int getDeviceCount(EnrolmentInfo.Status status) throws DeviceManagementException; int getDeviceCount(EnrolmentInfo.Status status) throws DeviceManagementException;
HashMap<Integer, Device> getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; /**
* This method is used to retrieve a device of a given identifier with it's tenant id
*
* @param deviceIdentifier device identifier
* @param requireDeviceInfo boolean indicating whether the device info and properties
* is also required
* @return {@link SingletonMap} with a device and it's corresponding tenant id
* @throws DeviceManagementException will be thrown in case of a {@link SQLException}
* or {@link DeviceManagementDAOException}
*/
SingletonMap getTenantedDevice(DeviceIdentifier deviceIdentifier, boolean requireDeviceInfo)
throws DeviceManagementException;
void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException, void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException,
ConfigurationManagementException; ConfigurationManagementException;

@ -32,8 +32,27 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -1174,43 +1193,45 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public HashMap<Integer, Device> getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { public SingletonMap getTenantedDevice(DeviceIdentifier deviceIdentifier, boolean requireDeviceInfo)
throws DeviceManagementException {
if (deviceIdentifier == null) { if (deviceIdentifier == null) {
String msg = "Received null deviceIdentifier for getTenantedDevice"; String msg = "Received null deviceIdentifier for getTenantedDevice";
log.error(msg); log.error(msg);
throw new DeviceManagementException(msg); throw new DeviceManagementException(msg);
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type '" + log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type " +
deviceIdentifier.getType() + "'"); deviceIdentifier.getType());
} }
HashMap<Integer, Device> deviceHashMap;
SingletonMap deviceMap;
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
deviceHashMap = deviceDAO.getDevice(deviceIdentifier); deviceMap = deviceDAO.getDevice(deviceIdentifier);
if (deviceHashMap == null) { if (deviceMap == null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("No device is found upon the type '" + deviceIdentifier.getType() + "' and id '" + log.debug("Unable to find device for type " + deviceIdentifier.getType() +
deviceIdentifier.getId() + "'"); " and id " + deviceIdentifier.getId());
} }
return new HashMap<>();
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while obtaining the device for id '" + deviceIdentifier.getId() + "'"; String msg = "Error occurred while obtaining the device for id " + deviceIdentifier.getId();
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source"; String msg = "Error occurred while opening a connection for the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getTenantedDevice device: " + deviceIdentifier.getId();
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
return deviceHashMap;
if (requireDeviceInfo && deviceMap != null) {
getAllDeviceInfo((Device) deviceMap.getValue());
}
return deviceMap;
} }
@Override @Override

@ -33,9 +33,27 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -82,7 +100,6 @@ import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -732,11 +749,11 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})
public void testGetTenantedDevice() throws DeviceManagementException { public void testGetTenantedDevice() throws DeviceManagementException {
HashMap<Integer, Device> deviceMap = deviceMgtService.getTenantedDevice(new SingletonMap deviceMap = deviceMgtService.getTenantedDevice(new
DeviceIdentifier DeviceIdentifier
(DEVICE_ID, DEVICE_TYPE)); (DEVICE_ID, DEVICE_TYPE), false);
if (!isMock()) { if (!isMock()) {
Assert.assertTrue(!deviceMap.isEmpty()); Assert.assertFalse(deviceMap.isEmpty());
} }
} }

Loading…
Cancel
Save