From 4c8170520b8aa0bc0ee606af2c39a80134a51970 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Tue, 8 Nov 2011 09:56:12 +0100
Subject: [PATCH] FXOLAT-282: fix the configuration panel, load the list of
 customers if the url connection is ok

---
 .../com/frentix/olat/vitero/ViteroModule.java |  3 +-
 .../olat/vitero/manager/ViteroManager.java    | 22 ++++++
 .../ui/ViteroConfigurationController.java     | 71 +++++++++++++------
 3 files changed, 74 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/frentix/olat/vitero/ViteroModule.java b/src/main/java/com/frentix/olat/vitero/ViteroModule.java
index adea8968755..4b6d9e19f73 100644
--- a/src/main/java/com/frentix/olat/vitero/ViteroModule.java
+++ b/src/main/java/com/frentix/olat/vitero/ViteroModule.java
@@ -34,6 +34,7 @@ import org.quartz.CronTrigger;
 import org.quartz.JobDetail;
 import org.quartz.Scheduler;
 import org.quartz.SchedulerException;
+import org.quartz.Trigger;
 
 import com.frentix.olat.vitero.manager.ViteroZombieSlayerJob;
 
@@ -140,7 +141,7 @@ public class ViteroModule extends AbstractOLATModule implements ConfigOnOff {
 	
 	private void initCronJob() {
 		try {
-			if(scheduler.getTrigger("Vitero_Cleaner_Cron_Job", Scheduler.DEFAULT_GROUP) == null) {
+			if(scheduler.getTrigger("Vitero_Cleaner_Cron_Trigger", Scheduler.DEFAULT_GROUP) == null) {
 				JobDetail jobDetail = new JobDetail("Vitero_Cleaner_Cron_Job", Scheduler.DEFAULT_GROUP, ViteroZombieSlayerJob.class);
 				CronTrigger trigger = new CronTrigger();
 				
diff --git a/src/main/java/com/frentix/olat/vitero/manager/ViteroManager.java b/src/main/java/com/frentix/olat/vitero/manager/ViteroManager.java
index 3ea88bae966..8a9e5effb00 100644
--- a/src/main/java/com/frentix/olat/vitero/manager/ViteroManager.java
+++ b/src/main/java/com/frentix/olat/vitero/manager/ViteroManager.java
@@ -199,6 +199,28 @@ public class ViteroManager extends BasicManager implements UserDataDeletable {
 		}
 	}
 	
+	public List<ViteroCustomer> getCustomers(String url, String login, String password) 
+			throws VmsNotAvailableException {
+				try {
+					CustomerServiceStub customerWs = new CustomerServiceStub(url + "/services");
+					SecurityHeader.addAdminSecurityHeader(login, password, customerWs);
+					CustomerServiceStub.GetCustomerListRequest listRequest = new CustomerServiceStub.GetCustomerListRequest();
+					listRequest.setGetCustomerListRequest(new EmptyOMElement());
+					CustomerServiceStub.GetCustomerListResponse response = customerWs.getCustomerList(listRequest);
+					CustomerServiceStub.Customertype[] customerTypes = response.getCustomer();
+					return convert(customerTypes);
+				} catch (AxisFault f) {
+					int code = handleAxisFault(f);
+					switch(code) {
+						default: logAxisError("Cannot get the list of customers.", f);
+					}
+					return Collections.emptyList();
+				} catch (RemoteException e) {
+					logError("Cannot get the list of customers.", e);
+					return Collections.emptyList();
+				}
+			}
+	
 	/**
 	 * Create a session code with a one hour expiration date
 	 * @param identity
diff --git a/src/main/java/com/frentix/olat/vitero/ui/ViteroConfigurationController.java b/src/main/java/com/frentix/olat/vitero/ui/ViteroConfigurationController.java
index 65b5f78e0bb..30129572e40 100644
--- a/src/main/java/com/frentix/olat/vitero/ui/ViteroConfigurationController.java
+++ b/src/main/java/com/frentix/olat/vitero/ui/ViteroConfigurationController.java
@@ -85,8 +85,22 @@ public class ViteroConfigurationController extends FormBasicController {
 		
 		enabledValues = new String[]{translate("enabled")};
 		
+		loadCustomers(false);
+		
+		initForm(ureq);
+	}
+	
+	private void loadCustomers(boolean fromEditor) {
 		try {
-			List<ViteroCustomer> customers = viteroManager.getCustomers();
+			List<ViteroCustomer> customers;
+			if(fromEditor) {
+				String url = urlEl.getValue();
+				String login = loginEl.getValue();
+				String password = passwordEl.getValue();
+				customers = viteroManager.getCustomers(url, login, password);
+			} else {
+				customers = viteroManager.getCustomers();
+			}
 			customerKeys = new String[customers.size()];
 			customerValues = new String[customers.size()];
 			int i=0;
@@ -98,8 +112,6 @@ public class ViteroConfigurationController extends FormBasicController {
 			customerKeys = new String[0];
 			customerValues = new String[0];
 		}
-		
-		initForm(ureq);
 	}
 
 	@Override
@@ -192,6 +204,30 @@ public class ViteroConfigurationController extends FormBasicController {
 
 	@Override
 	protected boolean validateFormLogic(UserRequest ureq) {
+		boolean allOk = validateURL();
+
+		customersEl.clearError();
+		if(customersEl.isOneSelected()) {
+			try {
+				String customerId = customersEl.getSelectedKey();
+				Integer.parseInt(customerId);
+			} catch(Exception e) {
+				customersEl.setErrorKey("error.customer.invalid", null);
+				allOk = false;
+			}
+		} else {
+			if(customersEl.getSize() == 0) {
+				loadCustomers(true);
+				customersEl.setKeysAndValues(customerKeys, customerValues, null);
+			}
+			customersEl.setErrorKey("form.legende.mandatory", null);
+			allOk = false;
+		}
+		
+		return allOk && super.validateFormLogic(ureq);
+	}
+	
+	private boolean validateURL() {
 		boolean allOk = true;
 		
 		String url = urlEl.getValue();
@@ -221,22 +257,8 @@ public class ViteroConfigurationController extends FormBasicController {
 			passwordEl.setErrorKey("form.legende.mandatory", null);
 			allOk = false;
 		}
-
-		customersEl.clearError();
-		if(customersEl.isOneSelected()) {
-			try {
-				String customerId = customersEl.getSelectedKey();
-				Integer.parseInt(customerId);
-			} catch(Exception e) {
-				customersEl.setErrorKey("error.customer.invalid", null);
-				allOk = false;
-			}
-		} else {
-			customersEl.setErrorKey("form.legende.mandatory", null);
-			allOk = false;
-		}
 		
-		return allOk && super.validateFormLogic(ureq);
+		return allOk;
 	}
 
 	@Override
@@ -245,14 +267,18 @@ public class ViteroConfigurationController extends FormBasicController {
 			boolean enabled = viteroEnabled.isSelected(0);
 			viteroModule.setEnabled(enabled);
 		} else if(source == checkLink) {
-			if(validateFormLogic(ureq)) {
-				checkConnection(ureq);
+			if(validateURL()) {
+				boolean ok = checkConnection(ureq);
+				if(ok && customersEl.getSize() == 0) {
+					loadCustomers(true);
+					customersEl.setKeysAndValues(customerKeys, customerValues, null);
+				}
 			}
 		}
 		super.formInnerEvent(ureq, source, event);
 	}
 	
-	protected void checkConnection(UserRequest ureq) {
+	protected boolean checkConnection(UserRequest ureq) {
 		String url = urlEl.getValue();
 		String login = loginEl.getValue();
 		String password = passwordEl.getValue();
@@ -265,10 +291,13 @@ public class ViteroConfigurationController extends FormBasicController {
 			} else {
 				showError("check.nok");
 			}
+			return ok;
 		} catch (NumberFormatException e) {
 			showError("error.customer.invalid");
+			return false;
 		} catch (VmsNotAvailableException e) {
 			showError(VmsNotAvailableException.I18N_KEY);
+			return false;
 		}
 	}
 }
\ No newline at end of file
-- 
GitLab