-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteRecord.java
More file actions
133 lines (127 loc) · 4.06 KB
/
DeleteRecord.java
File metadata and controls
133 lines (127 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package freshWorks;
import org.json.JSONObject;
import org.json.JSONArray;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DeleteRecord {
public String deleteRecordDS(String key, String value, String validTillTime, String isExists) {
String status="fail";
String retVal="fail";
if(isExists.equalsIgnoreCase("EXISTS"))
{
try
{
//FileReader reader = new FileReader("C:/Users/User/Desktop/json_array_output.json");
//String actual = Files.readString("C:/Users/User/Desktop/json_array_output.json");
String content = new String ( Files.readAllBytes(Paths.get("C:/Users/User/Desktop/dataStore.json")));
JSONObject requestBody = new JSONObject(content);
String ttl="0";
String cdt="";
//System.out.println(requestBody.toString());
if(requestBody.has(key))
{
ttl=requestBody.getJSONObject(key).getString("Time to live");
//System.out.println("ttl::"+ttl);
cdt=requestBody.getJSONObject(key).getString("createdDateTime");
//System.out.println("cdt::"+cdt);
}
if(!ttl.equalsIgnoreCase("0"))
{
//System.out.println("ttl!=0");
Date date=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(cdt);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.SECOND, Integer.parseInt(ttl));
Date cdtPlusTtl = c.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
formatter.format(cdtPlusTtl);
Date currDate = new Date();
formatter.format(currDate);
if(currDate.compareTo(cdtPlusTtl)>0)
{
requestBody.remove(key);
try
{
FileWriter file = new FileWriter("C:/Users/User/Desktop/dataStore.json");
file.write(requestBody.toString());
file.close();
retVal="deleted";
} catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace();
System.out.println("Exception::"+e);
retVal="fail";
}
//System.out.println("The key you're looking for no longer exists in the data store as it has exceeded its time to live");
}
else
{
if(requestBody.has(key))
{
//System.out.println("The data store already has data associated with this key, please enter a unique key.");
//retVal=requestBody.getJSONObject(key).getString("value");
requestBody.remove(key);
try
{
FileWriter file = new FileWriter("C:/Users/User/Desktop/dataStore.json");
file.write(requestBody.toString());
file.close();
retVal="success";
} catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace();
System.out.println("Exception::"+e);
retVal="fail";
}
}
else
{
retVal="key not found";
}
}
}
else
{
if(requestBody.has(key))
{
//System.out.println("The data store already has data associated with this key, please enter a unique key.");
retVal=requestBody.getJSONObject(key).getString("value");
}
else
{
retVal="key not found";
}
}
}
catch(Exception e)
{
System.out.println("Exception::"+e);
retVal="fail";
}
}
return retVal;
}
/*public String writeToFile(String requestBody)
{
String status="";
try
{
FileWriter file = new FileWriter("C:/Users/User/Desktop/json_array_output.json");
file.write(requestBody.toString());
file.close();
status="success";
} catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace();
System.out.println("Exception::"+e);
status="fail";
}
return status;
}*/
}