fixed for demo, code cleanup

This commit is contained in:
anhefti 2019-04-05 15:58:59 +02:00
parent eb8e37a888
commit 71a75bb406
13 changed files with 148 additions and 115 deletions

View file

@ -195,12 +195,13 @@ public class ExamForm implements TemplateComposer {
"sebserver.exam.form.status",
i18nSupport.getText(new LocTextKey("sebserver.exam.status." + examStatus.name())))
.readonly(true))
.addField(FormBuilder.multiComboSelection(
Domain.EXAM.ATTR_SUPPORTER,
"sebserver.exam.form.supporter",
StringUtils.join(exam.supporter, Constants.LIST_SEPARATOR_CHAR),
this.resourceService::examSupporterResources)
.withCondition(isNotNew))
.addFieldIf(
isNotNew,
() -> 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)
@ -267,10 +268,12 @@ public class ExamForm implements TemplateComposer {
thresholdColumnKey,
ExamForm::thresholdsValue,
false))
.withDefaultAction(actionBuilder
.newAction(ActionDefinition.EXAM_INDICATOR_MODIFY_FROM_LIST)
.withParentEntityKey(entityKey)
.create())
.withDefaultActionIf(
() -> editable,
() -> actionBuilder
.newAction(ActionDefinition.EXAM_INDICATOR_MODIFY_FROM_LIST)
.withParentEntityKey(entityKey)
.create())
.compose(content);

View file

@ -11,6 +11,8 @@ package ch.ethz.seb.sebserver.gui.content;
import java.util.function.Function;
import org.eclipse.swt.widgets.Composite;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@ -18,17 +20,16 @@ import org.springframework.stereotype.Component;
import ch.ethz.seb.sebserver.gbl.Constants;
import ch.ethz.seb.sebserver.gbl.api.EntityType;
import ch.ethz.seb.sebserver.gbl.model.Domain;
import ch.ethz.seb.sebserver.gbl.model.Entity;
import ch.ethz.seb.sebserver.gbl.model.exam.Exam;
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.user.UserRole;
import ch.ethz.seb.sebserver.gbl.profile.GuiProfile;
import ch.ethz.seb.sebserver.gui.content.action.ActionDefinition;
import ch.ethz.seb.sebserver.gui.service.ResourceService;
import ch.ethz.seb.sebserver.gui.service.i18n.I18nSupport;
import ch.ethz.seb.sebserver.gui.service.i18n.LocTextKey;
import ch.ethz.seb.sebserver.gui.service.page.PageContext;
import ch.ethz.seb.sebserver.gui.service.page.PageMessageException;
import ch.ethz.seb.sebserver.gui.service.page.PageService;
import ch.ethz.seb.sebserver.gui.service.page.PageService.PageActionBuilder;
import ch.ethz.seb.sebserver.gui.service.page.TemplateComposer;
@ -60,8 +61,9 @@ public class ExamList implements TemplateComposer {
new LocTextKey("sebserver.exam.list.column.name");
private final static LocTextKey columnTitleTypeKey =
new LocTextKey("sebserver.exam.list.column.type");
private final static LocTextKey noModifyOfOutDatedExams =
new LocTextKey("sebserver.exam.list.modify.out.dated");
private final TableFilterAttribute institutionFilter;
private final TableFilterAttribute lmsFilter;
private final TableFilterAttribute nameFilter =
new TableFilterAttribute(CriteriaType.TEXT, QuizData.FILTER_ATTR_NAME);
@ -77,11 +79,6 @@ public class ExamList implements TemplateComposer {
this.resourceService = resourceService;
this.pageSize = (pageSize != null) ? pageSize : 20;
this.institutionFilter = new TableFilterAttribute(
CriteriaType.SINGLE_SELECTION,
Entity.FILTER_ATTR_INSTITUTION,
this.resourceService::institutionResource);
this.lmsFilter = new TableFilterAttribute(
CriteriaType.SINGLE_SELECTION,
LmsSetup.FILTER_ATTR_LMS_SETUP,
@ -101,7 +98,6 @@ public class ExamList implements TemplateComposer {
pageContext.getParent(),
new LocTextKey("sebserver.exam.list.title"));
final boolean isSEBAdmin = currentUser.get().hasRole(UserRole.SEB_SERVER_ADMIN);
final PageActionBuilder actionBuilder = this.pageService.pageActionBuilder(pageContext.clearEntityKeys());
// table
@ -109,13 +105,6 @@ public class ExamList implements TemplateComposer {
this.pageService.entityTableBuilder(restService.getRestCall(GetExams.class))
.withEmptyMessage(new LocTextKey("sebserver.exam.list.empty"))
.withPaging(this.pageSize)
.withColumnIf(() -> isSEBAdmin,
new ColumnDefinition<>(
Domain.EXAM.ATTR_INSTITUTION_ID,
new LocTextKey("sebserver.exam.list.column.institution"),
examInstitutionNameFunction(this.resourceService),
this.institutionFilter,
false))
.withColumn(new ColumnDefinition<>(
Domain.EXAM.ATTR_LMS_SETUP_ID,
columnTitleLmsSetupKey,
@ -158,14 +147,25 @@ public class ExamList implements TemplateComposer {
.publishIf(table::hasAnyContent)
.newAction(ActionDefinition.EXAM_MODIFY_FROM_LIST)
.withSelect(table::getSelection, PageAction::applySingleSelection, emptySelectionTextKey)
.withSelect(
table::getSelection,
action -> this.modifyExam(action, table),
emptySelectionTextKey)
.publishIf(() -> userGrant.im() && table.hasAnyContent());
}
private static Function<Exam, String> examInstitutionNameFunction(final ResourceService resourceService) {
return exam -> resourceService.getInstitutionNameFunction()
.apply(String.valueOf(exam.institutionId));
private PageAction modifyExam(final PageAction action, final EntityTable<Exam> table) {
final Exam exam = table.getSelectedROWData();
if (exam.startTime != null) {
final DateTime now = DateTime.now(DateTimeZone.UTC);
if (exam.startTime.isBefore(now)) {
throw new PageMessageException(noModifyOfOutDatedExams);
}
}
return action.withEntityKey(action.getSingleSelection());
}
private static Function<Exam, String> examLmsSetupNameFunction(final ResourceService resourceService) {

View file

@ -129,11 +129,12 @@ public class InstitutionForm implements TemplateComposer {
Domain.INSTITUTION.ATTR_URL_SUFFIX,
"sebserver.institution.form.urlSuffix",
institution.urlSuffix))
.addField(FormBuilder.imageUpload(
Domain.INSTITUTION.ATTR_LOGO_IMAGE,
"sebserver.institution.form.logoImage",
institution.logoImage)
.withCondition(() -> !isNew && modifyGrant))
.addFieldIf(
() -> !isNew && modifyGrant,
() -> 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));

View file

@ -137,13 +137,14 @@ public class LmsSetupForm implements TemplateComposer {
.putStaticValueIf(isNotNew,
Domain.LMS_SETUP.ATTR_LMS_TYPE,
String.valueOf(lmsSetup.getLmsType()))
.addField(FormBuilder.singleSelection(
Domain.LMS_SETUP.ATTR_INSTITUTION_ID,
"sebserver.lmssetup.form.institution",
String.valueOf(lmsSetup.getInstitutionId()),
() -> this.resourceService.institutionResource())
.withCondition(isSEBAdmin)
.readonlyIf(isNotNew))
.addFieldIf(
isSEBAdmin,
() -> FormBuilder.singleSelection(
Domain.LMS_SETUP.ATTR_INSTITUTION_ID,
"sebserver.lmssetup.form.institution",
String.valueOf(lmsSetup.getInstitutionId()),
() -> this.resourceService.institutionResource())
.readonlyIf(isNotNew))
.addField(FormBuilder.text(
Domain.LMS_SETUP.ATTR_NAME,
"sebserver.lmssetup.form.name",
@ -154,21 +155,24 @@ public class LmsSetupForm implements TemplateComposer {
(lmsType != null) ? lmsType.name() : null,
this.resourceService::lmsTypeResources)
.readonlyIf(isNotNew))
.addField(FormBuilder.text(
Domain.LMS_SETUP.ATTR_LMS_URL,
"sebserver.lmssetup.form.url",
lmsSetup.getLmsApiUrl())
.withCondition(() -> isNotNew.getAsBoolean()))
.addField(FormBuilder.text(
Domain.LMS_SETUP.ATTR_LMS_CLIENTNAME,
"sebserver.lmssetup.form.clientname.lms",
lmsSetup.getLmsAuthName())
.withCondition(() -> isNotNew.getAsBoolean()))
.addField(FormBuilder.text(
Domain.LMS_SETUP.ATTR_LMS_CLIENTSECRET,
"sebserver.lmssetup.form.secret.lms")
.asPasswordField()
.withCondition(() -> isNotNew.getAsBoolean()))
.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())
.buildFor((entityKey == null)
? restService.getRestCall(NewLmsSetup.class)

View file

@ -108,8 +108,9 @@ public class LmsSetupList implements TemplateComposer {
this.pageService.entityTableBuilder(restService.getRestCall(GetLmsSetups.class))
.withEmptyMessage(EMPTY_LIST_TEXT_KEY)
.withPaging(this.pageSize)
.withColumnIf(() -> isSEBAdmin,
new ColumnDefinition<>(
.withColumnIf(
() -> isSEBAdmin,
() -> new ColumnDefinition<>(
Domain.LMS_SETUP.ATTR_INSTITUTION_ID,
INSTITUTION_TEXT_KEY,
lmsSetupInstitutionNameFunction(this.resourceService),

View file

@ -95,11 +95,12 @@ public class UserAccountChangePasswordForm implements TemplateComposer {
PasswordChange.ATTR_NAME_NEW_PASSWORD,
"sebserver.useraccount.form.password.new")
.asPasswordField())
.addField(FormBuilder.text(
PasswordChange.ATTR_NAME_CONFIRM_NEW_PASSWORD,
"sebserver.useraccount.form.password.new.confirm")
.asPasswordField()
.withCondition(() -> entityKey != null))
.addFieldIf(
() -> entityKey != null,
() -> FormBuilder.text(
PasswordChange.ATTR_NAME_CONFIRM_NEW_PASSWORD,
"sebserver.useraccount.form.password.new.confirm")
.asPasswordField())
.buildFor(this.restService.getRestCall(ChangePassword.class));
this.pageService.pageActionBuilder(pageContext)

View file

@ -138,13 +138,14 @@ public class UserAccountForm implements TemplateComposer {
.putStaticValueIf(isNotNew,
Domain.USER.ATTR_INSTITUTION_ID,
String.valueOf(userAccount.getInstitutionId()))
.addField(FormBuilder.singleSelection(
Domain.USER.ATTR_INSTITUTION_ID,
"sebserver.useraccount.form.institution",
String.valueOf(userAccount.getInstitutionId()),
() -> this.resourceService.institutionResource())
.withCondition(isSEBAdmin)
.readonlyIf(isNotNew))
.addFieldIf(
isSEBAdmin,
() -> FormBuilder.singleSelection(
Domain.USER.ATTR_INSTITUTION_ID,
"sebserver.useraccount.form.institution",
String.valueOf(userAccount.getInstitutionId()),
() -> this.resourceService.institutionResource())
.readonlyIf(isNotNew))
.addField(FormBuilder.text(
Domain.USER.ATTR_NAME,
"sebserver.useraccount.form.name",
@ -161,29 +162,33 @@ public class UserAccountForm implements TemplateComposer {
Domain.USER.ATTR_LANGUAGE,
"sebserver.useraccount.form.language",
userAccount.getLanguage().getLanguage(),
this.resourceService::languageResources))
this.resourceService::languageResources)
.readonly(true))
.addField(FormBuilder.singleSelection(
Domain.USER.ATTR_TIMEZONE,
"sebserver.useraccount.form.timezone",
userAccount.getTimeZone().getID(),
this.resourceService::timeZoneResources))
.addField(FormBuilder.multiSelection(
USER_ROLE.REFERENCE_NAME,
"sebserver.useraccount.form.roles",
StringUtils.join(userAccount.getRoles(), Constants.LIST_SEPARATOR_CHAR),
this.resourceService::userRoleResources)
.withCondition(() -> modifyGrant)
.visibleIf(writeGrant))
.addField(FormBuilder.text(
PasswordChange.ATTR_NAME_NEW_PASSWORD,
"sebserver.useraccount.form.password")
.asPasswordField()
.withCondition(isNew))
.addField(FormBuilder.text(
PasswordChange.ATTR_NAME_CONFIRM_NEW_PASSWORD,
"sebserver.useraccount.form.password.confirm")
.asPasswordField()
.withCondition(isNew))
.addFieldIf(
() -> modifyGrant,
() -> FormBuilder.multiSelection(
USER_ROLE.REFERENCE_NAME,
"sebserver.useraccount.form.roles",
StringUtils.join(userAccount.getRoles(), Constants.LIST_SEPARATOR_CHAR),
this.resourceService::userRoleResources)
.visibleIf(writeGrant))
.addFieldIf(
isNew,
() -> FormBuilder.text(
PasswordChange.ATTR_NAME_NEW_PASSWORD,
"sebserver.useraccount.form.password")
.asPasswordField())
.addFieldIf(
isNew,
() -> FormBuilder.text(
PasswordChange.ATTR_NAME_CONFIRM_NEW_PASSWORD,
"sebserver.useraccount.form.password.confirm")
.asPasswordField())
.buildFor((entityKey == null)
? restService.getRestCall(NewUserAccount.class)
: restService.getRestCall(SaveUserAccount.class));

View file

@ -118,8 +118,9 @@ public class UserAccountList implements TemplateComposer {
.withEmptyMessage(new LocTextKey("sebserver.useraccount.list.empty"))
.withPaging(this.pageSize)
.withColumnIf(isSEBAdmin,
new ColumnDefinition<>(
.withColumnIf(
isSEBAdmin,
() -> new ColumnDefinition<>(
Domain.USER.ATTR_INSTITUTION_ID,
INSTITUTION_TEXT_KEY,
userInstitutionNameFunction(this.resourceService),

View file

@ -16,7 +16,6 @@ public abstract class FieldBuilder<T> {
int spanEmptyCell = -1;
boolean autoEmptyCellSeparation = false;
String group = null;
BooleanSupplier condition = null;
boolean readonly = false;
boolean visible = true;
@ -55,11 +54,6 @@ public abstract class FieldBuilder<T> {
return this;
}
public FieldBuilder<T> withCondition(final BooleanSupplier condition) {
this.condition = condition;
return this;
}
public FieldBuilder<T> readonly(final boolean readonly) {
this.readonly = readonly;
return this;

View file

@ -145,23 +145,33 @@ public class FormBuilder {
return this;
}
public FormBuilder addField(final FieldBuilder<?> template) {
if (template.condition == null || template.condition.getAsBoolean()) {
template.spanLabel = (template.spanLabel < 0) ? this.defaultSpanLabel : template.spanLabel;
template.spanInput = (template.spanInput < 0) ? this.defaultSpanInput : template.spanInput;
template.spanEmptyCell = (template.spanEmptyCell < 0) ? this.defaultSpanEmptyCell : template.spanEmptyCell;
template.autoEmptyCellSeparation = template.autoEmptyCellSeparation || this.emptyCellSeparation;
public FormBuilder addFieldIf(
final BooleanSupplier condition,
final Supplier<FieldBuilder<?>> templateSupplier) {
if (template.autoEmptyCellSeparation && this.form.hasFields()) {
addEmptyCell(template.spanEmptyCell);
}
template.build(this);
if (StringUtils.isNoneBlank(template.group)) {
this.form.addToGroup(template.group, template.name);
}
if (condition.getAsBoolean()) {
return addField(templateSupplier.get());
}
return this;
}
public FormBuilder addField(final FieldBuilder<?> template) {
template.spanLabel = (template.spanLabel < 0) ? this.defaultSpanLabel : template.spanLabel;
template.spanInput = (template.spanInput < 0) ? this.defaultSpanInput : template.spanInput;
template.spanEmptyCell = (template.spanEmptyCell < 0) ? this.defaultSpanEmptyCell : template.spanEmptyCell;
template.autoEmptyCellSeparation = template.autoEmptyCellSeparation || this.emptyCellSeparation;
if (template.autoEmptyCellSeparation && this.form.hasFields()) {
addEmptyCell(template.spanEmptyCell);
}
template.build(this);
if (StringUtils.isNoneBlank(template.group)) {
this.form.addToGroup(template.group, template.name);
}
return this;
}

View file

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
@ -80,10 +81,10 @@ public class TableBuilder<ROW extends Entity> {
public TableBuilder<ROW> withColumnIf(
final BooleanSupplier condition,
final ColumnDefinition<ROW> columnDefinition) {
final Supplier<ColumnDefinition<ROW>> columnDefSupplier) {
if (condition != null && condition.getAsBoolean()) {
this.columns.add(columnDefinition);
this.columns.add(columnDefSupplier.get());
}
return this;
}
@ -98,6 +99,17 @@ public class TableBuilder<ROW extends Entity> {
return this;
}
public TableBuilder<ROW> withDefaultActionIf(
final BooleanSupplier condition,
final Supplier<PageAction> actionSupplier) {
if (condition.getAsBoolean()) {
return withDefaultAction(actionSupplier.get());
}
return this;
}
public TableBuilder<ROW> withDefaultAction(final PageAction action) {
this.defaultActionFunction = table -> action;
return this;

View file

@ -110,7 +110,7 @@ public class ExamDAOImpl implements ExamDAO {
}
if (from != null) {
if (exam.startTime.isAfter(from)) {
if (exam.startTime.isBefore(from)) {
return false;
}
}

View file

@ -234,6 +234,7 @@ sebserver.exam.list.column.starttime=Start Time {0}
sebserver.exam.list.column.type=Type
sebserver.exam.list.empty=No Exams has been found. Please adapt the filter or import one from Quiz
sebserver.exam.list.modify.out.dated=Running or finished exams cannot be modified.
sebserver.exam.action.list=Exam
sebserver.exam.action.list.view=View Exam