Fix checkstyle contridictions on datarouter node
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / BaseLogRecord.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 \r
25 package org.onap.dmaap.datarouter.provisioning.beans;\r
26 \r
27 import java.sql.PreparedStatement;\r
28 import java.sql.ResultSet;\r
29 import java.sql.SQLException;\r
30 import java.text.ParseException;\r
31 import java.text.SimpleDateFormat;\r
32 import java.util.Calendar;\r
33 import java.util.Date;\r
34 import java.util.GregorianCalendar;\r
35 \r
36 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;\r
37 \r
38 /**\r
39  * Define the common fields used by the three types of records generated by DR nodes.\r
40  *\r
41  * @author Robert Eby\r
42  * @version $Id: BaseLogRecord.java,v 1.10 2013/10/29 16:57:57 eby Exp $\r
43  */\r
44 public class BaseLogRecord implements LOGJSONable, Loadable {\r
45     protected static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");\r
46 \r
47     private long eventTime;\r
48     private String publishId;\r
49     private int feedid;\r
50     private String requestUri;\r
51     private String method;\r
52     private String contentType;\r
53     private long contentLength;\r
54 \r
55     protected BaseLogRecord(String[] pp) throws ParseException {\r
56 //        This throws exceptions occasionally - don't know why.\r
57 //        Date d = null;\r
58 //        synchronized (sdf) {\r
59 //            d = sdf.parse(pp[0]);\r
60 //        }\r
61         Date d = parseDate(pp[0]);\r
62         this.eventTime     = d.getTime();\r
63         this.publishId     = pp[2];\r
64         this.feedid        = Integer.parseInt(pp[3]);\r
65         if (pp[1].equals("DLX")) {\r
66             this.requestUri    = "";\r
67             this.method        = "GET";    // Note: we need a valid value in this field, even though unused\r
68             this.contentType   = "";\r
69             this.contentLength = Long.parseLong(pp[5]);\r
70         } else  if (pp[1].equals("PUB") || pp[1].equals("LOG") || pp[1].equals("PBF")) {\r
71             this.requestUri    = pp[4];\r
72             this.method        = pp[5];\r
73             this.contentType   = pp[6];\r
74             this.contentLength = Long.parseLong(pp[7]);\r
75         } else {\r
76             this.requestUri    = pp[5];\r
77             this.method        = pp[6];\r
78             this.contentType   = pp[7];\r
79             this.contentLength = Long.parseLong(pp[8]);\r
80         }\r
81     }\r
82     protected BaseLogRecord(ResultSet rs) throws SQLException {\r
83         this.eventTime     = rs.getLong("EVENT_TIME");\r
84         this.publishId     = rs.getString("PUBLISH_ID");\r
85         this.feedid        = rs.getInt("FEEDID");\r
86         this.requestUri    = rs.getString("REQURI");\r
87         this.method        = rs.getString("METHOD");\r
88         this.contentType   = rs.getString("CONTENT_TYPE");\r
89         this.contentLength = rs.getLong("CONTENT_LENGTH");\r
90     }\r
91     protected Date parseDate(final String s) throws ParseException {\r
92         int[] n = new int[7];\r
93         int p = 0;\r
94         for (int i = 0; i < s.length(); i++) {\r
95             char c = s.charAt(i);\r
96             if (c < '0' || c > '9') {\r
97                 p++;\r
98             } else {\r
99                 if (p > n.length)\r
100                     throw new ParseException("parseDate()", 0);\r
101                 n[p] = (n[p] * 10) + (c - '0');\r
102             }\r
103         }\r
104         if (p != 7)\r
105             throw new ParseException("parseDate()", 1);\r
106         Calendar cal = new GregorianCalendar();\r
107         cal.set(Calendar.YEAR, n[0]);\r
108         cal.set(Calendar.MONTH, n[1]-1);\r
109         cal.set(Calendar.DAY_OF_MONTH, n[2]);\r
110         cal.set(Calendar.HOUR_OF_DAY, n[3]);\r
111         cal.set(Calendar.MINUTE, n[4]);\r
112         cal.set(Calendar.SECOND, n[5]);\r
113         cal.set(Calendar.MILLISECOND, n[6]);\r
114         return cal.getTime();\r
115     }\r
116     public long getEventTime() {\r
117         return eventTime;\r
118     }\r
119     public void setEventTime(long eventTime) {\r
120         this.eventTime = eventTime;\r
121     }\r
122     public String getPublishId() {\r
123         return publishId;\r
124     }\r
125     public void setPublishId(String publishId) {\r
126         this.publishId = publishId;\r
127     }\r
128     public int getFeedid() {\r
129         return feedid;\r
130     }\r
131     public void setFeedid(int feedid) {\r
132         this.feedid = feedid;\r
133     }\r
134     public String getRequestUri() {\r
135         return requestUri;\r
136     }\r
137     public void setRequestUri(String requestUri) {\r
138         this.requestUri = requestUri;\r
139     }\r
140     public String getMethod() {\r
141         return method;\r
142     }\r
143     public void setMethod(String method) {\r
144         this.method = method;\r
145     }\r
146     public String getContentType() {\r
147         return contentType;\r
148     }\r
149     public void setContentType(String contentType) {\r
150         this.contentType = contentType;\r
151     }\r
152     public long getContentLength() {\r
153         return contentLength;\r
154     }\r
155     public void setContentLength(long contentLength) {\r
156         this.contentLength = contentLength;\r
157     }\r
158     @Override\r
159     public LOGJSONObject asJSONObject() {\r
160         LOGJSONObject jo = new LOGJSONObject();\r
161         String t = "";\r
162         synchronized (sdf) {\r
163             t = sdf.format(eventTime);\r
164         }\r
165         jo.put("date", t);\r
166         jo.put("publishId", publishId);\r
167         jo.put("requestURI", requestUri);\r
168         jo.put("method", method);\r
169         if (method.equals("PUT")) {\r
170             jo.put("contentType", contentType);\r
171             jo.put("contentLength", contentLength);\r
172         }\r
173         return jo;\r
174     }\r
175     @Override\r
176     public void load(PreparedStatement ps) throws SQLException {\r
177         ps.setLong  (2, getEventTime());\r
178         ps.setString(3, getPublishId());\r
179         ps.setInt   (4, getFeedid());\r
180         ps.setString(5, getRequestUri());\r
181         ps.setString(6, getMethod());\r
182         ps.setString(7, getContentType());\r
183         ps.setLong  (8, getContentLength());\r
184     }\r
185 }\r