]> git.basschouten.com Git - openhab-addons.git/blob
32113c5765fdf28e22af96d2ddf4dad507e0050f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.jdbc.internal;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Objects;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.core.items.ItemUtil;
24 import org.openhab.persistence.jdbc.internal.dto.ItemVO;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * This class manages strategy for table names.
30  *
31  * @author Jacob Laursen - Initial contribution
32  */
33 @NonNullByDefault
34 public class NamingStrategy {
35
36     private final Logger logger = LoggerFactory.getLogger(NamingStrategy.class);
37
38     private JdbcConfiguration configuration;
39
40     public NamingStrategy(JdbcConfiguration configuration) {
41         this.configuration = configuration;
42     }
43
44     public String getTableName(int itemId, String itemName) {
45         if (!ItemUtil.isValidItemName(itemName)) {
46             throw new IllegalArgumentException(itemName + " is not a valid item name");
47         }
48         if (configuration.getTableUseRealItemNames()) {
49             return formatTableName(itemName, itemId);
50         } else {
51             return configuration.getTableNamePrefix() + getSuffix(itemId);
52         }
53     }
54
55     private String formatTableName(String itemName, int itemId) {
56         if (configuration.getTableCaseSensitiveItemNames()) {
57             return itemName;
58         } else {
59             return itemName.toLowerCase() + "_" + getSuffix(itemId);
60         }
61     }
62
63     private String getSuffix(int itemId) {
64         int digits = configuration.getTableIdDigitCount();
65         if (digits > 0) {
66             return String.format("%0" + configuration.getTableIdDigitCount() + "d", itemId);
67         } else {
68             return String.valueOf(itemId);
69         }
70     }
71
72     public List<ItemVO> prepareMigration(List<String> itemTables, Map<Integer, String> itemIdToItemNameMap,
73             String itemsManageTable) {
74         List<ItemVO> oldNewTableNames = new ArrayList<>();
75         Map<String, Integer> tableNameToItemIdMap = new HashMap<>();
76
77         for (Entry<Integer, String> entry : itemIdToItemNameMap.entrySet()) {
78             String itemName = entry.getValue();
79             tableNameToItemIdMap.put(itemName, entry.getKey());
80         }
81
82         for (String oldName : itemTables) {
83             Integer itemIdBoxed = tableNameToItemIdMap.get(oldName);
84             int itemId = -1;
85
86             if (Objects.nonNull(itemIdBoxed)) {
87                 itemId = itemIdBoxed;
88                 logger.info("JDBC::formatTableNames: found by name; table name= {} id= {}", oldName, itemId);
89             } else {
90                 try {
91                     itemId = Integer.parseInt(oldName.replaceFirst("^.*\\D", ""));
92                     logger.info("JDBC::formatTableNames: found by id; table name= {} id= {}", oldName, itemId);
93                 } catch (NumberFormatException e) {
94                     // Fall through.
95                 }
96             }
97
98             String itemName = itemIdToItemNameMap.get(itemId);
99
100             if (!Objects.isNull(itemName)) {
101                 String newName = getTableName(itemId, itemName);
102                 if (newName.equalsIgnoreCase(itemsManageTable)) {
103                     logger.error(
104                             "JDBC::formatTableNames: Table '{}' could NOT be renamed to '{}' since it conflicts with manage table",
105                             oldName, newName);
106                 } else if (!oldName.equals(newName)) {
107                     oldNewTableNames.add(new ItemVO(oldName, newName));
108                     logger.info("JDBC::formatTableNames: Table '{}' will be renamed to '{}'", oldName, newName);
109                 } else {
110                     logger.info("JDBC::formatTableNames: Table oldName='{}' newName='{}' nothing to rename", oldName,
111                             newName);
112                 }
113             } else {
114                 logger.error("JDBC::formatTableNames: Table '{}' could NOT be renamed for id '{}'", oldName, itemId);
115             }
116         }
117
118         return oldNewTableNames;
119     }
120 }