All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tentackle.dbms.prefs.DbPreferencesKey Maven / Gradle / Ivy

There is a newer version: 21.16.1.0
Show newest version
/*
 * Tentackle - https://tentackle.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */



package org.tentackle.dbms.prefs;

import org.tentackle.dbms.AbstractDbObject;
import org.tentackle.dbms.Db;
import org.tentackle.dbms.DbObjectClassVariables;
import org.tentackle.dbms.PreparedStatementWrapper;
import org.tentackle.dbms.ResultSetWrapper;
import org.tentackle.dbms.StatementId;
import org.tentackle.misc.TrackedArrayList;
import org.tentackle.session.ClassId;
import org.tentackle.session.Persistent;
import org.tentackle.session.TableName;
import org.tentackle.sql.Backend;

import java.io.Serial;
import java.util.Objects;



/*
 * @> $mapfile
 *
 * # a preferences key/value pair
 * name := $classname
 * id := $classid
 * table := $tablename
 *
 * ## attributes
 * [tableserial, tracked]
 *
 * long           nodeId          nodeid          ID of the preferences-node
 * long           rootNodeId      rootnodeid      ID of the root-node
 * String(128)    key             pkey            name of the key
 * String         value           pvalue          value of the key
 *
 * ## indexes
 * unique index path := nodeid, pkey
 * index tableserial := tableserial
 * index root := rootnodeid
 *
 * @<
 */



/**
 * A preferences key/value pair stored in the database.
 *
 * @author harald
 */
@ClassId(/**/4/**/)               // @wurblet < Inject $classid
@TableName(/**/"prefkey"/**/)     // @wurblet < Inject --string $tablename
public class DbPreferencesKey extends AbstractDbObject {

  @Serial
  private static final long serialVersionUID = 1L;

  /** Variables common to all instances of {@link DbPreferencesKey}. */
  public static final DbObjectClassVariables CLASSVARIABLES =
      DbObjectClassVariables.create(DbPreferencesKey.class);


  private boolean removed;                        // true if marked deleted (used by factory)


  /**
   * Creates a preferences key.
   *
   * @param db the session
   */
  public DbPreferencesKey (Db db)    {
    super(db);
  }

  /**
   * Creates a preferences key without a session.
   */
  public DbPreferencesKey() {
    super();
  }


  /**
   * Returns whether key has been marked as removed.
   *
   * @return true if removed
   */
  public boolean isRemoved() {
    return removed;
  }

  /**
   * Sets the removed flag.
   *
   * @param removed true if removed
   */
  public void setRemoved(boolean removed) {
    this.removed = removed;
  }


  @Override
  public String toString()  {
    return "nodeId=" + nodeId + ", key='" + key + "', value='" + value + "'";
  }



  @Override
  public DbObjectClassVariables getClassVariables() {
    return CLASSVARIABLES;
  }


  /**
   * Gets all keys belonging to a root node and all its sub nodes sorted by id.
   *
   * @param rootNodeId the root's ID
   * @return List of keys
   *
   * @wurblet selectByRootNodeId DbSelectList --model=$mapfile rootNodeId | +id
   */
  ////GEN-BEGIN:selectByRootNodeId

  public TrackedArrayList selectByRootNodeId(long rootNodeId) {
    Db db = getSession();
    PreparedStatementWrapper st = getPreparedStatement(SELECT_BY_ROOT_NODE_ID_STMT,
      b -> {
        StringBuilder sql = createSelectAllInnerSql(b);
        sql.append(Backend.SQL_AND);
        sql.append(CN_ROOTNODEID);
        sql.append(Backend.SQL_EQUAL_PAR);
        sql.append(Backend.SQL_ORDERBY)
           .append(CN_ID).append(Backend.SQL_SORTASC);
        b.buildSelectSql(sql, false, 0, 0);
        return sql.toString();
      }
    );
    int ndx = 1;
    st.setLong(ndx, rootNodeId);
    try (ResultSetWrapper rs = st.executeQuery()) {
      TrackedArrayList list = new TrackedArrayList<>();
      boolean derived = getClass() != DbPreferencesKey.class;
      while (rs.next()) {
        DbPreferencesKey obj = derived ? newInstance() : new DbPreferencesKey(db);
        list.add(obj.readFromResultSetWrapper(rs));
      }
      list.setModified(false);
      return list;
    }
  }

  private static final StatementId SELECT_BY_ROOT_NODE_ID_STMT = new StatementId();


  ////GEN-END:selectByRootNodeId



  /**
   * Gets all keys belonging to a node.
   *
   * @param nodeId the node's ID
   * @return List of keys
   *
   * @wurblet selectByNodeId DbSelectList --model=$mapfile nodeId
   */
  ////GEN-BEGIN:selectByNodeId

  public TrackedArrayList selectByNodeId(long nodeId) {
    Db db = getSession();
    PreparedStatementWrapper st = getPreparedStatement(SELECT_BY_NODE_ID_STMT,
      b -> {
        StringBuilder sql = createSelectAllInnerSql(b);
        sql.append(Backend.SQL_AND);
        sql.append(CN_NODEID);
        sql.append(Backend.SQL_EQUAL_PAR);
        b.buildSelectSql(sql, false, 0, 0);
        return sql.toString();
      }
    );
    int ndx = 1;
    st.setLong(ndx, nodeId);
    try (ResultSetWrapper rs = st.executeQuery()) {
      TrackedArrayList list = new TrackedArrayList<>();
      boolean derived = getClass() != DbPreferencesKey.class;
      while (rs.next()) {
        DbPreferencesKey obj = derived ? newInstance() : new DbPreferencesKey(db);
        list.add(obj.readFromResultSetWrapper(rs));
      }
      list.setModified(false);
      return list;
    }
  }

  private static final StatementId SELECT_BY_NODE_ID_STMT = new StatementId();


  ////GEN-END:selectByNodeId


  /**
   * Gets the prefkey belonging to a node for a given key.
   *
   * @param nodeId the node's ID
   * @param key the prefkey's key
   * @return the prefkey
   *
   * @wurblet selectByNodeIdAndKey DbSelectUnique --model=$mapfile nodeId key
   */
  ////GEN-BEGIN:selectByNodeIdAndKey

  public DbPreferencesKey selectByNodeIdAndKey(long nodeId, String key) {
    PreparedStatementWrapper st = getPreparedStatement(SELECT_BY_NODE_ID_AND_KEY_STMT,
      b -> {
        StringBuilder sql = createSelectAllInnerSql(b);
        sql.append(Backend.SQL_AND);
        sql.append(CN_NODEID);
        sql.append(Backend.SQL_EQUAL_PAR);
        sql.append(Backend.SQL_AND);
        sql.append(CN_KEY);
        sql.append(Backend.SQL_EQUAL_PAR);
        b.buildSelectSql(sql, false, 0, 0);
        return sql.toString();
      }
    );
    int ndx = 1;
    st.setLong(ndx++, nodeId);
    st.setString(ndx, key);
    try (ResultSetWrapper rs = st.executeQuery()) {
      if (rs.next()) {
        return readFromResultSetWrapper(rs);
      }
      return null;  // not found
    }
  }

  private static final StatementId SELECT_BY_NODE_ID_AND_KEY_STMT = new StatementId();


  ////GEN-END:selectByNodeIdAndKey


  // @wurblet methods MethodsImpl --tracked --noif --model=$mapfile

  ////GEN-BEGIN:methods


  @Override
  public boolean isTableSerialProvided() {
    return true;
  }

  @Override
  public boolean isTracked() {
    return true;
  }

  @Override
  public void getFields(ResultSetWrapper rs) {
    super.getFields(rs);
    if (rs.configureSection(CLASSVARIABLES)) {
      rs.configureColumn(CN_TABLESERIAL);
      rs.configureColumn(CN_NODEID);
      rs.configureColumn(CN_ROOTNODEID);
      rs.configureColumn(CN_KEY);
      rs.configureColumn(CN_VALUE);
      rs.configureColumn(CN_ID);
      rs.configureColumn(CN_SERIAL);
    }
    setTableSerial(rs.getLong());
    nodeId = rs.getLong();
    rootNodeId = rs.getLong();
    key = rs.getString();
    value = rs.getString();
    setId(rs.getLong());
    setSerial(rs.getLong());
  }

  @Override
  public int setFields(PreparedStatementWrapper st) {
    int ndx = super.setFields(st);
    st.setLong(++ndx, getTableSerial());
    st.setLong(++ndx, nodeId);
    st.setLong(++ndx, rootNodeId);
    st.setString(++ndx, key);
    st.setString(++ndx, value);
    st.setLong(++ndx, getId());
    st.setLong(++ndx, getSerial());
    return ndx;
  }

  @Override
  public String createInsertSql(Backend backend) {
    return Backend.SQL_INSERT_INTO + getTableName() + Backend.SQL_LEFT_PARENTHESIS +
           CN_TABLESERIAL + Backend.SQL_COMMA +
           CN_NODEID + Backend.SQL_COMMA +
           CN_ROOTNODEID + Backend.SQL_COMMA +
           CN_KEY + Backend.SQL_COMMA +
           CN_VALUE + Backend.SQL_COMMA +
           CN_ID + Backend.SQL_COMMA +
           CN_SERIAL +
           Backend.SQL_INSERT_VALUES +
           Backend.SQL_PAR_COMMA.repeat(6) +
           Backend.SQL_PAR + Backend.SQL_RIGHT_PARENTHESIS;
  }

  @Override
  public String createUpdateSql(Backend backend) {
    return Backend.SQL_UPDATE + getTableName() + Backend.SQL_SET +
           CN_TABLESERIAL + Backend.SQL_EQUAL_PAR_COMMA +
           CN_NODEID + Backend.SQL_EQUAL_PAR_COMMA +
           CN_ROOTNODEID + Backend.SQL_EQUAL_PAR_COMMA +
           CN_KEY + Backend.SQL_EQUAL_PAR_COMMA +
           CN_VALUE + Backend.SQL_EQUAL_PAR_COMMA +
           CN_SERIAL + Backend.SQL_EQUAL + CN_SERIAL + Backend.SQL_PLUS_ONE +
           Backend.SQL_WHERE + CN_ID + Backend.SQL_EQUAL_PAR +
           Backend.SQL_AND + CN_SERIAL + Backend.SQL_EQUAL_PAR;
  }

  /**
   * Gets the attribute nodeId.
   *
   * @return ID of the preferences-node
   */
  @Persistent(ordinal=1, comment="ID of the preferences-node")
  public long getNodeId()    {
    return nodeId;
  }

  /**
   * Sets the attribute nodeId.
   *
   * @param nodeId ID of the preferences-node
   */
  public void setNodeId(long nodeId) {
    if (this.nodeId != nodeId) {
      setModified(true);
      this.nodeId = nodeId;
    }
  }

  /**
   * Gets the attribute rootNodeId.
   *
   * @return ID of the root-node
   */
  @Persistent(ordinal=2, comment="ID of the root-node")
  public long getRootNodeId()    {
    return rootNodeId;
  }

  /**
   * Sets the attribute rootNodeId.
   *
   * @param rootNodeId ID of the root-node
   */
  public void setRootNodeId(long rootNodeId) {
    if (this.rootNodeId != rootNodeId) {
      setModified(true);
      this.rootNodeId = rootNodeId;
    }
  }

  /**
   * Gets the attribute key.
   *
   * @return name of the key
   */
  @Persistent(ordinal=3, comment="name of the key")
  public String getKey()    {
    return key;
  }

  /**
   * Sets the attribute key.
   *
   * @param key name of the key
   */
  public void setKey(String key) {
    if (!Objects.equals(this.key, key)) {
      setModified(true);
      this.key = key;
    }
  }

  /**
   * Gets the attribute value.
   *
   * @return value of the key
   */
  @Persistent(ordinal=4, comment="value of the key")
  public String getValue()    {
    return value;
  }

  /**
   * Sets the attribute value.
   *
   * @param value value of the key
   */
  public void setValue(String value) {
    if (!Objects.equals(this.value, value)) {
      setModified(true);
      this.value = value;
    }
  }

  ////GEN-END:methods


  // record members
  // @wurblet declare Declare --model=$mapfile

  ////GEN-BEGIN:declare


  /** ID of the preferences-node. */
  private long nodeId;

  /** ID of the root-node. */
  private long rootNodeId;

  /** name of the key. */
  private String key;

  /** value of the key. */
  private String value;

  ////GEN-END:declare


  // @wurblet fieldNames ColumnNames --model=$mapfile

  ////GEN-BEGIN:fieldNames


  /** database column name for 'nodeId'. */
  public static final String CN_NODEID = "nodeid";

  /** database column name for 'rootNodeId'. */
  public static final String CN_ROOTNODEID = "rootnodeid";

  /** database column name for 'key'. */
  public static final String CN_KEY = "pkey";

  /** database column name for 'value'. */
  public static final String CN_VALUE = "pvalue";

  ////GEN-END:fieldNames

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy