forked from community/device-mgt-core
commit
83bc51b9df
@ -0,0 +1,258 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (pvt) Ltd. 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { Icon, message, notification, Radio, Table, Tag, Tooltip } from 'antd';
|
||||||
|
import TimeAgo from 'javascript-time-ago';
|
||||||
|
// Load locale-specific relative date/time formatting rules.
|
||||||
|
import en from 'javascript-time-ago/locale/en';
|
||||||
|
import { withConfigContext } from '../../../../../../../../components/ConfigContext';
|
||||||
|
|
||||||
|
let config = null;
|
||||||
|
|
||||||
|
const columns = [
|
||||||
|
{
|
||||||
|
title: 'Device',
|
||||||
|
dataIndex: 'name',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Type',
|
||||||
|
dataIndex: 'type',
|
||||||
|
key: 'type',
|
||||||
|
// eslint-disable-next-line react/display-name
|
||||||
|
render: type => {
|
||||||
|
const defaultPlatformIcons = config.defaultPlatformIcons;
|
||||||
|
let icon = defaultPlatformIcons.default.icon;
|
||||||
|
let color = defaultPlatformIcons.default.color;
|
||||||
|
let theme = defaultPlatformIcons.default.theme;
|
||||||
|
|
||||||
|
if (defaultPlatformIcons.hasOwnProperty(type)) {
|
||||||
|
icon = defaultPlatformIcons[type].icon;
|
||||||
|
color = defaultPlatformIcons[type].color;
|
||||||
|
theme = defaultPlatformIcons[type].theme;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span style={{ fontSize: 20, color: color, textAlign: 'center' }}>
|
||||||
|
<Icon type={icon} theme={theme} />
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
// todo add filtering options
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Owner',
|
||||||
|
dataIndex: 'enrolmentInfo',
|
||||||
|
key: 'owner',
|
||||||
|
render: enrolmentInfo => enrolmentInfo.owner,
|
||||||
|
// todo add filtering options
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Ownership',
|
||||||
|
dataIndex: 'enrolmentInfo',
|
||||||
|
key: 'ownership',
|
||||||
|
render: enrolmentInfo => enrolmentInfo.ownership,
|
||||||
|
// todo add filtering options
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Status',
|
||||||
|
dataIndex: 'enrolmentInfo',
|
||||||
|
key: 'status',
|
||||||
|
// eslint-disable-next-line react/display-name
|
||||||
|
render: enrolmentInfo => {
|
||||||
|
const status = enrolmentInfo.status.toLowerCase();
|
||||||
|
let color = '#f9ca24';
|
||||||
|
switch (status) {
|
||||||
|
case 'active':
|
||||||
|
color = '#badc58';
|
||||||
|
break;
|
||||||
|
case 'created':
|
||||||
|
color = '#6ab04c';
|
||||||
|
break;
|
||||||
|
case 'removed':
|
||||||
|
color = '#ff7979';
|
||||||
|
break;
|
||||||
|
case 'inactive':
|
||||||
|
color = '#f9ca24';
|
||||||
|
break;
|
||||||
|
case 'blocked':
|
||||||
|
color = '#636e72';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return <Tag color={color}>{status}</Tag>;
|
||||||
|
},
|
||||||
|
// todo add filtering options
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Last Updated',
|
||||||
|
dataIndex: 'enrolmentInfo',
|
||||||
|
key: 'dateOfLastUpdate',
|
||||||
|
// eslint-disable-next-line react/display-name
|
||||||
|
render: data => {
|
||||||
|
const { dateOfLastUpdate } = data;
|
||||||
|
const timeAgoString = getTimeAgo(dateOfLastUpdate);
|
||||||
|
return (
|
||||||
|
<Tooltip title={new Date(dateOfLastUpdate).toString()}>
|
||||||
|
{timeAgoString}
|
||||||
|
</Tooltip>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
// todo add filtering options
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const getTimeAgo = time => {
|
||||||
|
const timeAgo = new TimeAgo('en-US');
|
||||||
|
return timeAgo.format(time);
|
||||||
|
};
|
||||||
|
|
||||||
|
class EncryptedDeviceTable extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
config = this.props.context;
|
||||||
|
TimeAgo.addLocale(en);
|
||||||
|
this.state = {
|
||||||
|
data: [],
|
||||||
|
pagination: {},
|
||||||
|
loading: false,
|
||||||
|
isEncrypted: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.fetch();
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch data from api
|
||||||
|
fetch = (params = {}) => {
|
||||||
|
const config = this.props.context;
|
||||||
|
this.setState({ loading: true });
|
||||||
|
// get current page
|
||||||
|
const currentPage = params.hasOwnProperty('page') ? params.page : 1;
|
||||||
|
|
||||||
|
const extraParams = {
|
||||||
|
offset: 10 * (currentPage - 1), // calculate the offset
|
||||||
|
limit: 10,
|
||||||
|
requireDeviceInfo: true,
|
||||||
|
isEncrypted: this.state.isEncrypted,
|
||||||
|
};
|
||||||
|
|
||||||
|
const encodedExtraParams = Object.keys(extraParams)
|
||||||
|
.map(key => key + '=' + extraParams[key])
|
||||||
|
.join('&');
|
||||||
|
|
||||||
|
// send request to the invoker
|
||||||
|
axios
|
||||||
|
.get(
|
||||||
|
window.location.origin +
|
||||||
|
config.serverConfig.invoker.uri +
|
||||||
|
config.serverConfig.invoker.deviceMgt +
|
||||||
|
'/reports/encryption-status?' +
|
||||||
|
encodedExtraParams,
|
||||||
|
)
|
||||||
|
.then(res => {
|
||||||
|
if (res.status === 200) {
|
||||||
|
const pagination = { ...this.state.pagination };
|
||||||
|
this.setState({
|
||||||
|
loading: false,
|
||||||
|
data: res.data.data.devices,
|
||||||
|
pagination,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
if (error.hasOwnProperty('response') && error.response.status === 401) {
|
||||||
|
// todo display a popop with error
|
||||||
|
message.error('You are not logged in');
|
||||||
|
window.location.href = window.location.origin + '/entgra/login';
|
||||||
|
} else {
|
||||||
|
notification.error({
|
||||||
|
message: 'There was a problem',
|
||||||
|
duration: 0,
|
||||||
|
description: 'Error occurred while trying to load devices.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.setState({ loading: false });
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
handleTableChange = (pagination, filters, sorter) => {
|
||||||
|
const pager = { ...this.state.pagination };
|
||||||
|
pager.current = pagination.current;
|
||||||
|
this.setState({
|
||||||
|
pagination: pager,
|
||||||
|
});
|
||||||
|
this.fetch({
|
||||||
|
results: pagination.pageSize,
|
||||||
|
page: pagination.current,
|
||||||
|
sortField: sorter.field,
|
||||||
|
sortOrder: sorter.order,
|
||||||
|
...filters,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
handleModeChange = value => {
|
||||||
|
this.setState(
|
||||||
|
{
|
||||||
|
isEncrypted: value.target.value,
|
||||||
|
},
|
||||||
|
this.fetch,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { data, pagination, loading } = this.state;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Radio.Group
|
||||||
|
onChange={this.handleModeChange}
|
||||||
|
defaultValue={'true'}
|
||||||
|
style={{ marginBottom: 8, marginRight: 5 }}
|
||||||
|
>
|
||||||
|
<Radio.Button value={'true'}>Enabled Devices</Radio.Button>
|
||||||
|
<Radio.Button value={'false'}>Disabled Devices</Radio.Button>
|
||||||
|
</Radio.Group>
|
||||||
|
<div style={{ backgroundColor: '#ffffff', borderRadius: 5 }}>
|
||||||
|
<Table
|
||||||
|
columns={columns}
|
||||||
|
rowKey={record =>
|
||||||
|
record.deviceIdentifier +
|
||||||
|
record.enrolmentInfo.owner +
|
||||||
|
record.enrolmentInfo.ownership
|
||||||
|
}
|
||||||
|
dataSource={data}
|
||||||
|
pagination={{
|
||||||
|
...pagination,
|
||||||
|
size: 'small',
|
||||||
|
// position: "top",
|
||||||
|
showTotal: (total, range) =>
|
||||||
|
`showing ${range[0]}-${range[1]} of ${total} devices`,
|
||||||
|
// showQuickJumper: true
|
||||||
|
}}
|
||||||
|
loading={loading}
|
||||||
|
onChange={this.handleTableChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default withConfigContext(EncryptedDeviceTable);
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (pvt) Ltd. 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
import { PageHeader, Breadcrumb, Icon } from 'antd';
|
||||||
|
import { Link } from 'react-router-dom';
|
||||||
|
import EncryptedDeviceTable from './components/EncryptedDeviceTable/EncryptedDevicesTable';
|
||||||
|
|
||||||
|
class EncryptionStatus extends React.Component {
|
||||||
|
routes;
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.routes = props.routes;
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<PageHeader style={{ paddingTop: 0 }}>
|
||||||
|
<Breadcrumb style={{ paddingBottom: 16 }}>
|
||||||
|
<Breadcrumb.Item>
|
||||||
|
<Link to="/entgra/devices">
|
||||||
|
<Icon type="home" /> Home
|
||||||
|
</Link>
|
||||||
|
</Breadcrumb.Item>
|
||||||
|
<Breadcrumb.Item>
|
||||||
|
<Link to="/entgra/reports">Reports</Link>
|
||||||
|
</Breadcrumb.Item>
|
||||||
|
<Breadcrumb.Item>Encryption Status</Breadcrumb.Item>
|
||||||
|
</Breadcrumb>
|
||||||
|
<div className="wrap">
|
||||||
|
<h3>Encryption Status Report</h3>
|
||||||
|
</div>
|
||||||
|
<EncryptedDeviceTable />
|
||||||
|
</PageHeader>
|
||||||
|
<div
|
||||||
|
style={{ background: '#f0f2f5', padding: 24, minHeight: 720 }}
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default EncryptionStatus;
|
@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015, 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.mgt.core.dao;
|
|
||||||
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface ApplicationMappingDAO {
|
|
||||||
|
|
||||||
int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException;
|
|
||||||
|
|
||||||
void addApplicationMappings(int deviceId, List<Integer> applicationIds, int tenantId)
|
|
||||||
throws DeviceManagementDAOException;
|
|
||||||
|
|
||||||
void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List<Application> applications, int tenantId)
|
|
||||||
throws DeviceManagementDAOException;
|
|
||||||
|
|
||||||
void removeApplicationMapping(int deviceId, int enrolmentId, List<Integer> appIdList, int tenantId)
|
|
||||||
throws DeviceManagementDAOException;
|
|
||||||
}
|
|
@ -1,183 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015, 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.mgt.core.dao.impl;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.ApplicationMappingDAO;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.sql.*;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
|
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(ApplicationMappingDAOImpl.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int addApplicationMapping(int deviceId, int applicationId,
|
|
||||||
int tenantId) throws DeviceManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
int mappingId = -1;
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
|
|
||||||
"TENANT_ID) VALUES (?, ?, ?)";
|
|
||||||
stmt = conn.prepareStatement(sql, new String[]{"id"});
|
|
||||||
stmt.setInt(1, deviceId);
|
|
||||||
stmt.setInt(2, applicationId);
|
|
||||||
stmt.setInt(3, tenantId);
|
|
||||||
stmt.execute();
|
|
||||||
|
|
||||||
rs = stmt.getGeneratedKeys();
|
|
||||||
if (rs.next()) {
|
|
||||||
mappingId = rs.getInt(1);
|
|
||||||
}
|
|
||||||
return mappingId;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addApplicationMappings(int deviceId, List<Integer> applicationIds,
|
|
||||||
int tenantId) throws DeviceManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
|
|
||||||
"TENANT_ID) VALUES (?, ?, ?)";
|
|
||||||
|
|
||||||
conn.setAutoCommit(false);
|
|
||||||
stmt = conn.prepareStatement(sql);
|
|
||||||
|
|
||||||
for (int applicationId : applicationIds) {
|
|
||||||
stmt.setInt(1, deviceId);
|
|
||||||
stmt.setInt(2, applicationId);
|
|
||||||
stmt.setInt(3, tenantId);
|
|
||||||
stmt.addBatch();
|
|
||||||
}
|
|
||||||
stmt.executeBatch();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List<Application> applications, int tenantId)
|
|
||||||
throws DeviceManagementDAOException {
|
|
||||||
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
ByteArrayOutputStream bao = null;
|
|
||||||
ObjectOutputStream oos = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, ENROLMENT_ID, APPLICATION_ID, " +
|
|
||||||
"APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
|
||||||
|
|
||||||
conn.setAutoCommit(false);
|
|
||||||
stmt = conn.prepareStatement(sql);
|
|
||||||
|
|
||||||
for (Application application : applications) {
|
|
||||||
stmt.setInt(1, deviceId);
|
|
||||||
stmt.setInt(2, enrolmentId);
|
|
||||||
stmt.setInt(3, application.getId());
|
|
||||||
|
|
||||||
bao = new ByteArrayOutputStream();
|
|
||||||
oos = new ObjectOutputStream(bao);
|
|
||||||
oos.writeObject(application.getAppProperties());
|
|
||||||
stmt.setBytes(4, bao.toByteArray());
|
|
||||||
|
|
||||||
stmt.setInt(5, application.getMemoryUsage());
|
|
||||||
stmt.setBoolean(6, application.isActive());
|
|
||||||
|
|
||||||
stmt.setInt(7, tenantId);
|
|
||||||
stmt.addBatch();
|
|
||||||
}
|
|
||||||
stmt.executeBatch();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e);
|
|
||||||
} finally {
|
|
||||||
if (bao != null) {
|
|
||||||
try {
|
|
||||||
bao.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("Error occurred while closing ByteArrayOutputStream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (oos != null) {
|
|
||||||
try {
|
|
||||||
oos.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("Error occurred while closing ObjectOutputStream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeApplicationMapping(int deviceId, int enrolmentId, List<Integer> appIdList,
|
|
||||||
int tenantId) throws DeviceManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
try {
|
|
||||||
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
|
|
||||||
"APPLICATION_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
|
|
||||||
|
|
||||||
conn = this.getConnection();
|
|
||||||
for (int appId : appIdList) {
|
|
||||||
stmt = conn.prepareStatement(sql);
|
|
||||||
stmt.setInt(1, deviceId);
|
|
||||||
stmt.setInt(2, appId);
|
|
||||||
stmt.setInt(3, tenantId);
|
|
||||||
stmt.setInt(4, enrolmentId);
|
|
||||||
stmt.execute();
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while removing device application mapping", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Connection getConnection() throws SQLException {
|
|
||||||
return DeviceManagementDAOFactory.getConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,91 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2018 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.mgt.core.dao.impl;
|
|
||||||
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic DAO implementation for Application Management Operations
|
|
||||||
*/
|
|
||||||
public class GenericApplicationDAOImpl extends AbstractApplicationDAOImpl{
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Integer> addApplications(List<Application> applications,
|
|
||||||
int tenantId) throws DeviceManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs;
|
|
||||||
List<Integer> applicationIds = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " +
|
|
||||||
"CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES, " +
|
|
||||||
"APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE) " +
|
|
||||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new String[]{"id"});
|
|
||||||
|
|
||||||
for (Application application : applications) {
|
|
||||||
|
|
||||||
stmt.setString(1, application.getName());
|
|
||||||
stmt.setString(2, application.getPlatform());
|
|
||||||
stmt.setString(3, application.getCategory());
|
|
||||||
stmt.setString(4, application.getVersion());
|
|
||||||
stmt.setString(5, application.getType());
|
|
||||||
stmt.setString(6, application.getLocationUrl());
|
|
||||||
stmt.setString(7, application.getImageUrl());
|
|
||||||
stmt.setInt(8, tenantId);
|
|
||||||
|
|
||||||
// Removing the application properties saving from the application table.
|
|
||||||
stmt.setBigDecimal(9, null);
|
|
||||||
|
|
||||||
stmt.setString(10, application.getApplicationIdentifier());
|
|
||||||
|
|
||||||
// Removing the application memory
|
|
||||||
stmt.setInt(11, 0);
|
|
||||||
stmt.setBoolean(12, true);
|
|
||||||
|
|
||||||
stmt.executeUpdate();
|
|
||||||
|
|
||||||
rs = stmt.getGeneratedKeys();
|
|
||||||
if (rs.next()) {
|
|
||||||
applicationIds.add(rs.getInt(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return applicationIds;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Connection getConnection() throws SQLException {
|
|
||||||
return DeviceManagementDAOFactory.getConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,90 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2018 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.mgt.core.dao.impl;
|
|
||||||
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* PosgreSQL specific DAO implementation for Application Management Operations
|
|
||||||
*/
|
|
||||||
public class PostgreSQLApplicationDAOImpl extends AbstractApplicationDAOImpl{
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Integer> addApplications(List<Application> applications,
|
|
||||||
int tenantId) throws DeviceManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs;
|
|
||||||
List<Integer> applicationIds = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " +
|
|
||||||
"CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES, " +
|
|
||||||
"APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE) " +
|
|
||||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new String[]{"id"});
|
|
||||||
|
|
||||||
for (Application application : applications) {
|
|
||||||
|
|
||||||
stmt.setString(1, application.getName());
|
|
||||||
stmt.setString(2, application.getPlatform());
|
|
||||||
stmt.setString(3, application.getCategory());
|
|
||||||
stmt.setString(4, application.getVersion());
|
|
||||||
stmt.setString(5, application.getType());
|
|
||||||
stmt.setString(6, application.getLocationUrl());
|
|
||||||
stmt.setString(7, application.getImageUrl());
|
|
||||||
stmt.setInt(8, tenantId);
|
|
||||||
|
|
||||||
// Removing the application properties saving from the application table.
|
|
||||||
stmt.setBytes(9, null);
|
|
||||||
|
|
||||||
stmt.setString(10, application.getApplicationIdentifier());
|
|
||||||
|
|
||||||
// Removing the application memory
|
|
||||||
stmt.setInt(11, 0);
|
|
||||||
stmt.setBoolean(12, true);
|
|
||||||
|
|
||||||
stmt.executeUpdate();
|
|
||||||
|
|
||||||
rs = stmt.getGeneratedKeys();
|
|
||||||
if (rs.next()) {
|
|
||||||
applicationIds.add(rs.getInt(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return applicationIds;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Connection getConnection() throws SQLException {
|
|
||||||
return DeviceManagementDAOFactory.getConnection();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,85 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015, 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.mgt.core.dao;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.testng.Assert;
|
|
||||||
import org.testng.annotations.BeforeClass;
|
|
||||||
import org.testng.annotations.Test;
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
|
||||||
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
|
|
||||||
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class ApplicationPersistenceTests extends BaseDeviceManagementTest {
|
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(ApplicationPersistenceTests.class);
|
|
||||||
private ApplicationDAO applicationDAO = null;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAddApplication() {
|
|
||||||
/* Adding dummy application to the application store */
|
|
||||||
String testAppIdentifier = "test sample1";
|
|
||||||
try {
|
|
||||||
DeviceManagementDAOFactory.openConnection();
|
|
||||||
applicationDAO.addApplication(TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234);
|
|
||||||
} catch (DeviceManagementDAOException | SQLException e) {
|
|
||||||
log.error("Error occurred while adding application test sample1", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOFactory.closeConnection();
|
|
||||||
}
|
|
||||||
/* Retrieving the application by its name */
|
|
||||||
Application target = null;
|
|
||||||
try {
|
|
||||||
target = this.getApplication(testAppIdentifier, -1234);
|
|
||||||
} catch (DeviceManagementDAOException e) {
|
|
||||||
String msg = "Error occurred while retrieving application info";
|
|
||||||
log.error(msg, e);
|
|
||||||
Assert.fail(msg, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isMock()) {
|
|
||||||
Assert.assertEquals(target.getApplicationIdentifier(), testAppIdentifier,
|
|
||||||
"Application added is not as same as what's retrieved");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Application getApplication(String appIdentifier, int tenantId) throws DeviceManagementDAOException {
|
|
||||||
Application application = null;
|
|
||||||
try {
|
|
||||||
DeviceManagementDAOFactory.openConnection();
|
|
||||||
application = applicationDAO.getApplication(appIdentifier, tenantId);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.error("Error occurred while metadata corresponding to the application '" + appIdentifier + "'", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOFactory.closeConnection();
|
|
||||||
}
|
|
||||||
return application;
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
@Override
|
|
||||||
public void init() throws Exception {
|
|
||||||
this.initDataSource();
|
|
||||||
applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in new issue