Fix Device renaming is not working issue#11452

#11349
Nipuni Kavindya 3 months ago
parent 7a8c63f31f
commit 61a21edb9d

@ -557,22 +557,49 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@Path("/type/{deviceType}/id/{deviceId}/rename")
public Response renameDevice(Device device, @PathParam("deviceType") String deviceType,
@PathParam("deviceId") String deviceId) {
if (device == null) {
String msg = "Required values are not set to rename device";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
if (StringUtils.isEmpty(device.getName())) {
String msg = "Device name is not set to rename device";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService();
try {
Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier
(deviceId, deviceType), true);
persistedDevice.setName(device.getName());
System.out.println("This is rename device");
boolean responseOfmodifyEnrollment = deviceManagementProviderService.modifyEnrollment(persistedDevice);
boolean responseOfDeviceNameChanged = deviceManagementProviderService.sendDeviceNameChangedNotification(
persistedDevice);
boolean response = responseOfmodifyEnrollment && responseOfDeviceNameChanged;
return Response.status(Response.Status.CREATED).entity(response).build();
} catch (DeviceManagementException e) {
String msg = "Error encountered while updating requested device of type : " + deviceType ;
Device updatedDevice = deviceManagementProviderService.updateDeviceName(device, deviceType, deviceId);
if (updatedDevice != null) {
boolean notificationResponse = deviceManagementProviderService.sendDeviceNameChangedNotification(updatedDevice);
if (notificationResponse) {
return Response.status(Response.Status.CREATED).entity(updatedDevice).build();
} else {
String msg = "Device updated successfully, but failed to send notification.";
log.warn(msg);
return Response.status(Response.Status.CREATED).entity(updatedDevice).header("Warning", msg).build();
}
} else {
String msg = "Device update failed for device of type : " + deviceType;
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} catch (BadRequestException e) {
String msg = "Bad request: " + e.getMessage();
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (DeviceNotFoundException e) {
String msg = "Device not found: " + e.getMessage();
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (DeviceManagementException e) {
String msg = "Error encountered while updating requested device of type : " + deviceType;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (ConflictException e) {
String msg = "Conflict encountered while updating requested device of type : " + deviceType;
log.error(msg, e);
return Response.status(Response.Status.CONFLICT).entity(msg).build();
}
}

@ -0,0 +1,32 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.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 io.entgra.device.mgt.core.device.mgt.common.exceptions;
public class ConflictException extends Exception {
private static final long serialVersionUID = -4998775497944307646L;
public ConflictException(String message) {
super(message);
}
public ConflictException(String message, Throwable cause) {
super(message, cause);
}
}

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.device.mgt.core.service;
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.ConflictException;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO;
import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO;
import io.entgra.device.mgt.core.device.mgt.core.dto.OwnerWithDeviceDTO;
@ -1125,4 +1126,17 @@ public interface DeviceManagementProviderService {
*/
PaginationResult getDevicesNotInGroup(PaginationRequest request, boolean requireDeviceInfo)
throws DeviceManagementException;
/**
* This method is to update devices names
* @param device {@link Device}
* @param deviceType the type of the device.
* @param deviceId ID of the device.
* @return boolean value of the update status.
* @throws DeviceManagementException if any service level or DAO level error occurs.
* @throws DeviceManagementException if service level null device error occurs.
* @throws ConflictException if service level data conflicts occurs.
*/
Device updateDeviceName(Device device, String deviceType, String deviceId)
throws DeviceManagementException, DeviceNotFoundException, ConflictException;
}

@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.core.service;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.ConflictException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO;
@ -3917,6 +3918,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceIdentifier, this.getTenantId());
}
private void updateDeviceInCache(DeviceIdentifier deviceIdentifier, Device device) {
DeviceCacheManagerImpl.getInstance().updateDeviceInCache(deviceIdentifier, device, this.getTenantId());
}
/***
* This method removes a given list of devices from the cache
* @param deviceList list of DeviceCacheKey objects
@ -5503,4 +5508,52 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
paginationResult.setRecordsTotal(count);
return paginationResult;
}
@Override
public Device updateDeviceName(Device device, String deviceType, String deviceId)
throws DeviceManagementException, DeviceNotFoundException, ConflictException {
Device persistedDevice = this.getDevice(new DeviceIdentifier(deviceId, deviceType), true);
if (persistedDevice == null) {
String msg = "Device not found for the given deviceId and deviceType";
log.error(msg);
throw new DeviceNotFoundException(msg);
}
if (persistedDevice.getName().equals(device.getName())) {
String msg = "Device names are the same.";
log.info(msg);
throw new ConflictException(msg);
}
persistedDevice.setName(device.getName());
if (log.isDebugEnabled()) {
log.debug("Rename Device name of: " + persistedDevice.getId() + " of type '" + persistedDevice.getType() + "'");
}
DeviceManager deviceManager = this.getDeviceManager(persistedDevice.getType());
if (deviceManager == null) {
String msg = "Device Manager associated with the device type '" + persistedDevice.getType() + "' is null. " +
"Therefore, not attempting method 'modifyEnrolment'";
log.error(msg);
throw new DeviceManagementException(msg);
}
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(persistedDevice.getDeviceIdentifier(), persistedDevice.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
int tenantId = this.getTenantId();
deviceDAO.updateDevice(persistedDevice, tenantId);
DeviceManagementDAOFactory.commitTransaction();
this.updateDeviceInCache(deviceIdentifier, persistedDevice);
return persistedDevice;
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while renaming the device '" + persistedDevice.getId() + "'";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while initiating transaction to rename device: " + persistedDevice.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
}

Loading…
Cancel
Save