|
| 1 | +/** |
| 2 | + * * Copyright (C) 2012 Citrix Systems, Inc. All rights reserved |
| 3 | +* |
| 4 | + * |
| 5 | + * This software is licensed under the GNU General Public License v3 or later. |
| 6 | + * |
| 7 | + * It is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or any later version. |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +package com.cloud.usage.dao; |
| 21 | + |
| 22 | +import java.sql.PreparedStatement; |
| 23 | +import java.sql.ResultSet; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Date; |
| 26 | +import java.util.List; |
| 27 | +import java.util.TimeZone; |
| 28 | + |
| 29 | +import javax.ejb.Local; |
| 30 | + |
| 31 | +import org.apache.log4j.Logger; |
| 32 | + |
| 33 | +import com.cloud.usage.UsageSecurityGroupVO; |
| 34 | +import com.cloud.utils.DateUtil; |
| 35 | +import com.cloud.utils.db.GenericDaoBase; |
| 36 | +import com.cloud.utils.db.Transaction; |
| 37 | + |
| 38 | +@Local(value={UsageSecurityGroupDao.class}) |
| 39 | +public class UsageSecurityGroupDaoImpl extends GenericDaoBase<UsageSecurityGroupVO, Long> implements UsageSecurityGroupDao { |
| 40 | + public static final Logger s_logger = Logger.getLogger(UsageSecurityGroupDaoImpl.class.getName()); |
| 41 | + |
| 42 | + protected static final String UPDATE_DELETED = "UPDATE usage_security_group SET deleted = ? WHERE account_id = ? AND vm_instance_id = ? AND security_group_id = ? and deleted IS NULL"; |
| 43 | + protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT zone_id, account_id, domain_id, vm_instance_id, security_group_id, created, deleted " + |
| 44 | + "FROM usage_security_group " + |
| 45 | + "WHERE account_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR " + |
| 46 | + " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))"; |
| 47 | + protected static final String GET_USAGE_RECORDS_BY_DOMAIN = "SELECT zone_id, account_id, domain_id, vm_instance_id, security_group_id, created, deleted " + |
| 48 | + "FROM usage_security_group " + |
| 49 | + "WHERE domain_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR " + |
| 50 | + " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))"; |
| 51 | + protected static final String GET_ALL_USAGE_RECORDS = "SELECT zone_id, account_id, domain_id, vm_instance_id, security_group_id, created, deleted " + |
| 52 | + "FROM usage_security_group " + |
| 53 | + "WHERE (deleted IS NULL) OR (created BETWEEN ? AND ?) OR " + |
| 54 | + " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?))"; |
| 55 | + |
| 56 | + public UsageSecurityGroupDaoImpl() {} |
| 57 | + |
| 58 | + public void update(UsageSecurityGroupVO usage) { |
| 59 | + Transaction txn = Transaction.open(Transaction.USAGE_DB); |
| 60 | + PreparedStatement pstmt = null; |
| 61 | + try { |
| 62 | + txn.start(); |
| 63 | + if (usage.getDeleted() != null) { |
| 64 | + pstmt = txn.prepareAutoCloseStatement(UPDATE_DELETED); |
| 65 | + pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted())); |
| 66 | + pstmt.setLong(2, usage.getAccountId()); |
| 67 | + pstmt.setLong(3, usage.getVmInstanceId()); |
| 68 | + pstmt.setLong(4, usage.getSecurityGroupId()); |
| 69 | + } |
| 70 | + pstmt.executeUpdate(); |
| 71 | + txn.commit(); |
| 72 | + } catch (Exception e) { |
| 73 | + txn.rollback(); |
| 74 | + s_logger.warn("Error updating UsageSecurityGroupVO", e); |
| 75 | + } finally { |
| 76 | + txn.close(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public List<UsageSecurityGroupVO> getUsageRecords(Long accountId, Long domainId, Date startDate, Date endDate, boolean limit, int page) { |
| 82 | + List<UsageSecurityGroupVO> usageRecords = new ArrayList<UsageSecurityGroupVO>(); |
| 83 | + |
| 84 | + Long param1 = null; |
| 85 | + String sql = null; |
| 86 | + if (accountId != null) { |
| 87 | + sql = GET_USAGE_RECORDS_BY_ACCOUNT; |
| 88 | + param1 = accountId; |
| 89 | + } else if (domainId != null) { |
| 90 | + sql = GET_USAGE_RECORDS_BY_DOMAIN; |
| 91 | + param1 = domainId; |
| 92 | + } else { |
| 93 | + sql = GET_ALL_USAGE_RECORDS; |
| 94 | + } |
| 95 | + |
| 96 | + if (limit) { |
| 97 | + int startIndex = 0; |
| 98 | + if (page > 0) { |
| 99 | + startIndex = 500 * (page-1); |
| 100 | + } |
| 101 | + sql += " LIMIT " + startIndex + ",500"; |
| 102 | + } |
| 103 | + |
| 104 | + Transaction txn = Transaction.open(Transaction.USAGE_DB); |
| 105 | + PreparedStatement pstmt = null; |
| 106 | + |
| 107 | + try { |
| 108 | + int i = 1; |
| 109 | + pstmt = txn.prepareAutoCloseStatement(sql); |
| 110 | + if (param1 != null) { |
| 111 | + pstmt.setLong(i++, param1); |
| 112 | + } |
| 113 | + pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate)); |
| 114 | + pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate)); |
| 115 | + pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate)); |
| 116 | + pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate)); |
| 117 | + pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate)); |
| 118 | + pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate)); |
| 119 | + |
| 120 | + ResultSet rs = pstmt.executeQuery(); |
| 121 | + while (rs.next()) { |
| 122 | + //zoneId, account_id, domain_id, vm_instance_id, security_group_id, created, deleted |
| 123 | + Long zoneId = Long.valueOf(rs.getLong(1)); |
| 124 | + Long acctId = Long.valueOf(rs.getLong(2)); |
| 125 | + Long dId = Long.valueOf(rs.getLong(3)); |
| 126 | + long vmId = Long.valueOf(rs.getLong(4)); |
| 127 | + long sgId = Long.valueOf(rs.getLong(5)); |
| 128 | + Date createdDate = null; |
| 129 | + Date deletedDate = null; |
| 130 | + String createdTS = rs.getString(6); |
| 131 | + String deletedTS = rs.getString(7); |
| 132 | + |
| 133 | + |
| 134 | + if (createdTS != null) { |
| 135 | + createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS); |
| 136 | + } |
| 137 | + if (deletedTS != null) { |
| 138 | + deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS); |
| 139 | + } |
| 140 | + |
| 141 | + usageRecords.add(new UsageSecurityGroupVO(zoneId, acctId, dId, vmId, sgId, createdDate, deletedDate)); |
| 142 | + } |
| 143 | + } catch (Exception e) { |
| 144 | + txn.rollback(); |
| 145 | + s_logger.warn("Error getting usage records", e); |
| 146 | + } finally { |
| 147 | + txn.close(); |
| 148 | + } |
| 149 | + |
| 150 | + return usageRecords; |
| 151 | + } |
| 152 | +} |
0 commit comments