Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add uniqyue constraint on URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Sep 12, 2016
1 parent 6b92942 commit daea921
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 14 additions & 0 deletions zanata-model/src/main/java/org/zanata/model/WebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import lombok.Setter;

import org.zanata.model.type.WebhookType;
import org.zanata.model.validator.Unique;
import org.zanata.model.validator.Url;

/**
Expand All @@ -55,6 +56,7 @@
@Getter
@Setter(AccessLevel.PRIVATE)
@NoArgsConstructor
@Unique(properties = { "url" })
public class WebHook implements Serializable {

private Long id;
Expand Down Expand Up @@ -103,10 +105,22 @@ public Set<WebhookType> getTypes() {
return types;
}

/**
* This will replace all properties with given ones.
*
* @param url - new url
* @param newTypes - new types
* @param secret - new secret key
*/
@Transient
public void update(String url, Set<WebhookType> newTypes, String secret) {
this.url = url;
this.secret = secret;

/**
* Copy all newTypes into currentTypes and remove those that are not
* in the newTypes
*/
this.types.addAll(newTypes);
Set<WebhookType> currentTypes = Sets.newHashSet(this.types);
for (WebhookType type: currentTypes) {
Expand Down
21 changes: 14 additions & 7 deletions zanata-war/src/main/java/org/zanata/action/ProjectHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ public Map<LocaleId, Boolean> getSelectedEnabledLocales() {
@Setter
private Boolean selectedCheckbox = Boolean.TRUE;

// private Map<Long, List<WebhookType>> webhookUrlMapToType = null;
// private Map<String, List<WebHook>> webhookUrlMapToWebHook = null;

private List<HLocale> disabledLocales;

public ProjectHome() {
Expand Down Expand Up @@ -1091,7 +1088,12 @@ public List<ValidationAction> getValidationList() {
public void addWebHook(String url, String secret, String strTypes) {
identity.checkPermission(getInstance(), "update");
Set<WebhookType> types = getTypesFromString(strTypes);
if (isValidUrl(url, true) && !types.isEmpty()) {
if(types.isEmpty()) {
facesMessages.addGlobal(
msgs.get("jsf.project.webhookType.empty"));
return;
}
if (isValidUrl(url, true)) {
secret = StringUtils.isBlank(secret) ? null : secret;
WebHook webHook =
new WebHook(this.getInstance(), url, types, secret);
Expand Down Expand Up @@ -1121,11 +1123,16 @@ public void updateWebhook(String id, String url, String secret,
identity.checkPermission(getInstance(), "update");
WebHook webHook = webHookDAO.findById(new Long(id));
Set<WebhookType> types = getTypesFromString(strTypes);
if (webHook != null && isValidUrl(url, false) && !types.isEmpty()) {
getInstance().getWebHooks().remove(webHook);
if(types.isEmpty()) {
facesMessages.addGlobal(
msgs.get("jsf.project.webhookType.empty"));
return;
}
if (webHook != null && isValidUrl(url, false)) {
// getInstance().getWebHooks().remove(webHook);
secret = StringUtils.isBlank(secret) ? null : secret;
webHook.update(url, types, secret);
getInstance().getWebHooks().add(webHook);
// getInstance().getWebHooks().add(webHook);
webHookDAO.makePersistent(webHook);
facesMessages.addGlobal(
msgs.format("jsf.project.UpdateWebhook", url));
Expand Down
1 change: 1 addition & 0 deletions zanata-war/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ jsf.project.AddWebhook=Add webhook
jsf.project.NewWebhook=New webhook
jsf.project.RemoveWebhook=Webhook {0} removed.
jsf.project.AddNewWebhook=Webhook {0} added.
jsf.project.webhookType.empty=Please select a type for this webhook.
jsf.project.UpdateWebhook=Webhook {0} updated.
jsf.project.PayloadURL=Payload URL
jsf.project.WebhookType.label=Type
Expand Down

0 comments on commit daea921

Please sign in to comment.