Fix Device renaming is not working issue#11452

remotes/1721635363905829615/tmp_refs/heads/master
Nipuni Kavindya 4 months ago
parent 7a8c63f31f
commit 61a21edb9d

@ -557,22 +557,49 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@Path("/type/{deviceType}/id/{deviceId}/rename") @Path("/type/{deviceType}/id/{deviceId}/rename")
public Response renameDevice(Device device, @PathParam("deviceType") String deviceType, public Response renameDevice(Device device, @PathParam("deviceType") String deviceType,
@PathParam("deviceId") String deviceId) { @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(); DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService();
try { try {
Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier Device updatedDevice = deviceManagementProviderService.updateDeviceName(device, deviceType, deviceId);
(deviceId, deviceType), true); if (updatedDevice != null) {
persistedDevice.setName(device.getName()); boolean notificationResponse = deviceManagementProviderService.sendDeviceNameChangedNotification(updatedDevice);
System.out.println("This is rename device"); if (notificationResponse) {
boolean responseOfmodifyEnrollment = deviceManagementProviderService.modifyEnrollment(persistedDevice); return Response.status(Response.Status.CREATED).entity(updatedDevice).build();
boolean responseOfDeviceNameChanged = deviceManagementProviderService.sendDeviceNameChangedNotification( } else {
persistedDevice); String msg = "Device updated successfully, but failed to send notification.";
boolean response = responseOfmodifyEnrollment && responseOfDeviceNameChanged; log.warn(msg);
return Response.status(Response.Status.CREATED).entity(updatedDevice).header("Warning", msg).build();
return Response.status(Response.Status.CREATED).entity(response).build(); }
} catch (DeviceManagementException e) { } else {
String msg = "Error encountered while updating requested device of type : " + deviceType ; 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); log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); 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; 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.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.DeviceDetailsDTO;
import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO; import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO;
import io.entgra.device.mgt.core.device.mgt.core.dto.OwnerWithDeviceDTO; import io.entgra.device.mgt.core.device.mgt.core.dto.OwnerWithDeviceDTO;
@ -1125,4 +1126,17 @@ public interface DeviceManagementProviderService {
*/ */
PaginationResult getDevicesNotInGroup(PaginationRequest request, boolean requireDeviceInfo) PaginationResult getDevicesNotInGroup(PaginationRequest request, boolean requireDeviceInfo)
throws DeviceManagementException; 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.common.reflect.TypeToken;
import com.google.gson.Gson; 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.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO; import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO; 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()); 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 * This method removes a given list of devices from the cache
* @param deviceList list of DeviceCacheKey objects * @param deviceList list of DeviceCacheKey objects
@ -5503,4 +5508,52 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
paginationResult.setRecordsTotal(count); paginationResult.setRecordsTotal(count);
return paginationResult; 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