diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java index 5ffc2ccda5d..6817698a355 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java @@ -24,6 +24,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import javax.ws.rs.core.Response; @@ -38,6 +39,7 @@ public class APIUtil { private static ApplicationManager applicationManager; private static PlatformManager platformManager; + private static LifecycleStateManager lifecycleStateManager; public static ApplicationManager getApplicationManager() { if (applicationManager == null) { @@ -76,6 +78,24 @@ public class APIUtil { return platformManager; } + public static LifecycleStateManager getLifecycleStateManager() { + if (lifecycleStateManager == null) { + synchronized (APIUtil.class) { + if (lifecycleStateManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + lifecycleStateManager = + (LifecycleStateManager) ctx.getOSGiService(LifecycleStateManager.class, null); + if (lifecycleStateManager == null) { + String msg = "Lifecycle Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return lifecycleStateManager; + } + public static Response getResponse(ApplicationManagementException ex, Response.Status status) { return getResponse(ex.getMessage(), status); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java new file mode 100644 index 00000000000..b2f56851007 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.application.mgt.api.services; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Info; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; +import org.wso2.carbon.apimgt.annotations.api.Scope; +import org.wso2.carbon.apimgt.annotations.api.Scopes; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "Lifecycle Management Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "LifecycleManagementService"), + @ExtensionProperty(name = "context", value = "/api/application-mgt/v1.0/lifecycle"), + }) + } + ), + tags = { + @Tag(name = "lifecycle_management", description = "Lifecycle Management related APIs") + } +) +@Scopes( + scopes = { + @Scope( + name = "Get Lifecycle Details", + description = "Get lifecycle details", + key = "perm:lifecycle:get", + permissions = {"/device-mgt/lifecycle/get"} + ), + @Scope( + name = "Add a lifecycle state", + description = "Add a lifecycle state", + key = "perm:lifecycle:add", + permissions = {"/device-mgt/lifecycle/add"} + ), + } +) +@Path("/lifecycle") +@Api(value = "Lifecycle Management", description = "This API carries all lifecycle management related operations.") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface LifecycleManagementAPI { + + @GET + @Path("/states") + Response getLifecycleStates(); +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java index d1a9bd3249c..3fa2ffc4c27 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java @@ -64,6 +64,29 @@ public class ApplicationManagementAPIImpl { } } + @GET + @Consumes("application/json") + @Path("applications/{uuid}") + public Response getApplication(@PathParam("uuid") String uuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + return null; + } + + @PUT + @Consumes("application/json") + @Path("applications/{uuid}/lifecycle") + public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, @QueryParam("state") String state) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.changeLifecycle(applicationUUID, state); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while changing the lifecycle of application: " + applicationUUID; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity("Successfully changed the lifecycle state of the application: " + applicationUUID).build(); + } + @POST @Consumes("application/json") @Path("applications") diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java new file mode 100644 index 00000000000..4be4b90ac7b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.application.mgt.api.services.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.api.APIUtil; +import org.wso2.carbon.device.application.mgt.api.services.LifecycleManagementAPI; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.User; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; + +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; + +@Path("/lifecycle") +public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { + + private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class); + + @GET + @Path("/states") + public Response getLifecycleStates() { + LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); + List lifecycleStates = new ArrayList<>(); + try { + lifecycleStates = lifecycleStateManager.getLifecycleStates(); + } catch (LifecycleManagementException e) { + String msg = "Error occurred while retrieving lifecycle states."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity(lifecycleStates).build(); + } + + @POST + @Path("/states") + public Response addLifecycleState(LifecycleState state) { + LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); + try { + lifecycleStateManager.addLifecycleState(state); + } catch (LifecycleManagementException e) { + String msg = "Error occurred while adding lifecycle state."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity("Lifecycle state added successfully.").build(); + } + + @DELETE + @Path("/states/{identifier}") + public Response deleteLifecycleState(@PathParam("identifier") String identifier) { + LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); + try { + lifecycleStateManager.deleteLifecycleState(identifier); + } catch (LifecycleManagementException e) { + String msg = "Error occurred while deleting lifecycle state."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity("Lifecycle state deleted successfully.").build(); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 8d238a363de..b215ae2108b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -26,6 +26,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + @@ -33,6 +34,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/LifecycleManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/LifecycleManagementException.java new file mode 100644 index 00000000000..09fe89a4d95 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/LifecycleManagementException.java @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ +package org.wso2.carbon.device.application.mgt.common.exception; + +/** + * Exception caused during the lifecycle management. + */ +public class LifecycleManagementException extends ApplicationManagementException { + + public LifecycleManagementException(String message, Throwable ex) { + super(message, ex); + } + + public LifecycleManagementException(String message) { + super(message); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index cc4f8790c68..58b03d4ccd7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -34,7 +34,7 @@ public interface ApplicationManager { * @return Created application * @throws ApplicationManagementException Application Management Exception */ - public Application createApplication(Application application) throws ApplicationManagementException; + Application createApplication(Application application) throws ApplicationManagementException; /** * Updates an already existing application. @@ -42,14 +42,14 @@ public interface ApplicationManager { * @return Updated Application * @throws ApplicationManagementException Application Management Exception */ - public Application editApplication(Application application) throws ApplicationManagementException; + Application editApplication(Application application) throws ApplicationManagementException; /** * Delete an application identified by the unique ID. * @param uuid Unique ID for tha application * @throws ApplicationManagementException Application Management Exception */ - public void deleteApplication(String uuid) throws ApplicationManagementException; + void deleteApplication(String uuid) throws ApplicationManagementException; /** * To get the applications based on the search filter. @@ -57,5 +57,7 @@ public interface ApplicationManager { * @return Applications that matches the given filter criteria. * @throws ApplicationManagementException Application Management Exception */ - public ApplicationList getApplications(Filter filter) throws ApplicationManagementException; + ApplicationList getApplications(Filter filter) throws ApplicationManagementException; + + void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java index fa3bb75a69a..82238a716a8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java @@ -18,9 +18,19 @@ */ package org.wso2.carbon.device.application.mgt.common.services; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; + +import java.util.List; + /** * This interface manages all the operations related with lifecycle state. */ public interface LifecycleStateManager { + List getLifecycleStates() throws LifecycleManagementException; + + void addLifecycleState(LifecycleState state) throws LifecycleManagementException; + + void deleteLifecycleState(String identifier) throws LifecycleManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 860ca00c896..6b9695f0595 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -52,4 +52,6 @@ public interface ApplicationDAO { void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException; + void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementDAOException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java index 9f4674d0690..dbe164b7cb1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java @@ -20,9 +20,18 @@ 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.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.DAOException; + +import java.util.List; public interface LifecycleStateDAO { - public LifecycleState getLifeCycleStateByIdentifier(String identifier) throws ApplicationManagementDAOException; + LifecycleState getLifeCycleStateByIdentifier(String identifier) throws ApplicationManagementDAOException; + + List getLifecycleStates() throws DAOException; + + void addLifecycleState(LifecycleState state) throws DAOException; + + void deleteLifecycleState(String identifier) throws DAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java index 695ff6052da..5a6cd5fe6c6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java @@ -47,6 +47,11 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl { return null; } + @Override + public void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementDAOException { + + } + @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java index 5e9c74370b4..b55a9fba4a2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java @@ -375,6 +375,34 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { } } + @Override + public void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "UPDATE APPM_APPLICATION SET " + + "LIFECYCLE_STATE_ID = (SELECT ID FROM APPM_LIFECYCLE_STATE WHERE IDENTIFIER = ?), " + + "LIFECYCLE_STATE_MODIFIED_BY = ?, " + + "LIFECYCLE_STATE_MODIFIED_AT = ? " + + "WHERE UUID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, lifecycleIdentifier); + stmt.setString(2, "admin"); + stmt.setDate(3, new Date(System.currentTimeMillis())); + stmt.setString(4, applicationUUID); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while changing lifecycle of application: " + applicationUUID + " to: " + lifecycleIdentifier + " state.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + @Override public void deleteTags(int applicationId) throws ApplicationManagementDAOException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java index 41640e06691..6b3cef24a08 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java @@ -24,11 +24,14 @@ 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.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.DAOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class GenericLifecycleStateImpl extends AbstractDAOImpl implements LifecycleStateDAO { @@ -59,7 +62,6 @@ public class GenericLifecycleStateImpl extends AbstractDAOImpl implements Lifecy lifecycleState.setIdentifier(rs.getString("IDENTIFIER")); lifecycleState.setDescription(rs.getString("DESCRIPTION")); } - return lifecycleState; } catch (SQLException e) { @@ -69,7 +71,77 @@ public class GenericLifecycleStateImpl extends AbstractDAOImpl implements Lifecy } finally { Util.cleanupResources(stmt, rs); } + } + @Override + public List getLifecycleStates() throws DAOException { + List lifecycleStates = new ArrayList<>(); + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "SELECT IDENTIFIER, NAME, DESCRIPTION FROM APPM_LIFECYCLE_STATE"; + stmt = conn.prepareStatement(sql); + rs = stmt.executeQuery(); + while(rs.next()) { + LifecycleState lifecycleState = new LifecycleState(); + lifecycleState.setIdentifier(rs.getString("IDENTIFIER")); + lifecycleState.setName(rs.getString("NAME")); + lifecycleState.setDescription(rs.getString("DESCRIPTION")); + lifecycleStates.add(lifecycleState); + } + } catch (DBConnectionException e) { + throw new DAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new DAOException("Error occurred while retrieving lifecycle states.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + return lifecycleStates; + } + @Override + public void addLifecycleState(LifecycleState state) throws DAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "INSERT INTO APPM_LIFECYCLE_STATE ('NAME', 'IDENTIFIER', 'DESCRIPTION') VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, state.getName()); + stmt.setString(2, state.getIdentifier()); + stmt.setString(3, state.getDescription()); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new DAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new DAOException("Error occurred while adding lifecycle: " + state.getIdentifier(), e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public void deleteLifecycleState(String identifier) throws DAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "DELETE FROM APPM_LIFECYCLE_STATE WHERE IDENTIFIER = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, identifier); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new DAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new DAOException("Error occurred while deleting lifecycle: " + identifier, e); + } finally { + Util.cleanupResources(stmt, rs); + } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/DAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/DAOException.java new file mode 100644 index 00000000000..2d5121bdca1 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/DAOException.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.application.mgt.core.exception; + +public class DAOException extends Exception { + + public DAOException(String message) { + super(message); + } + + public DAOException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index e3e5bf08794..5b14e04c5da 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -148,6 +148,17 @@ public class ApplicationManagerImpl implements ApplicationManager { } + @Override + public void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementException { + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); + applicationDAO.changeLifecycle(applicationUUID, lifecycleIdentifier); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + private void validateApplication(Application application, boolean isEdit) throws ValidationException { if (application.getName() == null) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java index 2c71738f40d..eccf7d4530a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java @@ -17,7 +17,62 @@ */ package org.wso2.carbon.device.application.mgt.core.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; +import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; +import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; +import org.wso2.carbon.device.application.mgt.core.exception.DAOException; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; + +import java.util.List; public class LifecycleStateManagerImpl implements LifecycleStateManager { + + private static final Log log = LogFactory.getLog(LifecycleStateManagerImpl.class); + + @Override + public List getLifecycleStates() throws LifecycleManagementException { + List lifecycleStates = null; + try { + ConnectionManagerUtil.openDBConnection(); + LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO(); + lifecycleStates = lifecycleStateDAO.getLifecycleStates(); + } catch (DAOException | DBConnectionException e) { + throw new LifecycleManagementException("Failed get lifecycle states.", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + return lifecycleStates; + } + + @Override + public void addLifecycleState(LifecycleState state) throws LifecycleManagementException { + try { + ConnectionManagerUtil.openDBConnection(); + LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO(); + lifecycleStateDAO.addLifecycleState(state); + } catch (DAOException | DBConnectionException e) { + throw new LifecycleManagementException("Failed to add lifecycle state", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteLifecycleState(String identifier) throws LifecycleManagementException { + + try { + ConnectionManagerUtil.openDBConnection(); + LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO(); + lifecycleStateDAO.deleteLifecycleState(identifier); + } catch (DAOException | DBConnectionException e) { + throw new LifecycleManagementException("Failed to add lifecycle state: " + identifier, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index d663a53c0ae..25fd828d343 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -41,15 +41,23 @@ public class ConnectionManagerUtil { private static ThreadLocal currentTxState = new ThreadLocal<>(); private static DataSource dataSource; + public static void openDBConnection() throws DBConnectionException { + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("Database connection has already been obtained."); + } + try { + conn = dataSource.getConnection(); + } catch (SQLException e) { + throw new DBConnectionException("Failed to get a database connection.", e); + } + currentConnection.set(conn); + } + public static Connection getDBConnection() throws DBConnectionException { Connection conn = currentConnection.get(); if (conn == null) { - try { - conn = dataSource.getConnection(); - currentConnection.set(conn); - } catch (SQLException e) { - throw new DBConnectionException("Failed to get database connection.", e); - } + throw new IllegalTransactionStateException("Database connection is not active."); } return conn; } @@ -57,7 +65,7 @@ public class ConnectionManagerUtil { public static void beginDBTransaction() throws TransactionManagementException, DBConnectionException { Connection conn = currentConnection.get(); if (conn == null) { - conn = getDBConnection(); + throw new IllegalTransactionStateException("Database connection is not active."); } if (inTransaction(conn)) {