Preferences System

This module works only for Ontimize Boot version 3.9.0 or above. Actual release version: Ontimize Boot

Introduction

The Ontimize preferences allow you to save the settings that are stored in the localstorage (window size, tables preferences, graphics, or reports) into the database.

Prerequisites

You can follow this tutorial using your own application, although for this example we will use an application created using the archetype that can be found on this page and with a REST service.

There are 2 options to follow this tutorial, clone the repository with the initial state and follow the tutorial step by step, or download the final example and see which files are new and which have been updated.

Initial project

/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-preferences-initial

Final example

/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-preferences

To simplify the code being written, three dots (…) may appear in some parts of the code. This indicates that there may be previous code before and after those dots.

Steps

Database

Configurations Table

With the database started, we create the new table that will store the configuration preferences information.

CREATE TABLE TCONFIGS ( ID_CONFIG INTEGER NOT NULL IDENTITY, USER_CONFIG VARCHAR(255), TYPE_CONFIG VARCHAR(255), COMPONENTS VARCHAR(16777216));

Server

Configuration Dao

A specific DAO will be created for the table in the Preferences system, and it will implement a interface.

ConfigsDao.java

package com.ontimize.projectwiki.model.core.dao;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.ontimize.jee.server.dao.common.ConfigurationFile;
import com.ontimize.jee.server.dao.jdbc.OntimizeJdbcDaoSupport;

@Lazy
@Repository(value = "ConfigsDao")
@ConfigurationFile(
	configurationFile = "dao/ConfigsDao.xml",
	configurationFilePlaceholder = "dao/placeholders.properties")
public class ConfigsDao extends OntimizeJdbcDaoSupport {

	public static final String ID   	  = "ID_CONFIG";
	public static final String USER 	  = "USER_CONFIG";
	public static final String TYPE  	  = "TYPE_CONFIG";
	public static final String COMPONENTS = "COMPONENTS";

}

ConfigsDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<JdbcEntitySetup
    xmlns="http://www.ontimize.com/schema/jdbc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.ontimize.com/schema/jdbc http://www.ontimize.com/schema/jdbc/ontimize-jdbc-dao.xsd"
    catalog="" schema="${mainschema}" table="TCONFIGS"
    datasource="mainDataSource" sqlhandler="dbSQLStatementHandler">
    <DeleteKeys>
        <Column>ID_CONFIG</Column>
    </DeleteKeys>
    <UpdateKeys>
        <Column>ID_CONFIG</Column>
    </UpdateKeys>
</JdbcEntitySetup>

Add parameters to Application YML

As has already been explained previously (in this link) we add the following parameters to the application.yml to define the name of the DAO of the preferences system and to activate autoconfiguration.

application.yml

ontimize:
   save-config: true
   save-config-dao: ConfigsDao

Testing the preferences system

Once the preferences system is already configured and the server and the database are running, we will follow the next steps:

Add preferences to database

To add or modify preferences to the database, we will execute the following REST Request: http://localhost:33333/configuration/preferences

The type of the request is POST.

Element Meaning
localhost:33333 Indicates the host
/configuration Indicates the service
/preferences Indicates the type of configuration that you’re going to save. You can write the name you want.

The body of the request needs to have the following structure:

{
  "user":"demo",
  "components":{
    "lang":"es",
    ...
    "theme":{"primary":"#242424","accent":"#ffcc00","href":"ontimize-black-yellow.css","href_dark":"ontimize-black-yellow-dark.css","isDefault":true,"isDark":false},
  }
}

By default the request will create a new entry in the database. If it already exists, it will be modified.

Query preferences

To query preferences of the database, we will execute the following REST Request: http://localhost:33333/configuration/preferences?user=demo

The type of the request is GET.

Element Meaning
localhost:33333 Indicates the host.
/configuration Indicates the service to be queried.
/preferences Indicates the type of configuration that will be queried.
?user=demo Indicates the user.