diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java b/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java index e89f9222..3d14ba06 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java @@ -46,6 +46,10 @@ public final class API { public static final String LMS_SETUP_TEST_ENDPOINT = LMS_SETUP_ENDPOINT + LMS_SETUP_TEST_PATH_SEGMENT + MODEL_ID_VAR_PATH_SEGMENT; + public static final String LMS_SETUP_TEST_AD_HOC_PATH_SEGMENT = "/adhoc"; + public static final String LMS_SETUP_TEST_AD_HOC_ENDPOINT = LMS_SETUP_ENDPOINT + + LMS_SETUP_TEST_PATH_SEGMENT + + LMS_SETUP_TEST_AD_HOC_PATH_SEGMENT; public static final String USER_ACCOUNT_ENDPOINT = "/useraccount"; diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java index 36269bfc..46e7c2bc 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java @@ -195,13 +195,11 @@ public class ExamForm implements TemplateComposer { "sebserver.exam.form.status", i18nSupport.getText(new LocTextKey("sebserver.exam.status." + examStatus.name()))) .readonly(true)) - .addFieldIf( - isNotNew, - () -> FormBuilder.multiComboSelection( - Domain.EXAM.ATTR_SUPPORTER, - "sebserver.exam.form.supporter", - StringUtils.join(exam.supporter, Constants.LIST_SEPARATOR_CHAR), - this.resourceService::examSupporterResources)) + .addField(FormBuilder.multiComboSelection( + Domain.EXAM.ATTR_SUPPORTER, + "sebserver.exam.form.supporter", + StringUtils.join(exam.supporter, Constants.LIST_SEPARATOR_CHAR), + this.resourceService::examSupporterResources)) .buildFor(importFromQuizData ? restService.getRestCall(ImportAsExam.class) @@ -228,17 +226,6 @@ public class ExamForm implements TemplateComposer { .withExec(this::cancelModify) .publishIf(() -> !readonly); -// .newAction(ActionDefinition.EXAM_DEACTIVATE) -// .withEntityKey(entityKey) -// .withSimpleRestCall(restService, DeactivateExam.class) -// .withConfirm(PageUtils.confirmDeactivation(exam, restService)) -// .publishIf(() -> writeGrant && readonly && exam.isActive()) -// -// .newAction(ActionDefinition.EXAM_ACTIVATE) -// .withEntityKey(entityKey) -// .withSimpleRestCall(restService, ActivateExam.class) -// .publishIf(() -> writeGrant && readonly && !exam.isActive()); - // additional data in read-only view if (readonly) { @@ -250,6 +237,9 @@ public class ExamForm implements TemplateComposer { final EntityTable indicatorTable = this.pageService.entityTableBuilder(restService.getRestCall(GetIndicators.class)) + .withRestCallAdapter(builder -> builder.withQueryParam( + Indicator.FILTER_ATTR_EXAM, + entityKey.modelId)) .withEmptyMessage(new LocTextKey("sebserver.exam.indicator.list.empty")) .withPaging(5) .hideNavigation() diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/InstitutionForm.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/InstitutionForm.java index ef353efa..2106b82a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/InstitutionForm.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/InstitutionForm.java @@ -129,12 +129,10 @@ public class InstitutionForm implements TemplateComposer { Domain.INSTITUTION.ATTR_URL_SUFFIX, "sebserver.institution.form.urlSuffix", institution.urlSuffix)) - .addFieldIf( - () -> !isNew && modifyGrant, - () -> FormBuilder.imageUpload( - Domain.INSTITUTION.ATTR_LOGO_IMAGE, - "sebserver.institution.form.logoImage", - institution.logoImage)) + .addField(FormBuilder.imageUpload( + Domain.INSTITUTION.ATTR_LOGO_IMAGE, + "sebserver.institution.form.logoImage", + institution.logoImage)) .buildFor((isNew) ? this.restService.getRestCall(NewInstitution.class) : this.restService.getRestCall(SaveInstitution.class)); diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/LmsSetupForm.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/LmsSetupForm.java index b4b68d0e..611fb566 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/LmsSetupForm.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/LmsSetupForm.java @@ -9,6 +9,7 @@ package ch.ethz.seb.sebserver.gui.content; import java.util.function.BooleanSupplier; +import java.util.function.Function; import org.apache.commons.lang3.StringUtils; import org.eclipse.swt.widgets.Composite; @@ -47,6 +48,7 @@ import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.lmssetup.GetLmsSe import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.lmssetup.NewLmsSetup; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.lmssetup.SaveLmsSetup; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.lmssetup.TestLmsSetup; +import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.lmssetup.TestLmsSetupAdHoc; import ch.ethz.seb.sebserver.gui.service.remote.webservice.auth.CurrentUser; import ch.ethz.seb.sebserver.gui.service.remote.webservice.auth.CurrentUser.EntityGrantCheck; import ch.ethz.seb.sebserver.gui.widget.WidgetFactory; @@ -155,24 +157,18 @@ public class LmsSetupForm implements TemplateComposer { (lmsType != null) ? lmsType.name() : null, this.resourceService::lmsTypeResources) .readonlyIf(isNotNew)) - .addFieldIf( - () -> isNotNew.getAsBoolean(), - () -> FormBuilder.text( - Domain.LMS_SETUP.ATTR_LMS_URL, - "sebserver.lmssetup.form.url", - lmsSetup.getLmsApiUrl())) - .addFieldIf( - () -> isNotNew.getAsBoolean(), - () -> FormBuilder.text( - Domain.LMS_SETUP.ATTR_LMS_CLIENTNAME, - "sebserver.lmssetup.form.clientname.lms", - lmsSetup.getLmsAuthName())) - .addFieldIf( - () -> isNotNew.getAsBoolean(), - () -> FormBuilder.text( - Domain.LMS_SETUP.ATTR_LMS_CLIENTSECRET, - "sebserver.lmssetup.form.secret.lms") - .asPasswordField()) + .addField(FormBuilder.text( + Domain.LMS_SETUP.ATTR_LMS_URL, + "sebserver.lmssetup.form.url", + lmsSetup.getLmsApiUrl())) + .addField(FormBuilder.text( + Domain.LMS_SETUP.ATTR_LMS_CLIENTNAME, + "sebserver.lmssetup.form.clientname.lms", + lmsSetup.getLmsAuthName())) + .addField(FormBuilder.text( + Domain.LMS_SETUP.ATTR_LMS_CLIENTSECRET, + "sebserver.lmssetup.form.secret.lms") + .asPasswordField()) .buildFor((entityKey == null) ? restService.getRestCall(NewLmsSetup.class) @@ -196,6 +192,12 @@ public class LmsSetupForm implements TemplateComposer { .ignoreMoveAwayFromEdit() .publishIf(() -> modifyGrant && isNotNew.getAsBoolean() && !readonly) + .newAction(ActionDefinition.LMS_SETUP_TEST_AND_SAVE) + .withEntityKey(entityKey) + .withExec(action -> this.testAdHoc(action, formHandle)) + .ignoreMoveAwayFromEdit() + .publishIf(() -> modifyGrant && isNew.getAsBoolean() && !readonly) + .newAction(ActionDefinition.LMS_SETUP_DEACTIVATE) .withEntityKey(entityKey) .withSimpleRestCall(restService, DeactivateLmsSetup.class) @@ -236,6 +238,40 @@ public class LmsSetupForm implements TemplateComposer { return testLmsSetup; } + /** LmsSetup test action implementation */ + private PageAction testAdHoc(final PageAction action, final FormHandle formHandle) { + + // reset previous errors + formHandle.process( + name -> true, + fieldAccessor -> fieldAccessor.resetError()); + + // first test the connection on ad hoc object + final Result result = this.resourceService.getRestService() + .getBuilder(TestLmsSetupAdHoc.class) + .withFormBinding(formHandle.getFormBinding()) + .call(); + + // ... and handle the response + if (result.hasError()) { + if (formHandle.handleError(result.getError())) { + throw new PageMessageException( + new LocTextKey("sebserver.lmssetup.action.test.missingParameter")); + } + } + + return handleTestResult( + action, + a -> { + // try to save the LmsSetup + final PageAction processFormSave = formHandle.processFormSave(a); + processFormSave.pageContext().publishInfo( + new LocTextKey("sebserver.lmssetup.action.test.ok")); + return processFormSave; + }, + result.getOrThrow()); + } + /** LmsSetup test action implementation */ private PageAction testLmsSetup(final PageAction action, final FormHandle formHandle) { // If we are in edit-mode we have to save the form before testing @@ -262,13 +298,24 @@ public class LmsSetupForm implements TemplateComposer { } } - final LmsSetupTestResult testResult = result.getOrThrow(); + return handleTestResult( + action, + a -> { + action.pageContext().publishInfo( + new LocTextKey("sebserver.lmssetup.action.test.ok")); + + return action; + }, + result.getOrThrow()); + } + + private PageAction handleTestResult( + final PageAction action, + final Function onOK, + final LmsSetupTestResult testResult) { if (testResult.isOk()) { - action.pageContext().publishInfo( - new LocTextKey("sebserver.lmssetup.action.test.ok")); - - return action; + return onOK.apply(action); } else if (StringUtils.isNoneBlank(testResult.tokenRequestError)) { throw new PageMessageException( new LocTextKey("sebserver.lmssetup.action.test.tokenRequestError", diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountChangePasswordForm.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountChangePasswordForm.java index 2c1ba340..9a9fba1c 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountChangePasswordForm.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountChangePasswordForm.java @@ -117,7 +117,7 @@ public class UserAccountChangePasswordForm implements TemplateComposer { pageContext.getShell(), this.i18nSupport.getText("sebserver.login.password.change"), this.i18nSupport.getText("sebserver.login.password.change.success"), - SWT.ERROR); + SWT.ICON_INFORMATION); error.open(null); } return saveAction; diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountForm.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountForm.java index 9ae9be92..697ea134 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountForm.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/UserAccountForm.java @@ -138,6 +138,9 @@ public class UserAccountForm implements TemplateComposer { .putStaticValueIf(isNotNew, Domain.USER.ATTR_INSTITUTION_ID, String.valueOf(userAccount.getInstitutionId())) + .putStaticValue( + Domain.USER.ATTR_LANGUAGE, + "en") .addFieldIf( isSEBAdmin, () -> FormBuilder.singleSelection( @@ -158,12 +161,12 @@ public class UserAccountForm implements TemplateComposer { Domain.USER.ATTR_EMAIL, "sebserver.useraccount.form.mail", userAccount.getEmail())) - .addField(FormBuilder.singleSelection( - Domain.USER.ATTR_LANGUAGE, - "sebserver.useraccount.form.language", - userAccount.getLanguage().getLanguage(), - this.resourceService::languageResources) - .readonly(true)) +// .addField(FormBuilder.singleSelection( +// Domain.USER.ATTR_LANGUAGE, +// "sebserver.useraccount.form.language", +// userAccount.getLanguage().getLanguage(), +// this.resourceService::languageResources) +// .readonly(true)) .addField(FormBuilder.singleSelection( Domain.USER.ATTR_TIMEZONE, "sebserver.useraccount.form.timezone", diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/action/ActionDefinition.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/action/ActionDefinition.java index fa801207..9fc760f7 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/action/ActionDefinition.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/action/ActionDefinition.java @@ -150,6 +150,11 @@ public enum ActionDefinition { ImageIcon.TEST, PageStateDefinition.LMS_SETUP_EDIT, ActionCategory.FORM), + LMS_SETUP_TEST_AND_SAVE( + new LocTextKey("sebserver.lmssetup.action.testsave"), + ImageIcon.TEST, + PageStateDefinition.LMS_SETUP_VIEW, + ActionCategory.FORM), LMS_SETUP_CANCEL_MODIFY( new LocTextKey("sebserver.overall.action.modify.cancel"), ImageIcon.CANCEL, diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/form/FormHandle.java b/src/main/java/ch/ethz/seb/sebserver/gui/form/FormHandle.java index ec1890cd..df30484a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/form/FormHandle.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/form/FormHandle.java @@ -9,6 +9,7 @@ package ch.ethz.seb.sebserver.gui.form; import java.util.function.Consumer; +import java.util.function.Predicate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,6 +24,7 @@ import ch.ethz.seb.sebserver.gui.service.page.FieldValidationError; import ch.ethz.seb.sebserver.gui.service.page.PageContext; import ch.ethz.seb.sebserver.gui.service.page.PageService; import ch.ethz.seb.sebserver.gui.service.page.impl.PageAction; +import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.FormBinding; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.RestCall; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.RestCallError; @@ -51,6 +53,10 @@ public class FormHandle { this.i18nSupport = pageService.getI18nSupport(); } + public FormBinding getFormBinding() { + return this.form; + } + /** Process an API post request to send and save the form field values * to the webservice and publishes a page event to return to read-only-view * to indicate that the data was successfully saved or process an validation @@ -69,10 +75,12 @@ public class FormHandle { * * @return the response result of the post (or put) RestCall */ public Result doAPIPost() { + // reset all errors that may still be displayed this.form.process( name -> true, fieldAccessor -> fieldAccessor.resetError()); + // post return this.post .newBuilder() .withFormBinding(this.form) @@ -135,8 +143,11 @@ public class FormHandle { (Object[]) valError.getAttributes()))); } - public FormHandle process(final Consumer
consumer) { - consumer.accept(this.form); + public FormHandle process( + final Predicate nameFilter, + final Consumer processor) { + + this.form.process(nameFilter, processor); return this; } diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/RestCall.java b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/RestCall.java index 69f0651e..edb2ee60 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/RestCall.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/RestCall.java @@ -13,6 +13,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -186,6 +187,10 @@ public abstract class RestCall { return this; } + public RestCallBuilder apply(final Function f) { + return f.apply(this); + } + public RestCallBuilder withBody(final Object body) { if (body instanceof String) { this.body = String.valueOf(body); diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/lmssetup/TestLmsSetupAdHoc.java b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/lmssetup/TestLmsSetupAdHoc.java new file mode 100644 index 00000000..cfdb5826 --- /dev/null +++ b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/lmssetup/TestLmsSetupAdHoc.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.ethz.seb.sebserver.gui.service.remote.webservice.api.lmssetup; + +import org.springframework.context.annotation.Lazy; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.core.type.TypeReference; + +import ch.ethz.seb.sebserver.gbl.api.API; +import ch.ethz.seb.sebserver.gbl.api.EntityType; +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetupTestResult; +import ch.ethz.seb.sebserver.gbl.profile.GuiProfile; +import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.RestCall; + +@Lazy +@Component +@GuiProfile +public class TestLmsSetupAdHoc extends RestCall { + + protected TestLmsSetupAdHoc() { + super(new TypeKey<>( + CallType.UNDEFINED, + EntityType.LMS_SETUP, + new TypeReference() { + }), + HttpMethod.PUT, + MediaType.APPLICATION_JSON_UTF8, + API.LMS_SETUP_TEST_AD_HOC_ENDPOINT); + } + +} diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/table/EntityTable.java b/src/main/java/ch/ethz/seb/sebserver/gui/table/EntityTable.java index 612e212a..a8dc6ddd 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/table/EntityTable.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/table/EntityTable.java @@ -56,6 +56,7 @@ public class EntityTable { final PageService pageService; final WidgetFactory widgetFactory; final RestCall> restCall; + final Function>.RestCallBuilder, RestCall>.RestCallBuilder> restCallAdapter; final I18nSupport i18nSupport; final List> columns; @@ -78,6 +79,7 @@ public class EntityTable { final int type, final Composite parent, final RestCall> restCall, + final Function>.RestCallBuilder, RestCall>.RestCallBuilder> restCallAdapter, final PageService pageService, final List> columns, final List actions, @@ -91,6 +93,7 @@ public class EntityTable { this.i18nSupport = pageService.getI18nSupport(); this.widgetFactory = pageService.getWidgetFactory(); this.restCall = restCall; + this.restCallAdapter = (restCallAdapter != null) ? restCallAdapter : Function.identity(); this.columns = Utils.immutableListOf(columns); this.actions = Utils.immutableListOf(actions); this.emptyMessage = emptyMessage; @@ -288,6 +291,7 @@ public class EntityTable { .withPaging(pageNumber, pageSize) .withSorting(sortColumn, sortOrder) .withQueryParams((this.filter != null) ? this.filter.getFilterParameter() : null) + .apply(this.restCallAdapter) .call() .map(this::createTableRowsFromPage) .map(this.navigator::update) diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/table/TableBuilder.java b/src/main/java/ch/ethz/seb/sebserver/gui/table/TableBuilder.java index 8220f0b8..d0d22207 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/table/TableBuilder.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/table/TableBuilder.java @@ -50,6 +50,7 @@ public class TableBuilder { private int pageSize = -1; private int type = SWT.NONE; private boolean hideNavigation = false; + private Function>.RestCallBuilder, RestCall>.RestCallBuilder> restCallAdapter;; public TableBuilder( final PageService pageService, @@ -94,6 +95,12 @@ public class TableBuilder { return this; } + public TableBuilder withRestCallAdapter( + final Function>.RestCallBuilder, RestCall>.RestCallBuilder> adapter) { + this.restCallAdapter = adapter; + return this; + } + public TableBuilder withMultiselection() { this.type |= SWT.MULTI; return this; @@ -125,6 +132,7 @@ public class TableBuilder { this.type, parent, this.restCall, + this.restCallAdapter, this.pageService, this.columns, this.actions, diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java index dbe18e88..cafb2537 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java @@ -197,7 +197,9 @@ public class ExamDAOImpl implements ExamDAO { exam.lmsSetupId, exam.externalId, exam.owner, - null, + (exam.supporter != null) + ? StringUtils.join(exam.supporter, Constants.LIST_SEPARATOR_CHAR) + : null, (exam.type != null) ? exam.type.name() : ExamType.UNDEFINED.name(), null, BooleanUtils.toInteger(true)); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/LmsSetupDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/LmsSetupDAOImpl.java index df113100..c8cae887 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/LmsSetupDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/LmsSetupDAOImpl.java @@ -45,14 +45,14 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.dao.TransactionHandler; public class LmsSetupDAOImpl implements LmsSetupDAO { private final LmsSetupRecordMapper lmsSetupRecordMapper; - private final ClientCredentialService internalEncryptionService; + private final ClientCredentialService clientCredentialService; protected LmsSetupDAOImpl( final LmsSetupRecordMapper lmsSetupRecordMapper, - final ClientCredentialService internalEncryptionService) { + final ClientCredentialService clientCredentialService) { this.lmsSetupRecordMapper = lmsSetupRecordMapper; - this.internalEncryptionService = internalEncryptionService; + this.clientCredentialService = clientCredentialService; } @Override @@ -130,7 +130,7 @@ public class LmsSetupDAOImpl implements LmsSetupDAO { public Result save(final LmsSetup lmsSetup) { return Result.tryCatch(() -> { - final ClientCredentials lmsCredentials = this.internalEncryptionService.encryptedClientCredentials( + final ClientCredentials lmsCredentials = this.clientCredentialService.encryptedClientCredentials( new ClientCredentials( lmsSetup.lmsAuthName, lmsSetup.lmsAuthSecret, @@ -159,7 +159,7 @@ public class LmsSetupDAOImpl implements LmsSetupDAO { public Result createNew(final LmsSetup lmsSetup) { return Result.tryCatch(() -> { - final ClientCredentials lmsCredentials = this.internalEncryptionService.encryptedClientCredentials( + final ClientCredentials lmsCredentials = this.clientCredentialService.encryptedClientCredentials( new ClientCredentials( lmsSetup.lmsAuthName, lmsSetup.lmsAuthSecret, @@ -311,8 +311,8 @@ public class LmsSetupDAOImpl implements LmsSetupDAO { record.getLmsClientsecret(), record.getLmsRestApiToken()); - final CharSequence plainClientId = this.internalEncryptionService.getPlainClientId(clientCredentials); - final CharSequence plainAccessToken = this.internalEncryptionService.getPlainAccessToken(clientCredentials); + final CharSequence plainClientId = this.clientCredentialService.getPlainClientId(clientCredentials); + final CharSequence plainAccessToken = this.clientCredentialService.getPlainAccessToken(clientCredentials); return Result.tryCatch(() -> new LmsSetup( record.getId(), diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPIService.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPIService.java index d15c88e1..4b371778 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPIService.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPIService.java @@ -19,6 +19,8 @@ import org.joda.time.DateTime; import ch.ethz.seb.sebserver.gbl.model.Page; import ch.ethz.seb.sebserver.gbl.model.exam.QuizData; +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup; +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetupTestResult; import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap; @@ -44,6 +46,13 @@ public interface LmsAPIService { * @return LmsAPITemplate for specified LmsSetup configuration */ Result getLmsAPITemplate(String lmsSetupId); + /** This can be used to test an LmsSetup connection parameter without saving or heaving + * an already persistent version of an LmsSetup. + * + * @param lmsSetup + * @return */ + LmsSetupTestResult testAdHoc(LmsSetup lmsSetup); + /** Get a LmsAPITemplate for specified LmsSetup configuration by primary key * * @param lmsSetupId the primary key of the LmsSetup diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java index 65ad14ea..8fa4246d 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java @@ -28,6 +28,7 @@ import ch.ethz.seb.sebserver.gbl.async.AsyncService; import ch.ethz.seb.sebserver.gbl.model.Page; import ch.ethz.seb.sebserver.gbl.model.exam.QuizData; import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup; +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetupTestResult; import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.webservice.servicelayer.client.ClientCredentialService; @@ -140,6 +141,17 @@ public class LmsAPIServiceImpl implements LmsAPIService { .flatMap(this::getLmsAPITemplate); } + @Override + public LmsSetupTestResult testAdHoc(final LmsSetup lmsSetup) { + final ClientCredentials lmsCredentials = this.clientCredentialService.encryptedClientCredentials( + new ClientCredentials( + lmsSetup.lmsAuthName, + lmsSetup.lmsAuthSecret, + lmsSetup.lmsRestApiToken)); + + return createLmsSetupTemplate(lmsSetup, lmsCredentials).testLmsSetup(); + } + private Result getLmsAPITemplate(final LmsSetup lmsSetup) { return Result.tryCatch(() -> { LmsAPITemplate lmsAPITemplate = getFromCache(lmsSetup); @@ -176,6 +188,10 @@ public class LmsAPIServiceImpl implements LmsAPIService { .getLmsAPIAccessCredentials(lmsSetup.getModelId()) .getOrThrow(); + return createLmsSetupTemplate(lmsSetup, credentials); + } + + private LmsAPITemplate createLmsSetupTemplate(final LmsSetup lmsSetup, final ClientCredentials credentials) { switch (lmsSetup.lmsType) { case MOCKUP: return new MockupLmsAPITemplate( @@ -231,5 +247,4 @@ public class LmsAPIServiceImpl implements LmsAPIService { return true; } } - } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java index 50479869..60158f95 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java @@ -8,10 +8,13 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api; +import javax.validation.Valid; + import org.mybatis.dynamic.sql.SqlTable; import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @@ -77,7 +80,7 @@ public class LmsSetupController extends ActivatableEntityController