Skip to content

Commit

Permalink
avniproject/avni-webapp#1297 - moved getVoidedName from ReactAdminUti…
Browse files Browse the repository at this point in the history
…l. set a voided name for card being deleted. handle unique constraint error. added unique constraint for name.
  • Loading branch information
petmongrels committed Sep 18, 2024
1 parent dcd6368 commit d8dcdb8
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.avni.server.domain.util;

public class EntityUtil {
public static String getVoidedName(String name, Long id) {
return String.format("%s (voided~%d)", name, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.avni.server.common.BulkItemSaveException;
import org.avni.server.dao.*;
import org.avni.server.domain.*;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.util.BadRequestError;
import org.avni.server.util.ObjectMapperSingleton;
import org.avni.server.web.contract.ReportCardContract;
Expand Down Expand Up @@ -70,8 +71,10 @@ public ReportCard editCard(ReportCardWebRequest request, Long cardId) {
return cardRepository.save(existingCard);
}

public void deleteCard(ReportCard card) {
public void deleteCard(Long id) {
ReportCard card = cardRepository.findEntity(id);
card.setVoided(true);
card.setName(EntityUtil.getVoidedName(card.getName(), card.getId()));
cardRepository.save(card);
}

Expand Down Expand Up @@ -161,7 +164,7 @@ private void assertNewNameIsUnique(String newName, String oldName) {
private void assertNoExistingCardWithName(String name) {
ReportCard existingCard = cardRepository.findByName(name);
if (existingCard != null) {
throw new BadRequestError(String.format("Card %s already exists", name));
throw new BadRequestError(String.format("Report card with same name (%s) already exists", name));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.avni.server.dao.*;
import org.avni.server.domain.*;
import org.avni.server.domain.app.dashboard.DashboardFilter;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.util.BadRequestError;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.web.contract.reports.*;
import org.avni.server.web.request.DashboardFilterRequest;
import org.avni.server.web.request.DashboardWebRequest;
Expand Down Expand Up @@ -120,7 +120,7 @@ public Dashboard editDashboard(DashboardWebRequest dashboardRequest, Long dashbo

public void deleteDashboard(Dashboard dashboard) {
dashboard.setVoided(true);
dashboard.setName((ReactAdminUtil.getVoidedName(dashboard.getName(), dashboard.getId())));
dashboard.setName((EntityUtil.getVoidedName(dashboard.getName(), dashboard.getId())));
dashboardRepository.save(dashboard);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.avni.server.dao.NewsRepository;
import org.avni.server.domain.CHSEntity;
import org.avni.server.domain.News;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.util.BadRequestError;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.web.request.NewsContract;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -44,7 +44,7 @@ public News editNews(NewsContract newsContract, Long id) {

public void deleteNews(News news) {
news.setVoided(true);
news.setTitle(ReactAdminUtil.getVoidedName(news.getTitle(), news.getId()));
news.setTitle(EntityUtil.getVoidedName(news.getTitle(), news.getId()));
newsRepository.save(news);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ public static Map<String, String> generateJsonError(String errorMsg) {
return errorMap;
}

public static String getVoidedName(String name, Long id) {
return String.format("%s (voided~%d)", name, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.avni.server.domain.Catchment;
import org.avni.server.domain.Organisation;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.framework.security.UserContextHolder;
import org.avni.server.service.CatchmentService;
import org.avni.server.service.ResetSyncService;
Expand Down Expand Up @@ -148,7 +149,7 @@ public ResponseEntity<?> voidCatchment(@PathVariable("id") Long id) {
return ResponseEntity.badRequest().body(ReactAdminUtil.generateJsonError(String.format("AddressLevelType with id %d not found", id)));
}
catchment.setVoided(true);
catchment.setName(ReactAdminUtil.getVoidedName(catchment.getName(),catchment.getId()));
catchment.setName(EntityUtil.getVoidedName(catchment.getName(),catchment.getId()));
catchmentRepository.save(catchment);
return new ResponseEntity<>(CatchmentContract.fromEntity(catchment), HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import org.avni.server.domain.ConceptAnswer;
import org.avni.server.domain.ConceptDataType;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.projection.CodedConceptProjection;
import org.avni.server.projection.ConceptProjection;
import org.avni.server.service.ConceptService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.util.S;
import org.avni.server.web.request.ConceptContract;
import org.avni.server.web.request.application.ConceptUsageContract;
Expand Down Expand Up @@ -132,7 +132,7 @@ public ResponseEntity deleteWeb(@PathVariable String conceptUUID) {
try {
Concept existingConcept = conceptRepository.findByUuid(conceptUUID);
existingConcept.setVoided(!existingConcept.isVoided());
existingConcept.setName(ReactAdminUtil.getVoidedName(existingConcept.getName(), existingConcept.getId()));
existingConcept.setName(EntityUtil.getVoidedName(existingConcept.getName(), existingConcept.getId()));
conceptRepository.save(existingConcept);
} catch (Exception e) {
logger.error(format("Error deleting concept: %s", conceptUUID), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.avni.server.dao.DocumentationRepository;
import org.avni.server.domain.Documentation;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.service.DocumentationService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.web.request.webapp.documentation.DocumentationContract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -58,7 +58,7 @@ public void deleteDocumentation(@PathVariable String id) {
Documentation documentation = documentationRepository.findByUuid(id);
if (documentation != null) {
documentation.setVoided(true);
documentation.setName(ReactAdminUtil.getVoidedName(documentation.getName(), documentation.getId()));
documentation.setName(EntityUtil.getVoidedName(documentation.getName(), documentation.getId()));
documentationRepository.save(documentation);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package org.avni.server.web;

import org.apache.commons.collections.ListUtils;
import org.avni.server.application.Form;
import org.avni.server.application.FormMapping;
import org.avni.server.application.FormType;
import org.avni.server.dao.EncounterTypeRepository;
import org.avni.server.dao.OperationalEncounterTypeRepository;
import org.avni.server.domain.EncounterType;
import org.avni.server.domain.OperationalEncounterType;
import org.avni.server.domain.Program;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.service.EncounterTypeService;
import org.avni.server.service.FormMappingParameterObject;
import org.avni.server.service.FormMappingService;
import org.avni.server.service.FormService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.web.contract.EncounterTypeContract;
import org.avni.server.web.contract.ProgramContract;
import org.avni.server.web.request.EncounterContract;
import org.avni.server.web.request.EntityTypeContract;
import org.avni.server.web.request.webapp.EncounterTypeContractWeb;
import org.slf4j.Logger;
Expand All @@ -29,7 +25,6 @@
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -214,9 +209,9 @@ public ResponseEntity voidEncounterType(@PathVariable("id") Long id) {
if (encounterType == null)
return ResponseEntity.notFound().build();

operationalEncounterType.setName(ReactAdminUtil.getVoidedName(operationalEncounterType.getName(), operationalEncounterType.getId()));
operationalEncounterType.setName(EntityUtil.getVoidedName(operationalEncounterType.getName(), operationalEncounterType.getId()));
operationalEncounterType.setVoided(true);
encounterType.setName(ReactAdminUtil.getVoidedName(encounterType.getName(), encounterType.getId()));
encounterType.setName(EntityUtil.getVoidedName(encounterType.getName(), encounterType.getId()));
encounterType.setVoided(true);
operationalEncounterTypeRepository.save(operationalEncounterType);
encounterTypeRepository.save(encounterType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.avni.server.dao.application.FormRepository;
import org.avni.server.domain.*;
import org.avni.server.domain.task.TaskType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.framework.security.UserContextHolder;
import org.avni.server.projection.FormWebProjection;
import org.avni.server.projection.IdentifierAssignmentProjection;
Expand All @@ -19,7 +20,6 @@
import org.avni.server.web.util.ErrorBodyBuilder;
import org.avni.server.web.validation.ValidationException;
import org.avni.server.util.BadRequestError;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.web.request.ConceptContract;
import org.avni.server.web.request.FormMappingContract;
import org.avni.server.web.request.FormatContract;
Expand Down Expand Up @@ -178,7 +178,7 @@ public ResponseEntity deleteWeb(@PathVariable String formUUID) {
formMappingService.createOrUpdateFormMapping(formMappingContract);
}
existingForm.setVoided(!existingForm.isVoided());
existingForm.setName(ReactAdminUtil.getVoidedName(existingForm.getName(), existingForm.getId()));
existingForm.setName(EntityUtil.getVoidedName(existingForm.getName(), existingForm.getId()));
accessControlService.checkPrivilege(FormType.getPrivilegeType(existingForm));
formRepository.save(existingForm);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import org.avni.server.domain.Organisation;
import org.avni.server.domain.UserGroup;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.framework.security.UserContextHolder;
import org.avni.server.service.GroupsService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.web.request.GroupContract;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -125,7 +125,7 @@ public ResponseEntity voidGroup(@PathVariable("id") Long id) {
return ResponseEntity.badRequest().body(String.format("Default group %s cannot be deleted", group.getName()));

group.setVoided(true);
group.setName((ReactAdminUtil.getVoidedName(group.getName(), group.getId())));
group.setName((EntityUtil.getVoidedName(group.getName(), group.getId())));
group.updateAudit();
groupRepository.save(group);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.avni.server.domain.OperationalProgram;
import org.avni.server.domain.Program;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.service.FormMappingParameterObject;
import org.avni.server.service.FormMappingService;
import org.avni.server.service.FormService;
Expand All @@ -27,7 +28,6 @@
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -151,8 +151,8 @@ public ResponseEntity voidProgram(@PathVariable("id") Long id) {
if (program == null)
return ResponseEntity.notFound().build();

operationalProgram.setName(ReactAdminUtil.getVoidedName(operationalProgram.getName(), operationalProgram.getId()));
program.setName(ReactAdminUtil.getVoidedName(program.getName(), program.getId()));
operationalProgram.setName(EntityUtil.getVoidedName(operationalProgram.getName(), operationalProgram.getId()));
program.setName(EntityUtil.getVoidedName(program.getName(), program.getId()));
operationalProgram.setVoided(true);
program.setVoided(true);
operationalProgramRepository.save(operationalProgram);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.avni.server.mapper.dashboard.ReportCardMapper;
import org.avni.server.service.CardService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.util.BadRequestError;
import org.avni.server.web.request.reports.ReportCardWebRequest;
import org.avni.server.web.response.reports.ReportCardWebResponse;
import org.joda.time.DateTime;
Expand All @@ -17,6 +18,7 @@
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -61,10 +63,14 @@ public ResponseEntity<ReportCardWebResponse> getById(@PathVariable Long id) {
@PostMapping(value = "/web/reportCard")
@ResponseBody
@Transactional
public ResponseEntity<ReportCardWebResponse> newCard(@RequestBody ReportCardWebRequest cardRequest) {
accessControlService.checkPrivilege(PrivilegeType.EditOfflineDashboardAndReportCard);
ReportCard card = cardService.saveCard(cardRequest);
return ResponseEntity.ok(reportCardMapper.toWebResponse(card));
public ResponseEntity<?> newCard(@RequestBody ReportCardWebRequest cardRequest) {
try {
accessControlService.checkPrivilege(PrivilegeType.EditOfflineDashboardAndReportCard);
ReportCard card = cardService.saveCard(cardRequest);
return ResponseEntity.ok(reportCardMapper.toWebResponse(card));
} catch (BadRequestError e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}

@PutMapping(value = "/web/reportCard/{id}")
Expand All @@ -85,8 +91,7 @@ public ResponseEntity<ReportCardWebResponse> editCard(@PathVariable Long id, @Re
@Transactional
public void deleteCard(@PathVariable Long id) {
accessControlService.checkPrivilege(PrivilegeType.EditOfflineDashboardAndReportCard);
Optional<ReportCard> card = cardRepository.findById(id);
card.ifPresent(cardService::deleteCard);
cardService.deleteCard(id);
}

@GetMapping(value = "/v2/card/search/lastModified")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.avni.server.domain.OrganisationConfig;
import org.avni.server.domain.SubjectType;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.service.*;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.util.ReactAdminUtil;
Expand Down Expand Up @@ -245,9 +246,9 @@ public ResponseEntity voidSubjectType(@PathVariable("id") Long id) {
if (subjectType == null)
return ResponseEntity.notFound().build();

operationalSubjectType.setName(ReactAdminUtil.getVoidedName(operationalSubjectType.getName(), operationalSubjectType.getId()));
operationalSubjectType.setName(EntityUtil.getVoidedName(operationalSubjectType.getName(), operationalSubjectType.getId()));
operationalSubjectType.setVoided(true);
subjectType.setName(ReactAdminUtil.getVoidedName(subjectType.getName(), subjectType.getId()));
subjectType.setName(EntityUtil.getVoidedName(subjectType.getName(), subjectType.getId()));
subjectType.setVoided(true);
operationalSubjectTypeRepository.save(operationalSubjectType);
subjectTypeRepository.save(subjectType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import org.avni.server.dao.VideoRepository;
import org.avni.server.domain.Video;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.domain.util.EntityUtil;
import org.avni.server.service.VideoService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.util.ReactAdminUtil;
import org.avni.server.web.request.VideoContract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
Expand Down Expand Up @@ -80,7 +79,7 @@ public ResponseEntity voidVideo(@PathVariable Long id) {
return ResponseEntity.notFound().build();
}
video.setVoided(true);
video.setTitle(ReactAdminUtil.getVoidedName(video.getTitle(), video.getId()));
video.setTitle(EntityUtil.getVoidedName(video.getTitle(), video.getId()));
videoRepository.save(video);
return ResponseEntity.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table report_card add constraint report_card_name_unique unique (name, organisation_id);

0 comments on commit d8dcdb8

Please sign in to comment.