From f5be8aaf8f3de491d09c287e3fa6f0497a5e5e06 Mon Sep 17 00:00:00 2001 From: Rohit Yadav Date: Thu, 23 Jul 2015 15:11:02 +0530 Subject: [PATCH] quota: email template backend and apis Signed-off-by: Rohit Yadav --- client/tomcatconf/commands.properties.in | 13 +- .../api/command/QuotaEmailTemplateAddCmd.java | 82 ------------- .../command/QuotaEmailTemplateListCmd.java | 56 +++++++++ .../command/QuotaEmailTemplateUpdateCmd.java | 111 ++++++++++++++++++ .../response/QuotaEmailTemplateResponse.java | 74 ++++++------ .../api/response/QuotaResponseBuilder.java | 5 + .../response/QuotaResponseBuilderImpl.java | 51 +++++++- .../cloudstack/quota/QuotaServiceImpl.java | 12 +- .../quota/constant/QuotaConfig.java | 65 +++++----- .../quota/dao/QuotaEmailTemplatesDao.java | 16 +-- .../quota/dao/QuotaEmailTemplatesDaoImpl.java | 50 +++++--- .../quota/dao/QuotaTariffDaoImpl.java | 4 +- .../cloudstack/quota/job/QuotaManager.java | 2 +- .../quota/job/QuotaManagerImpl.java | 4 +- .../quota/vo/QuotaEmailTemplatesVO.java | 61 ++++------ setup/db/db/schema-451to452.sql | 14 ++- 16 files changed, 381 insertions(+), 239 deletions(-) delete mode 100644 plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateAddCmd.java create mode 100644 plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateListCmd.java create mode 100644 plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateUpdateCmd.java diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in index 161a0cce7d7..981fae74eec 100644 --- a/client/tomcatconf/commands.properties.in +++ b/client/tomcatconf/commands.properties.in @@ -783,13 +783,12 @@ listOpenDaylightControllers=1 addGloboDnsHost=1 ### Quota Service -quotaTariffList=15 -quotaTariffUpdate=7 +quotaRefresh=1 quotaStatement=15 -quotaCredits=1 quotaBalance=15 -quotaRefresh=7 -quotaEmailTemplateUpdate=15 -quotaEmailTemplateDelete=15 -quotaEmailTemplateAdd=15 +quotaCredits=1 +quotaTariffList=15 +quotaTariffUpdate=1 +quotaEmailTemplateList=1 +quotaEmailTemplateUpdate=1 diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateAddCmd.java b/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateAddCmd.java deleted file mode 100644 index 44aebc3b95e..00000000000 --- a/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateAddCmd.java +++ /dev/null @@ -1,82 +0,0 @@ -//Licensed to the Apache Software Foundation (ASF) under one -//or more contributor license agreements. See the NOTICE file -//distributed with this work for additional information -//regarding copyright ownership. The ASF 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.apache.cloudstack.api.command; - -import org.apache.log4j.Logger; -import org.apache.cloudstack.api.APICommand; -import org.apache.cloudstack.api.BaseListCmd; -import org.apache.cloudstack.api.Parameter; -import org.apache.cloudstack.api.response.QuotaEmailTemplateResponse; - -@APICommand(name = "quotaEmailTemplateAdd", responseObject = QuotaEmailTemplateResponse.class, description = "Add a new email template", since = "4.2.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) -public class QuotaEmailTemplateAddCmd extends BaseListCmd { - - public static final Logger s_logger = Logger.getLogger(QuotaEmailTemplateAddCmd.class.getName()); - - private static final String s_name = "quotaemailtemplateresponse"; - - @Parameter(name = "templatename", type = CommandType.STRING, required=true, description = "The name of email template") - private String templateName; - - @Parameter(name = "templatetext", type = CommandType.STRING, required=true, description = "The text of the email") - private Long templateText; - - @Parameter(name = "locale", type = CommandType.STRING, description = "The locale of the email text") - private Integer locale; - - public QuotaEmailTemplateAddCmd() { - super(); - } - - @Override - public String getCommandName() { - return s_name; - } - - @Override - public void execute() { - - final QuotaEmailTemplateResponse templResponse = null; - - setResponseObject(templResponse); - } - - public String getTemplateName() { - return templateName; - } - - public void setTemplateName(String templateName) { - this.templateName = templateName; - } - - public Long getTemplateText() { - return templateText; - } - - public void setTemplateText(Long templateText) { - this.templateText = templateText; - } - - public Integer getLocale() { - return locale; - } - - public void setLocale(Integer locale) { - this.locale = locale; - } - -} diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateListCmd.java b/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateListCmd.java new file mode 100644 index 00000000000..e2985febe21 --- /dev/null +++ b/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateListCmd.java @@ -0,0 +1,56 @@ +//Licensed to the Apache Software Foundation (ASF) under one +//or more contributor license agreements. See the NOTICE file +//distributed with this work for additional information +//regarding copyright ownership. The ASF 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.apache.cloudstack.api.command; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.BaseListCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.response.ListResponse; +import org.apache.cloudstack.api.response.QuotaEmailTemplateResponse; +import org.apache.cloudstack.api.response.QuotaResponseBuilder; +import org.apache.log4j.Logger; + +import javax.inject.Inject; + +@APICommand(name = "quotaEmailTemplateList", responseObject = QuotaEmailTemplateResponse.class, description = "Lists all quota email templates", since = "4.6.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) +public class QuotaEmailTemplateListCmd extends BaseListCmd { + public static final Logger s_logger = Logger.getLogger(QuotaEmailTemplateListCmd.class.getName()); + private static final String s_name = "quotaemailtemplatelistresponse"; + + @Inject + QuotaResponseBuilder _quotaResponseBuilder; + + @Parameter(name = "templatetype", type = CommandType.STRING, description = "List by type of the quota email template, allowed types: QUOTA_LOW, QUOTA_EMPTY") + private String templateName; + + public String getTemplateName() { + return templateName; + } + + @Override + public void execute() { + final ListResponse response = new ListResponse(); + response.setResponses(_quotaResponseBuilder.listQuotaEmailTemplates(this)); + response.setResponseName(getCommandName()); + setResponseObject(response); + } + + @Override + public String getCommandName() { + return s_name; + } +} diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateUpdateCmd.java b/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateUpdateCmd.java new file mode 100644 index 00000000000..85299bcf00d --- /dev/null +++ b/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaEmailTemplateUpdateCmd.java @@ -0,0 +1,111 @@ +//Licensed to the Apache Software Foundation (ASF) under one +//or more contributor license agreements. See the NOTICE file +//distributed with this work for additional information +//regarding copyright ownership. The ASF 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.apache.cloudstack.api.command; + +import com.cloud.user.Account; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.BaseCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.response.QuotaResponseBuilder; +import org.apache.cloudstack.api.response.SuccessResponse; +import org.apache.cloudstack.quota.constant.QuotaConfig; +import org.apache.log4j.Logger; + +import javax.inject.Inject; +import java.util.Arrays; + +@APICommand(name = "quotaEmailTemplateUpdate", responseObject = SuccessResponse.class, description = "Updates existing email templates for quota alerts", since = "4.2.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) +public class QuotaEmailTemplateUpdateCmd extends BaseCmd { + public static final Logger s_logger = Logger.getLogger(QuotaEmailTemplateUpdateCmd.class.getName()); + private static final String s_name = "quotaemailtemplateupdateresponse"; + + @Inject + QuotaResponseBuilder _quotaResponseBuilder; + + @Parameter(name = "templatetype", type = CommandType.STRING, required=true, description = "Type of the quota email template, allowed types: QUOTA_LOW, QUOTA_EMPTY") + private String templateName; + + @Parameter(name = "templatesubject", type = CommandType.STRING, required=true, description = "The quota email template subject") + private String templateSubject; + + @Parameter(name = "templatebody", type = CommandType.STRING, required=true, description = "The quota email template body") + private String templateBody; + + @Parameter(name = "locale", type = CommandType.STRING, description = "The locale of the email text") + private String locale; + + @Override + public void execute() { + final String templateName = getTemplateName(); + if (templateName == null || getTemplateSubject() == null || getTemplateBody() == null) { + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Failed to update quota email template due to empty or invalid template name or text"); + } + + boolean isValidTemplateName = false; + for (QuotaConfig.QuotaEmailTemplateTypes e: QuotaConfig.QuotaEmailTemplateTypes.values()) { + if (e.toString().equalsIgnoreCase(templateName)) { + isValidTemplateName = true; + setTemplateName(e.toString()); + break; + } + } + if (!isValidTemplateName) { + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Invalid quota email template type, allowed values are: " + Arrays.toString(QuotaConfig.QuotaEmailTemplateTypes.values())); + } + + if (!_quotaResponseBuilder.updateQuotaEmailTemplate(this)) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable to update quota email template due to an internal error"); + } + final SuccessResponse response = new SuccessResponse(); + response.setResponseName(getCommandName()); + response.setSuccess(true); + setResponseObject(response); + } + + @Override + public String getCommandName() { + return s_name; + } + + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } + + private void setTemplateName(String templateName) { + this.templateName = templateName; + } + + public String getTemplateName() { + return templateName; + } + + public String getTemplateSubject() { + return templateSubject; + } + + public String getTemplateBody() { + return templateBody; + } + + public String getLocale() { + return locale; + } + +} diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaEmailTemplateResponse.java b/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaEmailTemplateResponse.java index 40d97a20506..c4a2b7c3a60 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaEmailTemplateResponse.java +++ b/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaEmailTemplateResponse.java @@ -16,67 +16,75 @@ //under the License. package org.apache.cloudstack.api.response; -import java.sql.Timestamp; - +import com.cloud.serializer.Param; import com.google.gson.annotations.SerializedName; - -import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.BaseResponse; -import com.cloud.serializer.Param; +import java.util.Date; public class QuotaEmailTemplateResponse extends BaseResponse { + @SerializedName("templatetype") + @Param(description = "Template type") + private String templateType; - @SerializedName(ApiConstants.ID) - @Param(description = "the ID of the credit") - private String id; + @SerializedName("templatesubject") + @Param(description = "The quota email template subject") + private String templateSubject; - @SerializedName(ApiConstants.ACCOUNT) - @Param(description = "the account name of the api remaining count") - private String accountName; + @SerializedName("templatebody") + @Param(description = "The quota email template content") + private String templateText; - @SerializedName(ApiConstants.DOMAIN_ID) - @Param(description = "the domain ID of the iam policy") - private String domainId; + @SerializedName("locale") + @Param(description = "The quota email template locale") + private String locale; - @SerializedName("sent_on") - @Param(description = "the account name of the admin who updated the credits") - private Timestamp SentOn; + @SerializedName("last_updated") + @Param(description = "Last date/time when template was updated") + private Date lastUpdatedOn; public QuotaEmailTemplateResponse() { super(); + this.setObjectName("quotaemailtemplate"); } - public String getId() { - return id; + public String getTemplateType() { + return templateType; } - public void setId(String id) { - this.id = id; + public void setTemplateType(String templateType) { + this.templateType = templateType; } - public String getAccountName() { - return accountName; + public String getTemplateSubject() { + return templateSubject; } - public void setAccountName(String accountName) { - this.accountName = accountName; + public void setTemplateSubject(String templateSubject) { + this.templateSubject = templateSubject; } - public String getDomainId() { - return domainId; + public String getTemplateText() { + return templateText; } - public void setDomainId(String domainId) { - this.domainId = domainId; + public void setTemplateText(String templateText) { + this.templateText = templateText; } - public Timestamp getSentOn() { - return SentOn; + public String getLocale() { + return locale; } - public void setSentOn(Timestamp sentOn) { - SentOn = sentOn; + public void setLocale(String locale) { + this.locale = locale; } + public Date getLastUpdatedOn() { + return lastUpdatedOn; + } + + public void setLastUpdatedOn(Date lastUpdatedOn) { + this.lastUpdatedOn = lastUpdatedOn; + } } diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilder.java b/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilder.java index 232b3cc10cd..436e643fc6f 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilder.java +++ b/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilder.java @@ -18,6 +18,8 @@ package org.apache.cloudstack.api.response; import org.apache.cloudstack.api.command.QuotaBalanceCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd; import org.apache.cloudstack.api.command.QuotaStatementCmd; import org.apache.cloudstack.api.command.QuotaTariffListCmd; import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd; @@ -50,4 +52,7 @@ public interface QuotaResponseBuilder { QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy); + List listQuotaEmailTemplates(QuotaEmailTemplateListCmd cmd); + + boolean updateQuotaEmailTemplate(QuotaEmailTemplateUpdateCmd cmd); } diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java b/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java index b924780f526..cbab4a813b3 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java @@ -20,8 +20,9 @@ import com.cloud.exception.InvalidParameterValueException; import com.cloud.user.User; import com.cloud.user.dao.UserDao; import com.cloud.utils.db.TransactionLegacy; - import org.apache.cloudstack.api.command.QuotaBalanceCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd; import org.apache.cloudstack.api.command.QuotaStatementCmd; import org.apache.cloudstack.api.command.QuotaTariffListCmd; import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd; @@ -29,17 +30,19 @@ import org.apache.cloudstack.quota.QuotaService; import org.apache.cloudstack.quota.constant.QuotaTypes; import org.apache.cloudstack.quota.dao.QuotaBalanceDao; import org.apache.cloudstack.quota.dao.QuotaCreditsDao; +import org.apache.cloudstack.quota.dao.QuotaEmailTemplatesDao; import org.apache.cloudstack.quota.dao.QuotaTariffDao; import org.apache.cloudstack.quota.vo.QuotaBalanceVO; import org.apache.cloudstack.quota.vo.QuotaCreditsVO; +import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO; import org.apache.cloudstack.quota.vo.QuotaTariffVO; import org.apache.cloudstack.quota.vo.QuotaUsageVO; +import org.apache.commons.lang.StringEscapeUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import javax.ejb.Local; import javax.inject.Inject; - import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; @@ -60,6 +63,9 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder { private QuotaBalanceDao _quotaBalanceDao; @Inject private QuotaCreditsDao _quotaCreditsDao; + @Inject + private QuotaEmailTemplatesDao _quotaEmailTemplateDao; + @Inject private UserDao _userDao; @Inject @@ -295,6 +301,47 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder { return new QuotaCreditsResponse(result, creditor); } + private QuotaEmailTemplateResponse createQuotaEmailResponse(QuotaEmailTemplatesVO template) { + QuotaEmailTemplateResponse response = new QuotaEmailTemplateResponse(); + response.setTemplateType(template.getTemplateName()); + response.setTemplateSubject(template.getTemplateSubject()); + response.setTemplateText(template.getTemplateBody()); + response.setLocale(template.getLocale()); + response.setLastUpdatedOn(template.getLastUpdated()); + return response; + } + + @Override + public List listQuotaEmailTemplates(QuotaEmailTemplateListCmd cmd) { + final String templateName = cmd.getTemplateName(); + List templates = _quotaEmailTemplateDao.listAllQuotaEmailTemplates(templateName); + final List responses = new ArrayList(); + for (final QuotaEmailTemplatesVO template : templates) { + responses.add(createQuotaEmailResponse(template)); + } + return responses; + } + + @Override + public boolean updateQuotaEmailTemplate(QuotaEmailTemplateUpdateCmd cmd) { + final String templateName = cmd.getTemplateName(); + final String templateSubject = StringEscapeUtils.escapeHtml(cmd.getTemplateSubject()); + final String templateBody = StringEscapeUtils.escapeHtml(cmd.getTemplateBody()); + final String locale = cmd.getLocale(); + + final List templates = _quotaEmailTemplateDao.listAllQuotaEmailTemplates(templateName); + if (templates.size() == 1) { + final QuotaEmailTemplatesVO template = templates.get(0); + template.setTemplateSubject(templateSubject); + template.setTemplateBody(templateBody); + if (locale != null) { + template.setLocale(locale); + } + return _quotaEmailTemplateDao.updateQuotaEmailTemplate(template); + } + return false; + } + @Override public List getQuotaUsage(QuotaStatementCmd cmd) { return _quotaService.getQuotaUsage(cmd.getAccountId(), cmd.getAccountName(), cmd.getDomainId(), cmd.getUsageType(), cmd.getStartDate(), cmd.getEndDate()); diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java index 59f1f38790c..0e9d2f131b1 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java @@ -29,7 +29,8 @@ import com.cloud.utils.db.TransactionLegacy; import org.apache.cloudstack.api.command.QuotaBalanceCmd; import org.apache.cloudstack.api.command.QuotaCreditsCmd; -import org.apache.cloudstack.api.command.QuotaEmailTemplateAddCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd; import org.apache.cloudstack.api.command.QuotaRefreshCmd; import org.apache.cloudstack.api.command.QuotaStatementCmd; import org.apache.cloudstack.api.command.QuotaTariffListCmd; @@ -108,13 +109,14 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi @Override public List> getCommands() { final List> cmdList = new ArrayList>(); - cmdList.add(QuotaTariffListCmd.class); - cmdList.add(QuotaTariffUpdateCmd.class); - cmdList.add(QuotaCreditsCmd.class); - cmdList.add(QuotaEmailTemplateAddCmd.class); cmdList.add(QuotaRefreshCmd.class); cmdList.add(QuotaStatementCmd.class); cmdList.add(QuotaBalanceCmd.class); + cmdList.add(QuotaTariffListCmd.class); + cmdList.add(QuotaTariffUpdateCmd.class); + cmdList.add(QuotaCreditsCmd.class); + cmdList.add(QuotaEmailTemplateListCmd.class); + cmdList.add(QuotaEmailTemplateUpdateCmd.class); return cmdList; } diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/constant/QuotaConfig.java b/plugins/database/quota/src/org/apache/cloudstack/quota/constant/QuotaConfig.java index cc9bd7d4549..9fba0b70e82 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/constant/QuotaConfig.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/constant/QuotaConfig.java @@ -20,51 +20,52 @@ import org.apache.cloudstack.framework.config.ConfigKey; public interface QuotaConfig { - public static final ConfigKey QuotaPluginEnabled = new ConfigKey("Advanced", Boolean.class, "quota.enable.service", "false", - "Indicates whether Quota plugin is enabled or not", true); + public static final ConfigKey QuotaPluginEnabled = new ConfigKey("Advanced", Boolean.class, "quota.enable.service", "false", + "Indicates whether Quota plugin is enabled or not", true); - public static final ConfigKey QuotaPeriodType = new ConfigKey("Advanced", String.class, "quota.period.type", "2", - "Quota period type: 1 for every x days, 2 for certain day of the month, 3 for yearly on activation day - default quota usage reporting cycl", true); + public static final ConfigKey QuotaPeriodType = new ConfigKey("Advanced", String.class, "quota.period.type", "2", + "Quota period type: 1 for every x days, 2 for certain day of the month, 3 for yearly on activation day - default quota usage reporting cycl", true); - public static final ConfigKey QuotaPeriod = new ConfigKey("Advanced", String.class, "quota.period.config", "15", - "The period config in number of days for the quota period type", true); + public static final ConfigKey QuotaPeriod = new ConfigKey("Advanced", String.class, "quota.period.config", "15", + "The period config in number of days for the quota period type", true); - public static final ConfigKey QuotaGenerateActivity = new ConfigKey("Advanced", String.class, "quota.activity.generate", "true", - "Set true to enable a detailed log of the quota usage, rating and billing activity, on daily basis. Valid values (true, false)", true); + public static final ConfigKey QuotaGenerateActivity = new ConfigKey("Advanced", String.class, "quota.activity.generate", "true", + "Set true to enable a detailed log of the quota usage, rating and billing activity, on daily basis. Valid values (true, false)", true); - public static final ConfigKey QuotaEmailRecordOutgoing = new ConfigKey("Advanced", String.class, "quota.email.outgoing.record", "false", - "true means all the emails sent out will be stored in local DB, by default it is false", true); + public static final ConfigKey QuotaEmailRecordOutgoing = new ConfigKey("Advanced", String.class, "quota.email.outgoing.record", "false", + "true means all the emails sent out will be stored in local DB, by default it is false", true); - public static final ConfigKey QuotaEnableEnforcement = new ConfigKey("Advanced", String.class, "quota.enable.enforcement", "true", - "Enable the usage quota enforcement, i.e. on true exceeding quota the respective account will be locked.", true); + public static final ConfigKey QuotaEnableEnforcement = new ConfigKey("Advanced", String.class, "quota.enable.enforcement", "true", + "Enable the usage quota enforcement, i.e. on true exceeding quota the respective account will be locked.", true); - public static final ConfigKey QuotaCurrencySymbol = new ConfigKey("Advanced", String.class, "quota.currency.symbol", "R", - "The symbol for the currency in use to measure usage.", true); + public static final ConfigKey QuotaCurrencySymbol = new ConfigKey("Advanced", String.class, "quota.currency.symbol", "R", + "The symbol for the currency in use to measure usage.", true); - public static final ConfigKey QuotaLimitCritical = new ConfigKey("Advanced", String.class, "quota.limit.critical", "80", - "A percentage limit for quota when it is reached user is sent and alert.", true); + public static final ConfigKey QuotaLimitCritical = new ConfigKey("Advanced", String.class, "quota.limit.critical", "80", + "A percentage limit for quota when it is reached user is sent and alert.", true); - public static final ConfigKey QuotaLimitIncremental = new ConfigKey("Advanced", String.class, "quota.limit.increment", "5", - "Quota limit incremental", true); + public static final ConfigKey QuotaLimitIncremental = new ConfigKey("Advanced", String.class, "quota.limit.increment", "5", + "Quota limit incremental", true); - public static final ConfigKey QuotaSmtpHost = new ConfigKey("Advanced", String.class, "quota.usage.smtp.host", "", - "Quota SMTP host for quota related emails", true); + public static final ConfigKey QuotaSmtpHost = new ConfigKey("Advanced", String.class, "quota.usage.smtp.host", "", + "Quota SMTP host for quota related emails", true); - public static final ConfigKey QuotaSmtpTimeout = new ConfigKey("Advanced", String.class, "quota.usage.smtp.connection.timeout", "60", - "Quota SMTP server connection timeout duration", true); + public static final ConfigKey QuotaSmtpTimeout = new ConfigKey("Advanced", String.class, "quota.usage.smtp.connection.timeout", "60", + "Quota SMTP server connection timeout duration", true); - public static final ConfigKey QuotaSmtpUser = new ConfigKey("Advanced", String.class, "quota.usage.smtp.user", "", - "Quota SMTP server username", true); + public static final ConfigKey QuotaSmtpUser = new ConfigKey("Advanced", String.class, "quota.usage.smtp.user", "", + "Quota SMTP server username", true); - public static final ConfigKey QuotaSmtpPassword = new ConfigKey("Advanced", String.class, "quota.usage.smtp.password", "", - "Quota SMTP server password", true); - - public static final ConfigKey QuotaSmtpPort = new ConfigKey("Advanced", String.class, "quota.usage.smtp.port", "", - "Quota SMTP port", true); - - public static final ConfigKey QuotaSmtpAuthType = new ConfigKey("Advanced", String.class, "quota.usage.smtp.useAuth", "", - "Quota SMTP authorization type", true); + public static final ConfigKey QuotaSmtpPassword = new ConfigKey("Advanced", String.class, "quota.usage.smtp.password", "", + "Quota SMTP server password", true); + public static final ConfigKey QuotaSmtpPort = new ConfigKey("Advanced", String.class, "quota.usage.smtp.port", "", + "Quota SMTP port", true); + public static final ConfigKey QuotaSmtpAuthType = new ConfigKey("Advanced", String.class, "quota.usage.smtp.useAuth", "", + "Quota SMTP authorization type", true); + enum QuotaEmailTemplateTypes { + QUOTA_LOW, QUOTA_EMPTY + } } \ No newline at end of file diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDao.java b/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDao.java index b7c8c697867..573a7539744 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDao.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDao.java @@ -16,20 +16,12 @@ //under the License. package org.apache.cloudstack.quota.dao; -import java.util.List; - -import org.apache.cloudstack.quota.vo.QuotaBalanceVO; +import com.cloud.utils.db.GenericDao; import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO; -import com.cloud.utils.Pair; -import com.cloud.utils.db.Filter; -import com.cloud.utils.db.GenericDao; -import com.cloud.utils.db.SearchCriteria; +import java.util.List; public interface QuotaEmailTemplatesDao extends GenericDao { - - QuotaEmailTemplatesVO fetchTemplate(String templateName); - - Pair, Integer> searchBalance(SearchCriteria sc, Filter filter); - + List listAllQuotaEmailTemplates(String templateName); + boolean updateQuotaEmailTemplate(QuotaEmailTemplatesVO template); } diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java b/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java index 4db5276f7ac..c7b4908a181 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java @@ -16,33 +16,49 @@ //under the License. package org.apache.cloudstack.quota.dao; -import java.util.List; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionLegacy; +import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO; +import org.springframework.stereotype.Component; import javax.ejb.Local; - -import org.springframework.stereotype.Component; -import org.apache.cloudstack.quota.vo.QuotaBalanceVO; -import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO; - -import com.cloud.utils.Pair; -import com.cloud.utils.db.Filter; -import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.SearchCriteria; +import java.util.List; @Component @Local(value = { QuotaEmailTemplatesDao.class }) public class QuotaEmailTemplatesDaoImpl extends GenericDaoBase implements QuotaEmailTemplatesDao { - @Override - public QuotaEmailTemplatesVO fetchTemplate(String templateName) { - // TODO Auto-generated method stub - return null; + protected SearchBuilder QuotaEmailTemplateSearch; + + public QuotaEmailTemplatesDaoImpl() { + super(); + + QuotaEmailTemplateSearch = createSearchBuilder(); + QuotaEmailTemplateSearch.and("template_name", QuotaEmailTemplateSearch.entity().getTemplateName(), SearchCriteria.Op.EQ); + QuotaEmailTemplateSearch.done(); } @Override - public Pair, Integer> searchBalance(SearchCriteria sc, Filter filter) { - // TODO Auto-generated method stub - return null; + public List listAllQuotaEmailTemplates(String templateName) { + final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + TransactionLegacy.open(TransactionLegacy.USAGE_DB); + SearchCriteria sc = QuotaEmailTemplateSearch.create(); + if (templateName != null) { + sc.setParameters("template_name", templateName); + } + List result = this.listBy(sc); + TransactionLegacy.open(opendb).close(); + return result; } + @Override + public boolean updateQuotaEmailTemplate(QuotaEmailTemplatesVO template) { + final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + TransactionLegacy.open(TransactionLegacy.USAGE_DB); + final boolean result = this.update(template.getId(), template); + TransactionLegacy.open(opendb).close(); + return result; + } } diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java b/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java index 7e3f7141748..3efe1dc86aa 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java @@ -112,7 +112,7 @@ public class QuotaTariffDaoImpl extends GenericDaoBase impl final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); // Switch to // Usage DB - boolean result = this.update(plan.getId(), plan); + final boolean result = this.update(plan.getId(), plan); TransactionLegacy.open(opendb).close(); // Switch back return result; } @@ -123,7 +123,7 @@ public class QuotaTariffDaoImpl extends GenericDaoBase impl TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); // Switch to // Usage DB plan.setId(null); - QuotaTariffVO result = this.persist(plan); + final QuotaTariffVO result = this.persist(plan); TransactionLegacy.open(opendb).close(); // Switch back return result; } diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManager.java b/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManager.java index 924315b438a..34879a9c345 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManager.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManager.java @@ -20,6 +20,6 @@ import com.cloud.utils.component.Manager; public interface QuotaManager extends Manager { - public boolean calculateQuotaUsage(); + boolean calculateQuotaUsage(); } diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManagerImpl.java b/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManagerImpl.java index 61f5faa5a2e..4ff2800f7d3 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManagerImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/job/QuotaManagerImpl.java @@ -27,11 +27,10 @@ import com.cloud.utils.Pair; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.TransactionLegacy; - import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.quota.constant.QuotaTypes; -import org.apache.cloudstack.quota.dao.QuotaTariffDao; import org.apache.cloudstack.quota.dao.QuotaBalanceDao; +import org.apache.cloudstack.quota.dao.QuotaTariffDao; import org.apache.cloudstack.quota.dao.QuotaUsageDao; import org.apache.cloudstack.quota.vo.QuotaBalanceVO; import org.apache.cloudstack.quota.vo.QuotaTariffVO; @@ -43,7 +42,6 @@ import org.springframework.stereotype.Component; import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; - import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/vo/QuotaEmailTemplatesVO.java b/plugins/database/quota/src/org/apache/cloudstack/quota/vo/QuotaEmailTemplatesVO.java index b185cb00969..54013fc96eb 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/vo/QuotaEmailTemplatesVO.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/vo/QuotaEmailTemplatesVO.java @@ -16,60 +16,54 @@ //under the License. package org.apache.cloudstack.quota.vo; -import java.util.Date; +import org.apache.cloudstack.api.InternalIdentity; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; - -import org.apache.cloudstack.api.InternalIdentity; +import java.util.Date; @Entity @Table(name = "quota_email_templates") public class QuotaEmailTemplatesVO implements InternalIdentity { - - private static final long serialVersionUID = -7117933842834553210L; - @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @Column(name = "template_name") private String templateName; - @Column(name = "template_text") - private String templateText; + @Column(name = "template_subject") + private String templateSubject; - @Column(name = "category") - private Integer category; - - @Column(name = "last_updated") - @Temporal(value = TemporalType.TIMESTAMP) - private Date lastUpdated = null; + @Column(name = "template_body") + private String templateBody; @Column(name = "locale") private String locale; - @Column(name = "version") - private Integer version; + @Column(name = "updated") + @Temporal(value = TemporalType.TIMESTAMP) + private Date lastUpdated = null; public QuotaEmailTemplatesVO() { } - public QuotaEmailTemplatesVO(String templateName, String templateText, String locale, Integer version) { + public QuotaEmailTemplatesVO(String templateName, String templateSubject, String templateBody) { super(); this.templateName = templateName; - this.templateText = templateText; - this.locale = locale; - this.version = version; + this.templateSubject = templateSubject; + this.templateBody = templateBody; } @Override public long getId() { - // TODO Auto-generated method stub return id; } @@ -81,20 +75,20 @@ public class QuotaEmailTemplatesVO implements InternalIdentity { this.templateName = templateName; } - public String getTemplateText() { - return templateText; + public String getTemplateSubject() { + return templateSubject; } - public void setTemplateText(String templateText) { - this.templateText = templateText; + public void setTemplateSubject(String templateSubject) { + this.templateSubject = templateSubject; } - public Integer getCategory() { - return category; + public String getTemplateBody() { + return templateBody; } - public void setCategory(Integer version) { - this.category = category; + public void setTemplateBody(String templateBody) { + this.templateBody = templateBody; } public Date getLastUpdated() { @@ -112,13 +106,4 @@ public class QuotaEmailTemplatesVO implements InternalIdentity { public void setLocale(String locale) { this.locale = locale; } - - public Integer getVersion() { - return version; - } - - public void setVersion(Integer version) { - this.version = version; - } - } diff --git a/setup/db/db/schema-451to452.sql b/setup/db/db/schema-451to452.sql index 82b2753df1a..08671766266 100644 --- a/setup/db/db/schema-451to452.sql +++ b/setup/db/db/schema-451to452.sql @@ -119,15 +119,19 @@ CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_balance` ( CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_email_templates` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, - `template_name` varchar(64) DEFAULT NULL, - `template_text` longtext, - `category` int(10) unsigned NOT NULL DEFAULT '0', - `last_updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `template_name` varchar(64) NOT NULL UNIQUE, + `template_subject` longtext, + `template_body` longtext, `locale` varchar(25) DEFAULT 'en_US', - `version` int(11) DEFAULT '0', + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +LOCK TABLES `cloud_usage`.`quota_email_templates` WRITE; +INSERT INTO `cloud_usage`.`quota_email_templates` (`template_name`, `template_subject`, `template_body`) VALUES + ('QUOTA_LOW', 'Quota Usage Threshold crossed ${accountName}', 'Your account "${accountName}" in the domain "${domainName}" has reached usage threshold, the current balance is ${quotaCurrency} ${quotaValue}'), + ('QUOTA_EMPTY', 'Quota Exhausted, account ${accountName} is locked now', 'Your account "${accountName}" in the domain "${domainName}" has exhausted allocated quota due to which your account has locked now, please contact the administrator'); +UNLOCK TABLES; CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_sent_emails` ( `id` bigint(20) NOT NULL AUTO_INCREMENT,