SPRINT-12: updated to define widgets at table level, refactoring of some of the widget stuff to match other "Action"s

This commit is contained in:
Tim Chamberlain
2022-09-23 15:55:27 -05:00
parent 3d07d215a9
commit 5efd2da636
16 changed files with 450 additions and 63 deletions

View File

@ -22,10 +22,12 @@
package com.kingsrook.qqq.backend.core.actions.dashboard;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import com.kingsrook.qqq.backend.core.actions.values.QValueFormatter;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetOutput;
/*******************************************************************************
@ -34,20 +36,15 @@ import com.kingsrook.qqq.backend.core.model.session.QSession;
*******************************************************************************/
public abstract class AbstractWidgetRenderer
{
/*******************************************************************************
**
*******************************************************************************/
public abstract Object render(QInstance qInstance, QSession session, QWidgetMetaDataInterface qWidgetMetaData) throws QException;
public static final QValueFormatter valueFormatter = new QValueFormatter();
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mma").withZone(ZoneId.systemDefault());
public static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());
/*******************************************************************************
**
*******************************************************************************/
public String getWidgetName()
{
return this.getClass().getSimpleName();
}
public abstract RenderWidgetOutput render(RenderWidgetInput input) throws QException;
}

View File

@ -23,11 +23,11 @@ package com.kingsrook.qqq.backend.core.actions.dashboard;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetOutput;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.QWidget;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.QuickSightChart;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QuickSightChartMetaData;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
@ -49,11 +49,11 @@ public class QuickSightChartRenderer extends AbstractWidgetRenderer
**
*******************************************************************************/
@Override
public Object render(QInstance qInstance, QSession session, QWidgetMetaDataInterface metaData) throws QException
public RenderWidgetOutput render(RenderWidgetInput input) throws QException
{
try
{
QuickSightChartMetaData quickSightMetaData = (QuickSightChartMetaData) metaData;
QuickSightChartMetaData quickSightMetaData = (QuickSightChartMetaData) input.getWidgetMetaData();
QuickSightClient quickSightClient = getQuickSightClient(quickSightMetaData);
final RegisteredUserEmbeddingExperienceConfiguration experienceConfiguration = RegisteredUserEmbeddingExperienceConfiguration.builder()
@ -71,8 +71,9 @@ public class QuickSightChartRenderer extends AbstractWidgetRenderer
final GenerateEmbedUrlForRegisteredUserResponse generateEmbedUrlForRegisteredUserResponse = quickSightClient.generateEmbedUrlForRegisteredUser(generateEmbedUrlForRegisteredUserRequest);
String embedUrl = generateEmbedUrlForRegisteredUserResponse.embedUrl();
return (new QuickSightChart(metaData.getName(), quickSightMetaData.getLabel(), embedUrl));
String embedUrl = generateEmbedUrlForRegisteredUserResponse.embedUrl();
QWidget widget = new QuickSightChart(input.getWidgetMetaData().getName(), quickSightMetaData.getLabel(), embedUrl);
return (new RenderWidgetOutput(widget));
}
catch(Exception e)
{

View File

@ -24,26 +24,23 @@ package com.kingsrook.qqq.backend.core.actions.dashboard;
import com.kingsrook.qqq.backend.core.actions.customizers.QCodeLoader;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetOutput;
/*******************************************************************************
** Class for loading widget implementation code and rendering of widgets
**
*******************************************************************************/
public class WidgetDataLoader
public class RenderWidgetAction
{
/*******************************************************************************
**
*******************************************************************************/
public Object execute(QInstance qInstance, QSession session, String name) throws QException
public RenderWidgetOutput execute(RenderWidgetInput input) throws QException
{
QWidgetMetaDataInterface widget = qInstance.getWidget(name);
AbstractWidgetRenderer widgetRenderer = QCodeLoader.getAdHoc(AbstractWidgetRenderer.class, widget.getCodeReference());
Object w = widgetRenderer.render(qInstance, session, widget);
return (widgetRenderer.render(qInstance, session, widget));
AbstractWidgetRenderer widgetRenderer = QCodeLoader.getAdHoc(AbstractWidgetRenderer.class, input.getWidgetMetaData().getCodeReference());
return (widgetRenderer.render(input));
}
}

View File

@ -0,0 +1,171 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2022. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.backend.core.model.actions.widgets;
import java.util.HashMap;
import java.util.Map;
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionInput;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.session.QSession;
/*******************************************************************************
** Input data container for the RenderWidget action
**
*******************************************************************************/
public class RenderWidgetInput extends AbstractActionInput
{
private QSession session;
private QWidgetMetaDataInterface widgetMetaData;
private Map<String, String> queryParams;
/*******************************************************************************
**
*******************************************************************************/
public RenderWidgetInput(QInstance instance)
{
super(instance);
}
/*******************************************************************************
** Getter for session
**
*******************************************************************************/
public QSession getSession()
{
return session;
}
/*******************************************************************************
** Setter for session
**
*******************************************************************************/
public void setSession(QSession session)
{
this.session = session;
}
/*******************************************************************************
** Fluent setter for session
**
*******************************************************************************/
public RenderWidgetInput withSession(QSession session)
{
this.session = session;
return (this);
}
/*******************************************************************************
** Getter for widgetMetaData
**
*******************************************************************************/
public QWidgetMetaDataInterface getWidgetMetaData()
{
return widgetMetaData;
}
/*******************************************************************************
** Setter for widgetMetaData
**
*******************************************************************************/
public void setWidgetMetaData(QWidgetMetaDataInterface widgetMetaData)
{
this.widgetMetaData = widgetMetaData;
}
/*******************************************************************************
** Fluent setter for widgetMetaData
**
*******************************************************************************/
public RenderWidgetInput withWidgetMetaData(QWidgetMetaDataInterface widgetMetaData)
{
this.widgetMetaData = widgetMetaData;
return (this);
}
/*******************************************************************************
** Getter for urlParams
**
*******************************************************************************/
public Map<String, String> getQueryParams()
{
return queryParams;
}
/*******************************************************************************
** Setter for urlParams
**
*******************************************************************************/
public void setQueryParams(Map<String, String> queryParams)
{
this.queryParams = queryParams;
}
/*******************************************************************************
** Fluent setter for urlParams
**
*******************************************************************************/
public RenderWidgetInput withUrlParams(Map<String, String> urlParams)
{
this.queryParams = urlParams;
return (this);
}
/*******************************************************************************
** adds a query param value
**
*******************************************************************************/
public void addQueryParam(String name, String value)
{
if(this.queryParams == null)
{
this.queryParams = new HashMap<>();
}
this.queryParams.put(name, value);
}
}

View File

@ -0,0 +1,80 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2022. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.backend.core.model.actions.widgets;
import java.io.Serializable;
/*******************************************************************************
** Output for an Export action
*******************************************************************************/
public class RenderWidgetOutput implements Serializable
{
public Object widgetData;
/*******************************************************************************
** constructor taking in widget data
**
*******************************************************************************/
public RenderWidgetOutput(Object widgetData)
{
this.widgetData = widgetData;
}
/*******************************************************************************
** Getter for widgetData
**
*******************************************************************************/
public Object getWidgetData()
{
return widgetData;
}
/*******************************************************************************
** Setter for widgetData
**
*******************************************************************************/
public void setWidgetData(Object widgetData)
{
this.widgetData = widgetData;
}
/*******************************************************************************
** Fluent setter for widgetData
**
*******************************************************************************/
public RenderWidgetOutput withWidgetData(Object widgetData)
{
this.widgetData = widgetData;
return (this);
}
}

View File

@ -229,6 +229,7 @@ public class TableData implements QWidget
private String accessor;
private String width;
private String align;
private String verticalAlign;
@ -413,5 +414,40 @@ public class TableData implements QWidget
this.type = type;
return this;
}
/*******************************************************************************
** Getter for verticalAlign
**
*******************************************************************************/
public String getVerticalAlign()
{
return verticalAlign;
}
/*******************************************************************************
** Setter for verticalAlign
**
*******************************************************************************/
public void setVerticalAlign(String verticalAlign)
{
this.verticalAlign = verticalAlign;
}
/*******************************************************************************
** Fluent setter for verticalAlign
**
*******************************************************************************/
public Column withVerticalAlign(String verticalAlign)
{
this.verticalAlign = verticalAlign;
return (this);
}
}
}

View File

@ -31,6 +31,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
*******************************************************************************/
public class QWidgetMetaData implements QWidgetMetaDataInterface
{
private String id;
protected String name;
protected QCodeReference codeReference;
@ -102,4 +103,38 @@ public class QWidgetMetaData implements QWidgetMetaDataInterface
return (this);
}
/*******************************************************************************
** Getter for id
**
*******************************************************************************/
public String getId()
{
return id;
}
/*******************************************************************************
** Setter for id
**
*******************************************************************************/
public void setId(String id)
{
this.id = id;
}
/*******************************************************************************
** Fluent setter for id
**
*******************************************************************************/
public QWidgetMetaData withId(String id)
{
this.id = id;
return (this);
}
}

View File

@ -30,6 +30,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QFieldSection;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
/*******************************************************************************
@ -50,6 +51,8 @@ public class QFrontendTableMetaData
private Map<String, QFrontendFieldMetaData> fields;
private List<QFieldSection> sections;
private List<String> widgets;
//////////////////////////////////////////////////////////////////////////////////
// do not add setters. take values from the source-object in the constructor!! //
//////////////////////////////////////////////////////////////////////////////////
@ -81,6 +84,11 @@ public class QFrontendTableMetaData
{
this.iconName = tableMetaData.getIcon().getName();
}
if(CollectionUtils.nullSafeHasContents(tableMetaData.getWidgets()))
{
this.widgets = tableMetaData.getWidgets();
}
}
@ -159,4 +167,15 @@ public class QFrontendTableMetaData
{
return iconName;
}
/*******************************************************************************
** Getter for widgets
**
*******************************************************************************/
public List<String> getWidgets()
{
return widgets;
}
}

View File

@ -77,6 +77,8 @@ public class QTableMetaData implements QAppChildMetaData, Serializable
private List<QFieldSection> sections;
private List<String> widgets;
/*******************************************************************************
@ -715,4 +717,38 @@ public class QTableMetaData implements QAppChildMetaData, Serializable
return (this);
}
/*******************************************************************************
** Getter for widgets
**
*******************************************************************************/
public List<String> getWidgets()
{
return widgets;
}
/*******************************************************************************
** Setter for widgets
**
*******************************************************************************/
public void setWidgets(List<String> widgets)
{
this.widgets = widgets;
}
/*******************************************************************************
** Fluent setter for widgets
**
*******************************************************************************/
public QTableMetaData withWidgets(List<String> widgets)
{
this.widgets = widgets;
return (this);
}
}

View File

@ -25,10 +25,9 @@ package com.kingsrook.qqq.backend.core.actions.dashboard;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetOutput;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.ChartData;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.session.QSession;
/*******************************************************************************
@ -40,7 +39,7 @@ public class PersonsByCreateDateBarChart extends AbstractWidgetRenderer
**
*******************************************************************************/
@Override
public Object render(QInstance qInstance, QSession session, QWidgetMetaDataInterface metaData) throws QException
public RenderWidgetOutput render(RenderWidgetInput input) throws QException
{
try
{
@ -62,7 +61,7 @@ public class PersonsByCreateDateBarChart extends AbstractWidgetRenderer
labels.add("May 2022");
data.add(64);
return (new ChartData("Persons created per Month", null, "Person records", labels, data));
return (new RenderWidgetOutput(new ChartData("Persons created per Month", null, "Person records", labels, data)));
}
catch(Exception e)
{

View File

@ -24,8 +24,7 @@ package com.kingsrook.qqq.backend.core.actions.dashboard;
import java.net.UnknownHostException;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QuickSightChartMetaData;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.utils.TestUtils;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ -43,7 +42,7 @@ class QuickSightChartRendererTest
@Test
void testWrongMetaDataClass() throws QException
{
assertThatThrownBy(() -> new QuickSightChartRenderer().render(TestUtils.defineInstance(), TestUtils.getMockSession(), new QWidgetMetaData()))
assertThatThrownBy(() -> new QuickSightChartRenderer().render(new RenderWidgetInput(TestUtils.defineInstance())))
.hasRootCauseInstanceOf(ClassCastException.class);
}
@ -55,7 +54,7 @@ class QuickSightChartRendererTest
@Test
void testNoCredentials() throws QException
{
assertThatThrownBy(() -> new QuickSightChartRenderer().render(TestUtils.defineInstance(), TestUtils.getMockSession(), new QuickSightChartMetaData()))
assertThatThrownBy(() -> new QuickSightChartRenderer().render(new RenderWidgetInput(TestUtils.defineInstance())))
.hasRootCauseInstanceOf(NullPointerException.class);
}
@ -67,14 +66,8 @@ class QuickSightChartRendererTest
@Test
void testBadCredentials() throws QException
{
assertThatThrownBy(() -> new QuickSightChartRenderer().render(TestUtils.defineInstance(), TestUtils.getMockSession(),
new QuickSightChartMetaData()
.withName("test")
.withAccessKey("FAIL")
.withSecretKey("FAIL")
.withRegion("FAIL")
.withAccountId("FAIL")
assertThatThrownBy(() -> new QuickSightChartRenderer().render(new RenderWidgetInput(TestUtils.defineInstance())
)).hasRootCauseInstanceOf(UnknownHostException.class);
}
}
}

View File

@ -23,7 +23,10 @@ package com.kingsrook.qqq.backend.core.actions.dashboard;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetOutput;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.ChartData;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.utils.TestUtils;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -32,9 +35,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
/*******************************************************************************
** Unit test for WidgetDataLoader
** Unit test for RenderWidgetAction
*******************************************************************************/
class WidgetDataLoaderTest
class RenderWidgetActionTest
{
/*******************************************************************************
**
@ -42,9 +45,14 @@ class WidgetDataLoaderTest
@Test
void test() throws QException
{
Object widgetData = new WidgetDataLoader().execute(TestUtils.defineInstance(), TestUtils.getMockSession(), PersonsByCreateDateBarChart.class.getSimpleName());
assertThat(widgetData).isInstanceOf(ChartData.class);
ChartData chartData = (ChartData) widgetData;
QInstance testQInstance = TestUtils.defineInstance();
RenderWidgetInput input = new RenderWidgetInput(testQInstance)
.withSession(TestUtils.getMockSession())
.withWidgetMetaData(testQInstance.getWidget(PersonsByCreateDateBarChart.class.getSimpleName()));
RenderWidgetOutput output = new RenderWidgetAction().execute(input);
assertThat(output.getWidgetData()).isInstanceOf(ChartData.class);
ChartData chartData = (ChartData) output.getWidgetData();
assertEquals("barChart", chartData.getType());
assertThat(chartData.getTitle()).isNotBlank();
assertNotNull(chartData.getChartData());