diff --git a/avni-server-api/src/main/java/org/avni/server/dao/StandardReportCardTypeRepository.java b/avni-server-api/src/main/java/org/avni/server/dao/StandardReportCardTypeRepository.java index 979f9297e..ac003030e 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/StandardReportCardTypeRepository.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/StandardReportCardTypeRepository.java @@ -26,4 +26,6 @@ Page findByLastModifiedDateTimeIsBetweenOrderByLastModif Pageable pageable); boolean existsByLastModifiedDateTimeGreaterThan(DateTime lastModifiedDateTime); + + List findAllByNameIn(List defaultDashboardStandardCardTypeNames); } diff --git a/avni-server-api/src/main/java/org/avni/server/service/CardService.java b/avni-server-api/src/main/java/org/avni/server/service/CardService.java index ada409565..ecd92efb5 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/CardService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/CardService.java @@ -14,7 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.List; +import java.util.*; @Service public class CardService implements NonScopeAwareService { @@ -109,12 +109,16 @@ private void buildStandardReportCardInputs(StandardReportCardType type, List createDefaultDashboardCards(Organisation organisation) { + List defaultDashboardStandardCardTypeNames = Arrays.asList("Scheduled visits", "Overdue visits", "Total", "Recent registrations", "Recent enrolments", "Recent visits", "Due checklist"); + List standardReportCardTypes = standardReportCardTypeRepository.findAllByNameIn(defaultDashboardStandardCardTypeNames); + Map savedCards = new HashMap<>(); + standardReportCardTypes.forEach(standardReportCardType -> { + ReportCard reportCard = new ReportCard(); + reportCard.setUuid(UUID.randomUUID().toString()); + reportCard.setStandardReportCardType(standardReportCardType); + reportCard.setOrganisationId(organisation.getId()); + reportCard.setName(standardReportCardType.getName()); + reportCard.setColour("#ffffff"); + reportCard.setStandardReportCardInputPrograms(Collections.emptyList()); + reportCard.setStandardReportCardInputEncounterTypes(Collections.emptyList()); + reportCard.setStandardReportCardInputSubjectTypes(Collections.emptyList()); + if (isRecentStandardReportCard(standardReportCardType.getName())) { + reportCard.setStandardReportCardInputRecentDuration(new ValueUnit("1", "days")); + } + if (standardReportCardType.getName().equals("Overdue visits")) { + reportCard.setColour("#d32f2f"); + } + if (standardReportCardType.getName().equals("Scheduled visits")) { + reportCard.setColour("#388e3c"); + } + savedCards.put(reportCard.getName(), cardRepository.save(reportCard)); + }); + return savedCards; + } } diff --git a/avni-server-api/src/main/java/org/avni/server/service/DashboardService.java b/avni-server-api/src/main/java/org/avni/server/service/DashboardService.java index f7a075ca9..80d633c0c 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/DashboardService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/DashboardService.java @@ -9,13 +9,15 @@ import org.avni.server.web.contract.reports.DashboardBundleContract; import org.avni.server.web.contract.reports.DashboardSectionBundleContract; import org.avni.server.web.contract.reports.DashboardSectionCardMappingBundleContract; -import org.avni.server.web.request.*; +import org.avni.server.web.request.DashboardFilterRequest; +import org.avni.server.web.request.DashboardFilterResponse; +import org.avni.server.web.request.DashboardWebRequest; import org.avni.server.web.request.reports.DashboardSectionCardMappingRequest; import org.avni.server.web.request.reports.DashboardSectionWebRequest; import org.avni.server.web.response.reports.DashboardFilterConfigResponse; +import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.joda.time.DateTime; import java.util.*; @@ -26,16 +28,18 @@ public class DashboardService implements NonScopeAwareService { private final DashboardSectionRepository dashboardSectionRepository; private final DashboardSectionCardMappingRepository dashboardSectionCardMappingRepository; private final DashboardFilterRepository dashboardFilterRepository; + private final CardService cardService; @Autowired public DashboardService(DashboardRepository dashboardRepository, CardRepository cardRepository, - DashboardSectionRepository dashboardSectionRepository, DashboardSectionCardMappingRepository dashboardSectionCardMappingRepository, DashboardFilterRepository dashboardFilterRepository) { + DashboardSectionRepository dashboardSectionRepository, DashboardSectionCardMappingRepository dashboardSectionCardMappingRepository, DashboardFilterRepository dashboardFilterRepository, CardService cardService) { this.dashboardRepository = dashboardRepository; this.cardRepository = cardRepository; this.dashboardSectionRepository = dashboardSectionRepository; this.dashboardSectionCardMappingRepository = dashboardSectionCardMappingRepository; this.dashboardFilterRepository = dashboardFilterRepository; + this.cardService = cardService; } public Dashboard saveDashboard(DashboardWebRequest dashboardRequest) { @@ -209,4 +213,79 @@ public void saveDashboards(DashboardBundleContract[] dashboardContracts) { } } } + + public Dashboard createDefaultDashboard(Organisation organisation) { + Map defaultDashboardCards = cardService.createDefaultDashboardCards(organisation); + Dashboard defaultDashboard = createDashboard(organisation, "Default Dashboard"); + + DashboardSection visitDetailsSection = createDashboardSection(organisation, "Visit Details", 1.0); + DashboardSectionCardMapping scheduledVisitsToVisitDetailsMapping = createDashboardSectionCardMapping(organisation, defaultDashboardCards.get("Scheduled visits"), visitDetailsSection, 1.0); + visitDetailsSection.addDashboardSectionCardMapping(scheduledVisitsToVisitDetailsMapping); + DashboardSectionCardMapping overdueVisitsToVisitDetailsMapping = createDashboardSectionCardMapping(organisation, defaultDashboardCards.get("Overdue visits"), visitDetailsSection, 2.0); + visitDetailsSection.addDashboardSectionCardMapping(overdueVisitsToVisitDetailsMapping); + defaultDashboard.addSection(visitDetailsSection); + + DashboardSection recentStatisticsSection = createDashboardSection(organisation, "Recent Statistics", 2.0); + DashboardSectionCardMapping recentRegistrationsToStatisticsMapping = createDashboardSectionCardMapping(organisation, defaultDashboardCards.get("Recent registrations"), recentStatisticsSection, 1.0); + recentStatisticsSection.addDashboardSectionCardMapping(recentRegistrationsToStatisticsMapping); + DashboardSectionCardMapping recentEnrolmentsToStatisticsMapping = createDashboardSectionCardMapping(organisation, defaultDashboardCards.get("Recent enrolments"), recentStatisticsSection, 2.0); + recentStatisticsSection.addDashboardSectionCardMapping(recentEnrolmentsToStatisticsMapping); + DashboardSectionCardMapping recentVisitsToStatisticsMapping = createDashboardSectionCardMapping(organisation, defaultDashboardCards.get("Recent visits"), recentStatisticsSection, 3.0); + recentStatisticsSection.addDashboardSectionCardMapping(recentVisitsToStatisticsMapping); + defaultDashboard.addSection(recentStatisticsSection); + + DashboardSection registrationOverviewSection = createDashboardSection(organisation, "Registration Overview", 3.0); + DashboardSectionCardMapping totalToRegistrationOverviewMapping = createDashboardSectionCardMapping(organisation, defaultDashboardCards.get("Total"), registrationOverviewSection, 1.0); + registrationOverviewSection.addDashboardSectionCardMapping(totalToRegistrationOverviewMapping); + defaultDashboard.addSection(registrationOverviewSection); + + DashboardFilter subjectTypeFilter = createDashboardFilter(organisation, "Subject Type", new JsonObject() + .with(DashboardFilter.DashboardFilterConfig.TypeFieldName, String.valueOf(DashboardFilter.FilterType.SubjectType))); + defaultDashboard.addUpdateFilter(subjectTypeFilter); + + DashboardFilter asOnDateFilter = createDashboardFilter(organisation, "As On Date", new JsonObject() + .with(DashboardFilter.DashboardFilterConfig.TypeFieldName, String.valueOf(DashboardFilter.FilterType.AsOnDate))); + defaultDashboard.addUpdateFilter(asOnDateFilter); + + return dashboardRepository.save(defaultDashboard); + } + + private OrganisationAwareEntity setDefaults(OrganisationAwareEntity entity, Organisation organisation) { + entity.assignUUID(); + entity.setOrganisationId(organisation.getId()); + return entity; + } + + private Dashboard createDashboard(Organisation organisation, String name) { + Dashboard dashboard = new Dashboard(); + setDefaults(dashboard, organisation); + dashboard.setName(name); + return dashboard; + } + + private DashboardSection createDashboardSection(Organisation organisation, String name, Double displayOrder) { + DashboardSection dashboardSection = new DashboardSection(); + setDefaults(dashboardSection, organisation); + dashboardSection.setName(name); + dashboardSection.setViewType(DashboardSection.ViewType.Default); + dashboardSection.setDisplayOrder(displayOrder); + return dashboardSection; + } + + private DashboardSectionCardMapping createDashboardSectionCardMapping(Organisation organisation, ReportCard reportCard, DashboardSection dashboardSection, Double displayOrder) { + DashboardSectionCardMapping dashboardSectionCardMapping = new DashboardSectionCardMapping(); + setDefaults(dashboardSectionCardMapping, organisation); + dashboardSectionCardMapping.setCard(reportCard); + dashboardSectionCardMapping.setDashboardSection(dashboardSection); + dashboardSectionCardMapping.setDisplayOrder(displayOrder); + return dashboardSectionCardMapping; + } + + private DashboardFilter createDashboardFilter(Organisation organisation, String name, JsonObject filterConfig) { + DashboardFilter dashboardFilter = new DashboardFilter(); + setDefaults(dashboardFilter, organisation); + dashboardFilter.setName(name); + dashboardFilter.setFilterConfig(filterConfig); + return dashboardFilter; + } } diff --git a/avni-server-api/src/main/java/org/avni/server/service/GroupDashboardService.java b/avni-server-api/src/main/java/org/avni/server/service/GroupDashboardService.java index b699cd2f0..1636cb710 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/GroupDashboardService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/GroupDashboardService.java @@ -4,10 +4,7 @@ import org.avni.server.dao.DashboardRepository; import org.avni.server.dao.GroupDashboardRepository; import org.avni.server.dao.GroupRepository; -import org.avni.server.domain.Dashboard; -import org.avni.server.domain.Group; -import org.avni.server.domain.GroupDashboard; -import org.avni.server.domain.ValidationException; +import org.avni.server.domain.*; import org.avni.server.framework.security.UserContextHolder; import org.avni.server.web.contract.GroupDashboardBundleContract; import org.avni.server.web.request.GroupDashboardContract; @@ -18,6 +15,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.UUID; @Service public class GroupDashboardService implements NonScopeAwareService { @@ -107,6 +105,16 @@ public void delete(GroupDashboard groupDashboard) { groupDashboardRepository.save(groupDashboard); } + public void createDefaultGroupDashboardForOrg(Organisation organisation, Group group, Dashboard dashboard) { + GroupDashboard groupDashboard = new GroupDashboard(); + groupDashboard.setOrganisationId(organisation.getId()); + groupDashboard.setUuid(UUID.randomUUID().toString()); + groupDashboard.setGroup(group); + groupDashboard.setDashboard(dashboard); + groupDashboard.setPrimaryDashboard(true); + groupDashboardRepository.save(groupDashboard); + } + @Override public boolean isNonScopeEntityChanged(DateTime lastModifiedDateTime) { return groupDashboardRepository.existsByLastModifiedDateTimeGreaterThan(lastModifiedDateTime); diff --git a/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java b/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java index 3b55c341a..36faf008d 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java @@ -107,6 +107,7 @@ public class OrganisationService { private final DashboardSectionCardMappingRepository dashboardSectionCardMappingRepository; private final DashboardSectionRepository dashboardSectionRepository; private final GroupDashboardRepository groupDashboardRepository; + private final DashboardService dashboardService; private final Msg91ConfigRepository msg91ConfigRepository; private final S3Service s3Service; @@ -146,6 +147,7 @@ public class OrganisationService { private final UserSubjectRepository userSubjectRepository; private final Logger logger; private final DashboardMapper dashboardMapper; + private final GroupDashboardService groupDashboardService; @Autowired public OrganisationService(FormRepository formRepository, @@ -225,7 +227,7 @@ public OrganisationService(FormRepository formRepository, ReportCardMapper reportCardMapper, DashboardMapper dashboardMapper, UserSubjectRepository userSubjectRepository, - DashboardFilterRepository dashboardFilterRepository) { + DashboardFilterRepository dashboardFilterRepository, GroupDashboardService groupDashboardService) { this.formRepository = formRepository; this.addressLevelTypeRepository = addressLevelTypeRepository; this.locationRepository = locationRepository; @@ -304,7 +306,9 @@ public OrganisationService(FormRepository formRepository, this.reportCardMapper = reportCardMapper; this.dashboardMapper = dashboardMapper; this.userSubjectRepository = userSubjectRepository; + this.dashboardService = dashboardService; logger = LoggerFactory.getLogger(this.getClass()); + this.groupDashboardService = groupDashboardService; } public void addOrganisationConfig(Long orgId, ZipOutputStream zos) throws IOException { @@ -774,14 +778,14 @@ private void createGender(String genderName, Organisation org) { genderRepository.save(gender); } - private void addDefaultGroup(Long organisationId, String groupType) { + private Group addDefaultGroup(Long organisationId, String groupType) { Group group = new Group(); group.setName(groupType); group.setOrganisationId(organisationId); group.setUuid(UUID.randomUUID().toString()); group.setHasAllPrivileges(group.isAdministrator()); group.setVersion(0); - groupRepository.save(group); + return groupRepository.save(group); } private void createDefaultGenders(Organisation org) { @@ -792,9 +796,11 @@ private void createDefaultGenders(Organisation org) { public void setupBaseOrganisationData(Organisation organisation) { createDefaultGenders(organisation); - addDefaultGroup(organisation.getId(), Group.Everyone); + Group everyoneGroup = addDefaultGroup(organisation.getId(), Group.Everyone); addDefaultGroup(organisation.getId(), Group.Administrators); organisationConfigService.createDefaultOrganisationConfig(organisation); + Dashboard defaultDashboard = dashboardService.createDefaultDashboard(organisation); + groupDashboardService.createDefaultGroupDashboardForOrg(organisation, everyoneGroup, defaultDashboard); } public Organisation getCurrentOrganisation() {