Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Issue 210 validate publisher form #416

Merged
merged 6 commits into from
Mar 5, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
Expand Down Expand Up @@ -66,6 +67,7 @@ public final class PublishPopup extends AbstractPopupBase {
// to hold the google spreadsheet only options
private final FlexTable gsBar;
private final TextBox gsName;
private final TextBox gsOwnerEmail;

// to hold the jsonServer only options
private final FlexTable jsBar;
Expand Down Expand Up @@ -120,6 +122,13 @@ public PublishPopup(String formId) {
gsName.setText(EMPTY_STRING);
gsName.setVisibleLength(35);
gsBar.setWidget(1, 1, gsName);
gsBar.setWidget(2, 0, new HTML("<h3>Owner's email:</h3>"));
gsOwnerEmail = new TextBox();
gsOwnerEmail.setText(EMPTY_STRING);
gsOwnerEmail.setVisibleLength(35);
gsOwnerEmail.getElement().setAttribute("type", "email");
gsBar.setWidget(2, 1, gsOwnerEmail);


// this is only for simple json server
jsBar = new FlexTable();
Expand Down Expand Up @@ -202,25 +211,45 @@ private class CreateExernalServiceHandler implements ClickHandler {

@Override
public void onClick(ClickEvent event) {
// Validate common required fields
if (serviceType.getSelectedValue() == null || serviceType.getSelectedValue().isEmpty()) {
Window.alert("You need to select a publisher type from the dropdown");
return;
}

String externalServiceTypeString = serviceType.getSelectedValue();
ExternalServiceType type = (externalServiceTypeString == null) ? null :
ExternalServiceType.valueOf(externalServiceTypeString);

String serviceOpString = esOptions.getSelectedValue();
ExternalServicePublicationOption serviceOp = (serviceOpString == null) ? null :
ExternalServicePublicationOption.valueOf(serviceOpString);

UserSecurityInfo info = AggregateUI.getUI().getUserInfo();
String ownerEmail = getOwnerEmail(info);
if (ownerEmail == null)
if (esOptions.getSelectedValue() == null || esOptions.getSelectedValue().isEmpty()) {
Window.alert("You need to select which submissions to publish");
return;
}

ExternalServiceType type = ExternalServiceType.valueOf(serviceType.getSelectedValue());
ExternalServicePublicationOption serviceOp = ExternalServicePublicationOption.valueOf(esOptions.getSelectedValue());

switch (type) {
case GOOGLE_SPREADSHEET:
// Validate the workbook name
String workbookName = gsName.getText();
if (workbookName == null || workbookName.isEmpty()) {
Window.alert("You must provide a workbook name");
return;
}

// Validate the owner's email
String ownerEmail = gsOwnerEmail.getText();
if (ownerEmail == null || ownerEmail.isEmpty()) {
Window.alert("You must provide the owner's email");
return;
} else if (!validateEmail(ownerEmail)) {
Window.alert("Invalid owner's email");
return;
} else if (!Window.confirm("Please, confirm that the owner's email you've introduced is correct: " + ownerEmail)) {
gsOwnerEmail.setTitle("");
return;
}

secureRequest(
SecureGWT.getServicesAdminService(),
(rpc, sc, cb) -> rpc.createGoogleSpreadsheet(formId, gsName.getText(), serviceOp, ownerEmail, cb),
(rpc, sc, cb) -> rpc.createGoogleSpreadsheet(formId, workbookName, serviceOp, ownerEmail, cb),
NO_OP_CONSUMER,
this::onFailure
);
Expand All @@ -230,7 +259,7 @@ public void onClick(ClickEvent event) {
final BinaryOption jsBinaryOp = (jsBinaryOpString == null) ? null : BinaryOption.valueOf(jsBinaryOpString);
secureRequest(
SecureGWT.getServicesAdminService(),
(rpc, sc, cb) -> rpc.createSimpleJsonServer(formId, jsAuthKey.getText(), jsUrl.getText(), serviceOp, ownerEmail, jsBinaryOp, cb),
(rpc, sc, cb) -> rpc.createSimpleJsonServer(formId, jsAuthKey.getText(), jsUrl.getText(), serviceOp, "mailto:N/A", jsBinaryOp, cb),
NO_OP_CONSUMER,
this::onFailure
);
Expand All @@ -246,18 +275,6 @@ public void onClick(ClickEvent event) {
private void onFailure(Throwable cause) {
AggregateUI.getUI().reportError(cause);
}

private String getOwnerEmail(UserSecurityInfo info) {
String ownerEmail = info.getEmail();
if (ownerEmail == null || ownerEmail.length() == 0) {
try {
ownerEmail = UIUtils.promptForEmailAddress();
} catch (Exception e) {
ownerEmail = null; // user pressed cancel
}
}
return ownerEmail;
}
}

private class ExternalServiceTypeChangeHandler implements ChangeHandler {
Expand All @@ -267,4 +284,9 @@ public void onChange(ChangeEvent event) {
}
}

public native static boolean validateEmail(String email) /*-{
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}-*/;

}