001: /*
002: * PkMapping.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.storage;
013:
014: import java.io.BufferedInputStream;
015: import java.io.BufferedWriter;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileWriter;
019: import java.io.InputStream;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Map.Entry;
027: import java.util.Properties;
028: import workbench.db.ColumnIdentifier;
029: import workbench.db.TableIdentifier;
030: import workbench.db.WbConnection;
031: import workbench.log.LogMgr;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034: import workbench.util.StringUtil;
035:
036: /**
037: * A class to hold user-defined primary key mappings for tables (or views)
038: * @author support@sql-workbench.net
039: */
040: public class PkMapping {
041: private HashMap<String, String> columnMapping = new HashMap<String, String>();
042:
043: private static PkMapping instance;
044:
045: public static synchronized boolean isInitialized() {
046: return instance != null;
047: }
048:
049: public static synchronized PkMapping getInstance() {
050: if (instance == null) {
051: instance = new PkMapping();
052: }
053: return instance;
054: }
055:
056: private PkMapping() {
057: String filename = Settings.getInstance().getPKMappingFilename();
058: loadMapping(filename);
059: }
060:
061: public synchronized String getMappingAsText() {
062: if (this .columnMapping == null)
063: return null;
064: if (this .columnMapping.size() == 0)
065: return null;
066:
067: StringBuilder result = new StringBuilder(this .columnMapping
068: .size() * 50);
069: Iterator<Entry<String, String>> itr = this .columnMapping
070: .entrySet().iterator();
071: while (itr.hasNext()) {
072: Map.Entry entry = itr.next();
073: result.append(entry.getKey() + "=" + entry.getValue());
074: result.append("\n");
075: }
076: return result.toString();
077: }
078:
079: public synchronized void loadMapping(String filename) {
080: if (filename == null)
081: return;
082: Properties props = new Properties();
083:
084: File f = new File(filename);
085: if (!f.exists()) {
086: LogMgr
087: .logError(
088: "PkConfig.readMappingFile()",
089: "Mapping file '"
090: + filename
091: + "' not found! Please check workbench.settings",
092: null);
093: return;
094: }
095: InputStream in = null;
096: try {
097: in = new BufferedInputStream(new FileInputStream(filename));
098: props.load(in);
099: } catch (Exception e) {
100: LogMgr.logError("PkMapping.readMappingFile()",
101: "Error reading mapping file", e);
102: this .columnMapping = null;
103: } finally {
104: try {
105: in.close();
106: } catch (Throwable th) {
107: }
108: }
109:
110: LogMgr.logInfo("PkMapping.readMappingFile()",
111: "Using PK mappings from " + f.getAbsolutePath());
112:
113: Iterator<Entry<Object, Object>> itr = props.entrySet()
114: .iterator();
115: while (itr.hasNext()) {
116: Entry<Object, Object> entry = itr.next();
117: String table = (String) entry.getKey();
118: String columns = (String) entry.getValue();
119: if (!StringUtil.isEmptyString(columns)) {
120: this .columnMapping.put(table, columns);
121: }
122: }
123: }
124:
125: public synchronized void removeMapping(WbConnection con,
126: String table) {
127: if (this .columnMapping == null)
128: return;
129: this .columnMapping.remove(table);
130: }
131:
132: public synchronized void addMapping(TableIdentifier table,
133: String columns) {
134: addMapping(table.getTableExpression(), columns);
135: }
136:
137: /**
138: * Defines a PK mapping for the specified table name
139: */
140: public synchronized void addMapping(String table, String columns) {
141: if (!StringUtil.isEmptyString(table)
142: && !StringUtil.isEmptyString(columns)) {
143: this .columnMapping.put(table.toLowerCase(), columns);
144: }
145: }
146:
147: public synchronized Collection<String> getPKColumns(
148: WbConnection con, TableIdentifier tbl) {
149: if (this .columnMapping == null)
150: return null;
151: String columns = this .columnMapping.get(tbl.getTableName()
152: .toLowerCase());
153: if (columns == null) {
154: columns = this .columnMapping.get(tbl
155: .getTableExpression(con).toLowerCase());
156: }
157: List<String> cols = null;
158: if (columns != null) {
159: cols = StringUtil.stringToList(columns, ",", true, true);
160: LogMgr.logInfo("PkMapping.getPKColumns()",
161: "Using PK Columns [" + columns + "]"
162: + " for table [" + tbl.getTableExpression()
163: + "]");
164: }
165: return cols;
166: }
167:
168: public synchronized Map<String, String> getMapping() {
169: if (this .columnMapping == null)
170: return Collections.emptyMap();
171: return Collections.unmodifiableMap(this .columnMapping);
172: }
173:
174: public synchronized void saveMapping(String filename) {
175: if (this .columnMapping == null)
176: return;
177: BufferedWriter out = null;
178: try {
179: Iterator<Entry<String, String>> itr = this .columnMapping
180: .entrySet().iterator();
181: File f = new File(filename);
182: out = new BufferedWriter(new FileWriter(f));
183: out.write("# Primary key mapping for "
184: + ResourceMgr.TXT_PRODUCT_NAME);
185: out.newLine();
186: while (itr.hasNext()) {
187: Map.Entry<String, String> entry = itr.next();
188: String table = entry.getKey();
189: String cols = entry.getValue();
190: out.write(table);
191: out.write('=');
192: out.write(cols);
193: out.newLine();
194: }
195: } catch (Exception e) {
196: LogMgr.logError("PkMapping.saveMapping()",
197: "Error saving mapping to properties file", e);
198: } finally {
199: try {
200: out.close();
201: } catch (Throwable th) {
202: }
203: }
204: }
205:
206: public synchronized void addMapping(TableIdentifier table,
207: ColumnIdentifier[] cols) {
208: StringBuilder colNames = new StringBuilder(50);
209: for (int i = 0; i < cols.length; i++) {
210: if (cols[i].isPkColumn()) {
211: if (colNames.length() > 0)
212: colNames.append(',');
213: colNames.append(cols[i].getColumnName());
214: }
215: }
216: if (colNames.length() > 0) {
217: this.addMapping(table, colNames.toString());
218: }
219: }
220: }
|