DMAAP-83 Initial code import
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / aaf / database / DBSingleton.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 import org.onap.dmaap.dbcapi.util.Singleton;
27
28 public class DBSingleton<C> extends TableHandler<C> implements Singleton<C>     {
29         private C singleton;
30         public DBSingleton(Class<C> cls, String tabname) throws Exception {
31                 this(ConnectionFactory.getDefaultInstance(), cls, tabname);
32         }
33         public DBSingleton(ConnectionFactory cf, Class<C> cls, String tabname) throws Exception {
34                 super(cf, cls, tabname, null);
35                 singleton = cls.newInstance();
36         }
37         public C get() throws DBException {
38                 return((new ConnWrapper<C, Object>() {
39                         protected C run(Object junk) throws Exception {
40                                 ps = c.prepareStatement(getstmt);
41                                 rs = ps.executeQuery();
42                                 if (!rs.next()) {
43                                         return(null);
44                                 }
45                                 for (DBFieldHandler f: fields) {
46                                         f.fromSQL(rs, singleton);
47                                 }
48                                 return(singleton);
49                         }
50                 }).protect(cf, null));
51         }
52         public void init(C val) throws DBException {
53                 if (get() != null) {
54                         return;
55                 }
56                 (new ConnWrapper<Void, C>() {
57                         protected Void run(C val) throws Exception {
58                                 ps = c.prepareStatement(initstmt);
59                                 for (DBFieldHandler f: fields) {
60                                         f.toSQL(val, ps);
61                                 }
62                                 ps.executeUpdate();
63                                 if (val != singleton) {
64                                         for (DBFieldHandler f: fields) {
65                                                 f.copy(val, singleton);
66                                         }
67                                 }
68                                 return(null);
69                         }
70                 }).protect(cf, val);
71         }
72         public void update(C val) throws DBException {
73                 (new ConnWrapper<Void, C>() {
74                         protected Void run(C val) throws Exception {
75                                 ps = c.prepareStatement(insorreplstmt);
76                                 for (DBFieldHandler f: fields) {
77                                         f.toSQL(val, ps);
78                                 }
79                                 ps.executeUpdate();
80                                 if (val != singleton) {
81                                         for (DBFieldHandler f: fields) {
82                                                 f.copy(val, singleton);
83                                         }
84                                 }
85                                 return(null);
86                         }
87                 }).protect(cf, val);
88         }
89         public void remove() throws DBException {
90                 (new ConnWrapper<Void, Object>() {
91                         protected Void run(Object junk) throws Exception {
92                                 ps = c.prepareStatement(delstmt);
93                                 ps.executeUpdate();
94                                 return(null);
95                         }
96                 }).protect(cf, null);
97         }
98 }