-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Implement create note endpoints in tracker [DHIS2-17579]
- Loading branch information
1 parent
99c00a5
commit 14ddac9
Showing
14 changed files
with
755 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/note/DefaultNoteService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
.../dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/note/JdbcNoteStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...es/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/note/NoteService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
5 changes: 5 additions & 0 deletions
5
...in/resources/org/hisp/dhis/db/migration/2.42/V2_42_34__Create_sequence_for_note_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.