DMAAP-83 Initial code import
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / aaf / database / DBMap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.aaf.database;
22
23 import java.sql.*;
24 import java.util.*;
25
26 public class DBMap<C> extends TableHandler<C> implements Map<String, C> {
27         public DBMap(Class<C> cls, String tabname, String keyfield) throws Exception {
28                 this(ConnectionFactory.getDefaultInstance(), cls, tabname, keyfield);
29         }
30         public DBMap(ConnectionFactory cf, Class<C> cls, String tabname, String keyfield) throws Exception {
31                 super(cf, cls, tabname, keyfield);
32         }
33         public void clear() throws UnsupportedOperationException {
34                 throw new UnsupportedOperationException();
35         }
36         public boolean containsKey(Object key) throws DBException {
37                 return(get(key) != null);
38         }
39         public boolean containsValue(Object value) throws UnsupportedOperationException {
40                 throw new UnsupportedOperationException();
41         }
42         public boolean isEmpty() {
43                 return(false);
44         }
45         public Set<Map.Entry<String, C>> entrySet() throws DBException {
46                 return(list());
47         }
48         public Set<String> keySet() throws DBException {
49                 Set<String> ret = new HashSet<String>();
50                 for (Map.Entry<String, C> x: list()) {
51                         ret.add(x.getKey());
52                 }
53                 return(ret);
54         }
55         public void putAll(Map<? extends String, ? extends C> m) throws UnsupportedOperationException {
56                 throw new UnsupportedOperationException();
57         }
58         public int size() {
59                 return(2);
60         }
61         public Collection<C> values() throws DBException {
62                 Collection<C> ret = new Vector<C>();
63                 for (Map.Entry<String, C> x: list()) {
64                         ret.add(x.getValue());
65                 }
66                 return(ret);
67         }
68         public C get(Object key) throws DBException {
69                 if (!(key instanceof String)) {
70                         return(null);
71                 }
72                 return((new ConnWrapper<C, String>() {
73                         protected C run(String key) throws Exception {
74                                 ps = c.prepareStatement(getstmt);
75                                 ps.setString(1, (String)key);
76                                 rs = ps.executeQuery();
77                                 if (!rs.next()) {
78                                         return(null);
79                                 }
80                                 C ret = cls.newInstance();
81                                 for (DBFieldHandler f: fields) {
82                                         f.fromSQL(rs, ret);
83                                 }
84                                 return(ret);
85                         }
86                 }).protect(cf, (String)key));
87         }
88         public Set<Map.Entry<String, C>> list() throws DBException {
89                 return((new ConnWrapper<Set<Map.Entry<String, C>>, Object>() {
90                         protected Set<Map.Entry<String, C>> run(Object junk) throws Exception {
91                                 DBFieldHandler keyfield = fields[fields.length - 1];
92                                 ps = c.prepareStatement(liststmt);
93                                 rs = ps.executeQuery();
94                                 Set<Map.Entry<String, C>> ret = new HashSet<Map.Entry<String, C>>();
95                                 while (rs.next()) {
96                                         C val = cls.newInstance();
97                                         for (DBFieldHandler f: fields) {
98                                                 f.fromSQL(rs, val);
99                                         }
100                                         String key = keyfield.getKey(val);
101                                         ret.add(new AbstractMap.SimpleEntry<String, C>(key, val));
102                                 }
103                                 return(ret);
104                         }
105                 }).protect(cf, null));
106         }
107         public C put(String key, C val) throws DBException {
108                 try {
109                         fields[fields.length - 1].setKey(val, key);
110                 } catch (Exception e) {
111                         throw new DBException(e);
112                 }
113                 PreparedStatement ps = null;
114                 return((new ConnWrapper<C, C>() {
115                         protected C run(C val) throws Exception {
116                                 ps = c.prepareStatement(insorreplstmt);
117                                 for (DBFieldHandler f: fields) {
118                                         f.toSQL(val, ps);
119                                 }
120                                 ps.executeUpdate();
121                                 return(null);
122                         }
123                 }).protect(cf, val));
124         }
125         public C remove(Object key) throws DBException {
126                 if (!(key instanceof String)) {
127                         return(null);
128                 }
129                 return((new ConnWrapper<C, String>() {
130                         protected C run(String key) throws Exception {
131                                 ps = c.prepareStatement(delstmt);
132                                 ps.setString(1, key);
133                                 ps.executeUpdate();
134                                 return(null);
135                         }
136                 }).protect(cf, (String)key));
137         }
138 }