@Override
  public void mergeDashboardItem(DashboardItem item) {
    if (item.getChart() != null) {
      item.setChart(objectManager.get(Chart.class, item.getChart().getUid()));
    }

    if (item.getEventChart() != null) {
      item.setEventChart(objectManager.get(EventChart.class, item.getEventChart().getUid()));
    }

    if (item.getMap() != null) {
      item.setMap(objectManager.get(Map.class, item.getMap().getUid()));
    }

    if (item.getReportTable() != null) {
      item.setReportTable(objectManager.get(ReportTable.class, item.getReportTable().getUid()));
    }

    if (item.getUsers() != null) {
      item.setUsers(objectManager.getByUid(User.class, getUids(item.getUsers())));
    }

    if (item.getReportTables() != null) {
      item.setReportTables(
          objectManager.getByUid(ReportTable.class, getUids(item.getReportTables())));
    }

    if (item.getReports() != null) {
      item.setReports(objectManager.getByUid(Report.class, getUids(item.getReports())));
    }

    if (item.getResources() != null) {
      item.setResources(objectManager.getByUid(Document.class, getUids(item.getResources())));
    }
  }
  @Test
  public void testPutFacilityUuid() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    Facility facility = new OrganisationUnitToFacilityConverter().convert(organisationUnit);
    facility.setName("FacilityB");
    facility.setActive(false);

    MockHttpSession session = getSession("ALL");

    mvc.perform(
            put("/fred/v1/facilities/" + organisationUnit.getUuid())
                .content(objectMapper.writeValueAsString(facility))
                .session(session)
                .contentType(MediaType.APPLICATION_JSON))
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath("$.uuid", Matchers.notNullValue()))
        .andExpect(jsonPath("$.name").value("FacilityB"))
        .andExpect(jsonPath("$.active").value(false))
        .andExpect(jsonPath("$.createdAt", Matchers.notNullValue()))
        .andExpect(jsonPath("$.updatedAt", Matchers.notNullValue()))
        .andExpect(header().string("ETag", Matchers.notNullValue()))
        .andExpect(status().isOk());
  }
  @Override
  public boolean addItemContent(String dashboardUid, String type, String contentUid) {
    Dashboard dashboard = getDashboard(dashboardUid);

    if (dashboard == null) {
      return false;
    }

    if (TYPE_CHART.equals(type)) {
      DashboardItem item = new DashboardItem();
      item.setChart(objectManager.get(Chart.class, contentUid));
      dashboard.getItems().add(0, item);
    } else if (TYPE_EVENT_CHART.equals(type)) {
      DashboardItem item = new DashboardItem();
      item.setEventChart(objectManager.get(EventChart.class, contentUid));
      dashboard.getItems().add(0, item);
    } else if (TYPE_MAP.equals(type)) {
      DashboardItem item = new DashboardItem();
      item.setMap(objectManager.get(Map.class, contentUid));
      dashboard.getItems().add(0, item);
    } else if (TYPE_REPORT_TABLE.equals(type)) {
      DashboardItem item = new DashboardItem();
      item.setReportTable(objectManager.get(ReportTable.class, contentUid));
      dashboard.getItems().add(0, item);
    } else if (TYPE_MESSAGES.equals(type)) {
      DashboardItem item = new DashboardItem();
      item.setMessages(true);
      dashboard.getItems().add(0, item);
    } else // Link item
    {
      DashboardItem availableItem = dashboard.getAvailableItemByType(type);

      DashboardItem item = availableItem == null ? new DashboardItem() : availableItem;

      if (TYPE_USERS.equals(type)) {
        item.getUsers().add(objectManager.get(User.class, contentUid));
      } else if (TYPE_REPORT_TABLES.equals(type)) {
        item.getReportTables().add(objectManager.get(ReportTable.class, contentUid));
      } else if (TYPE_REPORTS.equals(type)) {
        item.getReports().add(objectManager.get(Report.class, contentUid));
      } else if (TYPE_RESOURCES.equals(type)) {
        item.getResources().add(objectManager.get(Document.class, contentUid));
      }

      if (availableItem == null) {
        dashboard.getItems().add(0, item);
      }
    }

    if (dashboard.getItemCount() > Dashboard.MAX_ITEMS) {
      return false;
    }

    updateDashboard(dashboard);

    return true;
  }
  @Test
  public void testDeleteFacilityUuid() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    MockHttpSession session = getSession("ALL");

    mvc.perform(delete("/fred/v1/facilities/" + organisationUnit.getUuid()).session(session))
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk());
  }
  @Test
  public void testGetFacilitiesWithContent() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    MockHttpSession session = getSession("ALL");

    mvc.perform(get("/fred/v1/facilities").session(session).accept(MediaType.APPLICATION_JSON))
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath("$.facilities").isArray())
        .andExpect(jsonPath("$.facilities[0].name").value("OrgUnitA"))
        .andExpect(status().isOk());
  }
  @Test
  public void testPutFacilityWithoutRequiredPropertiesUuid() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    MockHttpSession session = getSession("ALL");

    mvc.perform(
            put("/fred/v1/facilities/" + organisationUnit.getUuid())
                .content("{}")
                .session(session)
                .contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isUnprocessableEntity());
  }
  @Test
  public void testPutInvalidJsonUuid() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    MockHttpSession session = getSession("ALL");

    mvc.perform(
            put("/fred/v1/facilities/" + organisationUnit.getUuid())
                .content("INVALID JSON")
                .session(session)
                .contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isBadRequest());
  }
  @Test
  public void testGetFacilityVerifyPresenceOfETag() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    MockHttpSession session = getSession("ALL");

    mvc.perform(
            get("/fred/v1/facilities/" + organisationUnit.getUid())
                .session(session)
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(header().string("ETag", Matchers.notNullValue()))
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk());
  }
  // TODO: this should fail, need to figure out which code to return
  @Test
  public void testPutChangeUuidShouldFail() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    organisationUnit.setUuid("ddccbbaa-bbaa-bbaa-bbaa-ffeeddccbbaa");
    manager.save(organisationUnit);

    Facility facility = new OrganisationUnitToFacilityConverter().convert(organisationUnit);
    facility.setUuid("aabbccdd-aabb-aabb-aabb-aabbccddeeff");

    MockHttpSession session = getSession("ALL");

    mvc.perform(
            put("/fred/v1/facilities/" + organisationUnit.getUuid())
                .content(objectMapper.writeValueAsString(facility))
                .session(session)
                .contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk());
  }
  @Test
  public void testPutInvalidUuidShouldFail() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    Facility facility = new OrganisationUnitToFacilityConverter().convert(organisationUnit);
    facility.setUuid("DUMMY_UUID");

    MockHttpSession session = getSession("ALL");

    mvc.perform(
            put("/fred/v1/facilities/" + organisationUnit.getUuid())
                .content(objectMapper.writeValueAsString(facility))
                .session(session)
                .contentType(MediaType.APPLICATION_JSON))
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
        .andExpect(status().isPreconditionFailed());
  }
  @Test
  public void testGetFacilityUuid() throws Exception {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    manager.save(organisationUnit);

    MockHttpSession session = getSession("ALL");

    mvc.perform(
            get("/fred/v1/facilities/" + organisationUnit.getUuid())
                .session(session)
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath("$.uuid", Matchers.notNullValue()))
        .andExpect(jsonPath("$.name").value("OrgUnitA"))
        .andExpect(jsonPath("$.active").value(true))
        .andExpect(jsonPath("$.createdAt", Matchers.notNullValue()))
        .andExpect(jsonPath("$.updatedAt", Matchers.notNullValue()))
        .andExpect(header().string("ETag", Matchers.notNullValue()))
        .andExpect(status().isOk());
  }
  @Override
  public DashboardSearchResult search(String query, Set<String> maxTypes) {
    DashboardSearchResult result = new DashboardSearchResult();

    result.setUsers(
        objectManager.getBetweenLikeName(User.class, query, 0, getMax(TYPE_USERS, maxTypes)));
    result.setCharts(
        objectManager.getBetweenLikeName(Chart.class, query, 0, getMax(TYPE_CHART, maxTypes)));
    result.setEventCharts(
        objectManager.getBetweenLikeName(
            EventChart.class, query, 0, getMax(TYPE_EVENT_CHART, maxTypes)));
    result.setMaps(
        objectManager.getBetweenLikeName(Map.class, query, 0, getMax(TYPE_MAP, maxTypes)));
    result.setReportTables(
        objectManager.getBetweenLikeName(
            ReportTable.class, query, 0, getMax(TYPE_REPORT_TABLE, maxTypes)));
    result.setReports(
        objectManager.getBetweenLikeName(Report.class, query, 0, getMax(TYPE_REPORTS, maxTypes)));
    result.setResources(
        objectManager.getBetweenLikeName(
            Document.class, query, 0, getMax(TYPE_RESOURCES, maxTypes)));

    return result;
  }