Code refactor application update

revert-70aa11f8
manoj 10 years ago
parent f8c1cbc405
commit ac3438e2cd

@ -118,35 +118,6 @@ public class Application implements Serializable {
} }
Application that = (Application) o; Application that = (Application) o;
if (id != that.id) {
return false;
}
if (appProperties != null ? !appProperties.equals(that.appProperties) : that.appProperties != null) {
return false;
}
if (category != null ? !category.equals(that.category) : that.category != null) {
return false;
}
if (imageUrl != null ? !imageUrl.equals(that.imageUrl) : that.imageUrl != null) {
return false;
}
if (locationUrl != null ? !locationUrl.equals(that.locationUrl) : that.locationUrl != null) {
return false;
}
if (name != null ? !name.equals(that.name) : that.name != null) {
return false;
}
if (platform != null ? !platform.equals(that.platform) : that.platform != null) {
return false;
}
if (type != null ? !type.equals(that.type) : that.type != null) {
return false;
}
if (version != null ? !version.equals(that.version) : that.version != null) {
return false;
}
if (applicationIdentifier != null ? !applicationIdentifier.equals(that.applicationIdentifier) : that.applicationIdentifier != null) { if (applicationIdentifier != null ? !applicationIdentifier.equals(that.applicationIdentifier) : that.applicationIdentifier != null) {
return false; return false;
} }
@ -156,14 +127,6 @@ public class Application implements Serializable {
@Override @Override
public int hashCode() { public int hashCode() {
int result = id; int result = id;
result = 31 * result + (platform != null ? platform.hashCode() : 0);
result = 31 * result + (category != null ? category.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (locationUrl != null ? locationUrl.hashCode() : 0);
result = 31 * result + (imageUrl != null ? imageUrl.hashCode() : 0);
result = 31 * result + (version != null ? version.hashCode() : 0);
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (appProperties != null ? appProperties.hashCode() : 0);
result = 31 * result + (applicationIdentifier != null ? applicationIdentifier.hashCode() : 0); result = 31 * result + (applicationIdentifier != null ? applicationIdentifier.hashCode() : 0);
return result; return result;
} }

@ -184,33 +184,66 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem
int tenantId = getTenantId(); int tenantId = getTenantId();
try { try {
DeviceManagementDAOFactory.beginTransaction();
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId);
if (log.isDebugEnabled()) {
log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId());
}
List<Application> installedAppList = getApplicationListForDevice(deviceIdentifier); List<Application> installedAppList = getApplicationListForDevice(deviceIdentifier);
if (log.isDebugEnabled()) {
log.debug("num of apps installed:" + installedAppList.size());
}
List<Application> appsToAdd = new ArrayList<Application>(); List<Application> appsToAdd = new ArrayList<Application>();
List<Integer> appIdsToRemove = new ArrayList<Integer>(); List<Integer> appIdsToRemove = new ArrayList<Integer>();
for(Application installedApp:installedAppList){ for(Application installedApp:installedAppList){
if (!applications.contains(installedApp)){ if (!applications.contains(installedApp)){
if (log.isDebugEnabled()) {
log.debug("Remove app Id:" + installedApp.getId());
}
appIdsToRemove.add(installedApp.getId()); appIdsToRemove.add(installedApp.getId());
} }
} }
Application installedApp;
List<Integer> applicationIds = new ArrayList<>();
for(Application application:applications){ for(Application application:applications){
if (!installedAppList.contains(application)) { if (!installedAppList.contains(application)) {
installedApp = applicationDAO.getApplication(application.getApplicationIdentifier(), tenantId);
if (installedApp == null){
appsToAdd.add(application); appsToAdd.add(application);
}else{
applicationIds.add(installedApp.getId());
} }
} }
}
if (log.isDebugEnabled()) {
log.debug("num of apps add:" + appsToAdd.size());
}
applicationIds.addAll(applicationDAO.addApplications(appsToAdd, tenantId));
if (log.isDebugEnabled()) {
List<Integer> applicationIds = applicationDAO.addApplications(appsToAdd, tenantId); log.debug("num of app Ids:" + applicationIds.size());
}
applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId); applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId);
if (log.isDebugEnabled()) {
log.debug("num of remove app Ids:" + appIdsToRemove.size());
}
applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove,tenantId); applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove,tenantId);
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException deviceDaoEx) { } catch (DeviceManagementDAOException deviceDaoEx) {
String errorMsg = "Error occurred saving application list to the device"; String errorMsg = "Error occurred saving application list to the device";
log.error(errorMsg + ":" + deviceIdentifier.toString()); log.error(errorMsg + ":" + deviceIdentifier.toString());
try {
DeviceManagementDAOFactory.rollbackTransaction();
} catch (DeviceManagementDAOException e) {
log.error("Error occurred while roll back transaction",e);
}
throw new ApplicationManagementException(errorMsg, deviceDaoEx); throw new ApplicationManagementException(errorMsg, deviceDaoEx);
} }
} }

@ -101,14 +101,13 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setInt(8, tenantId); stmt.setInt(8, tenantId);
stmt.setObject(9,application.getAppProperties()); stmt.setObject(9,application.getAppProperties());
stmt.setString(10,application.getApplicationIdentifier()); stmt.setString(10,application.getApplicationIdentifier());
stmt.addBatch(); stmt.executeUpdate();
}
stmt.executeBatch();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
while (rs.next()) { if (rs.next()){
applicationIds.add(rs.getInt(1)); applicationIds.add(rs.getInt(1));
} }
}
return applicationIds; return applicationIds;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e); throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e);
@ -228,6 +227,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
Application application = new Application(); Application application = new Application();
try { try {
application.setId(rs.getInt("ID"));
application.setName(rs.getString("NAME")); application.setName(rs.getString("NAME"));
application.setType(rs.getString("TYPE")); application.setType(rs.getString("TYPE"));

@ -108,11 +108,12 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
Connection conn; Connection conn;
ResultSet rs; ResultSet rs;
int mappingId = -1; int mappingId = -1;
PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
"APPLICATION_ID = ? AND TENANT_ID = ?"; "APPLICATION_ID = ? AND TENANT_ID = ?";
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for(Integer appId:appIdList){ for(Integer appId:appIdList){
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
@ -123,6 +124,8 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e); throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e);
}finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
} }

@ -62,10 +62,12 @@ public class ApplicationManagementProviderServiceTest {
Application application1 = TestDataHolder.generateApplicationDummyData("org.wso2.app1"); Application application1 = TestDataHolder.generateApplicationDummyData("org.wso2.app1");
Application application2 = TestDataHolder.generateApplicationDummyData("org.wso2.app2"); Application application2 = TestDataHolder.generateApplicationDummyData("org.wso2.app2");
Application application3 = TestDataHolder.generateApplicationDummyData("org.wso2.app3"); Application application3 = TestDataHolder.generateApplicationDummyData("org.wso2.app3");
Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app4");
applications.add(application1); applications.add(application1);
applications.add(application2); applications.add(application2);
applications.add(application3); applications.add(application3);
applications.add(application4);
Device device = TestDataHolder.initialTestDevice; Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
@ -83,15 +85,17 @@ public class ApplicationManagementProviderServiceTest {
Assert.fail(msg, appMgtEx); Assert.fail(msg, appMgtEx);
} }
Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app3"); Application application5 = TestDataHolder.generateApplicationDummyData("org.wso2.app5");
applications = new ArrayList<Application>(); applications = new ArrayList<Application>();
applications.add(application4); applications.add(application4);
applications.add(application3); applications.add(application3);
applications.add(application5);
try { try {
appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications); appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications);
List<Application> installedApps = appMgtProvider.getApplicationListForDevice(deviceIdentifier); List<Application> installedApps = appMgtProvider.getApplicationListForDevice(deviceIdentifier);
Assert.assertEquals(installedApps.size(),2,"Num of installed applications should be two"); log.info("Number of installed applications:"+installedApps.size());
Assert.assertEquals(installedApps.size(),3,"Num of installed applications should be two");
} catch (ApplicationManagementException appMgtEx){ } catch (ApplicationManagementException appMgtEx){
String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'";
log.error(msg, appMgtEx); log.error(msg, appMgtEx);

Loading…
Cancel
Save