e7575ff33f5d0426312c9399ee6b0358a3537a1b
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / PublishRecord.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 package org.onap.dmaap.datarouter.provisioning.beans;\r
25 \r
26 import java.sql.PreparedStatement;\r
27 import java.sql.ResultSet;\r
28 import java.sql.SQLException;\r
29 import java.sql.Types;\r
30 import java.text.ParseException;\r
31 import java.util.LinkedHashMap;\r
32 \r
33 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;\r
34 \r
35 \r
36 /**\r
37  * The representation of a Publish Record, as retrieved from the DB.\r
38  *\r
39  * @author Robert Eby\r
40  * @version $Id: PublishRecord.java,v 1.6 2013/10/28 18:06:53 eby Exp $\r
41  */\r
42 public class PublishRecord extends BaseLogRecord {\r
43     private String feedFileid;\r
44     private String remoteAddr;\r
45     private String user;\r
46     private int status;\r
47 \r
48     public PublishRecord(String[] pp) throws ParseException {\r
49         super(pp);\r
50 //        This is too slow!\r
51 //        Matcher m = Pattern.compile(".*/publish/(\\d+)/(.*)$").matcher(pp[4]);\r
52 //        if (!m.matches())\r
53 //            throw new ParseException("bad pattern", 0);\r
54 //        this.feedFileid = m.group(2);\r
55         int ix = pp[4].indexOf("/publish/");\r
56         if (ix < 0)\r
57             throw new ParseException("bad pattern", 0);\r
58         ix = pp[4].indexOf('/', ix + 9);\r
59         if (ix < 0)\r
60             throw new ParseException("bad pattern", 0);\r
61         this.feedFileid = pp[4].substring(ix + 1);\r
62         this.remoteAddr = pp[8];\r
63         this.user = pp[9];\r
64         this.status = Integer.parseInt(pp[10]);\r
65     }\r
66 \r
67     public PublishRecord(ResultSet rs) throws SQLException {\r
68         super(rs);\r
69         this.feedFileid = rs.getString("FEED_FILEID");\r
70         this.remoteAddr = rs.getString("REMOTE_ADDR");\r
71         this.user = rs.getString("USER");\r
72         this.status = rs.getInt("STATUS");\r
73     }\r
74 \r
75     public String getFeedFileid() {\r
76         return feedFileid;\r
77     }\r
78 \r
79     public void setFeedFileid(String feedFileid) {\r
80         this.feedFileid = feedFileid;\r
81     }\r
82 \r
83     public String getRemoteAddr() {\r
84         return remoteAddr;\r
85     }\r
86 \r
87     public void setRemoteAddr(String remoteAddr) {\r
88         this.remoteAddr = remoteAddr;\r
89     }\r
90 \r
91     public String getUser() {\r
92         return user;\r
93     }\r
94 \r
95     public void setUser(String user) {\r
96         this.user = user;\r
97     }\r
98 \r
99     public int getStatus() {\r
100         return status;\r
101     }\r
102 \r
103     public void setStatus(int status) {\r
104         this.status = status;\r
105     }\r
106 \r
107 \r
108     public LOGJSONObject reOrderObject(LOGJSONObject jo) {\r
109         LinkedHashMap<String, Object> logrecordObj = new LinkedHashMap<String, Object>();\r
110 \r
111 \r
112         logrecordObj.put("statusCode", jo.get("statusCode"));\r
113         logrecordObj.put("publishId", jo.get("publishId"));\r
114         logrecordObj.put("requestURI", jo.get("requestURI"));\r
115         logrecordObj.put("sourceIP", jo.get("sourceIP"));\r
116         logrecordObj.put("method", jo.get("method"));\r
117         logrecordObj.put("contentType", jo.get("contentType"));\r
118         logrecordObj.put("endpointId", jo.get("endpointId"));\r
119         logrecordObj.put("type", jo.get("type"));\r
120         logrecordObj.put("date", jo.get("date"));\r
121         logrecordObj.put("contentLength", jo.get("contentLength"));\r
122 \r
123         LOGJSONObject newjo = new LOGJSONObject(logrecordObj);\r
124         return newjo;\r
125     }\r
126 \r
127     @Override\r
128     public LOGJSONObject asJSONObject() {\r
129         LOGJSONObject jo = super.asJSONObject();\r
130         jo.put("type", "pub");\r
131 //        jo.put("feedFileid", feedFileid);\r
132 //        jo.put("remoteAddr", remoteAddr);\r
133 //        jo.put("user", user);\r
134         jo.put("sourceIP", remoteAddr);\r
135         jo.put("endpointId", user);\r
136         jo.put("statusCode", status);\r
137 \r
138         LOGJSONObject newjo = this.reOrderObject(jo);\r
139 \r
140         return newjo;\r
141     }\r
142 \r
143     @Override\r
144     public void load(PreparedStatement ps) throws SQLException {\r
145         ps.setString(1, "pub");        // field 1: type\r
146         super.load(ps);                // loads fields 2-8\r
147         ps.setString(9, getFeedFileid());\r
148         ps.setString(10, getRemoteAddr());\r
149         ps.setString(11, getUser());\r
150         ps.setInt(12, getStatus());\r
151         ps.setNull(13, Types.INTEGER);\r
152         ps.setNull(14, Types.VARCHAR);\r
153         ps.setNull(15, Types.INTEGER);\r
154         ps.setNull(16, Types.INTEGER);\r
155         ps.setNull(17, Types.VARCHAR);\r
156         ps.setNull(19, Types.BIGINT);\r
157     }\r
158 }\r