forked from community/device-mgt-core
fixes https://gitlab.com/entgra/product-iots/-/issues/1226feature/traccar-sync
parent
a4b2a325b1
commit
5ad9e3e6b4
@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.common;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@ApiModel(value = "Billing", description = "This class carries all information related to a device billing.")
|
||||||
|
public class Billing implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1998101711L;
|
||||||
|
|
||||||
|
@ApiModelProperty(name = "invoiceId", value = "ID of the billing.",
|
||||||
|
required = false)
|
||||||
|
private int invoiceId;
|
||||||
|
|
||||||
|
@ApiModelProperty(name = "deviceId", value = "Device id of the billing.",
|
||||||
|
required = false)
|
||||||
|
private int deviceId;
|
||||||
|
|
||||||
|
@ApiModelProperty(name = "tenantId", value = "Tenant of the device.",
|
||||||
|
required = false)
|
||||||
|
private int tenantId;
|
||||||
|
|
||||||
|
@ApiModelProperty(name = "billingStart", value = "Start date of the billing period.", required = false)
|
||||||
|
private Long billingStart;
|
||||||
|
|
||||||
|
@ApiModelProperty(name = "billingEnd", value = "End date of the billing period.", required = false)
|
||||||
|
private Long billingEnd;
|
||||||
|
|
||||||
|
@ApiModelProperty(name = "deviceCount", value = "Device count for a billing period",
|
||||||
|
required = false)
|
||||||
|
private int deviceCount;
|
||||||
|
|
||||||
|
|
||||||
|
public Billing() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Billing(int invoiceId, int deviceId, int tenantId, Long billingStart, Long billingEnd) {
|
||||||
|
this.invoiceId = invoiceId;
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
this.tenantId = tenantId;
|
||||||
|
this.billingStart = billingStart;
|
||||||
|
this.billingEnd = billingEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getInvoiceId() {
|
||||||
|
return invoiceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInvoiceId(int invoiceId) {
|
||||||
|
this.invoiceId = invoiceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDeviceId() {
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceId(int deviceId) {
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTenantId() {
|
||||||
|
return tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantId(int tenantId) {
|
||||||
|
this.tenantId = tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getBillingStart() {
|
||||||
|
return billingStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBillingStart(Long billingStart) {
|
||||||
|
this.billingStart = billingStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getBillingEnd() {
|
||||||
|
return billingEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBillingEnd(Long billingEnd) {
|
||||||
|
this.billingEnd = billingEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDeviceCount() {
|
||||||
|
return deviceCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceCount(int deviceCount) {
|
||||||
|
this.deviceCount = deviceCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return new Gson().toJson(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.core.dao;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Billing;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface BillingDAO {
|
||||||
|
|
||||||
|
void addBilling(int deviceId, int tenantId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
List<Billing> getBilling(int deviceId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException;
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.core.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Billing;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.BillingDAO;
|
||||||
|
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.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BillingDAOImpl implements BillingDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addBilling(int deviceId, int tenantId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "INSERT INTO DM_BILLING(DEVICE_ID, TENANT_ID, BILLING_START, BILLING_END) VALUES(?, ?, ?, ?)";
|
||||||
|
stmt = conn.prepareStatement(sql, new String[] {"invoice_id"});
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setInt(2,tenantId);
|
||||||
|
stmt.setTimestamp(3, billingStart);
|
||||||
|
stmt.setTimestamp(4, billingEnd);
|
||||||
|
stmt.execute();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while adding billing period", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Billing> getBilling(int deviceId, Timestamp billingStart, Timestamp billingEnd) throws DeviceManagementDAOException {
|
||||||
|
List<Billing> billings = new ArrayList<>();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
EnrolmentInfo.Status status = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql;
|
||||||
|
|
||||||
|
sql = "SELECT * FROM DM_BILLING WHERE DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
Billing bill = new Billing(rs.getInt("INVOICE_ID"), rs.getInt("DEVICE_ID"),rs.getInt("TENANT_ID"),
|
||||||
|
(rs.getTimestamp("BILLING_START").getTime()), (rs.getTimestamp("BILLING_END").getTime()));
|
||||||
|
billings.add(bill);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred getting billing periods", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return billings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue