-
Notifications
You must be signed in to change notification settings - Fork 0
Add Dashboard Config API for managing dashboard configurations #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
6 changes: 6 additions & 0 deletions
6
src/main/java/com/microboxlabs/dashboards/dashboard/service/DashboardConfigService.java
This file contains hidden or 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,6 @@ | ||
| package com.microboxlabs.dashboards.dashboard.service; | ||
|
|
||
| public interface DashboardConfigService { | ||
| String getConfig(String siteShortName, String slug); | ||
| void saveConfig(String siteShortName, String slug, String configJson); | ||
| } |
110 changes: 110 additions & 0 deletions
110
src/main/java/com/microboxlabs/dashboards/dashboard/service/DashboardConfigServiceImpl.java
This file contains hidden or 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,110 @@ | ||
| package com.microboxlabs.dashboards.dashboard.service; | ||
|
|
||
| import org.alfresco.model.ContentModel; | ||
| import org.alfresco.service.cmr.model.FileFolderService; | ||
| import org.alfresco.service.cmr.repository.ContentService; | ||
| import org.alfresco.service.cmr.repository.NodeRef; | ||
| import org.alfresco.service.cmr.repository.NodeService; | ||
| import org.alfresco.service.cmr.site.SiteService; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class DashboardConfigServiceImpl implements DashboardConfigService { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(DashboardConfigServiceImpl.class); | ||
|
|
||
| private static final String DOCUMENT_LIBRARY = "documentLibrary"; | ||
| private static final String DASHBOARD_FOLDER = "dashboard"; | ||
| private static final String MIMETYPE_JSON = "application/json"; | ||
|
|
||
| private NodeService nodeService; | ||
| private SiteService siteService; | ||
| private FileFolderService fileFolderService; | ||
| private ContentService contentService; | ||
|
|
||
| public void setNodeService(NodeService nodeService) { | ||
| this.nodeService = nodeService; | ||
| } | ||
|
|
||
| public void setSiteService(SiteService siteService) { | ||
| this.siteService = siteService; | ||
| } | ||
|
|
||
| public void setFileFolderService(FileFolderService fileFolderService) { | ||
| this.fileFolderService = fileFolderService; | ||
| } | ||
|
|
||
| public void setContentService(ContentService contentService) { | ||
| this.contentService = contentService; | ||
| } | ||
|
|
||
| @Override | ||
| public String getConfig(String siteShortName, String slug) { | ||
| var docLib = getDocumentLibrary(siteShortName); | ||
| if (docLib == null) { | ||
| return null; | ||
| } | ||
|
|
||
| var dashboardFolder = fileFolderService.searchSimple(docLib, DASHBOARD_FOLDER); | ||
| if (dashboardFolder == null) { | ||
| return null; | ||
| } | ||
|
|
||
| var configFile = fileFolderService.searchSimple(dashboardFolder, toFileName(slug)); | ||
| if (configFile == null) { | ||
| return null; | ||
| } | ||
|
|
||
| var reader = contentService.getReader(configFile, ContentModel.PROP_CONTENT); | ||
| if (reader == null || !reader.exists()) { | ||
| return null; | ||
| } | ||
|
|
||
| return reader.getContentString(); | ||
| } | ||
|
|
||
| @Override | ||
| public void saveConfig(String siteShortName, String slug, String configJson) { | ||
| var docLib = getDocumentLibrary(siteShortName); | ||
| if (docLib == null) { | ||
| throw new IllegalStateException("Document library not found for site: " + siteShortName); | ||
| } | ||
|
|
||
| var dashboardFolder = ensureDashboardFolder(docLib); | ||
| var fileName = toFileName(slug); | ||
| var configFile = fileFolderService.searchSimple(dashboardFolder, fileName); | ||
|
|
||
| if (configFile == null) { | ||
| configFile = fileFolderService.create(dashboardFolder, fileName, ContentModel.TYPE_CONTENT).getNodeRef(); | ||
| logger.debug("Created dashboard config file {} for site {}", fileName, siteShortName); | ||
| } | ||
|
|
||
| var writer = contentService.getWriter(configFile, ContentModel.PROP_CONTENT, true); | ||
| writer.setMimetype("application/json"); | ||
| writer.setEncoding("UTF-8"); | ||
| writer.putContent(configJson); | ||
| } | ||
|
|
||
| private NodeRef getDocumentLibrary(String siteShortName) { | ||
| var site = siteService.getSite(siteShortName); | ||
| if (site == null) { | ||
| throw new IllegalStateException("Site not found: " + siteShortName); | ||
| } | ||
| return siteService.getContainer(siteShortName, DOCUMENT_LIBRARY); | ||
| } | ||
|
|
||
| private NodeRef ensureDashboardFolder(NodeRef docLib) { | ||
| var existing = fileFolderService.searchSimple(docLib, DASHBOARD_FOLDER); | ||
| if (existing != null) { | ||
| return existing; | ||
| } | ||
| var folder = fileFolderService.create(docLib, DASHBOARD_FOLDER, ContentModel.TYPE_FOLDER).getNodeRef(); | ||
| nodeService.setProperty(folder, ContentModel.PROP_TITLE, "Dashboard"); | ||
| logger.debug("Created dashboard folder in document library"); | ||
| return folder; | ||
| } | ||
|
|
||
| private String toFileName(String slug) { | ||
| return slug + "-config.json"; | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
src/main/java/com/microboxlabs/dashboards/dashboard/webscript/DashboardConfigWebscript.java
This file contains hidden or 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,99 @@ | ||
| package com.microboxlabs.dashboards.dashboard.webscript; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| import org.json.JSONObject; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.extensions.webscripts.AbstractWebScript; | ||
| import org.springframework.extensions.webscripts.Status; | ||
| import org.springframework.extensions.webscripts.WebScriptException; | ||
| import org.springframework.extensions.webscripts.WebScriptRequest; | ||
| import org.springframework.extensions.webscripts.WebScriptResponse; | ||
|
|
||
| import com.microboxlabs.dashboards.dashboard.service.DashboardConfigService; | ||
|
|
||
| public class DashboardConfigWebscript extends AbstractWebScript { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(DashboardConfigWebscript.class); | ||
|
|
||
| private static final String CONTENT_TYPE_JSON = "application/json"; | ||
|
|
||
| private DashboardConfigService dashboardConfigService; | ||
|
|
||
| public void setDashboardConfigService(DashboardConfigService service) { | ||
| this.dashboardConfigService = service; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException { | ||
| var action = req.getServiceMatch().getTemplateVars().get("action"); | ||
| try { | ||
| switch (action) { | ||
| case "get": | ||
| handleGet(req, res); | ||
| break; | ||
| case "save": | ||
| handleSave(req, res); | ||
| break; | ||
| default: | ||
| res.setContentType(CONTENT_TYPE_JSON); | ||
| res.setStatus(Status.STATUS_BAD_REQUEST); | ||
| res.getWriter().write(jsonError("Unknown action")); | ||
| } | ||
| } catch (WebScriptException e) { | ||
| throw e; | ||
| } catch (IllegalStateException e) { | ||
| res.setStatus(Status.STATUS_NOT_FOUND); | ||
| res.setContentType(CONTENT_TYPE_JSON); | ||
| res.getWriter().write(jsonError(e.getMessage())); | ||
| } catch (Exception e) { | ||
| logger.error("Dashboard Config API error", e); | ||
| res.setStatus(Status.STATUS_INTERNAL_SERVER_ERROR); | ||
| res.setContentType(CONTENT_TYPE_JSON); | ||
| res.getWriter().write(jsonError(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private void handleGet(WebScriptRequest req, WebScriptResponse res) throws IOException { | ||
| var body = new JSONObject(req.getContent().getContent()); | ||
| var site = body.optString("site", null); | ||
| var slug = body.optString("slug", null); | ||
|
|
||
| if (site == null || site.isBlank() || slug == null || slug.isBlank()) { | ||
| throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing required parameters: site, slug"); | ||
| } | ||
|
|
||
| var configJson = dashboardConfigService.getConfig(site, slug); | ||
|
|
||
| res.setContentType("application/json;charset=UTF-8"); | ||
| var response = new JSONObject(); | ||
| response.put("data", configJson != null ? new JSONObject(configJson) : JSONObject.NULL); | ||
| res.getWriter().write(response.toString()); | ||
| } | ||
|
|
||
| private void handleSave(WebScriptRequest req, WebScriptResponse res) throws IOException { | ||
| var body = new JSONObject(req.getContent().getContent()); | ||
| var site = body.optString("site", null); | ||
| var slug = body.optString("slug", null); | ||
|
|
||
| if (site == null || site.isBlank() || slug == null || slug.isBlank()) { | ||
| throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing required parameters: site, slug"); | ||
| } | ||
|
|
||
| if (!body.has("config")) { | ||
| throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing required field: config"); | ||
| } | ||
|
|
||
| var config = body.getJSONObject("config"); | ||
| dashboardConfigService.saveConfig(site, slug, config.toString()); | ||
|
|
||
| res.setContentType("application/json;charset=UTF-8"); | ||
| res.getWriter().write(new JSONObject(Map.of("success", true)).toString()); | ||
| } | ||
|
|
||
| private String jsonError(String message) { | ||
| return new JSONObject(Map.of("error", message)).toString(); | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
...extension/templates/webscripts/microboxlabs/dashboards/dashboard-config/api.post.desc.xml
This file contains hidden or 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,9 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <webscript> | ||
| <shortname>Dashboard Config API</shortname> | ||
| <description>Get and save dashboard layout configurations</description> | ||
| <url>/microboxlabs/dashboards/dashboard-config/{action}</url> | ||
| <format default="json">argument</format> | ||
| <authentication>user</authentication> | ||
| <transaction>required</transaction> | ||
| </webscript> |
This file contains hidden or 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 hidden or 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
102 changes: 102 additions & 0 deletions
102
...st/java/com/microboxlabs/dashboards/dashboard/webscript/DashboardConfigWebscriptTest.java
This file contains hidden or 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,102 @@ | ||
| package com.microboxlabs.dashboards.dashboard.webscript; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.mockito.Mockito.RETURNS_DEEP_STUBS; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.StringWriter; | ||
| import java.util.Map; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.springframework.extensions.webscripts.Match; | ||
| import org.springframework.extensions.webscripts.WebScriptRequest; | ||
| import org.springframework.extensions.webscripts.WebScriptResponse; | ||
|
|
||
| import com.microboxlabs.dashboards.dashboard.service.DashboardConfigService; | ||
|
|
||
| public class DashboardConfigWebscriptTest { | ||
|
|
||
| private DashboardConfigWebscript webscript; | ||
| private DashboardConfigService service; | ||
| private WebScriptRequest req; | ||
| private WebScriptResponse res; | ||
| private StringWriter responseWriter; | ||
|
|
||
| @Before | ||
| public void setUp() throws IOException { | ||
| service = mock(DashboardConfigService.class); | ||
| req = mock(WebScriptRequest.class, RETURNS_DEEP_STUBS); | ||
| res = mock(WebScriptResponse.class); | ||
| responseWriter = new StringWriter(); | ||
| when(res.getWriter()).thenReturn(responseWriter); | ||
|
|
||
| webscript = new DashboardConfigWebscript(); | ||
| webscript.setDashboardConfigService(service); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHandleGetReturnsConfig() throws IOException { | ||
| Match match = new Match("", Map.of("action", "get"), ""); | ||
| when(req.getServiceMatch()).thenReturn(match); | ||
| when(req.getContent().getContent()).thenReturn("{\"site\":\"test-site\",\"slug\":\"my-dashboard\"}"); | ||
|
|
||
| when(service.getConfig("test-site", "my-dashboard")).thenReturn("{\"key\":\"value\"}"); | ||
|
|
||
| webscript.execute(req, res); | ||
|
|
||
| String response = responseWriter.toString(); | ||
| assertNotNull(response); | ||
| assertTrue(response.contains("data")); | ||
| assertTrue(response.contains("key")); | ||
| verify(service).getConfig("test-site", "my-dashboard"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHandleGetReturnsNullConfig() throws IOException { | ||
| Match match = new Match("", Map.of("action", "get"), ""); | ||
| when(req.getServiceMatch()).thenReturn(match); | ||
| when(req.getContent().getContent()).thenReturn("{\"site\":\"test-site\",\"slug\":\"nonexistent\"}"); | ||
|
|
||
| when(service.getConfig("test-site", "nonexistent")).thenReturn(null); | ||
|
|
||
| webscript.execute(req, res); | ||
|
|
||
| String response = responseWriter.toString(); | ||
| assertNotNull(response); | ||
| assertTrue(response.contains("data")); | ||
| verify(service).getConfig("test-site", "nonexistent"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHandleSaveCallsService() throws IOException { | ||
| Match match = new Match("", Map.of("action", "save"), ""); | ||
| when(req.getServiceMatch()).thenReturn(match); | ||
| when(req.getContent().getContent()).thenReturn("{\"site\":\"test-site\",\"slug\":\"my-dashboard\",\"config\":{\"key\":\"value\"}}"); | ||
|
|
||
| webscript.execute(req, res); | ||
|
|
||
| String response = responseWriter.toString(); | ||
| assertNotNull(response); | ||
| assertTrue(response.contains("success")); | ||
| assertTrue(response.contains("true")); | ||
| verify(service).saveConfig("test-site", "my-dashboard", "{\"key\":\"value\"}"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnknownActionReturnsBadRequest() throws IOException { | ||
| Match match = new Match("", Map.of("action", "unknown"), ""); | ||
| when(req.getServiceMatch()).thenReturn(match); | ||
|
|
||
| webscript.execute(req, res); | ||
|
|
||
| String response = responseWriter.toString(); | ||
| assertTrue(response.contains("error")); | ||
| assertTrue(response.contains("Unknown action")); | ||
| verify(res).setStatus(400); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.