View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.model.bean.database;
12  
13  import com.jsql.util.LogLevelUtil;
14  import org.apache.commons.lang3.StringUtils;
15  import org.apache.logging.log4j.LogManager;
16  import org.apache.logging.log4j.Logger;
17  
18  /**
19   * Define a Database, e.g. is sent to the view by the model after injection.
20   */
21  public class Database extends AbstractElementDatabase {
22      
23      private static final Logger LOGGER = LogManager.getRootLogger();
24      
25      // The number of tables in the database.
26      // TODO to int
27      private String tableCount;
28  
29      /**
30       * Define the database label and number of tables.
31       */
32      public Database(String databaseName, String tableCount) {
33          this.elementValue = databaseName;
34          this.tableCount = tableCount;
35      }
36  
37      // A database has no parent.
38      @Override
39      public AbstractElementDatabase getParent() {
40          return null;
41      }
42      
43      // Return the number of tables in the table.
44      @Override
45      public int getChildCount() {
46          return Integer.parseInt(this.tableCount);
47      }
48  
49      /**
50       * A readable label for the database, with number of tables,
51       * displayed by the view, e.g. my_database (7 tables).
52       */
53      @Override
54      public String getLabelWithCount() {
55          // Report #1500: detect incorrect number of tables
56          String sPlural = StringUtils.EMPTY;
57          
58          try {
59              if (Integer.parseInt(this.tableCount) > 1) {
60                  sPlural = "s";
61              }
62          } catch (NumberFormatException e) {
63              this.tableCount = "0";
64              LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Incorrect number of tables for [{}].", this);
65          }
66          
67          return String.format(
68              "%s (%s table%s)",
69              this.elementValue,
70              this.tableCount,
71              sPlural
72          );
73      }
74  }