Merge "Replace classes in the org.json package"
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / ExpiryRecord.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.sql.Types;\r
31 import java.text.ParseException;\r
32 import java.util.LinkedHashMap;\r
33 \r
34 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;\r
35 \r
36 \r
37 /**\r
38  * The representation of a Expiry Record, as retrieved from the DB.\r
39  * @author Robert Eby\r
40  * @version $Id: ExpiryRecord.java,v 1.4 2013/10/28 18:06:52 eby Exp $\r
41  */\r
42 public class ExpiryRecord extends BaseLogRecord {\r
43         private int subid;\r
44         private String fileid;\r
45         private int attempts;\r
46         private String reason;\r
47 \r
48         public ExpiryRecord(String[] pp) throws ParseException {\r
49                 super(pp);\r
50                 String fileid = pp[5];\r
51                 if (fileid.lastIndexOf('/') >= 0)\r
52                         fileid = fileid.substring(fileid.lastIndexOf('/')+1);\r
53                 this.subid    = Integer.parseInt(pp[4]);\r
54                 this.fileid   = fileid;\r
55                 this.attempts = Integer.parseInt(pp[10]);\r
56                 this.reason   = pp[9];\r
57                 if (!reason.equals("notRetryable") && !reason.equals("retriesExhausted") && !reason.equals("diskFull"))\r
58                         this.reason = "other";\r
59         }\r
60         public ExpiryRecord(ResultSet rs) throws SQLException {\r
61                 super(rs);\r
62                 this.subid    = rs.getInt("DELIVERY_SUBID");\r
63                 this.fileid   = rs.getString("DELIVERY_FILEID");\r
64                 this.attempts = rs.getInt("ATTEMPTS");\r
65                 this.reason   = rs.getString("REASON");\r
66         }\r
67 \r
68         public int getSubid() {\r
69                 return subid;\r
70         }\r
71 \r
72         public void setSubid(int subid) {\r
73                 this.subid = subid;\r
74         }\r
75 \r
76         public String getFileid() {\r
77                 return fileid;\r
78         }\r
79 \r
80         public void setFileid(String fileid) {\r
81                 this.fileid = fileid;\r
82         }\r
83 \r
84         public int getAttempts() {\r
85                 return attempts;\r
86         }\r
87 \r
88         public void setAttempts(int attempts) {\r
89                 this.attempts = attempts;\r
90         }\r
91 \r
92         public String getReason() {\r
93                 return reason;\r
94         }\r
95 \r
96         public void setReason(String reason) {\r
97                 this.reason = reason;\r
98         }\r
99         \r
100         public LOGJSONObject reOrderObject(LOGJSONObject jo) {\r
101                 LinkedHashMap<String,Object> logrecordObj = new LinkedHashMap<String,Object>();\r
102                 \r
103                 logrecordObj.put("expiryReason", jo.get("expiryReason"));\r
104                 logrecordObj.put("publishId", jo.get("publishId"));\r
105                 logrecordObj.put("attempts", jo.get("attempts"));\r
106                 logrecordObj.put("requestURI", jo.get("requestURI"));\r
107                 logrecordObj.put("method", jo.get("method"));\r
108                 logrecordObj.put("contentType", jo.get("contentType"));\r
109                 logrecordObj.put("type", jo.get("type"));\r
110                 logrecordObj.put("date", jo.get("date"));\r
111                 logrecordObj.put("contentLength", jo.get("contentLength"));\r
112 \r
113                 LOGJSONObject newjo = new LOGJSONObject(logrecordObj);\r
114                 return newjo;\r
115         }\r
116         \r
117         @Override\r
118         public LOGJSONObject asJSONObject() {\r
119                 LOGJSONObject jo = super.asJSONObject();\r
120                 jo.put("type", "exp");\r
121                 jo.put("expiryReason", reason);\r
122                 jo.put("attempts", attempts);\r
123                 \r
124                 LOGJSONObject newjo = this.reOrderObject(jo);\r
125                 return newjo;\r
126         }\r
127         @Override\r
128         public void load(PreparedStatement ps) throws SQLException {\r
129                 ps.setString(1, "exp");         // field 1: type\r
130                 super.load(ps);                         // loads fields 2-8\r
131                 ps.setNull  (9,  Types.VARCHAR);\r
132                 ps.setNull  (10, Types.VARCHAR);\r
133                 ps.setNull  (11, Types.VARCHAR);\r
134                 ps.setNull  (12, Types.INTEGER);\r
135                 ps.setInt   (13, getSubid());\r
136                 ps.setString(14, getFileid());\r
137                 ps.setNull  (15, Types.INTEGER);\r
138                 ps.setInt   (16, getAttempts());\r
139                 ps.setString(17, getReason());\r
140                 ps.setNull  (19, Types.BIGINT);\r
141         }\r
142 }\r