forked from community/device-mgt-core
parent
21253f2fdc
commit
9ec12587c7
@ -1,315 +0,0 @@
|
||||
/*
|
||||
* 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.core.dao.impl.application;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.wso2.carbon.device.application.mgt.common.Application;
|
||||
import org.wso2.carbon.device.application.mgt.common.Filter;
|
||||
import org.wso2.carbon.device.application.mgt.common.LifecycleStateTransition;
|
||||
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
|
||||
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.dao.common.Util;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.JSONUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class AbstractApplicationDAOImpl extends AbstractDAOImpl implements ApplicationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(AbstractApplicationDAOImpl.class);
|
||||
|
||||
public Application createApplication(Application application) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to create an application");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("UUID : " + application.getUuid() + " Name : " + application.getName() + " User name : "
|
||||
+ application.getUser().getUserName());
|
||||
}
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported();
|
||||
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
sql += "INSERT INTO APPM_APPLICATION (UUID, IDENTIFIER, NAME, SHORT_DESCRIPTION, DESCRIPTION, ICON_NAME, "
|
||||
+ "BANNER_NAME, VIDEO_NAME, SCREENSHOTS, CREATED_BY, CREATED_AT, MODIFIED_AT, "
|
||||
+ "APPLICATION_CATEGORY_ID, PLATFORM_ID, TENANT_ID, LIFECYCLE_STATE_ID, "
|
||||
+ "LIFECYCLE_STATE_MODIFIED_AT, LIFECYCLE_STATE_MODIFIED_BY) VALUES "
|
||||
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setString(1, application.getUuid());
|
||||
stmt.setString(2, application.getIdentifier());
|
||||
stmt.setString(3, application.getName());
|
||||
stmt.setString(4, application.getShortDescription());
|
||||
stmt.setString(5, application.getDescription());
|
||||
stmt.setString(6, application.getIconName());
|
||||
stmt.setString(7, application.getBannerName());
|
||||
stmt.setString(8, application.getVideoName());
|
||||
stmt.setString(9, JSONUtil.listToJsonArrayString(application.getScreenshots()));
|
||||
stmt.setString(10, application.getUser().getUserName());
|
||||
stmt.setDate(11, new Date(application.getCreatedAt().getTime()));
|
||||
stmt.setDate(12, new Date(application.getModifiedAt().getTime()));
|
||||
stmt.setInt(13, application.getCategory().getId());
|
||||
stmt.setInt(14, application.getPlatform().getId());
|
||||
stmt.setInt(15, application.getUser().getTenantId());
|
||||
stmt.setInt(16, application.getCurrentLifecycle().getLifecycleState().getId());
|
||||
stmt.setDate(17, new Date(
|
||||
application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime()));
|
||||
stmt.setString(18, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy());
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
application.setId(rs.getInt(1));
|
||||
}
|
||||
|
||||
if (application.getTags() != null && application.getTags().size() > 0) {
|
||||
sql = "INSERT INTO APPM_APPLICATION_TAG (NAME, APPLICATION_ID) VALUES (?, ?); ";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
for (String tag : application.getTags()) {
|
||||
stmt.setString(1, tag);
|
||||
stmt.setInt(2, application.getId());
|
||||
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.addBatch();
|
||||
} else {
|
||||
stmt.execute();
|
||||
}
|
||||
}
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.executeBatch();
|
||||
}
|
||||
}
|
||||
|
||||
if (application.getProperties() != null && application.getProperties().size() > 0) {
|
||||
sql = "INSERT INTO APPM_APPLICATION_PROPERTY (PROP_KEY, PROP_VAL, APPLICATION_ID) VALUES (?, ?, ?); ";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
Iterator it = application.getProperties().entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<String, String> property = (Map.Entry) it.next();
|
||||
stmt.setString(1, property.getKey());
|
||||
stmt.setString(2, property.getValue());
|
||||
stmt.setInt(3, application.getId());
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.addBatch();
|
||||
} else {
|
||||
stmt.execute();
|
||||
}
|
||||
}
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.executeBatch();
|
||||
}
|
||||
}
|
||||
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while adding the application", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
|
||||
return application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException {
|
||||
if(log.isDebugEnabled()){
|
||||
log.debug("Getting application count from the database");
|
||||
log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset()));
|
||||
}
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
int count = 0;
|
||||
|
||||
if (filter == null) {
|
||||
throw new ApplicationManagementDAOException("Filter need to be instantiated");
|
||||
}
|
||||
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
sql += "SELECT COUNT(APP.ID) AS APP_COUNT ";
|
||||
sql += "FROM APPM_APPLICATION AS APP ";
|
||||
sql += "INNER JOIN APPM_PLATFORM AS APL ON APP.PLATFORM_ID = APL.ID ";
|
||||
sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON APP.APPLICATION_CATEGORY_ID = CAT.ID ";
|
||||
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
sql += "WHERE APP.NAME LIKE ? ";
|
||||
}
|
||||
sql += ";";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
int index = 0;
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
stmt.setString(++index, "%" + filter.getSearchQuery() + "%");
|
||||
}
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
count = rs.getInt("APP_COUNT");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Application getApplication(String uuid) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Getting application with the UUID(" + uuid + ") from the database");
|
||||
}
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
Application application = null;
|
||||
try {
|
||||
|
||||
conn = this.getDBConnection();
|
||||
sql += "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, "
|
||||
+ "CAT.ID AS CAT_ID, CAT.NAME AS CAT_NAME FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS "
|
||||
+ "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON "
|
||||
+ "APP.APPLICATION_CATEGORY_ID = CAT.ID WHERE UUID = ?";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, uuid);
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved basic details of the application with the UUID " + uuid);
|
||||
}
|
||||
|
||||
if (rs.next()) {
|
||||
application = new Application();
|
||||
//Getting properties
|
||||
sql = "SELECT * FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsProperties = stmt.executeQuery();
|
||||
|
||||
//Getting tags
|
||||
sql = "SELECT * FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsTags = stmt.executeQuery();
|
||||
|
||||
application = Util.loadApplication(rs, rsProperties, rsTags);
|
||||
Util.cleanupResources(null, rsProperties);
|
||||
Util.cleanupResources(null, rsTags);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} catch (JSONException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
return application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String userName) throws
|
||||
ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Change Life cycle status change " + lifecycleIdentifier + "request received to the DAO "
|
||||
+ "level for the application with " + "the UUID '" + applicationUUID + "' from the user "
|
||||
+ userName);
|
||||
}
|
||||
Connection conn;
|
||||
PreparedStatement stmt = 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, userName);
|
||||
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, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LifecycleStateTransition> getNextLifeCycleStates(String applicationUUID, int tenantId)
|
||||
throws ApplicationManagementDAOException {
|
||||
Connection connection = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
|
||||
String sql = "SELECT STATE.NAME, TRANSITION.DESCRIPTION, TRANSITION.PERMISSION FROM ( SELECT * FROM "
|
||||
+ "APPM_LIFECYCLE_STATE ) STATE RIGHT JOIN (SELECT * FROM APPM_LIFECYCLE_STATE_TRANSITION WHERE "
|
||||
+ "INITIAL_STATE = (SELECT LIFECYCLE_STATE_ID FROM APPM_APPLICATION WHERE UUID = ?)) "
|
||||
+ "TRANSITION ON TRANSITION.NEXT_STATE = STATE.ID";
|
||||
|
||||
try {
|
||||
connection = this.getDBConnection();
|
||||
preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, applicationUUID);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
|
||||
List<LifecycleStateTransition> lifecycleStateTransitions = new ArrayList<>();
|
||||
|
||||
while(resultSet.next()) {
|
||||
LifecycleStateTransition lifecycleStateTransition = new LifecycleStateTransition();
|
||||
lifecycleStateTransition.setDescription(resultSet.getString(2));
|
||||
lifecycleStateTransition.setNextState(resultSet.getString(1));
|
||||
lifecycleStateTransition.setPermission(resultSet.getString(3));
|
||||
lifecycleStateTransitions.add(lifecycleStateTransition);
|
||||
}
|
||||
return lifecycleStateTransitions;
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error while getting the DBConnection for getting the life "
|
||||
+ "cycle states for the application with the UUID : " + applicationUUID, e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("SQL exception while executing the query '" + sql + "'.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(preparedStatement, resultSet);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,600 @@
|
||||
/*
|
||||
* 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.core.dao.impl.application;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.wso2.carbon.device.application.mgt.common.*;
|
||||
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
|
||||
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.dao.common.Util;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.JSONUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GenericApplicationDAOImpl extends AbstractDAOImpl implements ApplicationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(GenericApplicationDAOImpl.class);
|
||||
|
||||
public Application createApplication(Application application) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to create an application");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("UUID : " + application.getUuid() + " Name : " + application.getName() + " User name : "
|
||||
+ application.getUser().getUserName());
|
||||
}
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported();
|
||||
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
sql += "INSERT INTO APPM_APPLICATION (UUID, IDENTIFIER, NAME, SHORT_DESCRIPTION, DESCRIPTION, ICON_NAME, "
|
||||
+ "BANNER_NAME, VIDEO_NAME, SCREENSHOTS, CREATED_BY, CREATED_AT, MODIFIED_AT, "
|
||||
+ "APPLICATION_CATEGORY_ID, PLATFORM_ID, TENANT_ID, LIFECYCLE_STATE_ID, "
|
||||
+ "LIFECYCLE_STATE_MODIFIED_AT, LIFECYCLE_STATE_MODIFIED_BY) VALUES "
|
||||
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setString(1, application.getUuid());
|
||||
stmt.setString(2, application.getIdentifier());
|
||||
stmt.setString(3, application.getName());
|
||||
stmt.setString(4, application.getShortDescription());
|
||||
stmt.setString(5, application.getDescription());
|
||||
stmt.setString(6, application.getIconName());
|
||||
stmt.setString(7, application.getBannerName());
|
||||
stmt.setString(8, application.getVideoName());
|
||||
stmt.setString(9, JSONUtil.listToJsonArrayString(application.getScreenshots()));
|
||||
stmt.setString(10, application.getUser().getUserName());
|
||||
stmt.setDate(11, new Date(application.getCreatedAt().getTime()));
|
||||
stmt.setDate(12, new Date(application.getModifiedAt().getTime()));
|
||||
stmt.setInt(13, application.getCategory().getId());
|
||||
stmt.setInt(14, application.getPlatform().getId());
|
||||
stmt.setInt(15, application.getUser().getTenantId());
|
||||
stmt.setInt(16, application.getCurrentLifecycle().getLifecycleState().getId());
|
||||
stmt.setDate(17, new Date(
|
||||
application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime()));
|
||||
stmt.setString(18, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy());
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
application.setId(rs.getInt(1));
|
||||
}
|
||||
insertApplicationTagsAndProperties(application, stmt, conn, isBatchExecutionSupported);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while adding the application", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
|
||||
return application;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException {
|
||||
if(log.isDebugEnabled()){
|
||||
log.debug("Getting application data from the database");
|
||||
log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset()));
|
||||
}
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
ApplicationList applicationList = new ApplicationList();
|
||||
List<Application> applications = new ArrayList<>();
|
||||
Pagination pagination = new Pagination();
|
||||
|
||||
if (filter == null) {
|
||||
throw new ApplicationManagementDAOException("Filter need to be instantiated");
|
||||
} else {
|
||||
pagination.setLimit(filter.getLimit());
|
||||
pagination.setOffset(filter.getOffset());
|
||||
}
|
||||
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
sql += "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, "
|
||||
+ "CAT.ID AS CAT_ID, CAT.NAME AS CAT_NAME, LS.NAME AS LS_NAME, LS.IDENTIFIER AS LS_IDENTIFIER, "
|
||||
+ "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS "
|
||||
+ "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON "
|
||||
+ "APP.APPLICATION_CATEGORY_ID = CAT.ID INNER JOIN APPM_LIFECYCLE_STATE AS "
|
||||
+ "LS ON APP.LIFECYCLE_STATE_ID = LS.ID ";
|
||||
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
sql += "WHERE APP.NAME LIKE ? ";
|
||||
}
|
||||
sql += "LIMIT ?,?;";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
int index = 0;
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
stmt.setString(++index, "%" + filter.getSearchQuery() + "%");
|
||||
}
|
||||
stmt.setInt(++index, filter.getOffset());
|
||||
stmt.setInt(++index, filter.getLimit());
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
int length = 0;
|
||||
|
||||
while (rs.next()) {
|
||||
//Getting properties
|
||||
sql = "SELECT * FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsProperties = stmt.executeQuery();
|
||||
|
||||
//Getting tags
|
||||
sql = "SELECT * FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsTags = stmt.executeQuery();
|
||||
|
||||
applications.add(Util.loadApplication(rs, rsProperties, rsTags));
|
||||
Util.cleanupResources(null, rsProperties);
|
||||
Util.cleanupResources(null, rsTags);
|
||||
length++;
|
||||
}
|
||||
|
||||
pagination.setSize(length);
|
||||
pagination.setCount(this.getApplicationCount(filter));
|
||||
applicationList.setApplications(applications);
|
||||
applicationList.setPagination(pagination);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} catch (JSONException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
return applicationList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException {
|
||||
if(log.isDebugEnabled()){
|
||||
log.debug("Getting application count from the database");
|
||||
log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset()));
|
||||
}
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
int count = 0;
|
||||
|
||||
if (filter == null) {
|
||||
throw new ApplicationManagementDAOException("Filter need to be instantiated");
|
||||
}
|
||||
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
sql += "SELECT COUNT(APP.ID) AS APP_COUNT ";
|
||||
sql += "FROM APPM_APPLICATION AS APP ";
|
||||
sql += "INNER JOIN APPM_PLATFORM AS APL ON APP.PLATFORM_ID = APL.ID ";
|
||||
sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON APP.APPLICATION_CATEGORY_ID = CAT.ID ";
|
||||
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
sql += "WHERE APP.NAME LIKE ? ";
|
||||
}
|
||||
sql += ";";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
int index = 0;
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
stmt.setString(++index, "%" + filter.getSearchQuery() + "%");
|
||||
}
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
count = rs.getInt("APP_COUNT");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Application getApplication(String uuid) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Getting application with the UUID(" + uuid + ") from the database");
|
||||
}
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
Application application = null;
|
||||
try {
|
||||
|
||||
conn = this.getDBConnection();
|
||||
sql += "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, "
|
||||
+ "CAT.ID AS CAT_ID, CAT.NAME AS CAT_NAME, LS.NAME AS LS_NAME, LS.IDENTIFIER AS LS_IDENTIFIER, "
|
||||
+ "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS "
|
||||
+ "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON "
|
||||
+ "APP.APPLICATION_CATEGORY_ID = CAT.ID INNER JOIN APPM_LIFECYCLE_STATE AS "
|
||||
+ "LS ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE UUID = ?";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, uuid);
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved basic details of the application with the UUID " + uuid);
|
||||
}
|
||||
|
||||
if (rs.next()) {
|
||||
//Getting properties
|
||||
sql = "SELECT * FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsProperties = stmt.executeQuery();
|
||||
|
||||
//Getting tags
|
||||
sql = "SELECT * FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsTags = stmt.executeQuery();
|
||||
|
||||
application = Util.loadApplication(rs, rsProperties, rsTags);
|
||||
Util.cleanupResources(null, rsProperties);
|
||||
Util.cleanupResources(null, rsTags);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application details with UUID "
|
||||
+ uuid, e);
|
||||
} catch (JSONException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
return application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String userName) throws
|
||||
ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Change Life cycle status change " + lifecycleIdentifier + "request received to the DAO "
|
||||
+ "level for the application with " + "the UUID '" + applicationUUID + "' from the user "
|
||||
+ userName);
|
||||
}
|
||||
Connection conn;
|
||||
PreparedStatement stmt = 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, userName);
|
||||
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, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LifecycleStateTransition> getNextLifeCycleStates(String applicationUUID, int tenantId)
|
||||
throws ApplicationManagementDAOException {
|
||||
Connection connection = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
|
||||
String sql = "SELECT STATE.NAME, TRANSITION.DESCRIPTION, TRANSITION.PERMISSION FROM ( SELECT * FROM "
|
||||
+ "APPM_LIFECYCLE_STATE ) STATE RIGHT JOIN (SELECT * FROM APPM_LIFECYCLE_STATE_TRANSITION WHERE "
|
||||
+ "INITIAL_STATE = (SELECT LIFECYCLE_STATE_ID FROM APPM_APPLICATION WHERE UUID = ?)) "
|
||||
+ "TRANSITION ON TRANSITION.NEXT_STATE = STATE.ID";
|
||||
|
||||
try {
|
||||
connection = this.getDBConnection();
|
||||
preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, applicationUUID);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
|
||||
List<LifecycleStateTransition> lifecycleStateTransitions = new ArrayList<>();
|
||||
|
||||
while(resultSet.next()) {
|
||||
LifecycleStateTransition lifecycleStateTransition = new LifecycleStateTransition();
|
||||
lifecycleStateTransition.setDescription(resultSet.getString(2));
|
||||
lifecycleStateTransition.setNextState(resultSet.getString(1));
|
||||
lifecycleStateTransition.setPermission(resultSet.getString(3));
|
||||
lifecycleStateTransitions.add(lifecycleStateTransition);
|
||||
}
|
||||
return lifecycleStateTransitions;
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error while getting the DBConnection for getting the life "
|
||||
+ "cycle states for the application with the UUID : " + applicationUUID, e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("SQL exception while executing the query '" + sql + "'.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(preparedStatement, resultSet);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Application editApplication(Application application, int tenantId) throws ApplicationManagementDAOException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported();
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
int index = 0;
|
||||
sql += "UPDATE APPM_APPLICATION SET NAME = IFNULL (?, NAME), SHORT_DESCRIPTION = IFNULL "
|
||||
+ "(?, SHORT_DESCRIPTION), DESCRIPTION = IFNULL (?, DESCRIPTION), ICON_NAME = IFNULL (?, ICON_NAME)"
|
||||
+ ", BANNER_NAME = IFNULL (?, BANNER_NAME), VIDEO_NAME = IFNULL (?, VIDEO_NAME), "
|
||||
+ "SCREENSHOTS = IFNULL (?, SCREENSHOTS), MODIFIED_AT = IFNULL (?, MODIFIED_AT), ";
|
||||
|
||||
if (application.getPayment() != null) {
|
||||
sql += " IS_FREE = IFNULL (?, IS_FREE), ";
|
||||
if (application.getPayment().getPaymentCurrency() != null) {
|
||||
sql += "PAYMENT_CURRENCY = IFNULL (?, PAYMENT_CURRENCY), ";
|
||||
}
|
||||
sql += "PAYMENT_PRICE = IFNULL (?, PAYMENT_PRICE), ";
|
||||
}
|
||||
if (application.getCategory() != null && application.getCategory().getId() != 0) {
|
||||
sql += "APPLICATION_CATEGORY_ID = IFNULL (?, APPLICATION_CATEGORY_ID), ";
|
||||
}
|
||||
if (application.getPlatform() != null && application.getPlatform().getId() != 0) {
|
||||
sql += "PLATFORM_ID = IFNULL (?, PLATFORM_ID), ";
|
||||
}
|
||||
|
||||
sql += "TENANT_ID = IFNULL (?, TENANT_ID) WHERE UUID = ?";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(++index, application.getName());
|
||||
stmt.setString(++index, application.getShortDescription());
|
||||
stmt.setString(++index, application.getDescription());
|
||||
stmt.setString(++index, application.getIconName());
|
||||
stmt.setString(++index, application.getBannerName());
|
||||
stmt.setString(++index, application.getVideoName());
|
||||
stmt.setString(++index, JSONUtil.listToJsonArrayString(application.getScreenshots()));
|
||||
stmt.setDate(++index, new Date(application.getModifiedAt().getTime()));
|
||||
if (application.getPayment() != null) {
|
||||
stmt.setBoolean(++index, application.getPayment().isFreeApp());
|
||||
if (application.getPayment().getPaymentCurrency() != null) {
|
||||
stmt.setString(++index, application.getPayment().getPaymentCurrency());
|
||||
}
|
||||
stmt.setFloat(++index, application.getPayment().getPaymentPrice());
|
||||
}
|
||||
|
||||
if (application.getCategory() != null && application.getCategory().getId() != 0) {
|
||||
stmt.setInt(++index, application.getCategory().getId());
|
||||
}
|
||||
if (application.getPlatform() != null && application.getPlatform().getId() != 0) {
|
||||
stmt.setInt(++index, application.getPlatform().getId());
|
||||
}
|
||||
stmt.setInt(++index, tenantId);
|
||||
stmt.setString(++index, application.getUuid());
|
||||
stmt.executeUpdate();
|
||||
|
||||
application.setId(getApplicationId(application.getUuid()));
|
||||
|
||||
sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, application.getId());
|
||||
stmt.executeUpdate();
|
||||
|
||||
// delete existing properties and add new ones. if no properties are set, existing ones will be deleted.
|
||||
sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, application.getId());
|
||||
stmt.executeUpdate();
|
||||
|
||||
insertApplicationTagsAndProperties(application, stmt, conn, isBatchExecutionSupported);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while adding the application", e);
|
||||
}
|
||||
|
||||
return application;
|
||||
}
|
||||
|
||||
/**
|
||||
* To insert application properties and Tags
|
||||
*
|
||||
* @param application Application in which the properties and tags need to be inserted
|
||||
*/
|
||||
private void insertApplicationTagsAndProperties (Application application, PreparedStatement stmt, Connection
|
||||
conn, boolean isBatchExecutionSupported) throws SQLException {
|
||||
String sql;
|
||||
if (application.getTags() != null && application.getTags().size() > 0) {
|
||||
sql = "INSERT INTO APPM_APPLICATION_TAG (NAME, APPLICATION_ID) VALUES (?, ?); ";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
for (String tag : application.getTags()) {
|
||||
stmt.setString(1, tag);
|
||||
stmt.setInt(2, application.getId());
|
||||
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.addBatch();
|
||||
} else {
|
||||
stmt.execute();
|
||||
}
|
||||
}
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.executeBatch();
|
||||
}
|
||||
}
|
||||
|
||||
if (application.getProperties() != null && application.getProperties().size() > 0) {
|
||||
sql = "INSERT INTO APPM_APPLICATION_PROPERTY (PROP_KEY, PROP_VAL, APPLICATION_ID) VALUES (?, ?, ?); ";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
Iterator it = application.getProperties().entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<String, String> property = (Map.Entry) it.next();
|
||||
stmt.setString(1, property.getKey());
|
||||
stmt.setString(2, property.getValue());
|
||||
stmt.setInt(3, application.getId());
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.addBatch();
|
||||
} else {
|
||||
stmt.execute();
|
||||
}
|
||||
}
|
||||
if (isBatchExecutionSupported) {
|
||||
stmt.executeBatch();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteApplication(String uuid) throws ApplicationManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, uuid);
|
||||
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 deleting the application: " + uuid, e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteProperties(int applicationId) throws ApplicationManagementDAOException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
String sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, applicationId);
|
||||
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 deleting properties of application: " + applicationId, e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTags(int applicationId) throws ApplicationManagementDAOException {
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, applicationId);
|
||||
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 deleting tags of application: " + applicationId, e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getApplicationId(String uuid) throws ApplicationManagementDAOException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
|
||||
int id = 0;
|
||||
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
sql += "SELECT ID FROM APPM_APPLICATION WHERE UUID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, uuid);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void editProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,174 +0,0 @@
|
||||
/*
|
||||
* 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.core.dao.impl.application;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.wso2.carbon.device.application.mgt.common.*;
|
||||
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
||||
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This class holds the generic implementation of ApplicationDAO which can be used to support ANSI db syntax.
|
||||
*/
|
||||
public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl {
|
||||
|
||||
private static final Log log = LogFactory.getLog(H2ApplicationDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException {
|
||||
|
||||
if(log.isDebugEnabled()){
|
||||
log.debug("Getting application data from the database");
|
||||
log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset()));
|
||||
}
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
ApplicationList applicationList = new ApplicationList();
|
||||
List<Application> applications = new ArrayList<>();
|
||||
Pagination pagination = new Pagination();
|
||||
|
||||
if (filter == null) {
|
||||
throw new ApplicationManagementDAOException("Filter need to be instantiated");
|
||||
} else {
|
||||
pagination.setLimit(filter.getLimit());
|
||||
pagination.setOffset(filter.getOffset());
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
conn = this.getConnection();
|
||||
|
||||
sql += "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER," +
|
||||
" CAT.NAME AS CAT_NAME ";
|
||||
sql += "FROM APPM_APPLICATION AS APP ";
|
||||
sql += "INNER JOIN APPM_PLATFORM_APPLICATION_MAPPING AS APM ON APP.PLATFORM_APPLICATION_MAPPING_ID = APM.ID ";
|
||||
sql += "INNER JOIN APPM_PLATFORM AS APL ON APM.PLATFORM_ID = APL.ID ";
|
||||
sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON APP.APPLICATION_CATEGORY_ID = CAT.ID ";
|
||||
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
sql += "WHERE APP.NAME LIKE ? ";
|
||||
}
|
||||
sql += "LIMIT ?,?;";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
int index = 0;
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
stmt.setString(++index, "%" + filter.getSearchQuery() + "%");
|
||||
}
|
||||
stmt.setInt(++index, filter.getOffset());
|
||||
stmt.setInt(++index, filter.getLimit());
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
int length = 0;
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
//Getting properties
|
||||
sql = "SELECT * FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsProperties = stmt.executeQuery();
|
||||
|
||||
//Getting tags
|
||||
sql = "SELECT * FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsTags = stmt.executeQuery();
|
||||
|
||||
applications.add(Util.loadApplication(rs, rsProperties, rsTags));
|
||||
Util.cleanupResources(null, rsProperties);
|
||||
Util.cleanupResources(null, rsTags);
|
||||
length++;
|
||||
}
|
||||
|
||||
pagination.setSize(length);
|
||||
pagination.setCount(this.getApplicationCount(filter));
|
||||
applicationList.setApplications(applications);
|
||||
applicationList.setPagination(pagination);
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} catch (JSONException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
return applicationList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getApplicationId(String uuid) throws ApplicationManagementDAOException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteApplication(String uuid) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteProperties(int applicationId) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTags(int applicationId) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Application editApplication(Application application) throws ApplicationManagementDAOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void editProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,348 +0,0 @@
|
||||
/*
|
||||
* 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.core.dao.impl.application;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.wso2.carbon.device.application.mgt.common.*;
|
||||
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
||||
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
|
||||
import org.wso2.carbon.device.application.mgt.core.util.JSONUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This class holds the generic implementation of ApplicationDAO which can be used to support ANSI db syntax.
|
||||
*/
|
||||
public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MySQLApplicationDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Getting application data from the database");
|
||||
log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset()));
|
||||
}
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
ApplicationList applicationList = new ApplicationList();
|
||||
List<Application> applications = new ArrayList<>();
|
||||
Pagination pagination = new Pagination();
|
||||
|
||||
if (filter == null) {
|
||||
throw new ApplicationManagementDAOException("Filter need to be instantiated");
|
||||
} else {
|
||||
pagination.setLimit(filter.getLimit());
|
||||
pagination.setOffset(filter.getOffset());
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
conn = this.getConnection();
|
||||
|
||||
sql += "SELECT SQL_CALC_FOUND_ROWS APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, ";
|
||||
sql += "CAT.ID AS CAT_ID, CAT.NAME AS CAT_NAME ";
|
||||
sql += "FROM APPM_APPLICATION AS APP ";
|
||||
sql += "INNER JOIN APPM_PLATFORM AS APL ON APP.PLATFORM_ID = APL.ID ";
|
||||
sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON APP.APPLICATION_CATEGORY_ID = CAT.ID ";
|
||||
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
sql += "WHERE APP.NAME LIKE ? ";
|
||||
}
|
||||
sql += "LIMIT ?,?;";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
int index = 0;
|
||||
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
|
||||
stmt.setString(++index, "%" + filter.getSearchQuery() + "%");
|
||||
}
|
||||
stmt.setInt(++index, filter.getOffset());
|
||||
stmt.setInt(++index, filter.getLimit());
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
int length = 0;
|
||||
sql = "SELECT FOUND_ROWS() AS COUNT;"; //TODO: from which tables????
|
||||
stmt = conn.prepareStatement(sql);
|
||||
ResultSet rsCount = stmt.executeQuery();
|
||||
if (rsCount.next()) {
|
||||
pagination.setCount(rsCount.getInt("COUNT"));
|
||||
}
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
//Getting properties
|
||||
sql = "SELECT * FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsProperties = stmt.executeQuery();
|
||||
|
||||
//Getting tags
|
||||
sql = "SELECT * FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID=?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, rs.getInt("ID"));
|
||||
ResultSet rsTags = stmt.executeQuery();
|
||||
|
||||
applications.add(Util.loadApplication(rs, rsProperties, rsTags));
|
||||
Util.cleanupResources(null, rsProperties);
|
||||
Util.cleanupResources(null, rsTags);
|
||||
length++;
|
||||
}
|
||||
|
||||
pagination.setSize(length);
|
||||
|
||||
applicationList.setApplications(applications);
|
||||
applicationList.setPagination(pagination);
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} catch (JSONException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e);
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
return applicationList;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void deleteApplication(String uuid) throws ApplicationManagementDAOException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, uuid);
|
||||
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 deleting the application: " + uuid, e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getApplicationId(String uuid) throws ApplicationManagementDAOException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
|
||||
int id = 0;
|
||||
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
sql += "SELECT ID FROM APPM_APPLICATION WHERE UUID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, uuid);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Application editApplication(Application application) throws ApplicationManagementDAOException {
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
String sql = "";
|
||||
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
sql += "UPDATE APPM_APPLICATION SET ";
|
||||
sql += "NAME = IFNULL (?, NAME), ";
|
||||
sql += "SHORT_DESCRIPTION = IFNULL (?, SHORT_DESCRIPTION), DESCRIPTION = IFNULL (?, DESCRIPTION), ";
|
||||
sql += "ICON_NAME = IFNULL (?, ICON_NAME), BANNER_NAME = IFNULL (?, BANNER_NAME), ";
|
||||
sql += "VIDEO_NAME = IFNULL (?, VIDEO_NAME), SCREENSHOTS = IFNULL (?, SCREENSHOTS), ";
|
||||
sql += "MODIFIED_AT = IFNULL (?, MODIFIED_AT), ";
|
||||
if (application.getPayment() != null) {
|
||||
sql += " IS_FREE = IFNULL (?, IS_FREE), ";
|
||||
if (application.getPayment().getPaymentCurrency() != null) {
|
||||
sql += "PAYMENT_CURRENCY = IFNULL (?, PAYMENT_CURRENCY), ";
|
||||
}
|
||||
sql += "PAYMENT_PRICE = IFNULL (?, PAYMENT_PRICE), ";
|
||||
}
|
||||
if (application.getCategory() != null && application.getCategory().getId() != 0) {
|
||||
sql += "APPLICATION_CATEGORY_ID = IFNULL (?, APPLICATION_CATEGORY_ID), ";
|
||||
}
|
||||
if (application.getPlatform() != null && application.getPlatform().getId() != 0) {
|
||||
sql += "PLATFORM_ID = IFNULL (?, PLATFORM_ID), ";
|
||||
}
|
||||
|
||||
sql += "TENANT_ID = IFNULL (?, TENANT_ID) ";
|
||||
sql += "WHERE UUID = ?";
|
||||
|
||||
int i = 0;
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(++i, application.getName());
|
||||
stmt.setString(++i, application.getShortDescription());
|
||||
stmt.setString(++i, application.getDescription());
|
||||
stmt.setString(++i, application.getIconName());
|
||||
stmt.setString(++i, application.getBannerName());
|
||||
stmt.setString(++i, application.getVideoName());
|
||||
stmt.setString(++i, JSONUtil.listToJsonArrayString(application.getScreenshots()));
|
||||
stmt.setDate(++i, new Date(application.getModifiedAt().getTime()));
|
||||
if (application.getPayment() != null) {
|
||||
stmt.setBoolean(++i, application.getPayment().isFreeApp());
|
||||
if (application.getPayment().getPaymentCurrency() != null) {
|
||||
stmt.setString(++i, application.getPayment().getPaymentCurrency());
|
||||
}
|
||||
stmt.setFloat(++i, application.getPayment().getPaymentPrice());
|
||||
}
|
||||
|
||||
if (application.getCategory() != null && application.getCategory().getId() != 0) {
|
||||
stmt.setInt(++i, application.getCategory().getId());
|
||||
}
|
||||
if (application.getPlatform() != null && application.getPlatform().getId() != 0) {
|
||||
stmt.setInt(++i, application.getPlatform().getId());
|
||||
}
|
||||
stmt.setInt(++i, application.getUser().getTenantId());
|
||||
stmt.setString(++i, application.getUuid());
|
||||
stmt.executeUpdate();
|
||||
|
||||
application.setId(getApplicationId(application.getUuid()));
|
||||
|
||||
if (application.getTags() != null && application.getTags().size() > 0) {
|
||||
sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, application.getId());
|
||||
stmt.executeUpdate();
|
||||
|
||||
sql = "INSERT INTO APPM_APPLICATION_TAG (NAME, APPLICATION_ID) VALUES (?, ?); ";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
for (String tag : application.getTags()) {
|
||||
stmt.setString(1, tag);
|
||||
stmt.setInt(2, application.getId());
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
}
|
||||
|
||||
// delete existing properties and add new ones. if no properties are set, existing ones will be deleted.
|
||||
sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, application.getId());
|
||||
stmt.executeUpdate();
|
||||
|
||||
sql = "INSERT INTO APPM_APPLICATION_PROPERTY (PROP_KEY, PROP_VAL, APPLICATION_ID) VALUES (?, ?, ?); ";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
for (String propKey : application.getProperties().keySet()) {
|
||||
stmt.setString(1, propKey);
|
||||
stmt.setString(2, application.getProperties().get(propKey));
|
||||
stmt.setInt(3, application.getId());
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
|
||||
} catch (DBConnectionException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
|
||||
} catch (SQLException e) {
|
||||
throw new ApplicationManagementDAOException("Error occurred while adding the application", e);
|
||||
}
|
||||
|
||||
return application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void editProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteProperties(int applicationId) throws ApplicationManagementDAOException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
String sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, applicationId);
|
||||
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 deleting properties of application: " + applicationId, e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTags(int applicationId) throws ApplicationManagementDAOException {
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getDBConnection();
|
||||
String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, applicationId);
|
||||
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 deleting tags of application: " + applicationId, e);
|
||||
} finally {
|
||||
Util.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue