Skip to content

Commit

Permalink
fix: Implement create note endpoints in tracker [DHIS2-17579]
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Dec 11, 2024
1 parent 99c00a5 commit 14ddac9
Show file tree
Hide file tree
Showing 14 changed files with 755 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
<id name="id" column="noteid">
<generator class="native" />
</id>
&identifiableProperties;

<property name="uid" column="uid" unique="true" not-null="true" />

<property name="created" type="timestamp" not-null="true" update="false" />

<many-to-one name="lastUpdatedBy" class="org.hisp.dhis.user.User"
column="lastupdatedby" foreign-key="fk_lastupdateby_userid" />

<property name="noteText" column="notetext" type="text" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ class JdbcEventStore {
n.created as note_created,\
n.creator as note_creator,\
n.uid as note_uid,\
n.lastupdated as note_lastupdated,\
userinfo.userinfoid as note_user_id,\
userinfo.code as note_user_code,\
userinfo.uid as note_user_uid,\
Expand Down Expand Up @@ -467,8 +466,6 @@ private List<Event> fetchEvents(EventQueryParams queryParams, PageParams pagePar
note.setLastUpdatedBy(noteLastUpdatedBy);
}

note.setLastUpdated(resultSet.getTimestamp("note_lastupdated"));

event.getNotes().add(note);
notes.add(resultSet.getString("note_id"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.event.EventStatus;
Expand Down Expand Up @@ -302,13 +303,18 @@ private TrackerObjectsMapper() {
@Nonnull TrackerPreheat preheat,
@Nonnull org.hisp.dhis.tracker.imports.domain.Note note,
@Nonnull UserDetails user) {

return map(note, preheat.getUserByUid(user.getUid()).orElse(null));
}

public static @Nonnull Note map(
@Nonnull org.hisp.dhis.tracker.imports.domain.Note note, @Nullable User user) {
Date now = new Date();

Note dbNote = new Note();
dbNote.setUid(note.getNote().getValue());
dbNote.setCreated(now);
dbNote.setLastUpdated(now);
dbNote.setLastUpdatedBy(preheat.getUserByUid(user.getUid()).orElse(null));
dbNote.setLastUpdatedBy(user);
dbNote.setCreator(note.getStoredBy());
dbNote.setNoteText(note.getValue());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.tracker.imports.note;

import static org.apache.commons.lang3.StringUtils.isEmpty;

import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.tracker.export.enrollment.EnrollmentService;
import org.hisp.dhis.tracker.export.event.EventService;
import org.hisp.dhis.tracker.imports.domain.Note;
import org.hisp.dhis.user.CurrentUserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class DefaultNoteService implements NoteService {
private final EnrollmentService enrollmentService;

private final EventService eventService;

private final JdbcNoteStore noteStore;

@Transactional
@Override
public void addNoteForEnrollment(Note note, UID enrollment)
throws ForbiddenException, NotFoundException, BadRequestException {
// Check enrollment existence and access
enrollmentService.getEnrollment(enrollment);
validateNote(note);

noteStore.saveEnrollmentNote(enrollment, note, CurrentUserUtil.getCurrentUserDetails());
}

@Transactional
@Override
public void addNoteForEvent(Note note, UID event)
throws ForbiddenException, NotFoundException, BadRequestException {
// Check event existence and access
eventService.getEvent(event);
validateNote(note);

noteStore.saveEventNote(event, note, CurrentUserUtil.getCurrentUserDetails());
}

private void validateNote(Note note) throws BadRequestException {
if (isEmpty(note.getValue())) {
throw new BadRequestException("Value cannot be empty");
}

if (noteStore.exists(note.getNote())) {
throw new BadRequestException(String.format("Note `%s` already exists.", note.getNote()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.tracker.imports.note;

import java.util.Date;
import java.util.Map;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.tracker.imports.bundle.persister.PersistenceException;
import org.hisp.dhis.tracker.imports.domain.Note;
import org.hisp.dhis.user.UserDetails;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class JdbcNoteStore {
private final NamedParameterJdbcTemplate jdbcTemplate;

public void saveEnrollmentNote(
@Nonnull UID enrollment, @Nonnull Note note, @Nonnull UserDetails user) {
long noteId = saveNote(note, user);
String sql =
"""
INSERT INTO enrollment_notes(enrollmentid, noteid, sort_order)
VALUES ((select enrollmentid from enrollment where uid = :enrollment),
:noteId,
coalesce(
(select max(sort_order) + 1
from enrollment_notes
where enrollmentid = (select enrollmentid from enrollment where uid = :enrollment)
),
1)
)
""";
jdbcTemplate.update(sql, Map.of("enrollment", enrollment.getValue(), "noteId", noteId));
}

public void saveEventNote(@Nonnull UID event, @Nonnull Note note, @Nonnull UserDetails user) {
long noteId = saveNote(note, user);
String sql =
"""
INSERT INTO event_notes(eventid, noteid, sort_order)
VALUES ((select eventid from event where uid = :event),
:noteId,
coalesce(
(select max(sort_order) + 1
from event_notes
where eventid = (select eventid from event where uid = :event)
),
1)
)
""";
jdbcTemplate.update(sql, Map.of("event", event.getValue(), "noteId", noteId));
}

boolean exists(@Nonnull UID note) {
Integer count =
jdbcTemplate.queryForObject(
"select count(1) from note where uid = :uid",
Map.of("uid", note.getValue()),
Integer.class);
return count == null || count > 0;
}

private long saveNote(@Nonnull Note note, @Nonnull UserDetails user) {
String sql =
"""
INSERT INTO public.note(noteid, notetext, creator, lastupdatedby, uid, created)
VALUES (nextVal('note_id_sequence'),
:text,
:creator,
(select userinfoid from userinfo where uid = :lastUpdatedBy),
:uid,
:created)
RETURNING noteid
""";

MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("text", note.getValue());
params.addValue("creator", note.getStoredBy());
params.addValue("lastUpdatedBy", user.getUid());
params.addValue("uid", note.getNote().getValue());
params.addValue("created", new Date());

Long noteId = jdbcTemplate.queryForObject(sql, params, Long.class);

if (noteId == null) {
throw new PersistenceException("Note could not be saved");
}

return noteId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.tracker.imports.note;

import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.tracker.imports.domain.Note;

public interface NoteService {
void addNoteForEnrollment(Note note, UID enrollment)
throws ForbiddenException, NotFoundException, BadRequestException;

void addNoteForEvent(Note note, UID event)
throws ForbiddenException, NotFoundException, BadRequestException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create sequence if not exists note_id_sequence;
select setval('note_id_sequence', coalesce((select max(noteid) from note), 1)) FROM note;

alter table if exists note drop column code;
alter table if exists note drop column lastupdated;
Loading

0 comments on commit 14ddac9

Please sign in to comment.