Skip to content

Commit

Permalink
avniproject/avni-webapp#1254 | Validate length of name for FEG and FE…
Browse files Browse the repository at this point in the history
… names
  • Loading branch information
himeshr committed Aug 29, 2024
1 parent 66221cf commit 2134cc8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.avni.server.domain.Concept;
import org.avni.server.domain.DeclarativeRule;
import org.avni.server.domain.Documentation;
import org.springframework.util.StringUtils;

import static java.lang.String.format;

public class FormElementBuilder extends BaseBuilder<FormElement, FormElementBuilder> {
private final FormElementGroup formElementGroup;
Expand All @@ -17,6 +20,9 @@ public FormElementBuilder(FormElementGroup formElementGroup, FormElement existin
}

public FormElementBuilder withName(String name) {
if(StringUtils.hasLength(name) && name.length() > 255) {
throw new BuilderException(format("FormElement name \"%s\" exceeds allowed length of 255 characters", name));
}
this.set("Name", name, String.class);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import org.avni.server.service.DocumentationService;
import org.avni.server.web.request.application.FormElementContract;
import org.avni.server.web.request.application.FormElementGroupContract;
import org.springframework.util.StringUtils;

import java.util.List;

import static java.lang.String.format;

public class FormElementGroupBuilder extends BaseBuilder<FormElementGroup, FormElementGroupBuilder> {
private final ConceptService conceptService;
private final DocumentationService documentationService;
Expand All @@ -31,6 +34,9 @@ public FormElementGroupBuilder(Form form, FormElementGroup existingFormElementGr
}

public FormElementGroupBuilder withName(String name) {
if(StringUtils.hasLength(name) && name.length() > 255) {
throw new BuilderException(format("FormElementGroup name \"%s\" exceeds allowed length of 255 characters", name));
}
this.set("Name", name, String.class);
return this;
}
Expand Down Expand Up @@ -90,7 +96,7 @@ private Concept getExistingConcept(String uuid, FormElement formElement) throws
Concept concept = formElement.getConcept() != null && formElement.getConcept().getUuid().equals(uuid) ?
formElement.getConcept() : conceptService.get(uuid);
if (concept == null) {
throw new FormBuilderException(String.format("Concept with uuid '%s' not found", uuid));
throw new FormBuilderException(format("Concept with uuid '%s' not found", uuid));
}
return concept;
}
Expand Down Expand Up @@ -137,7 +143,7 @@ private FormElement getQuestionGroup(FormElementContract formElementContract) th
if (formElementContract.getParentFormElementUuid() != null) {
group = getExistingFormElement(formElementContract.getParentFormElementUuid());
if (group == null) {
throw new FormBuilderException(String.format("Parent form element with uuid '%s' not found", formElementContract.getParentFormElementUuid()));
throw new FormBuilderException(format("Parent form element with uuid '%s' not found", formElementContract.getParentFormElementUuid()));
}
}
return group;
Expand All @@ -159,7 +165,7 @@ public FormElementGroupBuilder withoutFormElements(Organisation organisation, Li
private FormElement getFormElement(FormElementContract formElementContract) throws FormBuilderException {
FormElement formElement = get().findFormElement(formElementContract.getUuid());
if (formElement == null) {
throw new FormBuilderException(String.format("FormElement with uuid '%s' not found", formElementContract.getUuid()));
throw new FormBuilderException(format("FormElement with uuid '%s' not found", formElementContract.getUuid()));
}
return formElement;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avni.server.web;

import org.avni.server.application.*;
import org.avni.server.builder.BuilderException;
import org.avni.server.builder.FormBuilder;
import org.avni.server.builder.FormBuilderException;
import org.avni.server.dao.OperationalProgramRepository;
Expand Down Expand Up @@ -143,7 +144,7 @@ public ResponseEntity<?> save(@RequestBody FormContract formRequest) {
formRequest.validate();
formService.checkIfLocationConceptsHaveBeenUsed(formRequest);
formService.saveForm(formRequest);
} catch (InvalidObjectException | FormBuilderException e) {
} catch (BuilderException | InvalidObjectException | FormBuilderException e) {
logger.error(format("Error saving form: %s, with UUID: %s", formRequest.getName(), formRequest.getUuid()), e);
return ResponseEntity.badRequest().body(errorBodyBuilder.getErrorMessageBody(e));
}
Expand Down Expand Up @@ -236,7 +237,7 @@ public ResponseEntity<?> patch(@RequestBody FormContract formRequest) {
logger.info(format("Patching form: %s, with UUID: %s", formRequest.getName(), formRequest.getUuid()));
try {
formService.saveForm(formRequest);
} catch (FormBuilderException e) {
} catch (BuilderException | FormBuilderException e) {
logger.info(format("Error patching form: %s, with UUID: %s", formRequest.getName(), formRequest.getUuid()), e);
return ResponseEntity.badRequest().body(errorBodyBuilder.getErrorMessageBody(e));
}
Expand Down

0 comments on commit 2134cc8

Please sign in to comment.