Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.microboxlabs</groupId>
<artifactId>miot-dashboards</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.0.1</version>
<name>miot-dashboards Platform/Repository JAR Module</name>
<description>Platform/Repo JAR Module (to be included in the alfresco.war)</description>
<packaging>jar</packaging>
Expand Down
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);
}
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";
}
}
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");
}
Comment thread
odtorres marked this conversation as resolved.

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();
}
}
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>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
<property name="fileFolderService" ref="FileFolderService" />
</bean>

<bean id="dashboardConfigService"
class="com.microboxlabs.dashboards.dashboard.service.DashboardConfigServiceImpl">
<property name="nodeService" ref="NodeService" />
<property name="siteService" ref="SiteService" />
<property name="fileFolderService" ref="FileFolderService" />
<property name="contentService" ref="ContentService" />
</bean>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
<property name="dataSourceConfigService" ref="dataSourceConfigService" />
</bean>

<bean id="webscript.microboxlabs.dashboards.dashboard-config.api.post"
class="com.microboxlabs.dashboards.dashboard.webscript.DashboardConfigWebscript"
parent="webscript">
<property name="dashboardConfigService" ref="dashboardConfigService" />
</bean>

</beans>
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);
}
}
Loading