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