A person editing a label template sees the picture that would print, with its refusals and warnings, before anything is printed, from the module's own screens and with no host code. - DocumentPreviewService: authorises by the existing permissions (a draft needs configure, a published revision needs view, a record needs generate and is read as the person, with their permissions and record locks, before any context is built), loads the draft or the published snapshot without writing, builds the context as generation does and calls IsolatedRenderer.preview. Data is a sample from the scene, the longest declared values, or one record. One log line, no content. - "Preview layout" action on templates and published revisions: data, sample, record, stock and resolution; the result is the preview widget. - "Label preview" section on the template record screen: the draft with sample data (the published revision for a person who may only view). - The widget is fetched, never seeded from a process value: QQQ keeps the state of a finished process for hours (in a table with the shared provider), so the picture, the context and the diagnostics exist only in the answer to the widget request. The process state holds the person's choices only. - Bounded answer: one page, a picture of at most 2 MiB, fifty diagnostics. - The existing action is relabelled "Generate retained preview file"; its process name and behaviour are unchanged. - The record selector asks for generate alone, so a person who may preview a published revision with a record can choose one. - Help on every new process, step, field, section, widget and choice list. Tests: DocumentPreviewTest (data sources, permission matrix, unreadable record, nothing written, process state, PDF refusal, refused scene, version 1 label, bounds, log line, help coverage, instance validation) and LayoutPreviewServiceTest in bridges/renderer (through the service and the bridge, the preview has the hash of the bytes the print path returns at 203, 300 and 600 dpi). The registration and process tests are updated for the new wiring and the relabel. bridges/renderer gains the RDBMS module at test scope for that test. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
152 lines
7.8 KiB
Java
152 lines
7.8 KiB
Java
/*
|
|
* QQQ - Low-code Application Framework for Engineers.
|
|
* Copyright (C) 2021-2025. Kingsrook, LLC
|
|
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
|
* contact@kingsrook.com
|
|
* https://github.com/Kingsrook/
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
|
|
package com.kingsrook.qbits.documentgeneration;
|
|
|
|
|
|
import java.io.Serializable;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionEnforcement;
|
|
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
|
|
import com.kingsrook.qqq.backend.core.actions.values.QCustomPossibleValueProvider;
|
|
import com.kingsrook.qqq.backend.core.actions.values.QValueFormatter;
|
|
import com.kingsrook.qqq.backend.core.context.QContext;
|
|
import com.kingsrook.qqq.backend.core.exceptions.QBadRequestException;
|
|
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
|
import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException;
|
|
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
|
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
|
|
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
|
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
|
|
import com.kingsrook.qqq.backend.core.model.actions.values.SearchPossibleValueSourceInput;
|
|
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
|
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.QPossibleValue;
|
|
|
|
|
|
/***************************************************************************
|
|
** Preview selection reads the configured source through current native authority.
|
|
***************************************************************************/
|
|
public class GenerationPreviewSources implements QCustomPossibleValueProvider<Serializable>
|
|
{
|
|
/***************************************************************************
|
|
** A bare ID has no source identity; exact refresh uses the named search source.
|
|
***************************************************************************/
|
|
@Override
|
|
public QPossibleValue<Serializable> getPossibleValue(Serializable id) throws QException
|
|
{
|
|
throw new QBadRequestException("Resolve preview records through their source-specific selector.");
|
|
}
|
|
|
|
/***************************************************************************
|
|
** One bounded page, with enforced query and exact read checks; never raw totals.
|
|
***************************************************************************/
|
|
@Override
|
|
public List<QPossibleValue<Serializable>> search(SearchPossibleValueSourceInput input) throws QException
|
|
{
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
// a record is chosen to generate from or to preview with, which both need `generate`; an //
|
|
// author previews a draft and a viewer previews a published revision, so `configure` is //
|
|
// not required of someone who may only generate. Every record is still read as the caller //
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
GenerationSecurity.require("generate");
|
|
var instance = QContext.getQInstance();
|
|
var source = instance.getPossibleValueSource(input.getPossibleValueSourceName());
|
|
var config = DocumentGenerationQBitConfig.from(instance);
|
|
String sourceTable = config.getSourceTables().stream()
|
|
.filter(name -> GenerationAuthoringProcesses.sourceRecordChoices(name).equals(input.getPossibleValueSourceName()))
|
|
.findFirst().orElse(null);
|
|
if(GenerationAuthoringProcesses.PREVIEW_SOURCE.equals(input.getPossibleValueSourceName()))
|
|
{
|
|
// The standard process route is keyed by the static field; the form says which source table is selected.
|
|
// Only a configured table is accepted, and every record below is still read under the caller's own authority.
|
|
Object selected = input.getOtherValues() == null ? null : input.getOtherValues().get("sourceTable");
|
|
sourceTable = selected instanceof String name && config.getSourceTables().contains(name) ? name : null;
|
|
}
|
|
if(source == null || sourceTable == null)
|
|
{
|
|
throw new QBadRequestException("Unknown preview source.");
|
|
}
|
|
var table = instance.getTable(sourceTable);
|
|
String term = Objects.requireNonNullElse(input.getSearchTerm(), "");
|
|
int skip = Objects.requireNonNullElse(input.getSkip(), 0);
|
|
if(term.length() > 128 || skip < 0 || skip > 10000 || input.getIdList() != null && input.getIdList().size() > 50)
|
|
{
|
|
throw new QBadRequestException("Narrow the preview record search.");
|
|
}
|
|
var filter = new QQueryFilter().withLimit(Math.max(1, Math.min(50, Objects.requireNonNullElse(input.getLimit(), 50))))
|
|
.withSkip(skip).withOrderBy(new QFilterOrderBy(table.getPrimaryKeyField()));
|
|
List<String> labels = Objects.requireNonNullElse(table.getRecordLabelFields(), List.of());
|
|
List<String> safeLabels = labels.stream().filter(name -> table.getField(name) != null && !Boolean.TRUE.equals(table.getField(name).getIsHidden())
|
|
&& table.getField(name).getType() == QFieldType.STRING).toList();
|
|
if(input.getIdList() != null)
|
|
{
|
|
filter.withCriteria(table.getPrimaryKeyField(), QCriteriaOperator.IN, input.getIdList());
|
|
}
|
|
else if(!term.isBlank())
|
|
{
|
|
if(safeLabels.isEmpty())
|
|
{
|
|
// Without a text label the only searchable key is the primary key; a term that is not a valid key matches nothing.
|
|
try
|
|
{
|
|
filter.withCriteria(table.getPrimaryKeyField(), QCriteriaOperator.EQUALS,
|
|
com.kingsrook.qqq.backend.core.utils.ValueUtils.getValueAsFieldType(table.getField(table.getPrimaryKeyField()).getType(), term));
|
|
}
|
|
catch(Exception notAKey)
|
|
{
|
|
return (new ArrayList<>());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var search = new QQueryFilter().withBooleanOperator(QQueryFilter.BooleanOperator.OR);
|
|
safeLabels.forEach(name -> search.withCriteria(name, QCriteriaOperator.CONTAINS, term));
|
|
filter.withSubFilter(search);
|
|
}
|
|
}
|
|
QueryInput query = new QueryInput(table.getName()).withFilter(filter).withShouldFetchHeavyFields(false);
|
|
query.setPermissionEnforcement(PermissionEnforcement.ENFORCE);
|
|
List<QPossibleValue<Serializable>> result = new ArrayList<>();
|
|
for(var candidate : new QueryAction().execute(query).getRecords())
|
|
{
|
|
Serializable id = candidate.getValue(table.getPrimaryKeyField());
|
|
if(id == null)
|
|
{
|
|
continue;
|
|
}
|
|
try
|
|
{
|
|
var record = GenerationSecurity.source(new GenerationPorts.Source(table.getName(), id.toString()));
|
|
String label = labels.isEmpty() || !safeLabels.containsAll(labels) || !record.getValues().keySet().containsAll(labels)
|
|
? id.toString() : QValueFormatter.formatRecordLabel(table, record);
|
|
result.add(new QPossibleValue<>(id, label));
|
|
}
|
|
catch(QNotFoundException denied)
|
|
{
|
|
// Exact Get customizers may narrow Query results; denied labels are omitted.
|
|
}
|
|
}
|
|
return (result);
|
|
}
|
|
}
|