Skip to content

Commit

Permalink
Add first draft of NotesRestController
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBestPessimist committed Apr 22, 2020
1 parent 39f76f8 commit b060661
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
90 changes: 90 additions & 0 deletions 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
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #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<JSONNote> 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);
}
}
75 changes: 75 additions & 0 deletions 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
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #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<JSONNote> getNotesFor(@NonNull final ITableRecordReference tableRecordReference)
{
final IUserDAO userDAO = Services.get(IUserDAO.class);

final List<I_CM_ChatEntry> 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);
}
}
37 changes: 37 additions & 0 deletions 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
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #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;
}

0 comments on commit b060661

Please sign in to comment.