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

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

/*
 * 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.TableName;
import org.tentackle.sql.Backend;

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



/*
 * @> $mapfile
 *
 * # a preferences node
 * name := $classname
 * id := $classid
 * table := $tablename
 *
 * ## attributes
 * [tableserial, tracked]
 *
 * String(32)     user            username        name of user, null if system [mapnull]
 * String(192)    name            nodename        name of the node
 * long           parentId        parentid        ID of parent-node, 0 if root-node
 * long           rootNodeId      rootnodeid      ID of the root-node
 *
 * ## indexes
 * unique index nodename := nodename, username
 * index parentid := parentid
 * index tableserial := tableserial
 * index root := rootnodeid
 *
 * @<
 */


/**
 * Preferences Node stored in the database.
 *
 * @author harald
 */
@ClassId(/**/3/**/)               // @wurblet < Inject $classid
@TableName(/**/"prefnode"/**/)    // @wurblet < Inject --string $tablename
public class DbPreferencesNode extends AbstractDbObject {

  @Serial
  private static final long serialVersionUID = 1L;

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


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


  /**
   * Creates a node.
   *
   * @param db the session
   */
  public DbPreferencesNode (Db db)    {
    super(db);
  }

  /**
   * Creates a node (without db).
   */
  public DbPreferencesNode() {
    super();
  }

  /**
   * Returns whether node 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 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() != DbPreferencesNode.class;
      while (rs.next()) {
        DbPreferencesNode obj = derived ? newInstance() : new DbPreferencesNode(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 nodes belonging to a parent node.
   *
   * @param parentId the ID of the parent node, 0 = rootnode
   * @return List of nodes
   *
   * @wurblet selectByParentId DbSelectList --model=$mapfile parentId
   */
  ////GEN-BEGIN:selectByParentId

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

  private static final StatementId SELECT_BY_PARENT_ID_STMT = new StatementId();


  ////GEN-END:selectByParentId



  /**
   * Selects a node by user and name.
   *
   * @param user is the username, null = system
   * @param name is the absolute pathname of the node, ""=root
   * @return the node
   *
   * @wurblet selectByUserAndName DbSelectUnique --model=$mapfile user name
   */
  ////GEN-BEGIN:selectByUserAndName

  public DbPreferencesNode selectByUserAndName(String user, String name) {
    PreparedStatementWrapper st = getPreparedStatement(SELECT_BY_USER_AND_NAME_STMT,
      b -> {
        StringBuilder sql = createSelectAllInnerSql(b);
        sql.append(Backend.SQL_AND);
        sql.append(CN_USER);
        sql.append(Backend.SQL_EQUAL_PAR);
        sql.append(Backend.SQL_AND);
        sql.append(CN_NAME);
        sql.append(Backend.SQL_EQUAL_PAR);
        b.buildSelectSql(sql, false, 0, 0);
        return sql.toString();
      }
    );
    int ndx = 1;
    st.setString(ndx++, user, true);
    st.setString(ndx, name);
    try (ResultSetWrapper rs = st.executeQuery()) {
      if (rs.next()) {
        return readFromResultSetWrapper(rs);
      }
      return null;  // not found
    }
  }

  private static final StatementId SELECT_BY_USER_AND_NAME_STMT = new StatementId();


  ////GEN-END:selectByUserAndName







  // generated GETTER/SETTER

  // @wurblet methods MethodsImpl --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_USER);
      rs.configureColumn(CN_NAME);
      rs.configureColumn(CN_PARENTID);
      rs.configureColumn(CN_ROOTNODEID);
      rs.configureColumn(CN_ID);
      rs.configureColumn(CN_SERIAL);
    }
    setTableSerial(rs.getLong());
    user = rs.getString(true);
    name = rs.getString();
    parentId = rs.getLong();
    rootNodeId = rs.getLong();
    setId(rs.getLong());
    setSerial(rs.getLong());
  }

  @Override
  public int setFields(PreparedStatementWrapper st) {
    int ndx = super.setFields(st);
    st.setLong(++ndx, getTableSerial());
    st.setString(++ndx, user, true);
    st.setString(++ndx, name);
    st.setLong(++ndx, parentId);
    st.setLong(++ndx, rootNodeId);
    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_USER + Backend.SQL_COMMA +
           CN_NAME + Backend.SQL_COMMA +
           CN_PARENTID + Backend.SQL_COMMA +
           CN_ROOTNODEID + 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_USER + Backend.SQL_EQUAL_PAR_COMMA +
           CN_NAME + Backend.SQL_EQUAL_PAR_COMMA +
           CN_PARENTID + Backend.SQL_EQUAL_PAR_COMMA +
           CN_ROOTNODEID + 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 user.
   *
   * @return name of user, null if system
   */
  public String getUser()    {
    return user;
  }

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

  /**
   * Gets the attribute name.
   *
   * @return name of the node
   */
  public String getName()    {
    return name;
  }

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

  /**
   * Gets the attribute parentId.
   *
   * @return ID of parent-node, 0 if root-node
   */
  public long getParentId()    {
    return parentId;
  }

  /**
   * Sets the attribute parentId.
   *
   * @param parentId ID of parent-node, 0 if root-node
   */
  public void setParentId(long parentId) {
    if (this.parentId != parentId) {
      setModified(true);
      this.parentId = parentId;
    }
  }

  /**
   * Gets the attribute rootNodeId.
   *
   * @return 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;
    }
  }

  ////GEN-END:methods



  /**
   * Gets the basename of the node.
   *
   * @return the node's basename
   */
  public String getBaseName() {
    int lastSlash = name.lastIndexOf('/');
    return lastSlash >= 0 ? name.substring(lastSlash + 1) : name;
  }


  @Override
  public String toString()  {
    StringBuilder buf = new StringBuilder();
    buf.append("ID=").append(getId())
       .append(", parentId=").append(parentId);
    if (user == null) {
      buf.append(", ");
    }
    else {
      buf.append(", user='").append(user).append("'");
    }
    buf.append(", name='").append(name).append("'");
    return buf.toString();
  }


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

  ////GEN-BEGIN:declare


  /** name of user, null if system. */
  private String user;

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

  /** ID of parent-node, 0 if root-node. */
  private long parentId;

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

  ////GEN-END:declare


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

  ////GEN-BEGIN:fieldNames


  /** database column name for 'user'. */
  public static final String CN_USER = "username";

  /** database column name for 'name'. */
  public static final String CN_NAME = "nodename";

  /** database column name for 'parentId'. */
  public static final String CN_PARENTID = "parentid";

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

  ////GEN-END:fieldNames

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy