refactored code issues

revert-dabc3590
hasuniea 9 years ago
parent f35c3d084a
commit 7091a45590

@ -43,6 +43,7 @@ import javax.xml.bind.Unmarshaller;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class WindowsDeviceManager implements DeviceManager { public class WindowsDeviceManager implements DeviceManager {
@ -137,6 +138,8 @@ public class WindowsDeviceManager implements DeviceManager {
WindowsDAOFactory.rollbackTransaction(); WindowsDAOFactory.rollbackTransaction();
throw new DeviceManagementException("Error while updating the enrollment of the Windows device : " + throw new DeviceManagementException("Error while updating the enrollment of the Windows device : " +
device.getDeviceIdentifier(), e); device.getDeviceIdentifier(), e);
} finally {
WindowsDAOFactory.closeConnection();
} }
return status; return status;
} }
@ -154,6 +157,8 @@ public class WindowsDeviceManager implements DeviceManager {
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
WindowsDAOFactory.rollbackTransaction(); WindowsDAOFactory.rollbackTransaction();
throw new DeviceManagementException("Error while removing the Windows device : " + deviceId.getId(), e); throw new DeviceManagementException("Error while removing the Windows device : " + deviceId.getId(), e);
} finally {
WindowsDAOFactory.closeConnection();
} }
return status; return status;
} }
@ -175,12 +180,45 @@ public class WindowsDeviceManager implements DeviceManager {
} }
public List<Device> getAllDevices() throws DeviceManagementException { public List<Device> getAllDevices() throws DeviceManagementException {
return null; List<Device> devices = null;
try {
if (log.isDebugEnabled()) {
log.debug("Fetching the details of all Windows devices");
}
WindowsDAOFactory.openConnection();
List<MobileDevice> mobileDevices = daoFactory.getMobileDeviceDAO().getAllMobileDevices();
if (mobileDevices != null) {
devices = new ArrayList<>();
for (MobileDevice mobileDevice : mobileDevices) {
devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevice));
}
}
} catch (MobileDeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while fetching all Windows devices", e);
} finally {
WindowsDAOFactory.closeConnection();
}
return devices;
} }
@Override @Override
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
return null; Device device = null;
try {
if (log.isDebugEnabled()) {
log.debug("Getting the details of Windows device : '" + deviceId.getId() + "'");
}
WindowsDAOFactory.openConnection();
MobileDevice mobileDevice = daoFactory.getMobileDeviceDAO().
getMobileDevice(deviceId.getId());
device = MobileDeviceManagementUtil.convertToDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
throw new DeviceManagementException(
"Error occurred while fetching the Windows device: '" + deviceId.getId() + "'", e);
} finally {
WindowsDAOFactory.closeConnection();
}
return device;
} }
@Override @Override
@ -221,10 +259,14 @@ public class WindowsDeviceManager implements DeviceManager {
boolean status; boolean status;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try { try {
WindowsDAOFactory.getConnection();
status = daoFactory.getMobileDeviceDAO().addMobileDevice(mobileDevice); status = daoFactory.getMobileDeviceDAO().addMobileDevice(mobileDevice);
WindowsDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
throw new DeviceManagementException("Error while enrolling the Windows device '" + throw new DeviceManagementException("Error while enrolling the Windows device '" +
device.getDeviceIdentifier() + "'", e); device.getDeviceIdentifier() + "'", e);
} finally {
WindowsDAOFactory.closeConnection();
} }
return status; return status;
} }

@ -36,8 +36,7 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>(); private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public WindowsDAOFactory() { public WindowsDAOFactory() {
this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS);
.MOBILE_DEVICE_TYPE_WINDOWS);
} }
@Override @Override
@ -80,13 +79,26 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
} }
} }
public static void openConnection() throws MobileDeviceManagementDAOException {
if (currentConnection.get() == null) {
Connection conn;
try {
conn = dataSource.getConnection();
currentConnection.set(conn);
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException
("Error occurred while retrieving data source connection", e);
}
}
}
public static Connection getConnection() throws MobileDeviceManagementDAOException { public static Connection getConnection() throws MobileDeviceManagementDAOException {
if (currentConnection.get() == null) { if (currentConnection.get() == null) {
try { try {
currentConnection.set(dataSource.getConnection()); currentConnection.set(dataSource.getConnection());
} catch (SQLException e) { } catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection", throw new MobileDeviceManagementDAOException
e); ("Error occurred while retrieving data source connection", e);
} }
} }
return currentConnection.get(); return currentConnection.get();

Loading…
Cancel
Save