Removing code smells
[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.apache.commons.lang3.StringUtils;\r
34 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;\r
35 \r
36 \r
37 /**\r
38  * The representation of a Publish Record, as retrieved from the DB.\r
39  *\r
40  * @author Robert Eby\r
41  * @version $Id: PublishRecord.java,v 1.6 2013/10/28 18:06:53 eby Exp $\r
42  */\r
43 \r
44 public class PublishRecord extends BaseLogRecord {\r
45 \r
46     public static final String STATUS_CODE = "statusCode";\r
47     public static final String SOURCE_IP = "sourceIP";\r
48     public static final String ENDPOINT_ID = "endpointId";\r
49     public static final String FILE_NAME = "fileName";\r
50     private String feedFileid;\r
51     private String remoteAddr;\r
52     private String user;\r
53     private int status;\r
54     private String fileName;\r
55 \r
56     /**\r
57      * Publish record constructor.\r
58      * @param pp string array of attributes\r
59      * @throws ParseException in case of parse error\r
60      */\r
61     public PublishRecord(String[] pp) throws ParseException {\r
62         super(pp);\r
63         int ix = pp[4].indexOf("/publish/");\r
64         if (ix < 0) {\r
65             throw new ParseException("bad pattern", 0);\r
66         }\r
67         ix = pp[4].indexOf('/', ix + 9);\r
68         if (ix < 0) {\r
69             throw new ParseException("bad pattern", 0);\r
70         }\r
71         this.feedFileid = pp[4].substring(ix + 1);\r
72         this.remoteAddr = pp[8];\r
73         this.user = pp[9];\r
74         this.status = Integer.parseInt(pp[10]);\r
75         this.fileName = StringUtils.substringAfterLast(this.getRequestUri(), "/");\r
76     }\r
77 \r
78     /**\r
79      * Publish record constructor.\r
80      * @param rs ResultSet from DB\r
81      * @throws SQLException in case of SQL error\r
82      */\r
83     public PublishRecord(ResultSet rs) throws SQLException {\r
84         super(rs);\r
85         this.feedFileid = rs.getString("FEED_FILEID");\r
86         this.remoteAddr = rs.getString("REMOTE_ADDR");\r
87         this.user = rs.getString("USER");\r
88         this.status = rs.getInt("STATUS");\r
89         this.fileName = rs.getString("FILENAME");\r
90     }\r
91 \r
92     public String getFeedFileid() {\r
93         return feedFileid;\r
94     }\r
95 \r
96     public void setFeedFileid(String feedFileid) {\r
97         this.feedFileid = feedFileid;\r
98     }\r
99 \r
100     public String getRemoteAddr() {\r
101         return remoteAddr;\r
102     }\r
103 \r
104     public void setRemoteAddr(String remoteAddr) {\r
105         this.remoteAddr = remoteAddr;\r
106     }\r
107 \r
108     public String getUser() {\r
109         return user;\r
110     }\r
111 \r
112     public void setUser(String user) {\r
113         this.user = user;\r
114     }\r
115 \r
116     public int getStatus() {\r
117         return status;\r
118     }\r
119 \r
120     public void setStatus(int status) {\r
121         this.status = status;\r
122     }\r
123 \r
124     public String getFileName() {\r
125         return fileName;\r
126     }\r
127 \r
128     public void setFileName(String fileName) {\r
129         this.fileName = fileName;\r
130     }\r
131 \r
132 \r
133     /**\r
134      * Method to reorder json object.\r
135      * @param jo LOGJSONObject\r
136      * @return LOGJSONObject\r
137      */\r
138     public LOGJSONObject reOrderObject(LOGJSONObject jo) {\r
139         LinkedHashMap<String, Object> logrecordObj = new LinkedHashMap<>();\r
140 \r
141 \r
142         logrecordObj.put(STATUS_CODE, jo.get(STATUS_CODE));\r
143         logrecordObj.put("publishId", jo.get("publishId"));\r
144         logrecordObj.put("requestURI", jo.get("requestURI"));\r
145         logrecordObj.put(SOURCE_IP, jo.get(SOURCE_IP));\r
146         logrecordObj.put("method", jo.get("method"));\r
147         logrecordObj.put("contentType", jo.get("contentType"));\r
148         logrecordObj.put(ENDPOINT_ID, jo.get(ENDPOINT_ID));\r
149         logrecordObj.put("type", jo.get("type"));\r
150         logrecordObj.put("date", jo.get("date"));\r
151         logrecordObj.put("contentLength", jo.get("contentLength"));\r
152         logrecordObj.put(FILE_NAME, jo.get(FILE_NAME));\r
153 \r
154         return new LOGJSONObject(logrecordObj);\r
155     }\r
156 \r
157 \r
158     @Override\r
159     public LOGJSONObject asJSONObject() {\r
160         LOGJSONObject jo = super.asJSONObject();\r
161         jo.put("type", "pub");\r
162         jo.put("feedFileid", feedFileid);\r
163         jo.put("remoteAddr", remoteAddr);\r
164         jo.put("user", user);\r
165         jo.put(SOURCE_IP, remoteAddr);\r
166         jo.put(ENDPOINT_ID, user);\r
167         jo.put(STATUS_CODE, status);\r
168         jo.put(FILE_NAME, fileName);\r
169 \r
170         return this.reOrderObject(jo);\r
171     }\r
172 \r
173     @Override\r
174     public void load(PreparedStatement ps) throws SQLException {\r
175         ps.setString(1, "pub");        // field 1: type\r
176         super.load(ps);                // loads fields 2-8\r
177         ps.setString(9, getFeedFileid());\r
178         ps.setString(10, getRemoteAddr());\r
179         ps.setString(11, getUser());\r
180         ps.setInt(12, getStatus());\r
181         ps.setNull(13, Types.INTEGER);\r
182         ps.setNull(14, Types.VARCHAR);\r
183         ps.setNull(15, Types.INTEGER);\r
184         ps.setNull(16, Types.INTEGER);\r
185         ps.setNull(17, Types.VARCHAR);\r
186         ps.setNull(19, Types.BIGINT);\r
187         ps.setString(20, getFileName());\r
188     }\r
189 }\r