Skip to content

EN Plagin

roosit edited this page May 12, 2026 · 2 revisions

Plagin

Up | Next | Русский

Full source with brief explanations

Below is the full source of the class in documentation form. Comments inside the block explain the purpose of the class parts, but they are not stored in the working file Plagin.java: production code stays clean, and the explanations live in the wiki.

package com.velitask.plugin.example;

import com.velitask.sdk.IFileSource;
import com.velitask.sdk.IIndicator;
import com.velitask.sdk.IPlagin;
import com.velitask.sdk.db.PluginDatabase;

/*
 * Entry point of the example plugin. Velitask loads the class by the
 * Velitask-Plugin-Class attribute from the built jar manifest and obtains
 * all required plugin metadata through the IPlagin interface.
 */
public class Plagin implements IPlagin {

    /*
     * Stable unique plugin identifier.
     * Velitask uses it for registration, conflict resolution,
     * and links from saved projects.
     */
    public static final String UID = "com.velitask.plugin.example";

    /*
     * Base prefix for this plugin's localization keys.
     * All strings in strings.properties start with PluginExample.
     */
    public static final String KEY = "PluginExample";

    /* Prefix for all plugin indicator keys: PluginExample.Indicator. */
    public static final String KEY_INDICATOR = KEY + ".Indicator";

    /*
     * Returns the plugin UID. It does not change between versions; otherwise
     * saved projects will lose links to the plugin and its indicators.
     */
    @Override
    public String getUID() {
        return UID;
    }

    /*
     * Plugin version in semver format. Used for the settings UI
     * and compatibility checks with projects.
     */
    @Override
    public String getVersion() {
        return "1.0.0";
    }

    /*
     * Plugin DB schema version = 1. SDK executes sql/setup/create.sql
     * on first installation and sql/setup/upgrade-N.sql on bump to N.
     * See resources/sql/setup/create.sql.
     */
    @Override
    public int getDbVersion() {
        return 1;
    }

    /*
     * Additional locales (besides the UI locale) guarantee that the
     * Russian bundle is loaded even if the UI runs with the English locale.
     * Names correspond to suffixes of strings_<locale>.properties files.
     */
    @Override
    public String[] defineAdditionLocales() {
        return new String[] { "ru" };
    }

    /*
     * Returns the list of plugin indicators in the order they appear
     * in the picker. This template has one indicator: Speedometer.
     * Add new ones to this array for your own plugin.
     */
    @Override
    public IIndicator[] defineIndicators() {
        return new IIndicator[] {
                new SpeedometerIndicator()
        };
    }

    /*
     * Source import hook: called by the SDK once for each imported source,
     * single-threaded. In the template, this is a PluginDatabase API smoke test:
     * write a "source X processed at moment Y" record to our import_log table.
     * INSERT OR REPLACE provides idempotency by source_id.
     */
    @Override
    public void onSourceImported(IFileSource source, PluginDatabase db) {
        if (db == null || source == null) {
            return;
        }
        db.execute(
                "INSERT OR REPLACE INTO ${table:import_log}"
                + " (source_id, source_path, processed_at) VALUES (?, ?, ?)",
                source.getId(),
                source.getRelativePath(),
                System.currentTimeMillis()
        );
    }
}

Purpose

Plagin is the entry point of the example plugin. It implements the IPlagin contract: fixes the uid and version, guarantees loading of the Russian localization bundle, registers the only example indicator (Speedometer), and, on import of each source, writes a smoke record to its own DB (a PluginDatabase API demonstration).

When forking the template, you must change: UID, KEY (localization prefix), and Velitask-Plugin-Class in build.gradle. The package and class name can be kept or renamed; this does not affect UID/KEY.

How it works

Registration and identification. Returns the stable uid com.velitask.plugin.example (used in all saved projects and DB metadata; must not be changed), the version (manually from build.gradle), an array with one IIndicator, and defineAdditionLocales() with the "ru" list so the Russian bundle is loaded even with the English UI locale.

Plugin-owned DB. getDbVersion() = 1: the runtime SDK executes sql/setup/create.sql on first plugin installation into a project; when the version is bumped, the corresponding upgrade-N.sql is run. The only table in the template is import_log (source_id, source_path, processed_at); it is only needed as a PluginDatabase API smoke test, with no business logic.

Source import hook. onSourceImported(source, db) is called by the SDK single-threaded for each imported source. In the template, it performs INSERT OR REPLACE into import_log (idempotent by source_id). This is a contract demonstration for the hook; in a production plugin, real analytics usually goes here (see VelitaskPlagin.onSourceImported: slope groups).

Details and limitations

  • Velitask-Plugin-Class in the manifest is hard-coded in build.gradle. When renaming the package or class, update the manifest too.
  • KEY = "PluginExample" does not match the UID intentionally: the UID is long (reverse-domain), while localization keys are better kept short and readable. When forking, remember to change both uid and KEY.
  • onSourceImported uses the ${table:import_log} placeholder: runtime substitutes the table name with the plugin prefix to avoid collisions with other plugins.
  • No inner classes: Plagin is a clean IPlagin implementation, without helper types nearby.

Clone this wiki locally