public List<DeviceList> getDeviceList(String location) { SessionFactory sessionFactory = HibernateSessionFactory.getSessionFactory(); List<DeviceList> results = new ArrayList<DeviceList>(); Session session = sessionFactory.openSession(); if (session != null) { SQLQuery query = session.createSQLQuery( "select d.*,da.*,du.* " + "from device_app.device d, device_app.device_availability da, device_app.device_user du " + "where d.id=da.device_id and " + "da.checked_out_user = du.email_id and " + "d.device_status='active' and " + "du.user_location=:location"); query.addEntity(Device.class); query.addEntity(DeviceAvailability.class); query.addEntity(DeviceUser.class); query.setString("location", location); List<Object[]> resultList = query.list(); if (resultList != null && resultList.size() > 0) { System.out.println("results size is " + resultList.size()); for (Object[] array : resultList) { DeviceList row = new DeviceList(); for (Object resultObject : array) { System.out.println(resultObject.getClass()); if (resultObject instanceof Device) { Device device = (Device) resultObject; row.setBarCodeIdentifier(device.getBarCodeId()); row.setDeviceCapacity(device.getDeviceCapacity()); row.setDeviceName(device.getDeviceName()); row.setDeviceNickname(device.getDeviceNickName()); row.setDeviceOs(device.getDeviceOs()); row.setDeviceOsVersion(device.getDeviceOsVersion()); row.setDeviceSerialNumber(device.getDeviceSerialNumber()); row.setDeviceStatus(device.getDeviceStatus()); row.setDeviceType(device.getDeviceType()); row.setId(device.getId()); row.setOwnedBy(device.getDeviceOwner()); } else if (resultObject instanceof DeviceAvailability) { DeviceAvailability availability = (DeviceAvailability) resultObject; // // row.setCheckedOutAt(DateUtils.truncate(availability.getModifiedDate(),Calendar.MINUTE).toString()); row.setCheckedOutAt( DateFormatUtils.format(availability.getModifiedDate(), "yyyy-MM-dd HH:mm:ss")); row.setCurrentlyCheckedOutById(availability.getDeviceUser().getEmailId()); row.setIsCheckedOut(availability.isIsCheckedOut()); row.setLocation(location); } else { DeviceUser user = (DeviceUser) resultObject; } } results.add(row); } } } return results; }
public List<DeviceList> getAllDevices() { SessionFactory sessionFactory = HibernateSessionFactory.getSessionFactory(); List<DeviceList> results = new ArrayList<DeviceList>(); Session session = sessionFactory.openSession(); if (session != null) { Query query = session.createQuery("from DeviceAvailability"); List<DeviceAvailability> resultList = query.list(); if (resultList != null && resultList.size() > 0) { System.out.println("results size is " + resultList.size()); DeviceList row = new DeviceList(); for (DeviceAvailability resultObject : resultList) { System.out.println(resultObject.getClass()); Device device = (Device) resultObject.getDevice(); row.setBarCodeIdentifier(device.getBarCodeId()); row.setDeviceCapacity(device.getDeviceCapacity()); row.setDeviceName(device.getDeviceName()); row.setDeviceNickname(device.getDeviceNickName()); row.setDeviceOs(device.getDeviceOs()); row.setDeviceOsVersion(device.getDeviceOsVersion()); row.setDeviceSerialNumber(device.getDeviceSerialNumber()); row.setDeviceStatus(device.getDeviceStatus()); row.setDeviceType(device.getDeviceType()); row.setId(device.getId()); row.setOwnedBy(device.getDeviceOwner()); DeviceAvailability availability = (DeviceAvailability) resultObject; row.setCheckedOutAt(availability.getModifiedDate().toString()); row.setIsCheckedOut(availability.isIsCheckedOut()); DeviceUser deviceUser = availability.getDeviceUser(); if (deviceUser != null) { row.setCurrentlyCheckedOutById(deviceUser.getEmailId()); row.setLocation(deviceUser.getUserLocation()); } results.add(row); } } } return results; }