-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateRecord.java
More file actions
145 lines (135 loc) · 3.79 KB
/
createRecord.java
File metadata and controls
145 lines (135 loc) · 3.79 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
134
135
136
137
138
139
140
141
142
143
144
145
package freshWorks;
import org.json.JSONObject;
import org.json.JSONArray;
import java.io.File;
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.Date;
public class createRecord {
public String createRecordDS(String key, String value, String validTillTime, String isExists) {
String status="fail";
int maxObjLen=16000;
double fileSize=1024;
int size=1000000000;
if(isExists.equalsIgnoreCase("EXISTS"))
{
double actualFileSize=getFileSize();
if(Double.compare(fileSize,actualFileSize) < 0)
{
return "maxFileSize breach";
}
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);
//System.out.println(requestBody.toString());
if(requestBody.has(key))
{
//System.out.println("The data store already has data associated with this key, please enter a unique key.");
return "exists";
}
else
{
JSONObject mainObj = new JSONObject();
JSONObject obj = new JSONObject();
String createdDateTime=getDateTime();
try
{
obj.put("value", value);
obj.put("Time to live",validTillTime);
obj.put("createdDateTime",createdDateTime);
int len=obj.toString().length();
if(len>maxObjLen)
{
return "maxLength breach";
}
//System.out.println("length::"+len);
//mainObj.put(key, obj);
requestBody.put(key, obj);
int requestBodyLen=requestBody.toString().length();
//System.out.println("file length::"+requestBodyLen);
if(requestBodyLen>size)
{
return "maxFileSize breach";
}
FileWriter file =new FileWriter("C:/Users/User/Desktop/dataStore.json");
file.write(requestBody.toString()); file.close();
status="success";
}
catch(Exception e)
{
System.out.println("Exception::"+e);
status="fail";
}
}
}
catch(Exception e)
{
System.out.println("Exception::"+e);
status="fail";
}
}
else
{
JSONObject mainObj = new JSONObject();
JSONObject obj = new JSONObject();
String createdDateTime=getDateTime();
try
{
obj.put("value", value);
obj.put("Time to live",validTillTime);
obj.put("createdDateTime",createdDateTime);
int len=obj.toString().length();
if(len>maxObjLen)
{
return "maxLength breach";
}
mainObj.put(key, obj);
try
{
FileWriter file = new FileWriter("C:/Users/User/Desktop/dataStore.json");
file.write(mainObj.toString());
file.close();
status="success";
} catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace();
System.out.println("Exception::"+e);
status="fail";
}
}
catch(Exception e)
{
System.out.println("Exception::"+e);
status="fail";
}
}
return status;
}
public String getDateTime()
{
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
return(formatter.format(date));
}
public double getFileSize()
{
try
{
String fileName = "C:/Users/User/Desktop/dataStore.json";
File file = new File(fileName);
//System.out.println("fileSize::"+(double) file.length() / (1024 * 1024));
return (double) file.length() / (1024 * 1024);
}
catch(Exception e)
{
return 0;
}
}
}