forked from community/device-mgt-core
parent
fa88673ac4
commit
d5ba266583
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* 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.ApplicationList;
|
||||
import org.wso2.carbon.device.application.mgt.common.Filter;
|
||||
import org.wso2.carbon.device.application.mgt.common.Pagination;
|
||||
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOException;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOUtil;
|
||||
import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractApplicationDAOImpl;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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(ApplicationManagementDAOUtil.loadApplication(rs, rsProperties, rsTags));
|
||||
ApplicationManagementDAOUtil.cleanupResources(null, rsProperties);
|
||||
ApplicationManagementDAOUtil.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 {
|
||||
ApplicationManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return applicationList;
|
||||
}
|
||||
|
||||
private Connection getConnection() throws DBConnectionException {
|
||||
return ConnectionManagerUtil.getConnection();
|
||||
}
|
||||
}
|
14
components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java → 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
14
components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java → 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
Loading…
Reference in new issue