From c0f68c13403a66656078d03659d5a62b1d8c6e3d Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 18 Apr 2019 16:01:56 +0200 Subject: [PATCH] SEBSERV-44 added Controllers --- .../ch/ethz/seb/sebserver/gbl/api/API.java | 3 + .../gbl/model/sebconfig/Configuration.java | 13 +- .../model/sebconfig/ConfigurationNode.java | 14 +- .../sebconfig/ConfigurationTableValue.java | 4 +- .../model/sebconfig/ConfigurationValue.java | 13 +- .../servicelayer/dao/ConfigurationDAO.java | 2 +- .../dao/ConfigurationValueDAO.java | 16 ++- .../dao/impl/ConfigurationDAOImpl.java | 2 +- .../dao/impl/ConfigurationNodeDAOImpl.java | 18 +-- .../dao/impl/ConfigurationValueDAOImpl.java | 92 +++++++++--- .../api/ConfigurationAttributeController.java | 1 - .../weblayer/api/ConfigurationController.java | 135 ++++++++++++++++++ .../api/ConfigurationNodeController.java | 59 ++++++++ .../api/ConfigurationValueController.java | 134 +++++++++++++++++ 14 files changed, 466 insertions(+), 40 deletions(-) create mode 100644 src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationController.java create mode 100644 src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationNodeController.java create mode 100644 src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationValueController.java 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 2439ace1..afd9efcc 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 @@ -70,8 +70,11 @@ public final class API { public static final String CONFIGURATION_NODE_ENDPOINT = "/configuration_node"; public static final String CONFIGURATION_ENDPOINT = "/configuration"; + public static final String CONFIGURATION_SAVE_TO_HISTORY_PATH_SEGMENT = "/save_to_history"; + public static final String CONFIGURATION_RESTORE_FROM_HISTORY_PATH_SEGMENT = "/restore"; public static final String CONFIGURATION_VALUE_ENDPOINT = "/configuration_value"; + public static final String CONFIGURATION_TABLE_VALUE_PATH_SEGMENT = "/table"; public static final String CONFIGURATION_ATTRIBUTE_ENDPOINT = "/configuration_attribute"; diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/Configuration.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/Configuration.java index 24d06028..bdab2414 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/Configuration.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/Configuration.java @@ -18,8 +18,10 @@ import com.fasterxml.jackson.annotation.JsonProperty; import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.api.EntityType; -import ch.ethz.seb.sebserver.gbl.model.GrantEntity; +import ch.ethz.seb.sebserver.gbl.api.POSTMapper; +import ch.ethz.seb.sebserver.gbl.model.Domain; import ch.ethz.seb.sebserver.gbl.model.Domain.CONFIGURATION; +import ch.ethz.seb.sebserver.gbl.model.GrantEntity; @JsonIgnoreProperties(ignoreUnknown = true) public final class Configuration implements GrantEntity { @@ -67,6 +69,15 @@ public final class Configuration implements GrantEntity { this.followup = followup; } + public Configuration(final Long institutionId, final POSTMapper postParams) { + this.id = null; + this.institutionId = institutionId; + this.configurationNodeId = postParams.getLong(Domain.CONFIGURATION.ATTR_CONFIGURATION_NODE_ID); + this.version = postParams.getString(Domain.CONFIGURATION.ATTR_VERSION); + this.versionDate = postParams.getDateTime(Domain.CONFIGURATION.ATTR_VERSION_DATE); + this.followup = null; + } + @Override public EntityType entityType() { return EntityType.CONFIGURATION; diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationNode.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationNode.java index e2923a5c..4d699a8f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationNode.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationNode.java @@ -16,7 +16,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import ch.ethz.seb.sebserver.gbl.api.EntityType; +import ch.ethz.seb.sebserver.gbl.api.POSTMapper; import ch.ethz.seb.sebserver.gbl.model.Activatable; +import ch.ethz.seb.sebserver.gbl.model.Domain; import ch.ethz.seb.sebserver.gbl.model.Domain.CONFIGURATION_NODE; import ch.ethz.seb.sebserver.gbl.model.GrantEntity; @@ -78,11 +80,21 @@ public final class ConfigurationNode implements GrantEntity, Activatable { this.name = name; this.description = description; this.type = type; - this.owner = owner; this.active = active; } + public ConfigurationNode(final Long institutionId, final POSTMapper postParams) { + this.id = null; + this.institutionId = institutionId; + this.templateId = postParams.getLong(Domain.CONFIGURATION_NODE.ATTR_TEMPLATE_ID); + this.name = postParams.getString(Domain.CONFIGURATION_NODE.ATTR_NAME); + this.description = postParams.getString(Domain.CONFIGURATION_NODE.ATTR_DESCRIPTION); + this.type = postParams.getEnum(Domain.CONFIGURATION_NODE.ATTR_TYPE, ConfigurationType.class); + this.owner = postParams.getString(Domain.CONFIGURATION_NODE.ATTR_OWNER); + this.active = postParams.getBoolean(Domain.CONFIGURATION_NODE.ATTR_ACTIVE); + } + @Override public EntityType entityType() { return EntityType.CONFIGURATION_NODE; diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationTableValue.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationTableValue.java index 3b19421f..2df6b4c7 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationTableValue.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationTableValue.java @@ -18,8 +18,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import ch.ethz.seb.sebserver.gbl.api.EntityType; -import ch.ethz.seb.sebserver.gbl.model.GrantEntity; import ch.ethz.seb.sebserver.gbl.model.Domain.CONFIGURATION_VALUE; +import ch.ethz.seb.sebserver.gbl.model.GrantEntity; @JsonIgnoreProperties(ignoreUnknown = true) public final class ConfigurationTableValue implements GrantEntity { @@ -31,9 +31,11 @@ public final class ConfigurationTableValue implements GrantEntity { @JsonProperty(CONFIGURATION_VALUE.ATTR_INSTITUTION_ID) public final Long institutionId; + @NotNull @JsonProperty(CONFIGURATION_VALUE.ATTR_CONFIGURATION_ID) public final Long configurationId; + @NotNull @JsonProperty(CONFIGURATION_VALUE.ATTR_CONFIGURATION_ATTRIBUTE_ID) public final Long attributeId; diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationValue.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationValue.java index af3158f0..b9d119d3 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationValue.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/sebconfig/ConfigurationValue.java @@ -15,8 +15,10 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import ch.ethz.seb.sebserver.gbl.api.EntityType; -import ch.ethz.seb.sebserver.gbl.model.GrantEntity; +import ch.ethz.seb.sebserver.gbl.api.POSTMapper; +import ch.ethz.seb.sebserver.gbl.model.Domain; import ch.ethz.seb.sebserver.gbl.model.Domain.CONFIGURATION_VALUE; +import ch.ethz.seb.sebserver.gbl.model.GrantEntity; @JsonIgnoreProperties(ignoreUnknown = true) public final class ConfigurationValue implements GrantEntity { @@ -60,6 +62,15 @@ public final class ConfigurationValue implements GrantEntity { this.value = value; } + public ConfigurationValue(final Long institutionId, final POSTMapper postParams) { + this.id = null; + this.institutionId = institutionId; + this.configurationId = postParams.getLong(Domain.CONFIGURATION_VALUE.ATTR_CONFIGURATION_ID); + this.attributeId = postParams.getLong(Domain.CONFIGURATION_VALUE.ATTR_CONFIGURATION_ATTRIBUTE_ID); + this.listIndex = postParams.getInteger(Domain.CONFIGURATION_VALUE.ATTR_LIST_INDEX); + this.value = postParams.getString(Domain.CONFIGURATION_VALUE.ATTR_VALUE); + } + @Override public String getModelId() { return (this.id != null) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationDAO.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationDAO.java index cc7a56c1..69076861 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationDAO.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationDAO.java @@ -40,7 +40,7 @@ public interface ConfigurationDAO extends EntityDAO saveInHistory(Long configurationNodeId); + Result saveToHistory(Long configurationNodeId); /** Restores the current follow-up Configuration to the values of a given Configuration * in the history of the specified ConfigurationNode. diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationValueDAO.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationValueDAO.java index a2e1d6cf..1e80c24a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationValueDAO.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ConfigurationValueDAO.java @@ -8,15 +8,27 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.dao; +import java.util.Collection; +import java.util.Set; + +import ch.ethz.seb.sebserver.gbl.model.EntityKey; import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationTableValue; import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationValue; import ch.ethz.seb.sebserver.gbl.util.Result; public interface ConfigurationValueDAO extends EntityDAO { + /** NOTE: Deletion is not supported for ConfigurationValue. + * A ConfigurationValue get automatically deleted on deletion of a Configuration */ + @Override + default Result> delete(final Set all) { + throw new UnsupportedOperationException( + "Deletion is not supported for ConfigurationValue. A ConfigurationValue get automatically deleted on deletion of a Configuration"); + } + Result getTableValue( - Long institutionId, - Long attributeId, + final Long institutionId, + final Long attributeId, final Long configurationId); Result saveTableValue(ConfigurationTableValue value); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationDAOImpl.java index c624954b..ce711dda 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationDAOImpl.java @@ -142,7 +142,7 @@ public class ConfigurationDAOImpl implements ConfigurationDAO { @Override @Transactional - public Result saveInHistory(final Long configurationNodeId) { + public Result saveToHistory(final Long configurationNodeId) { return Result.tryCatch(() -> { // get follow-up configuration... diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationNodeDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationNodeDAOImpl.java index 9a8ccd60..e9b06782 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationNodeDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationNodeDAOImpl.java @@ -441,14 +441,16 @@ public class ConfigurationNodeDAOImpl implements ConfigurationNodeDAO { attrRec.getId(), attrRec.getDefaultValue()); - batchValueMapper.insert(new ConfigurationValueRecord( - null, - configNode.institutionId, - config.getId(), - attrRec.getId(), - 0, - bigValue ? null : value, - bigValue ? value : null)); + if (StringUtils.isNoneBlank(value)) { + batchValueMapper.insert(new ConfigurationValueRecord( + null, + configNode.institutionId, + config.getId(), + attrRec.getId(), + 0, + bigValue ? null : value, + bigValue ? value : null)); + } }); batchSession.flushStatements(); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationValueDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationValueDAOImpl.java index e084f1d1..96f19741 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationValueDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ConfigurationValueDAOImpl.java @@ -20,13 +20,13 @@ import java.util.Set; import java.util.function.Predicate; import java.util.stream.Collectors; +import org.apache.commons.lang3.BooleanUtils; import org.mybatis.dynamic.sql.SqlBuilder; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import ch.ethz.seb.sebserver.gbl.api.EntityType; -import ch.ethz.seb.sebserver.gbl.model.EntityKey; import ch.ethz.seb.sebserver.gbl.model.sebconfig.AttributeType; import ch.ethz.seb.sebserver.gbl.model.sebconfig.AttributeValueType; import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationTableValue; @@ -123,8 +123,11 @@ public class ConfigurationValueDAOImpl implements ConfigurationValueDAO { @Transactional public Result createNew(final ConfigurationValue data) { return checkInstitutionalIntegrity(data) - .flatMap(v -> attributeRecordById(v.attributeId)) + .map(this::checkFollowUpIntegrity) + .map(this::checkCreationIntegrity) + .flatMap(this::attributeRecord) .map(attributeRecord -> { + final String value = (data.value != null) ? data.value : attributeRecord.getDefaultValue(); @@ -148,8 +151,11 @@ public class ConfigurationValueDAOImpl implements ConfigurationValueDAO { @Override @Transactional public Result save(final ConfigurationValue data) { - return attributeRecordById(data.attributeId) + return checkInstitutionalIntegrity(data) + .map(this::checkFollowUpIntegrity) + .flatMap(this::attributeRecord) .map(attributeRecord -> { + final boolean bigValue = isBigValue(attributeRecord); final ConfigurationValueRecord newRecord = new ConfigurationValueRecord( data.id, @@ -167,24 +173,6 @@ public class ConfigurationValueDAOImpl implements ConfigurationValueDAO { .onErrorDo(TransactionHandler::rollback); } - @Override - @Transactional - public Result> delete(final Set all) { - return Result.tryCatch(() -> { - - final List ids = extractListOfPKs(all); - - this.configurationValueRecordMapper.deleteByExample() - .where(ConfigurationValueRecordDynamicSqlSupport.id, isIn(ids)) - .build() - .execute(); - - return ids.stream() - .map(id -> new EntityKey(id, EntityType.CONFIGURATION_VALUE)) - .collect(Collectors.toList()); - }); - } - @Override @Transactional(readOnly = true) public Result getTableValue( @@ -257,14 +245,16 @@ public class ConfigurationValueDAOImpl implements ConfigurationValueDAO { new ArrayList<>(valueMapping.keySet()), values); }); - } @Override @Transactional public Result saveTableValue(final ConfigurationTableValue value) { - return attributeRecordById(value.attributeId) + return checkInstitutionalIntegrity(value) + .map(this::checkFollowUpIntegrity) + .flatMap(val -> attributeRecordById(val.attributeId)) .map(attributeRecord -> { + final List columnAttributes = this.configurationAttributeRecordMapper.selectByExample() .where( @@ -317,6 +307,10 @@ public class ConfigurationValueDAOImpl implements ConfigurationValueDAO { }); } + private Result attributeRecord(final ConfigurationValue value) { + return attributeRecordById(value.attributeId); + } + private Result attributeRecordById(final Long id) { return Result.tryCatch(() -> { final ConfigurationAttributeRecord record = this.configurationAttributeRecordMapper @@ -373,4 +367,56 @@ public class ConfigurationValueDAOImpl implements ConfigurationValueDAO { }); } + private Result checkInstitutionalIntegrity(final ConfigurationTableValue data) { + return Result.tryCatch(() -> { + final ConfigurationRecord r = this.configurationRecordMapper.selectByPrimaryKey(data.configurationId); + if (r.getInstitutionId().longValue() != data.institutionId.longValue()) { + throw new IllegalArgumentException("Institutional integrity constraint violation"); + } + return data; + }); + } + + private ConfigurationTableValue checkFollowUpIntegrity(final ConfigurationTableValue data) { + checkFollowUp(data.configurationId); + return data; + } + + private ConfigurationValue checkFollowUpIntegrity(final ConfigurationValue data) { + checkFollowUp(data.configurationId); + return data; + } + + private void checkFollowUp(final Long configurationId) { + final ConfigurationRecord config = this.configurationRecordMapper + .selectByPrimaryKey(configurationId); + + if (!BooleanUtils.toBoolean(config.getFollowup())) { + throw new IllegalArgumentException( + "Forbidden to modify an configuration value of a none follow-up configuration"); + } + } + + private ConfigurationValue checkCreationIntegrity(final ConfigurationValue data) { + final Long exists = this.configurationValueRecordMapper.countByExample() + .where( + ConfigurationValueRecordDynamicSqlSupport.configurationId, + isEqualTo(data.configurationId)) + .and( + ConfigurationValueRecordDynamicSqlSupport.configurationAttributeId, + isEqualTo(data.attributeId)) + .and( + ConfigurationValueRecordDynamicSqlSupport.listIndex, + isEqualTo(data.listIndex)) + .build() + .execute(); + + if (exists != null && exists.longValue() > 0) { + throw new IllegalArgumentException( + "The configuration value already exists"); + } + + return data; + } + } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationAttributeController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationAttributeController.java index 359f4cd4..5e55f9c1 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationAttributeController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationAttributeController.java @@ -49,7 +49,6 @@ public class ConfigurationAttributeController extends EntityController { + + private final ConfigurationDAO configurationDAO; + private final ConfigurationNodeDAO configurationNodeDAO; + + protected ConfigurationController( + final AuthorizationService authorization, + final BulkActionService bulkActionService, + final ConfigurationDAO entityDAO, + final UserActivityLogDAO userActivityLogDAO, + final PaginationService paginationService, + final BeanValidationService beanValidationService, + final ConfigurationNodeDAO configurationNodeDAO) { + + super(authorization, + bulkActionService, + entityDAO, + userActivityLogDAO, + paginationService, + beanValidationService); + + this.configurationDAO = entityDAO; + this.configurationNodeDAO = configurationNodeDAO; + } + + @RequestMapping( + path = API.CONFIGURATION_SAVE_TO_HISTORY_PATH_SEGMENT + API.MODEL_ID_VAR_PATH_SEGMENT, + method = RequestMethod.POST, + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, + produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public Configuration saveToHistory(@PathVariable final String configId) { + + return this.entityDAO.byModelId(configId) + .flatMap(this::checkModifyAccess) + .flatMap(config -> this.configurationDAO.saveToHistory(config.configurationNodeId)) + .getOrThrow(); + } + + @RequestMapping( + path = API.CONFIGURATION_RESTORE_FROM_HISTORY_PATH_SEGMENT + API.MODEL_ID_VAR_PATH_SEGMENT, + method = RequestMethod.POST, + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, + produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public Configuration restoreFormHistory( + @PathVariable final String configId, + @RequestParam(name = API.PARAM_PARENT_MODEL_ID, required = true) final Long configurationNodeId) { + + return this.entityDAO.byModelId(configId) + .flatMap(this::checkModifyAccess) + .flatMap(config -> this.configurationDAO.restoreToVersion(configurationNodeId, config.getId())) + .getOrThrow(); + } + + @Override + public Collection getDependencies(final String modelId, final BulkActionType bulkActionType) { + throw new UnsupportedOperationException(); + } + + @Override + public Configuration create(final MultiValueMap allRequestParams, final Long institutionId) { + throw new UnsupportedOperationException(); + } + + @Override + public EntityProcessingReport hardDelete(final String modelId) { + throw new UnsupportedOperationException(); + } + + @Override + protected Configuration createNew(final POSTMapper postParams) { + throw new UnsupportedOperationException(); + } + + @Override + protected SqlTable getSQLTableOfEntity() { + return ConfigurationRecordDynamicSqlSupport.configurationRecord; + } + + @Override + protected GrantEntity toGrantEntity(final Configuration entity) { + if (entity == null) { + return null; + } + + return this.configurationNodeDAO.byPK(entity.configurationNodeId) + .getOrThrow(); + } + + @Override + protected EntityType getGrantEntityType() { + return EntityType.CONFIGURATION_NODE; + } +} diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationNodeController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationNodeController.java new file mode 100644 index 00000000..4046797e --- /dev/null +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationNodeController.java @@ -0,0 +1,59 @@ +/* + * 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.webservice.weblayer.api; + +import org.mybatis.dynamic.sql.SqlTable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import ch.ethz.seb.sebserver.gbl.api.API; +import ch.ethz.seb.sebserver.gbl.api.POSTMapper; +import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationNode; +import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; +import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ConfigurationNodeRecordDynamicSqlSupport; +import ch.ethz.seb.sebserver.webservice.servicelayer.PaginationService; +import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.AuthorizationService; +import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionService; +import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ConfigurationNodeDAO; +import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO; +import ch.ethz.seb.sebserver.webservice.servicelayer.validation.BeanValidationService; + +@WebServiceProfile +@RestController +@RequestMapping("/${sebserver.webservice.api.admin.endpoint}" + API.CONFIGURATION_NODE_ENDPOINT) +public class ConfigurationNodeController extends EntityController { + + protected ConfigurationNodeController( + final AuthorizationService authorization, + final BulkActionService bulkActionService, + final ConfigurationNodeDAO entityDAO, + final UserActivityLogDAO userActivityLogDAO, + final PaginationService paginationService, + final BeanValidationService beanValidationService) { + + super(authorization, + bulkActionService, + entityDAO, + userActivityLogDAO, + paginationService, + beanValidationService); + } + + @Override + protected ConfigurationNode createNew(final POSTMapper postParams) { + final Long institutionId = postParams.getLong(API.PARAM_INSTITUTION_ID); + return new ConfigurationNode(institutionId, postParams); + } + + @Override + protected SqlTable getSQLTableOfEntity() { + return ConfigurationNodeRecordDynamicSqlSupport.configurationNodeRecord; + } + +} diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationValueController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationValueController.java new file mode 100644 index 00000000..333e336b --- /dev/null +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ConfigurationValueController.java @@ -0,0 +1,134 @@ +/* + * 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.webservice.weblayer.api; + +import javax.validation.Valid; + +import org.mybatis.dynamic.sql.SqlTable; +import org.springframework.http.MediaType; +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; +import org.springframework.web.bind.annotation.RestController; + +import ch.ethz.seb.sebserver.gbl.api.API; +import ch.ethz.seb.sebserver.gbl.api.EntityType; +import ch.ethz.seb.sebserver.gbl.api.POSTMapper; +import ch.ethz.seb.sebserver.gbl.model.Domain; +import ch.ethz.seb.sebserver.gbl.model.EntityProcessingReport; +import ch.ethz.seb.sebserver.gbl.model.GrantEntity; +import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationTableValue; +import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationValue; +import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; +import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ConfigurationValueRecordDynamicSqlSupport; +import ch.ethz.seb.sebserver.webservice.servicelayer.PaginationService; +import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.AuthorizationService; +import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionService; +import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ConfigurationDAO; +import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ConfigurationValueDAO; +import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO; +import ch.ethz.seb.sebserver.webservice.servicelayer.validation.BeanValidationService; + +@WebServiceProfile +@RestController +@RequestMapping("/${sebserver.webservice.api.admin.endpoint}" + API.CONFIGURATION_VALUE_ENDPOINT) +public class ConfigurationValueController extends EntityController { + + private final ConfigurationDAO configurationDAO; + private final ConfigurationValueDAO configurationValueDAO; + + protected ConfigurationValueController( + final AuthorizationService authorization, + final BulkActionService bulkActionService, + final ConfigurationValueDAO entityDAO, + final UserActivityLogDAO userActivityLogDAO, + final PaginationService paginationService, + final BeanValidationService beanValidationService, + final ConfigurationDAO configurationDAO) { + + super(authorization, + bulkActionService, + entityDAO, + userActivityLogDAO, + paginationService, + beanValidationService); + + this.configurationDAO = configurationDAO; + this.configurationValueDAO = entityDAO; + } + + @Override + protected ConfigurationValue createNew(final POSTMapper postParams) { + final Long institutionId = postParams.getLong(API.PARAM_INSTITUTION_ID); + return new ConfigurationValue(institutionId, postParams); + } + + @Override + protected SqlTable getSQLTableOfEntity() { + return ConfigurationValueRecordDynamicSqlSupport.configurationValueRecord; + } + + @Override + public EntityProcessingReport hardDelete(final String modelId) { + throw new UnsupportedOperationException(); + } + + @RequestMapping( + path = API.CONFIGURATION_TABLE_VALUE_PATH_SEGMENT, + method = RequestMethod.GET, + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, + produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ConfigurationTableValue getTableValueBy( + @RequestParam( + name = Domain.CONFIGURATION_VALUE.ATTR_CONFIGURATION_ATTRIBUTE_ID, + required = true) final Long attributeId, + @RequestParam( + name = Domain.CONFIGURATION_VALUE.ATTR_CONFIGURATION_ID, + required = true) final Long configurationId) { + + return this.configurationDAO.byPK(configurationId) + .flatMap(this.authorization::checkRead) + .flatMap(config -> this.configurationValueDAO.getTableValue( + config.institutionId, + attributeId, + configurationId)) + .getOrThrow(); + } + + @RequestMapping( + path = API.CONFIGURATION_TABLE_VALUE_PATH_SEGMENT, + method = RequestMethod.PUT, + consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, + produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ConfigurationTableValue savePut( + @Valid @RequestBody final ConfigurationTableValue tableValue) { + + return this.configurationDAO.byPK(tableValue.configurationId) + .flatMap(this.authorization::checkModify) + .flatMap(config -> this.configurationValueDAO.saveTableValue(tableValue)) + .getOrThrow(); + } + + @Override + protected GrantEntity toGrantEntity(final ConfigurationValue entity) { + if (entity == null) { + return null; + } + + return this.configurationDAO.byPK(entity.configurationId) + .getOrThrow(); + } + + @Override + protected EntityType getGrantEntityType() { + return EntityType.CONFIGURATION; + } + +}