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