Improve lifecycle management

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 409f6fce14
commit b283c1dfde

@ -19,7 +19,6 @@
package org.wso2.carbon.device.application.mgt.core.dao; package org.wso2.carbon.device.application.mgt.core.dao;
import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException;
import java.util.List; import java.util.List;
@ -29,14 +28,51 @@ import java.util.List;
*/ */
public interface LifecycleStateDAO { public interface LifecycleStateDAO {
LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException; /**
* To get the latest lifecycle state for the given application release id.
* @param applicationReleaseId id of the application release.
*
* @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException;
LifecycleState getLatestLifeCycleState(int appId, String uuid) throws ApplicationManagementDAOException; /**
* To get the latest lifecycle state for the given application id and the application release UUID.
* @param appId id of the application.
* @param uuid UUID of the application release
*
* @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException;
/**
* To get all changed lifecycle states for the given application release id.
* @param appReleaseId id of the application release.
*
* @return Lifecycle States for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
List<LifecycleState> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; List<LifecycleState> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException;
void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) throws LifeCycleManagementDAOException; /**
* To add new lifecycle states for the given application release.
* @param releaseId Id of the application release.
* @param appId Id of the application.
* @param state LifecycleState.
* @param tenantId Tenant id
*
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId)
throws LifeCycleManagementDAOException;
/**
* To delete a specific lifecycle state for application release.
* @param identifier Id of the LifecycleState.
*
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
void deleteLifecycleState(int identifier) throws LifeCycleManagementDAOException; void deleteLifecycleState(int identifier) throws LifeCycleManagementDAOException;
} }

@ -23,7 +23,6 @@ import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionExcep
import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException;
import java.sql.Connection; import java.sql.Connection;
@ -39,7 +38,7 @@ import java.util.List;
public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO { public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO {
@Override @Override
public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException { public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -52,29 +51,18 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationReleaseId); stmt.setInt(1, applicationReleaseId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
LifecycleState lifecycleState = null; return constructLifecycle(rs);
if (rs.next()) {
lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("ID"));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY"));
}
return lifecycleState;
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application List", e); throw new LifeCycleManagementDAOException("Error occurred while getting application List", e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection to get latest" throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest"
+ " lifecycle state for a specific application", e); + " lifecycle state for a specific application", e);
} finally { } finally {
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
} }
} }
public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws ApplicationManagementDAOException{ public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -88,22 +76,11 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
stmt.setInt(1, appId); stmt.setInt(1, appId);
stmt.setString(2, uuid); stmt.setString(2, uuid);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
LifecycleState lifecycleState = null; return constructLifecycle(rs);
if (rs.next()) {
lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("ID"));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY"));
}
return lifecycleState;
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application List", e); throw new LifeCycleManagementDAOException("Error occurred while getting application List", e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection to get latest" throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest"
+ " lifecycle state for a specific application", e); + " lifecycle state for a specific application", e);
} finally { } finally {
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
@ -191,4 +168,23 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
} }
} }
private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException {
LifecycleState lifecycleState = null;
try {
if (rs.next()) {
lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("ID"));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY"));
}
} catch (SQLException e) {
throw new LifeCycleManagementDAOException(
"Error occurred while construct lifecycle state by retrieving data from SQL query", e);
}
return lifecycleState;
}
} }

@ -432,8 +432,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.getDBConnection(); ConnectionManagerUtil.getDBConnection();
applicationReleases = this.applicationReleaseDAO.getReleases(application.getId(), tenantId); applicationReleases = this.applicationReleaseDAO.getReleases(application.getId(), tenantId);
for (ApplicationRelease applicationRelease : applicationReleases) { for (ApplicationRelease applicationRelease : applicationReleases) {
LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). LifecycleState lifecycleState = null;
try {
lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO().
getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
} catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException(
"Error occurred while getting the latest lifecycle state for the application release UUID: "
+ applicationRelease.getUuid(), e);
}
if (lifecycleState != null) { if (lifecycleState != null) {
applicationRelease.setLifecycleState(lifecycleState); applicationRelease.setLifecycleState(lifecycleState);
@ -768,10 +775,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
lifecycleState.setNextStates(new ArrayList<>(getLifecycleManagementService(). lifecycleState.setNextStates(new ArrayList<>(getLifecycleManagementService().
getNextLifecycleStates(lifecycleState.getCurrentState()))); getNextLifecycleStates(lifecycleState.getCurrentState())));
} catch (ApplicationManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException("Failed to get lifecycle state", e); throw new ApplicationManagementException("Failed to get lifecycle state from database", e);
} catch (ApplicationManagementException e) {
throw new ApplicationManagementException("Failed to get application and application management", e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }

@ -27,7 +27,7 @@ public class LifecycleStateManger {
} }
public Set<String> getNextLifecycleStates(String currentLifecycleState) { public Set<String> getNextLifecycleStates(String currentLifecycleState) {
return lifecycleStates.get(currentLifecycleState).getProceedingStates(); return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates();
} }
public boolean isValidStateChange(String currentState, String nextState) { public boolean isValidStateChange(String currentState, String nextState) {

@ -16,9 +16,9 @@ public class LifecycleManagementTest {
private List<LifecycleState> lifecycleStates; private List<LifecycleState> lifecycleStates;
private LifecycleStateManger lifecycleStateManger; private LifecycleStateManger lifecycleStateManger;
private final String CURRENT_STATE = "APPROVED"; private final String CURRENT_STATE = "Approved";
private final String NEXT_STATE = "PUBLISHED"; private final String NEXT_STATE = "Published";
private final String BOGUS_STATE = "REMOVED"; private final String BOGUS_STATE = "Removed";
@BeforeClass @BeforeClass
public void init() { public void init() {
@ -32,14 +32,14 @@ public class LifecycleManagementTest {
public void checkValidNextLifecycleState() { public void checkValidNextLifecycleState() {
Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE);
Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE, Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(NEXT_STATE)); proceedingStates.contains(NEXT_STATE.toUpperCase()));
} }
@Test @Test
public void checkInvalidNextLifecycleState() { public void checkInvalidNextLifecycleState() {
Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE);
Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(BOGUS_STATE)); proceedingStates.contains(BOGUS_STATE.toUpperCase()));
} }
@Test @Test

Loading…
Cancel
Save