Code refactor application update

merge-requests/7/head
manoj 9 years ago
parent f8c1cbc405
commit ac3438e2cd

@ -118,35 +118,6 @@ public class Application implements Serializable {
}
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) {
return false;
}
@ -156,14 +127,6 @@ public class Application implements Serializable {
@Override
public int hashCode() {
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);
return result;
}

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

@ -101,14 +101,13 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setInt(8, tenantId);
stmt.setObject(9,application.getAppProperties());
stmt.setString(10,application.getApplicationIdentifier());
stmt.addBatch();
}
stmt.executeBatch();
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
while (rs.next()) {
if (rs.next()){
applicationIds.add(rs.getInt(1));
}
}
return applicationIds;
} catch (SQLException 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();
try {
application.setId(rs.getInt("ID"));
application.setName(rs.getString("NAME"));
application.setType(rs.getString("TYPE"));

@ -108,11 +108,12 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
Connection conn;
ResultSet rs;
int mappingId = -1;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
"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){
stmt.setInt(1, deviceId);
@ -123,6 +124,8 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
stmt.executeBatch();
} catch (SQLException 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 application2 = TestDataHolder.generateApplicationDummyData("org.wso2.app2");
Application application3 = TestDataHolder.generateApplicationDummyData("org.wso2.app3");
Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app4");
applications.add(application1);
applications.add(application2);
applications.add(application3);
applications.add(application4);
Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
@ -83,15 +85,17 @@ public class ApplicationManagementProviderServiceTest {
Assert.fail(msg, appMgtEx);
}
Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app3");
Application application5 = TestDataHolder.generateApplicationDummyData("org.wso2.app5");
applications = new ArrayList<Application>();
applications.add(application4);
applications.add(application3);
applications.add(application5);
try {
appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications);
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){
String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'";
log.error(msg, appMgtEx);

Loading…
Cancel
Save