diff --git a/src/main/java/de/metas/ui/web/notes/NotesRestController.java b/src/main/java/de/metas/ui/web/notes/NotesRestController.java new file mode 100644 index 000000000..1e5cc6401 --- /dev/null +++ b/src/main/java/de/metas/ui/web/notes/NotesRestController.java @@ -0,0 +1,90 @@ +/* + * #%L + * metasfresh-webui-api + * %% + * Copyright (C) 2020 metas GmbH + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +package de.metas.ui.web.notes; + +import de.metas.ui.web.notes.json.JSONNote; +import de.metas.ui.web.session.UserSession; +import de.metas.ui.web.window.controller.WindowRestController; +import de.metas.ui.web.window.datatypes.DocumentPath; +import de.metas.ui.web.window.datatypes.WindowId; +import de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory; +import org.adempiere.util.lang.impl.TableRecordReference; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping(NotesRestController.ENDPOINT) +public class NotesRestController +{ + protected static final String ENDPOINT = WindowRestController.ENDPOINT + "/{windowId}/{documentId}/notes"; + private final UserSession userSession; + + private final DocumentDescriptorFactory documentDescriptorFactory; + private final NotesService notesService; + + public NotesRestController( + final UserSession userSession, + final DocumentDescriptorFactory documentDescriptorFactory, final NotesService notesService) + { + this.userSession = userSession; + this.documentDescriptorFactory = documentDescriptorFactory; + this.notesService = notesService; + } + + @GetMapping + public List getAll( + @PathVariable("windowId") final String windowIdStr, + @PathVariable("documentId") final String documentId + ) + { + // userSession.assertLoggedIn(); // TODO tbp: this needs to be enabled + + final DocumentPath documentPath = DocumentPath.rootDocumentPath(WindowId.fromJson(windowIdStr), documentId); + + final TableRecordReference tableRecordReference = documentDescriptorFactory.getTableRecordReference(documentPath); + + return notesService.getNotesFor(tableRecordReference); + } + + @PostMapping + public void addNote( + @PathVariable("windowId") final String windowIdStr, + @PathVariable("documentId") final String documentId, + @RequestParam("text") final String characterData + ) + { + // userSession.assertLoggedIn(); // TODO tbp: this needs to be enabled + + final DocumentPath documentPath = DocumentPath.rootDocumentPath(WindowId.fromJson(windowIdStr), documentId); + + final TableRecordReference tableRecordReference = documentDescriptorFactory.getTableRecordReference(documentPath); + + notesService.addNote(tableRecordReference, characterData); + } +} diff --git a/src/main/java/de/metas/ui/web/notes/NotesService.java b/src/main/java/de/metas/ui/web/notes/NotesService.java new file mode 100644 index 000000000..5ad38babd --- /dev/null +++ b/src/main/java/de/metas/ui/web/notes/NotesService.java @@ -0,0 +1,75 @@ +/* + * #%L + * metasfresh-webui-api + * %% + * Copyright (C) 2020 metas GmbH + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +package de.metas.ui.web.notes; + +import de.metas.adempiere.model.I_AD_User; +import de.metas.notes.NotesRepository; +import de.metas.ui.web.notes.json.JSONNote; +import de.metas.user.UserId; +import de.metas.user.api.IUserDAO; +import de.metas.util.GuavaCollectors; +import de.metas.util.Services; +import lombok.NonNull; +import org.adempiere.util.lang.ITableRecordReference; +import org.adempiere.util.lang.impl.TableRecordReference; +import org.compiere.model.I_CM_ChatEntry; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class NotesService +{ + private final NotesRepository notesRepository; + + public NotesService(final NotesRepository notesRepository) + { + this.notesRepository = notesRepository; + } + + @NonNull + public List getNotesFor(@NonNull final ITableRecordReference tableRecordReference) + { + final IUserDAO userDAO = Services.get(IUserDAO.class); + + final List chatEntries = notesRepository.retrieveAllNotes(tableRecordReference); + + return chatEntries.stream() + .map(it -> + { + final I_AD_User user = userDAO.getById(UserId.ofRepoId(it.getCreatedBy())); + + return JSONNote.builder() + .characterData(it.getCharacterData()) + .created(it.getCreated().toString()) + .createdBy(user.getName()) + .build(); + }) + .collect(GuavaCollectors.toImmutableList()); + } + + public void addNote(@NonNull final TableRecordReference tableRecordReference, @NonNull final String characterData) + { + notesRepository.createNote(characterData, tableRecordReference); + } +} diff --git a/src/main/java/de/metas/ui/web/notes/json/JSONNote.java b/src/main/java/de/metas/ui/web/notes/json/JSONNote.java new file mode 100644 index 000000000..dc150a7a7 --- /dev/null +++ b/src/main/java/de/metas/ui/web/notes/json/JSONNote.java @@ -0,0 +1,37 @@ +/* + * #%L + * metasfresh-webui-api + * %% + * Copyright (C) 2020 metas GmbH + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +package de.metas.ui.web.notes.json; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import lombok.Builder; + +@Builder +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE) +public class JSONNote +{ + String createdBy; + + String created; + + String characterData; +}