Source:
<code>public static void copyVmDevices(Guid srcId, Guid dstId) { Guid id; VmBase vmBase = DbFacade.getInstance().getVmStaticDAO().get(dstId); List<DiskImage> disks = DbFacade.getInstance().getDiskImageDAO().getAllForVm(dstId); List<VmNetworkInterface> ifaces; int diskCount=0; int ifaceCount=0; boolean isVm = (vmBase != null); if (isVm) { ifaces = DbFacade.getInstance().getVmNetworkInterfaceDAO().getAllForVm(dstId); } else { vmBase = DbFacade.getInstance().getVmTemplateDAO().get(dstId); ifaces = DbFacade.getInstance().getVmNetworkInterfaceDAO().getAllForTemplate(dstId); } List<VmDevice> devices = dao.getVmDeviceByVmId(srcId); for (VmDevice device : devices) { id = Guid.NewGuid(); String specParams = ""; if (srcId.equals(Guid.Empty)) { if (vmBase.getvm_type() == VmType.Desktop) { updateNumOfMonitorsInVmDevice(null, vmBase); } continue; } if (device.getType().equalsIgnoreCase(VmDeviceType.DISK.name()) && device.getDevice().equalsIgnoreCase(VmDeviceType.DISK.name())) { if (diskCount < disks.size()) { id = (disks.get(diskCount++)).getimage_group_id(); } } else if (device.getType().equalsIgnoreCase(VmDeviceType.INTERFACE.name())) { if (ifaceCount < ifaces.size()) { id = ifaces.get(ifaceCount++).getId(); } } <START> else if (device.getType().equalsIgnoreCase(VmDeviceType.VIDEO.name())) { <END> specParams = getMemExpr(vmBase.getnum_of_monitors()); } device.setId(new VmDeviceId(id, dstId)); device.setSpecParams(appendDeviceIdToSpecParams(id, specParams)); dao.save(device); } if (isVm) { updateBootOrderInVmDevice(vmBase); } }</code><technical_language>is safer perform comparison other way: VmDeviceType.VIDEO.name().equalsIgnoreCase(device.getType()) risk for NPE</technical_language>

Target:
public static void copyVmDevices(Guid srcId, Guid dstId) { Guid id; VmBase vmBase = DbFacade.getInstance().getVmStaticDAO().get(dstId); List<DiskImage> disks = DbFacade.getInstance().getDiskImageDAO().getAllForVm(dstId); List<VmNetworkInterface> ifaces; int diskCount = 0; int ifaceCount = 0; boolean isVm = (vmBase != null); if (isVm) { ifaces = DbFacade.getInstance().getVmNetworkInterfaceDAO().getAllForVm(dstId); } else { vmBase = DbFacade.getInstance().getVmTemplateDAO().get(dstId); ifaces = DbFacade.getInstance().getVmNetworkInterfaceDAO().getAllForTemplate(dstId); } List<VmDevice> devices = dao.getVmDeviceByVmId(srcId); for (VmDevice device : devices) { id = Guid.NewGuid(); String specParams = ""; if (srcId.equals(Guid.Empty)) { if (vmBase.getvm_type() == VmType.Desktop) { updateNumOfMonitorsInVmDevice(null, vmBase); } continue; } if (VmDeviceType.DISK.getName().equals(device.getType()) && VmDeviceType.DISK.getName().equals(device.getDevice())) { if (diskCount < disks.size()) { id = (disks.get(diskCount++)).getimage_group_id(); } } else if (VmDeviceType.INTERFACE.getName().equals(device.getType())) { if (ifaceCount < ifaces.size()) { id = ifaces.get(ifaceCount++).getId(); } } else if (VmDeviceType.VIDEO.getName().equals(device.getType())) { specParams = getMemExpr(vmBase.getnum_of_monitors()); } device.setId(new VmDeviceId(id, dstId)); device.setSpecParams(appendDeviceIdToSpecParams(id, specParams)); dao.save(device); } if (isVm) { updateBootOrderInVmDevice(vmBase); } }