Skip to main content

Views

Info

A view is a relational database view generated from a definition (CaseViewDefinition) that lists fields of case types. The service lets you generate views, check their state and read their rows in pages with filtering and sorting. The rows can also be previewed in the tabular model of the Iron POI server (Excel) and exported to an Excel file. Supported databases: PostgreSQL, Oracle, MySQL and Microsoft SQL Server.

View definition and generated view​

A view definition (CaseViewDefinition) has a name, a list of type fields (metadataInfo), a date range for the type versions, the credentials mode (AND/OR), the flag skipHeaderFields and optionally the list of base type codes (baseTypeCodes). The name of the definition is the name of the view in the database.

The system also generates a view for every case type. Its name is T_<type code>.

The generated view always has the columns:

ColumnDescription
m_idCase identifier
type_codeCode of the case type of the row
M_*Header fields of the case (for example M_CREATE_DATE). Omitted when skipHeaderFields is true
field columnsOne column per field from the definition. The column has the name of the field. A field of a dependent case has the name <field of the dependent case>_<field>. A field kept at several positions gets numbered columns (PHONE, PHONE1)

A column of a field that exists in several case types takes its value from the field of the type of the row. The value of a field that a type does not have is NULL. The case type is a column of every row, so the permissions are applied per row (see below).

The case fields are stored as text, so the columns of fields have a text type (varchar in PostgreSQL), and the filters on them compare text. The column type is visible in sqlType and mercuryType.

Operations​

The operations belong to the CaseViewDefinition service (SOAP: ICaseViewDefinitionAction, REST: /rest/service/CaseViewDefinitionRest/{operation}). Every request has the context parameter (see SOAP/REST service request context).

OperationDescriptionRequired permission
createView, updateViewCreate or change the definition of a viewRead permission to all case types
generateViewGenerate the view in the database from the definition. The generation is asynchronousRead permission to all case types
dropViewRemove the view from the database (the definition stays)Read permission to all case types
findViewInfosList of views with their statusRead permission to at least one type of the view
getViewInfoInformation about one view with its columnsRead permission to at least one type of the view
readViewDataA page of rows of a viewRead permission to at least one type of the view

View information​

getViewInfo and findViewInfos return ViewInfo:

FieldDescription
nameName of the view (and of the definition)
statusVALID: the view exists and can be read. INVALID: the view exists, but it cannot be read (for example it depends on a removed table). MISSING: the view does not exist in the database
generatedVersion, publishVersionThe version of the definition used to generate the view and the current version of the definition. When they differ, the view does not match the definition
generationErrorReason why the last generation failed. Empty when the last generation succeeded
columnsColumns of the view (getViewInfo only)

A column (ViewColumnDefinition) has: name, position, sqlType, mercuryType (STRING, INTEGER, DECIMAL, BOOLEAN, DATE or OTHER), nullable and typeFieldCode. typeFieldCode is the code of the field from the definition (for a dependent case <field of the dependent case>.<field>). It is empty for the columns m_id, type_code and M_*.

Reading rows​

readViewData takes the parameter query (ViewQuery):

FieldDescription
viewNameName of the view
pageNumberPage number, counted from 1 (default 1)
pageSizePage size (default 100, maximum 1000, the maximum is set by the parameter mercury.view.maxPageSize)
filtersList of conditions joined with AND: column, operator and value
sortingList of sorting criteria: column and direction (ASC or DESC). Without sorting the rows are sorted by the first column, so paging is stable
countModeEXACT (default) returns the number of all rows and the paging information. NONE skips counting and returns only hasNext and nextKey (see Reading without counting)
afterKeyThe key of the last row of the previous page (nextKey of the response). Reads the next page by key, without counting rows and without custom sorting
useSnapshottrue: read from a snapshot of keys kept in the cluster cache. The response contains snapshotToken
snapshotTokenThe token of the snapshot from the response to the first request with useSnapshot. The following pages (any number) are read from the snapshot

Filter operators: EQ, NE, LT, LE, GT, GE, LIKE (text columns only, case-insensitive), IS_NULL and IS_NOT_NULL. The value is converted to the type of the column. A column that is not in the view and a value that does not match the type of the column are rejected. The values of filters are passed to the database as query parameters. The names of columns are matched ignoring the case, and the result has the names as the database stores them (in PostgreSQL in lower case). The query time is limited by the parameter mercury.view.queryTimeoutMs (60000 ms by default).

The result (ViewDataPage) contains viewName, columns, rows (every row is a list of texts in the order of the columns), resultSize (the number of rows after filtering and applying the permissions) and the paging information as in the other services (see Paged result).

Reading without counting and key snapshot​

By default every request counts all the rows (COUNT(*)) and skips the rows of the previous pages (OFFSET), so on a large view the cost of a page grows with its number. Two optional modes avoid it. A client that does not use them gets the same result as before.

  • Reading by key (countMode: NONE or afterKey). The rows are read in the order of the view key (the column M_ID, the case identifier; for a view without this column the first column) and a page starts after the key afterKey. The cost of a page does not depend on how deep it is. The response has no resultSize and no page numbers, but it has hasNext (are there more rows) and nextKey (the key to put into afterKey of the next request). The first page is the request with countMode NONE and without afterKey. Custom sorting is not allowed in this mode (the order is set by the key), filters are allowed. Rows added during the reading are included, removed rows do not shift the pages.
  • Snapshot of keys (useSnapshot). The first request (with pageNumber, filters and sorting) reads the ordered list of the keys of the rows, keeps it in the cluster cache and returns the page together with snapshotToken. The next requests send the same query with snapshotToken and any pageNumber: the page is the slice of the list and the rows are read by their keys (the values are current, the set and the order of the rows come from the snapshot). resultSize comes from the snapshot, without a separate COUNT. A token is bound to the user and to the query (view, filters, sorting and the permissions). An expired or unknown token causes the query to be executed again and a new token to be issued, without an error. The snapshot has at most mercury.view.snapshot.maxKeys keys (50000 by default); a longer result sets truncated to true, the pages beyond the limit are empty and the rest is read by key (nextKey). The snapshot is valid for mercury.view.snapshot.ttl seconds (300 by default).

The response (ViewDataPage) has the fields nextKey, hasNext and snapshotToken for these modes.

Example of a request to the REST service:

{
"context": { "userName": "jan", "userRoles": ["mrc-user"] },
"query": {
"viewName": "T_SlmAddress",
"pageNumber": 1,
"pageSize": 50,
"filters": [ { "column": "city", "operator": "LIKE", "value": "Poz%" } ],
"sorting": [ { "column": "street", "direction": "ASC" } ]
}
}

Preview in the Excel model​

The REST operation POST /rest/service/CaseViewExtRest/readViewExcelData returns a page of the view rows in the tabular model used by the Iron POI server (the same model as the aggregations of the CaseSearchExt service). It is meant for previewing the data before the export. There is no SOAP equivalent.

The request has the parameters context and query. The query is the same ViewQuery as in readViewData (view name, page number and size, filters and sorting), and the permissions are the same too.

The response is tabular data, not an envelope with a status (an error is returned as an envelope with errorCode and errorMessage):

FieldDescription
headerNames of the columns of the view. The columns are numbered with Excel letters (A, B, C, ...) in the order of the columns of the view
columnTypesTypes of the columns: STRING, LONG, DOUBLE or DATE
resultRows of the current page. A row has the cells by the column letters and rowIndx, the number of the row in the whole result. A NULL value has no cell
resultSize, messageThe number of rows after filtering and applying the permissions, and ALL, FRAGMENT (more pages exist) or NO_DATA_FOUND
currentPageInfo, firstPageInfo, previousPageInfo, nextPageInfo, lastPageInfoPaging information (size and number of the page)

The text values are presented as in the export of cases to an Excel file: a value of a field is read according to the definition of this field in the case type of the row (the column type_code), so dictionary items and boolean values get their presentation (for example true is tak for a Polish user), and a value stored outside the row (a long text kept as LOB) is read in full. The numbers and the dates are written in the form accepted by the Iron POI server, so that the column keeps its numeric or date type: a number in the canonical form (a dot as the decimal separator, no thousand separators), a date in the date format of the context (in the export the format of the Iron POI server). The masked value ########### stays unchanged in a text column and gives an empty cell in a numeric or date column. The values of the fields that the type of the row does not have and the fields of the file type (LOB) stay unchanged.

The type of a column comes from the attribute type of the field in the case types of the view (the types listed in baseTypeCodes, and without this list the types that have the fields of the definition): STRING, LONG (integer), DOUBLE (number, currency) or DATE. When the field has different attribute types in these types, the column is text. The columns that are not fields keep the values of the database: the header columns M_* that are dates are written as dates, the numbers as numbers, and the identifiers stay identifiers (for example M_CREATED_BY is the identifier of the user, not his name).

{
"context": { "userName": "jan", "userRoles": ["mrc-user"] },
"query": { "viewName": "T_SlmAddress", "pageNumber": 1, "pageSize": 20 }
}

Export to an Excel file​

A view can be exported to a file in the same way as the cases of a type. The request is the operation prepareReportRequest of the ReportExportStatusRest service (/rest/service/ReportExportStatusRest/prepareReportRequest) with the parameter viewName instead of typeCode. The other parameters are the same as for the export of cases: subject, exportFileName (by default <view name>.xlsx), templateFileName, sheetName, dfrn, dfc, dlc, hrn and ps. The parameters of the cases (luceneQuery, createDateFrom, createDateTo and caseStatus) are not used.

PropertyDescription
ScopeThe whole view, without filters and sorting, within the permissions of the user from the context: the rows of the types without the permissions are skipped and the values of the fields without the permissions are masked (###########)
Page size psThe number of rows read and written at once, 1000 by default and at most mercury.view.maxPageSize. A larger value fails the export
ReadingThe export reads the view by key (see above), in short independent transactions, so a change of data during the export does not shift the pages and the export does not keep a database connection for its whole time
Format formatxlsx (default) or csv. The CSV file is UTF-8 with a byte order mark and the separator csvSeparator (one character, ; by default). Another value of format fails the request
ReportreportName is the name of the view, and params starts with the prefix [VIEW], which distinguishes the export of a view from the export of cases, followed by vName=<view name> and the parameters of the file
ValuesPresented as in the preview (see above): text as in the export of cases, numbers and dates in the form accepted by the Iron POI server, a masked value in a numeric or date column as an empty cell

The export is asynchronous. The operation loadReportStatus returns the status of the report (WAITING, PROCESSING, COMPLETED or FAILED) with the progress. After COMPLETED the field downloadUrl contains the address of the file on the Iron POI server (the beginning of the address is set by the parameter excel.server.docUrl). Exporting a very large view can take many minutes.

{
"context": { "userName": "jan", "userRoles": ["mrc-user"] },
"viewName": "T_SlmAddress",
"subject": "Addresses",
"exportFileName": "addresses.xlsx",
"ps": 1000
}

Permissions​

The permissions are the same as for cases (see Permissions mechanism):

  1. A user who can read all the case types sees all the rows and all the values.
  2. The other users see only the rows of the case types that they can read. The rows of the other types are skipped by a condition on type_code in the query, so the number of rows and the paging take the permissions into account. A view without the column type_code is not available for such users.
  3. The value of a field that the user cannot read (the type, the field name and the position of the field are checked) is replaced with ###########. The column of a field of a dependent case is masked in all the rows when the user cannot read this field in any type.

The context must pass the verification of the user and roles described in the article about the permissions mechanism.

Generating and changing views​

The generation is asynchronous, so a call of generateView finishes before the view is ready. The result of the generation is visible in getViewInfo: generatedVersion equals publishVersion and generationError is empty after a successful generation.

When the definition changes, the view is changed in place (CREATE OR REPLACE VIEW, in SQL Server CREATE OR ALTER VIEW), so the view does not disappear and the views that depend on it are not touched. The database accepts this only when the existing columns keep their order and types and the new columns are added at the end. For this reason the columns of fields that are already in the view keep their places, and the new ones are added at the end in alphabetical order. When the database rejects the change (in PostgreSQL after removing or changing a column), the view is removed and created again.

PostgreSQL does not allow removing a view on which other views depend. In this case the view is not changed, and generationError contains the list of the dependent views. Change or remove these views, or keep the existing columns, and generate the view again. The other supported databases allow removing such a view, and the dependent views become invalid (INVALID).

In PostgreSQL the waiting for a lock on a view is limited. The parameter mercury.view.lockTimeoutMs (5000 ms by default) sets the time, and mercury.view.ddlRetries (3 by default) sets the number of retries after exceeding it. After the last retry the generation fails with the information that the view is used by another session.

If the table of cases is partitioned by the case identifier (PostgreSQL, scripts db/postgresql), the generated view also contains a range of the case identifier equal to the bounds of the partition of the case type. The database then reads only this partition. The condition is added only when the table is partitioned and every case type of the view has a partition. When cases of the type exist outside the bounds of its partition (for example moved to a type with another code), the condition is not added, and the view reads all partitions. A view generated earlier does not change until it is generated again.