Search in sources :

Example 11 with DashboardItem

use of org.hisp.dhis.dashboard.DashboardItem in project dhis2-core by dhis2.

the class DashboardController method postJsonItemContent.

@RequestMapping(value = "/{dashboardUid}/items/content", method = RequestMethod.POST)
public void postJsonItemContent(HttpServletResponse response, HttpServletRequest request, @PathVariable String dashboardUid, @RequestParam DashboardItemType type, @RequestParam("id") String contentUid) throws Exception {
    Dashboard dashboard = dashboardService.getDashboard(dashboardUid);
    if (dashboard == null) {
        throw new WebMessageException(WebMessageUtils.notFound("Dashboard does not exist: " + dashboardUid));
    }
    if (!aclService.canUpdate(currentUserService.getCurrentUser(), dashboard)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this dashboard.");
    }
    DashboardItem item = dashboardService.addItemContent(dashboardUid, type, contentUid);
    if (item == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Max number of dashboard items reached: " + MAX_ITEMS));
    } else {
        response.addHeader("Location", DashboardItemSchemaDescriptor.API_ENDPOINT + "/" + item.getUid());
        webMessageService.send(WebMessageUtils.created("Dashboard item created"), response, request);
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) Dashboard(org.hisp.dhis.dashboard.Dashboard) DashboardItem(org.hisp.dhis.dashboard.DashboardItem) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with DashboardItem

use of org.hisp.dhis.dashboard.DashboardItem in project dhis2-core by dhis2.

the class DefaultPushAnalysisService method generateHtmlReport.

@Override
public String generateHtmlReport(PushAnalysis pushAnalysis, User user, JobConfiguration jobId) throws IOException {
    if (jobId == null) {
        jobId = new JobConfiguration("inMemoryGenerateHtmlReport", JobType.PUSH_ANALYSIS, currentUserService.getCurrentUser().getUid(), true);
        notifier.clear(jobId);
    }
    user = user == null ? currentUserService.getCurrentUser() : user;
    log(jobId, NotificationLevel.INFO, "Generating PushAnalysis for user '" + user.getUsername() + "'.", false, null);
    // ----------------------------------------------------------------------
    // Pre-process the dashboardItem and store them as Strings
    // ----------------------------------------------------------------------
    HashMap<String, String> itemHtml = new HashMap<>();
    HashMap<String, String> itemLink = new HashMap<>();
    for (DashboardItem item : pushAnalysis.getDashboard().getItems()) {
        // In normal conditions all DashboardItem has a type.
        if (item.getType() != null) {
            itemHtml.put(item.getUid(), getItemHtml(item, user, jobId));
            itemLink.put(item.getUid(), getItemLink(item));
        }
    }
    DateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy");
    itemHtml.put("date", dateFormat.format(Calendar.getInstance().getTime()));
    itemHtml.put("instanceBaseUrl", dhisConfigurationProvider.getServerBaseUrl());
    itemHtml.put("instanceName", systemSettingManager.getStringSetting(SettingKey.APPLICATION_TITLE));
    // ----------------------------------------------------------------------
    // Set up template context, including pre-processed dashboard items
    // ----------------------------------------------------------------------
    final VelocityContext context = new VelocityContext();
    context.put("pushAnalysis", pushAnalysis);
    context.put("itemHtml", itemHtml);
    context.put("itemLink", itemLink);
    context.put("encoder", encoder);
    // ----------------------------------------------------------------------
    // Render template and return result after removing newline characters
    // ----------------------------------------------------------------------
    StringWriter stringWriter = new StringWriter();
    new VelocityManager().getEngine().getTemplate("push-analysis-main-html.vm").merge(context, stringWriter);
    log(jobId, NotificationLevel.INFO, "Finished generating PushAnalysis for user '" + user.getUsername() + "'.", false, null);
    return stringWriter.toString().replaceAll("\\R", "");
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) VelocityManager(org.hisp.dhis.system.velocity.VelocityManager) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DashboardItem(org.hisp.dhis.dashboard.DashboardItem) SimpleDateFormat(java.text.SimpleDateFormat) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration)

Example 13 with DashboardItem

use of org.hisp.dhis.dashboard.DashboardItem in project dhis2-core by dhis2.

the class DashboardCascadeSharingTest method testUserGroup.

@Test
void testUserGroup() {
    Map mapA = createMap("A");
    mapA.setSharing(defaultSharing());
    objectManager.save(mapA, false);
    Map mapB = createMap("A");
    mapB.setSharing(defaultSharing());
    objectManager.save(mapB, false);
    DashboardItem itemA = createDashboardItem("A");
    itemA.setMap(mapA);
    DashboardItem itemB = createDashboardItem("B");
    itemB.setMap(mapB);
    Dashboard dashboard = createDashboard("A", sharingUserGroupA);
    dashboard.getItems().add(itemB);
    dashboard.getItems().add(itemA);
    objectManager.save(dashboard, false);
    CascadeSharingReport report = cascadeSharingService.cascadeSharing(dashboard, CascadeSharingParameters.builder().build());
    assertEquals(0, report.getErrorReports().size());
    assertEquals(1, report.getUpdateObjects().size());
    assertTrue(aclService.canRead(userA, mapA));
    assertTrue(aclService.canRead(userA, mapB));
}
Also used : Dashboard(org.hisp.dhis.dashboard.Dashboard) DashboardItem(org.hisp.dhis.dashboard.DashboardItem) Map(org.hisp.dhis.mapping.Map) Test(org.junit.jupiter.api.Test)

Example 14 with DashboardItem

use of org.hisp.dhis.dashboard.DashboardItem in project dhis2-core by dhis2.

the class DashboardCascadeSharingTest method testAtomicTrue.

@Test
void testAtomicTrue() {
    Map mapA = createMap("A");
    mapA.setSharing(sharingReadWriteForUserB);
    objectManager.save(mapA, false);
    assertFalse(aclService.canRead(userA, mapA));
    Map mapB = createMap("A");
    mapB.setSharing(defaultSharing());
    objectManager.save(mapB, false);
    DashboardItem itemB = createDashboardItem("B");
    itemB.setMap(mapB);
    DashboardItem itemA = createDashboardItem("A");
    itemA.setMap(mapA);
    Dashboard dashboard = createDashboard("A", sharingReadForUserA);
    dashboard.getItems().add(itemA);
    dashboard.getItems().add(itemB);
    objectManager.save(dashboard, false);
    assertFalse(aclService.canRead(userA, mapA));
    CascadeSharingReport report = cascadeSharingService.cascadeSharing(dashboard, CascadeSharingParameters.builder().atomic(true).user(userB).build());
    assertEquals(1, report.getErrorReports().size());
    assertEquals(0, report.getUpdateObjects().size());
    assertFalse(aclService.canRead(userA, mapA));
    assertFalse(aclService.canRead(userA, mapB));
}
Also used : Dashboard(org.hisp.dhis.dashboard.Dashboard) DashboardItem(org.hisp.dhis.dashboard.DashboardItem) Map(org.hisp.dhis.mapping.Map) Test(org.junit.jupiter.api.Test)

Example 15 with DashboardItem

use of org.hisp.dhis.dashboard.DashboardItem in project dhis2-core by dhis2.

the class DashboardItemController method putDashboardItemShape.

@PutMapping("/{uid}/shape/{shape}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void putDashboardItemShape(@PathVariable String uid, @PathVariable DashboardItemShape shape, @CurrentUser User currentUser, HttpServletRequest request, HttpServletResponse response) throws Exception {
    DashboardItem item = dashboardService.getDashboardItem(uid);
    if (item == null) {
        throw new WebMessageException(notFound("Dashboard item does not exist: " + uid));
    }
    Dashboard dashboard = dashboardService.getDashboardFromDashboardItem(item);
    if (!aclService.canUpdate(currentUser, dashboard)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this dashboard.");
    }
    item.setShape(shape);
    dashboardService.updateDashboardItem(item);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) Dashboard(org.hisp.dhis.dashboard.Dashboard) DashboardItem(org.hisp.dhis.dashboard.DashboardItem) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

DashboardItem (org.hisp.dhis.dashboard.DashboardItem)16 Dashboard (org.hisp.dhis.dashboard.Dashboard)11 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)6 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 Map (org.hisp.dhis.mapping.Map)4 Test (org.junit.jupiter.api.Test)3 StringWriter (java.io.StringWriter)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 VelocityContext (org.apache.velocity.VelocityContext)2 VelocityManager (org.hisp.dhis.system.velocity.VelocityManager)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 EventReport (org.hisp.dhis.eventreport.EventReport)1 EventVisualization (org.hisp.dhis.eventvisualization.EventVisualization)1 Report (org.hisp.dhis.report.Report)1 JobConfiguration (org.hisp.dhis.scheduling.JobConfiguration)1 TaskId (org.hisp.dhis.scheduling.TaskId)1 User (org.hisp.dhis.user.User)1