Skip to content

Commit

Permalink
avniproject/avni-client#1328 - use uuid instead of id for resolution …
Browse files Browse the repository at this point in the history
…of entities
  • Loading branch information
petmongrels committed Mar 13, 2024
1 parent 0156e9a commit 9ee6dbe
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public static <T extends CHSEntity> T newOrExistingEntity(CHSRepository<T> chsRe
}
return t;
}

public static <T extends CHSEntity> T newOrExistingEntity(CHSRepository<T> chsRepository, String uuid, T chsEntity) {
return EntityHelper.newOrExistingEntity(chsRepository, uuid, null, chsEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.avni.server.service.accessControl.GroupPrivilegeService;
import org.avni.server.service.application.MenuItemService;
import org.avni.server.util.ObjectMapperSingleton;
import org.avni.server.web.contract.GroupDashboardBundleContract;
import org.avni.server.web.request.*;
import org.avni.server.web.request.application.ChecklistDetailRequest;
import org.avni.server.web.request.application.FormContract;
Expand Down Expand Up @@ -360,8 +361,8 @@ private void deployFile(String fileName, String fileData, List<? extends BundleF
groupPrivilegeService.savePrivileges(groupPrivilegeContracts, organisation);
break;
case "groupDashboards.json":
GroupDashboardContract[] groupDashboardContracts = convertString(fileData, GroupDashboardContract[].class);
groupDashboardService.save(Arrays.asList(groupDashboardContracts));
GroupDashboardBundleContract[] contracts = convertString(fileData, GroupDashboardBundleContract[].class);
groupDashboardService.saveFromBundle(Arrays.asList(contracts));
break;
case "video.json":
VideoContract[] videoContracts = convertString(fileData, VideoContract[].class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.avni.server.service;

import org.avni.server.common.EntityHelper;
import org.avni.server.dao.DashboardRepository;
import org.avni.server.dao.GroupDashboardRepository;
import org.avni.server.dao.GroupRepository;
Expand All @@ -8,11 +9,11 @@
import org.avni.server.domain.GroupDashboard;
import org.avni.server.domain.ValidationException;
import org.avni.server.framework.security.UserContextHolder;
import org.avni.server.web.contract.GroupDashboardBundleContract;
import org.avni.server.web.request.GroupDashboardContract;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -33,25 +34,34 @@ public GroupDashboardService(GroupDashboardRepository groupDashboardRepository,
public List<GroupDashboard> save(List<GroupDashboardContract> request) throws ValidationException {
List<GroupDashboard> groupDashboards = new ArrayList<>();
for (GroupDashboardContract contract : request) {
Dashboard dashboard = dashboardRepository.findOne(contract.getDashboardId());
GroupDashboard groupDashboard = EntityHelper.newOrExistingEntity(groupDashboardRepository, contract.getUuid(), contract.getId(), new GroupDashboard());
Group group = groupRepository.findOne(contract.getGroupId());
Dashboard dashboard = dashboardRepository.findOne(contract.getDashboardId());
if (dashboard == null || group == null) {
throw new ValidationException(String.format("Invalid dashboard id %d or group id %d", contract.getDashboardId(), contract.getGroupId()));
}

GroupDashboard groupDashboard = new GroupDashboard();
groupDashboard.setDashboard(dashboard);
groupDashboard.setGroup(group);
if (StringUtils.isEmpty(contract.getUuid()))
groupDashboard.assignUUID();
else
groupDashboard.setUuid(contract.getUuid());
groupDashboard.setOrganisationId(UserContextHolder.getUserContext().getOrganisationId());
groupDashboards.add(groupDashboard);
}
return groupDashboards;
}

public void saveFromBundle(List<GroupDashboardBundleContract> request) {
List<GroupDashboard> groupDashboards = new ArrayList<>();
for (GroupDashboardBundleContract contract : request) {
GroupDashboard groupDashboard = EntityHelper.newOrExistingEntity(groupDashboardRepository, contract.getUuid(), null, new GroupDashboard());
Group group = groupRepository.findByUuid(contract.getGroupUUID());
Dashboard dashboard = dashboardRepository.findByUuid(contract.getDashboardUUID());
groupDashboard.setDashboard(dashboard);
groupDashboard.setGroup(group);
groupDashboard.setOrganisationId(UserContextHolder.getUserContext().getOrganisationId());
groupDashboards.add(groupDashboard);
}
groupDashboardRepository.saveAll(groupDashboards);
}

private GroupDashboard buildAndSave(GroupDashboardContract contract, GroupDashboard groupDashboard) {
groupDashboard.setDashboard(dashboardRepository.findOne(contract.getDashboardId()));
groupDashboard.setGroup(groupRepository.findOne(contract.getGroupId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

@Service
public class GroupsService implements NonScopeAwareService {

private final GroupRepository groupRepository;

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.avni.server.util.ObjectMapperSingleton;
import org.avni.server.util.S;
import org.avni.server.util.S3File;
import org.avni.server.web.contract.GroupDashboardBundleContract;
import org.avni.server.web.request.*;
import org.avni.server.web.request.application.ChecklistDetailRequest;
import org.avni.server.web.request.application.FormContract;
Expand Down Expand Up @@ -697,8 +698,8 @@ public void deleteMediaContent(boolean deleteMetadata) {
}

public void addGroupDashboardJson(ZipOutputStream zos) throws IOException {
List<GroupDashboardContract> groupDashboards = groupDashboardRepository.findAll().stream()
.map(GroupDashboardContract::fromEntityForExternal).collect(Collectors.toList());
List<GroupDashboardBundleContract> groupDashboards = groupDashboardRepository.findAll().stream()
.map(GroupDashboardBundleContract::fromEntity).collect(Collectors.toList());
if (!groupDashboards.isEmpty()) {
addFileToZip(zos, "groupDashboards.json", groupDashboards);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.avni.server.web;

import org.avni.server.dao.DashboardRepository;
import org.avni.server.dao.GroupDashboardRepository;
import org.avni.server.dao.GroupRepository;
import org.avni.server.domain.GroupDashboard;
import org.avni.server.domain.ValidationException;
import org.avni.server.domain.accessControl.PrivilegeType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.avni.server.web.contract;

public class BaseBundleContract {
private String uuid;
private boolean voided;

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

public boolean isVoided() {
return voided;
}

public void setVoided(boolean voided) {
this.voided = voided;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.avni.server.web.contract;

import org.avni.server.domain.GroupDashboard;

public class GroupDashboardBundleContract extends BaseBundleContract {
private boolean isPrimaryDashboard;
private boolean isSecondaryDashboard;
private String dashboardUUID;
private String groupUUID;
private String dashboardName;
private String dashboardDescription;

public static GroupDashboardBundleContract fromEntity(GroupDashboard groupDashboard) {
GroupDashboardBundleContract contract = new GroupDashboardBundleContract();
contract.setUuid(groupDashboard.getUuid());
contract.setVoided(groupDashboard.isVoided());
contract.isPrimaryDashboard = groupDashboard.isPrimaryDashboard();
contract.isSecondaryDashboard = groupDashboard.isSecondaryDashboard();
contract.groupUUID = groupDashboard.getGroup().getUuid();
contract.dashboardUUID = groupDashboard.getDashboard().getUuid();
contract.dashboardName = groupDashboard.getDashboard().getName();
contract.dashboardDescription = groupDashboard.getDashboard().getDescription();
return contract;
}

public boolean isPrimaryDashboard() {
return isPrimaryDashboard;
}

public void setPrimaryDashboard(boolean primaryDashboard) {
isPrimaryDashboard = primaryDashboard;
}

public boolean isSecondaryDashboard() {
return isSecondaryDashboard;
}

public void setSecondaryDashboard(boolean secondaryDashboard) {
isSecondaryDashboard = secondaryDashboard;
}

public String getDashboardUUID() {
return dashboardUUID;
}

public void setDashboardUUID(String dashboardUUID) {
this.dashboardUUID = dashboardUUID;
}

public String getGroupUUID() {
return groupUUID;
}

public void setGroupUUID(String groupUUID) {
this.groupUUID = groupUUID;
}

public String getDashboardName() {
return dashboardName;
}

public void setDashboardName(String dashboardName) {
this.dashboardName = dashboardName;
}

public String getDashboardDescription() {
return dashboardDescription;
}

public void setDashboardDescription(String dashboardDescription) {
this.dashboardDescription = dashboardDescription;
}
}

0 comments on commit 9ee6dbe

Please sign in to comment.