Skip to content

Commit

Permalink
Added embedded viewer, fixed templates, transform for lobs
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosjepard committed Dec 3, 2024
1 parent c2c4086 commit 066fcf6
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,18 +523,31 @@ public class ViewerConstants {
/**
* Template Engine
*/

public static final String OPEN_TEMPLATE_ENGINE = "{{";
public static final String CLOSE_TEMPLATE_ENGINE = "}}";
public static final String TEMPLATE_UV_LINK = "uv_link";
public static final String TEMPLATE_LOB_DOWNLOAD_LABEL = "download_label";
public static final String TEMPLATE_LOB_DOWNLOAD_LINK = "download_link";
public static final String TEMPLATE_LOB_ROW_INDEX = "row_index";
public static final String TEMPLATE_LOB_COLUMN_INDEX = "column_index";
public static final String TEMPLATE_LOB_AUTO_DETECTED_MIME_TYPE = "auto_detected_mime_type";
public static final String TEMPLATE_LOB_AUTO_DETECTED_EXTENSION = "auto_detected_extension";
public static final String DEFAULT_VIEWER_DOWNLOAD_LABEL_TEMPLATE = "<a href=\""
+ ViewerConstants.OPEN_TEMPLATE_ENGINE
+ ViewerConstants.TEMPLATE_UV_LINK + ViewerConstants.CLOSE_TEMPLATE_ENGINE + ViewerConstants.OPEN_TEMPLATE_ENGINE
+ ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK + ViewerConstants.CLOSE_TEMPLATE_ENGINE + "\">"
+ ViewerConstants.OPEN_TEMPLATE_ENGINE + ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL
+ ViewerConstants.CLOSE_TEMPLATE_ENGINE + "</a>";
public static final String DEFAULT_DOWNLOAD_LABEL_TEMPLATE = "<a href=\"" + ViewerConstants.OPEN_TEMPLATE_ENGINE
+ ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK + ViewerConstants.CLOSE_TEMPLATE_ENGINE + "\">"
+ ViewerConstants.OPEN_TEMPLATE_ENGINE + ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL
+ ViewerConstants.CLOSE_TEMPLATE_ENGINE + "</a>";
public static final String DEFAULT_DETAILED_VIEWER_LABEL_TEMPLATE = "<iframe class=\"embedded-viewer\" src=\""
+ ViewerConstants.OPEN_TEMPLATE_ENGINE + ViewerConstants.TEMPLATE_UV_LINK + ViewerConstants.CLOSE_TEMPLATE_ENGINE
+ ViewerConstants.OPEN_TEMPLATE_ENGINE + ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK
+ ViewerConstants.CLOSE_TEMPLATE_ENGINE + "\">" + ViewerConstants.OPEN_TEMPLATE_ENGINE
+ ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL + ViewerConstants.CLOSE_TEMPLATE_ENGINE + "</iframe>";

/**
* SIARD prefixes
Expand Down Expand Up @@ -579,4 +592,12 @@ public enum SiardVersion {
*/
private ViewerConstants() {
}

/**
* External viewer information
*/

public static final String UV_EXTERNAL_VIEWER_SERVICE_NAME = "ui.viewer.universalViewer.service_name";
public static final String PRESENTATION_EXTERNAL_SERVICE_NAME = "ui.viewer.presentation.service_name";
public static final String VIEWER_ENABLED = "ui.viewer.enabled";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static com.databasepreservation.common.client.models.structure.ViewerType.dbTypes.CLOB;
import static com.databasepreservation.common.client.models.structure.ViewerType.dbTypes.NESTED;

import com.databasepreservation.common.client.tools.ViewerCelllUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -20,7 +19,6 @@
import java.util.List;
import java.util.Map;

import com.databasepreservation.common.client.models.status.formatters.NoFormatter;
import org.fusesource.restygwt.client.MethodCallback;
import org.roda.core.data.v2.index.sublist.Sublist;

Expand All @@ -44,6 +42,7 @@
import com.databasepreservation.common.client.models.status.collection.ColumnStatus;
import com.databasepreservation.common.client.models.status.collection.LargeObjectConsolidateProperty;
import com.databasepreservation.common.client.models.status.collection.TableStatus;
import com.databasepreservation.common.client.models.status.formatters.NoFormatter;
import com.databasepreservation.common.client.models.status.formatters.NumberFormatter;
import com.databasepreservation.common.client.models.structure.ViewerCell;
import com.databasepreservation.common.client.models.structure.ViewerColumn;
Expand All @@ -56,6 +55,7 @@
import com.databasepreservation.common.client.tools.JSOUtils;
import com.databasepreservation.common.client.tools.NumberFormatUtils;
import com.databasepreservation.common.client.tools.RestUtils;
import com.databasepreservation.common.client.tools.ViewerCelllUtils;
import com.databasepreservation.common.client.tools.ViewerStringUtils;
import com.databasepreservation.common.client.widgets.Alert;
import com.google.gwt.cell.client.Cell;
Expand Down Expand Up @@ -186,11 +186,17 @@ protected void configureDisplay(CellTable<ViewerRow> display) {
// this is the table of nested document
ViewerTable nestedTable = database.getMetadata()
.getTableById(configColumn.getNestedColumns().getOriginalTable());

Column<ViewerRow, SafeHtml> templateColumn = buildTemplateColumn(configColumn, nestedTable);
templateColumn.setSortable(false);
addColumn(configColumn, templateColumn);
configColumns.put(configColumn, templateColumn);
if (configColumn.getTypeName().contains("BINARY LARGE OBJECT")) {
Column<ViewerRow, SafeHtml> binaryColumn = buildNestedRowDownloadColumn(configColumn, nestedTable);
binaryColumn.setSortable(true); // add to configuration file sortable options
addColumn(configColumn, binaryColumn);
configColumns.put(configColumn, binaryColumn);
} else {
Column<ViewerRow, SafeHtml> templateColumn = buildTemplateColumn(configColumn, nestedTable);
templateColumn.setSortable(false);
addColumn(configColumn, templateColumn);
configColumns.put(configColumn, templateColumn);
}
}
}

Expand Down Expand Up @@ -254,6 +260,71 @@ public SafeHtml getValue(ViewerRow row) {
};
}

private Column<ViewerRow, SafeHtml> buildNestedRowDownloadColumn(ColumnStatus configColumn, ViewerTable nestedTable) {
return new Column<ViewerRow, SafeHtml>(new SafeHtmlCell()) {
@Override
public void render(Cell.Context context, ViewerRow object, SafeHtmlBuilder sb) {
SafeHtml value = getValue(object);
String title = messages.row_downloadLOB();
if (value != null) {
sb.appendHtmlConstant("<div title=\"" + title + "\">");
sb.append(value);
sb.appendHtmlConstant("</div");
}
}

@Override
public SafeHtml getValue(ViewerRow row) {
List<String> aggregationList = new ArrayList<>();
StringBuilder sb = new StringBuilder();
String url;
SafeHtml ret = null;
if (row.getNestedRowList() != null) {
for (ViewerRow nestedRow : row.getNestedRowList()) {
if (nestedRow != null && nestedRow.getCells() != null && !nestedRow.getCells().isEmpty()
&& nestedRow.getUuid().equals(configColumn.getId())) {
Map<String, ViewerCell> cells = nestedRow.getCells();
String template = configColumn.getSearchStatus().getList().getTemplate().getTemplate();
if (template != null && !template.isEmpty()) {
String json = JSOUtils.cellsToJson(cells, nestedTable);
String blob = getBlobKey(configColumn.getTypeName());
if (!blob.isEmpty() && json.contains(blob)) {
String tempTemplate = template.replace("{{" + blob + "}}", "");
tempTemplate = tempTemplate.replace("}{", "} {");
if (tempTemplate.isEmpty())
tempTemplate = messages.row_downloadLOB();
template = "<a href=\"" + com.google.gwt.core.client.GWT.getHostPageBaseURL() + "{{" + blob + "}}\">"
+ tempTemplate + "</a>";
String s = JavascriptUtils.compileTemplate(template, json);
aggregationList.add(s);
} else {
String tempTemplate = template.replace("}{", "} {");
String s = JavascriptUtils.compileTemplate(tempTemplate, json);
aggregationList.add(s);
}
}
}
}
String separatorText = configColumn.getSearchStatus().getList().getTemplate().getSeparator();
if (separatorText != null) {
String separator = "";
for (String s : aggregationList) {
sb.append(separator);
sb.append(s);
separator = separatorText;
}
ret = SafeHtmlUtils.fromSafeConstant(sb.toString());
} else {
if (!aggregationList.isEmpty()) {
ret = SafeHtmlUtils.fromSafeConstant(aggregationList.get(0));
}
}
}
return ret;
}
};
}

private Column<ViewerRow, SafeHtml> buildSimpleColumn(ColumnStatus configColumn) {
return new Column<ViewerRow, SafeHtml>(new SafeHtmlCell()) {
@Override
Expand Down Expand Up @@ -289,11 +360,10 @@ public SafeHtml getValue(ViewerRow row) {
break;
case NUMERIC_FLOATING_POINT:
if (configColumn.getFormatter() instanceof NoFormatter) {
ret = SafeHtmlUtils
.fromString(NumberFormatUtils.getFormattedValue(new NumberFormatter(), value));
ret = SafeHtmlUtils.fromString(NumberFormatUtils.getFormattedValue(new NumberFormatter(), value));
} else {
ret = SafeHtmlUtils
.fromString(NumberFormatUtils.getFormattedValue((NumberFormatter) configColumn.getFormatter(), value));
ret = SafeHtmlUtils.fromString(
NumberFormatUtils.getFormattedValue((NumberFormatter) configColumn.getFormatter(), value));
}
break;
case BOOLEAN:
Expand Down Expand Up @@ -357,9 +427,17 @@ private SafeHtml getLobDownload(ViewerDatabase database, ColumnStatus configColu
int columnIndex) {
String template = configColumn.getSearchStatus().getList().getTemplate().getTemplate();
if (template != null && !template.isEmpty()) {
String json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_downloadLOB(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK, RestUtils.createExportLobUri(database.getUuid(),
table.getSchemaName(), table.getName(), row.getUuid(), columnIndex));
String json = "";
if (ClientConfigurationManager.getBoolean(false, ViewerConstants.VIEWER_ENABLED)) {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_openLOBViewer(),
ViewerConstants.TEMPLATE_UV_LINK, RestUtils.createUVLob(), ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK,
RestUtils.createExportLobUri(database.getUuid(), table.getSchemaName(), table.getName(), row.getUuid(),
columnIndex));
} else {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_downloadLOB(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK, RestUtils.createExportLobUri(database.getUuid(),
table.getSchemaName(), table.getName(), row.getUuid(), columnIndex));
}
return SafeHtmlUtils.fromSafeConstant(JavascriptUtils.compileTemplate(template, json));
}

Expand Down Expand Up @@ -558,4 +636,28 @@ private String getExportURL(String zipFilename, String filename, boolean exportA
private String getExportURL(String filename, boolean exportAll, boolean description) {
return getExportURL(null, filename, exportAll, description, false);
}

private String getBlobKey(String input) {
String result = "";

// Split the input string by commas to get individual key-value pairs
String[] pairs = input.split(",\\s*");

for (String pair : pairs) {
// Split each pair by the colon to separate the key and value
String[] keyValue = pair.split(":\\s*");

if (keyValue.length == 2) {
String key = keyValue[0];
String value = keyValue[1];

// Check if the value contains "BINARY LARGE OBJECT"
if (value.contains("BINARY LARGE OBJECT")) {
result = key;
}
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,18 @@ private void getCellHTML(ViewerColumn column, Set<Ref> relatedTo, Set<Ref> refer
} else {
SafeHtml safeHtml = SafeHtmlUtils.EMPTY_SAFE_HTML;
String template = columnStatus.getDetailsStatus().getTemplateStatus().getTemplate();
String json = "";
if (template != null && !template.isEmpty()) {
String json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_downloadLOB(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK, RestUtils.createExportLobUri(database.getUuid(),
table.getSchemaName(), table.getName(), row.getUuid(), columnStatus.getColumnIndex()));
if (ClientConfigurationManager.getBoolean(false, ViewerConstants.VIEWER_ENABLED)) {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_openLOBViewer(),
ViewerConstants.TEMPLATE_UV_LINK, RestUtils.createUVLob(), ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK,
RestUtils.createExportLobUri(database.getUuid(), table.getSchemaName(), table.getName(), row.getUuid(),
columnStatus.getColumnIndex()));
} else {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_downloadLOB(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK, RestUtils.createExportLobUri(database.getUuid(),
table.getSchemaName(), table.getName(), row.getUuid(), columnStatus.getColumnIndex()));
}
safeHtml = SafeHtmlUtils.fromSafeConstant(JavascriptUtils.compileTemplate(template, json));
}

Expand All @@ -349,9 +357,17 @@ private void getCellHTML(ViewerColumn column, Set<Ref> relatedTo, Set<Ref> refer
SafeHtml safeHtml;
String template = columnStatus.getDetailsStatus().getTemplateStatus().getTemplate();
if (template != null && !template.isEmpty()) {
String json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_downloadLOB(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK, RestUtils.createExportLobUri(database.getUuid(),
table.getSchemaName(), table.getName(), row.getUuid(), columnStatus.getColumnIndex()));
String json = "";
if (ClientConfigurationManager.getBoolean(false, ViewerConstants.VIEWER_ENABLED)) {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_openLOBViewer(),
ViewerConstants.TEMPLATE_UV_LINK, RestUtils.createUVLob(), ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK,
RestUtils.createExportLobUri(database.getUuid(), table.getSchemaName(), table.getName(), row.getUuid(),
columnStatus.getColumnIndex()));
} else {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_downloadLOB(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK, RestUtils.createExportLobUri(database.getUuid(),
table.getSchemaName(), table.getName(), row.getUuid(), columnStatus.getColumnIndex()));
}
safeHtml = SafeHtmlUtils.fromSafeConstant(JavascriptUtils.compileTemplate(template, json));
rowField = RowField.createInstance(label, new HTML(safeHtml));
} else {
Expand Down Expand Up @@ -394,7 +410,6 @@ private void getCellHTML(ViewerColumn column, Set<Ref> relatedTo, Set<Ref> refer

private void getNestedHTML(ColumnStatus columnStatus) {
NestedColumnStatus nestedColumns = columnStatus.getNestedColumns();

if (nestedColumns != null) {
ViewerTable nestedTable = database.getMetadata().getTableById(nestedColumns.getOriginalTable());

Expand All @@ -421,11 +436,49 @@ private void getNestedHTML(ColumnStatus columnStatus) {
null, false, new ArrayList<>());
CollectionService.Util.call((IndexResult<ViewerRow> result) -> {
if (result.getTotalCount() >= 1) {
RowField rowField;
String json = JSOUtils.cellsToJson(result.getResults().get(0).getCells(), nestedTable);
String s = JavascriptUtils.compileTemplate(template, json);
RowField rowField = RowField.createInstance(columnStatus.getCustomName(), new Label(s));
rowField.addColumnDescription(columnStatus.getCustomDescription());
if (columnStatus.getTypeName().contains("BINARY LARGE OBJECT")) {
String templateLob = "<a href=\"{{download_link}}\">{{download_label}}</a>";
int originalCollumnIndex = 0;

// loop to find the original column index
for (Map.Entry<String, ViewerCell> entry : result.getResults().get(0).getCells().entrySet()) {
ViewerCell v = entry.getValue();
if (v.getStoreType() != null)
break;
originalCollumnIndex++;
}

if ((database.getPath() == null || database.getPath().isEmpty())
&& !status.getConsolidateProperty().equals(LargeObjectConsolidateProperty.CONSOLIDATED)) {
rowField = RowField.createInstance(new Label(s).getText(),
new HTML(messages.rowPanelTextForLobUnavailable()));
} else {
SafeHtml safeHtml = SafeHtmlUtils.EMPTY_SAFE_HTML;
if (ClientConfigurationManager.getBoolean(false, ViewerConstants.VIEWER_ENABLED)) {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_openLOBViewer(),
ViewerConstants.TEMPLATE_UV_LINK, RestUtils.createUVLob(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK,
RestUtils.createExportLobUri(database.getUuid(), nestedTable.getSchemaName(),
nestedTable.getName(), result.getResults().get(0).getUuid(), originalCollumnIndex));
} else {
json = JSOUtils.cellsToJson(ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LABEL, messages.row_downloadLOB(),
ViewerConstants.TEMPLATE_LOB_DOWNLOAD_LINK,
RestUtils.createExportLobUri(database.getUuid(), nestedTable.getSchemaName(),
nestedTable.getName(), result.getResults().get(0).getUuid(), originalCollumnIndex));
}

safeHtml = SafeHtmlUtils.fromSafeConstant(JavascriptUtils.compileTemplate(templateLob, json));

rowField = RowField.createInstance(columnStatus.getCustomName(), new HTML(safeHtml));
}
} else {
rowField = RowField.createInstance(columnStatus.getCustomName(), new Label(s));
}

rowField.addColumnDescription(columnStatus.getCustomDescription());
panel.add(rowField);
}
}).findRows(database.getUuid(), database.getUuid(), nestedTable.getSchemaName(), nestedTable.getName(),
Expand Down
Loading

0 comments on commit 066fcf6

Please sign in to comment.