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      /**
24       * Log4j logger sent to view.
25       */
26      private static final Logger LOGGER = LogManager.getRootLogger();
27      
28      // The number of tables in the database.
29      // TODO to int
30      private String tableCount;
31  
32      /**
33       * Define the database label and number of tables.
34       */
35      public Database(String databaseName, String tableCount) {
36          this.elementValue = databaseName;
37          this.tableCount = tableCount;
38      }
39  
40      // A database has no parent.
41      @Override
42      public AbstractElementDatabase getParent() {
43          return null;
44      }
45      
46      // Return the number of tables in the table.
47      @Override
48      public int getChildCount() {
49          return Integer.parseInt(this.tableCount);
50      }
51  
52      /**
53       * A readable label for the database, with number of tables,
54       * displayed by the view, e.g. my_database (7 tables).
55       */
56      @Override
57      public String getLabelWithCount() {
58          // Report #1500: detect incorrect number of tables
59          String sPlural = StringUtils.EMPTY;
60          
61          try {
62              if (Integer.parseInt(this.tableCount) > 1) {
63                  sPlural = "s";
64              }
65          } catch (NumberFormatException e) {
66              this.tableCount = "0";
67              LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Incorrect number of tables for [{}].", this);
68          }
69          
70          return String.format(
71              "%s (%s table%s)",
72              this.elementValue,
73              this.tableCount,
74              sPlural
75          );
76      }
77  }