-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestaurantWorkingHoursParser.java
161 lines (149 loc) · 7.58 KB
/
RestaurantWorkingHoursParser.java
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.wolt.fissha.restaurantopeninghours;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import static android.content.ContentValues.TAG;
/**
* Created by Fissha on 26/05/2017.
*/
public class RestaurantWorkingHoursParser {
public RestaurantProperties restaurantProperties;
public ArrayList<HashMap<String, String>> parsedOpeninghoursArrayList;
public ArrayList<HashMap<String, String>> openingHoursArrayList;
public HashMap<String, String> openingHoursList;
public Context context;
public RestaurantWorkingHoursParser(Context context){
//super();
this.context = context;
restaurantProperties = new RestaurantProperties();
openingHoursArrayList = new ArrayList<HashMap<String, String>>();
openingHoursList = new HashMap<String, String>();
}
//Get parsed items method
public ArrayList<HashMap<String, String>> getParsedItems(){
try{
JSONObject jsonObject = new JSONObject(parseJSON());
JSONArray jsonArray1 = jsonObject.getJSONArray("monday");
parsedOpeninghoursArrayList = parseJSONArray(jsonArray1, "Monday");
JSONArray jsonArray2 = jsonObject.getJSONArray("tuesday");
parsedOpeninghoursArrayList = (parseJSONArray(jsonArray2, "Tuesday"));
JSONArray jsonArray3 = jsonObject.getJSONArray("wednesday");
parsedOpeninghoursArrayList = parseJSONArray(jsonArray3, "Wednesday");
JSONArray jsonArray4 = jsonObject.getJSONArray("thursday");
parsedOpeninghoursArrayList = parseJSONArray(jsonArray4,"Thursday");
JSONArray jsonArray5 = jsonObject.getJSONArray("friday");
parsedOpeninghoursArrayList = parseJSONArray(jsonArray5, "Friday");
JSONArray jsonArray6 = jsonObject.getJSONArray("saturday");
parsedOpeninghoursArrayList = parseJSONArray(jsonArray6, "Saturday");
JSONArray jsonArray7 = jsonObject.getJSONArray("sunday");
parsedOpeninghoursArrayList = parseJSONArray(jsonArray7, "Sunday");
}catch (JSONException e){
e.printStackTrace();
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
return parsedOpeninghoursArrayList;
}
//Parser method
public ArrayList<HashMap<String, String>> parseJSONArray(JSONArray jsonArray, String day){
if(jsonArray.length()!=0){
try{
for(int i = 0; i<jsonArray.length(); i++){
JSONObject jsonInsider = jsonArray.getJSONObject(i);
String theType = jsonInsider.getString("type");
restaurantProperties.setTheType(theType);
if(jsonArray.length()==1 && theType.equalsIgnoreCase("open")){
restaurantProperties.setJsonArrayLengthEqOne(true);
restaurantProperties.setSpecialDay(day);
}
else if(jsonArray.length()==1 && theType.equalsIgnoreCase("close")){
restaurantProperties.setJsonArrayLengthEqOne(false);
}
else if((theType.equalsIgnoreCase("close") && restaurantProperties.getJsonArrayLengthEqOne())){
String closingTime = jsonInsider.getString("value");
restaurantProperties.setClosingTime(closingTime);
String specialDay = restaurantProperties.getSpecialDay();
specialDayOperation(specialDay);
if(theType.equalsIgnoreCase("close") && jsonArray.length() == 2){
closingTime = jsonInsider.getString("value");
restaurantProperties.setClosingTime(closingTime);
commonTask(day);
}
}
else if(theType.equalsIgnoreCase("close") && jsonArray.length() == 2){
String closingTime = jsonInsider.getString("value");
restaurantProperties.setClosingTime(closingTime);
commonTask(day);
}
else if(theType.equalsIgnoreCase("close") && jsonArray.length()>2 && restaurantProperties.getJsonArrayLengthGrTwo()){
String closingTime = jsonInsider.getString("value");
restaurantProperties.setClosingTime(closingTime);
commonTask(day);
}
else if(theType.equalsIgnoreCase("open") && jsonArray.length() != 1){
if(jsonArray.length()>2){
restaurantProperties.setJsonArrayLengthGrTwo(true);
}
String openingTime = jsonInsider.getString("value");
restaurantProperties.setOpeningTime(openingTime);
}
}
}catch (JSONException e){
e.printStackTrace();
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
}else{
defaultOpeningStatus(day);
}
return openingHoursArrayList;
}
//This method assigns default values for closed working day
public void defaultOpeningStatus(String day){
openingHoursList = new HashMap<String, String>();
restaurantProperties.setNewDay(day.concat(": ").concat("Closed"));
openingHoursList.put("status", restaurantProperties.getNewDay());
openingHoursArrayList.add(openingHoursList);
}
//This method is used to execute common tasks and avoid duplication
public void commonTask(String day){
String openingTime = restaurantProperties.getOpeningTime();
String closingTime = restaurantProperties.getClosingTime();
restaurantProperties.setNewDay(day.concat(": ").concat(openingTime).concat(" - ").concat(closingTime));
//Adding the values into the ArrayList
openingHoursList = new HashMap<String, String>();
openingHoursList.put("status", restaurantProperties.getNewDay());
openingHoursArrayList.add(openingHoursList);
}
//This method is used to handle special long days having only opening hours
public void specialDayOperation(String specialDay){
String openingTime = restaurantProperties.getOpeningTime();
String closingTime = restaurantProperties.getClosingTime();
String theSpecialDay = specialDay.concat(": ").concat(openingTime).concat(" - ").concat(closingTime);
//Adding the values into the ArrayList
openingHoursList = new HashMap<String, String>();
openingHoursList.put("status", theSpecialDay);
openingHoursArrayList.add(openingHoursList);
restaurantProperties.setJsonArrayLengthEqOne(false);
}
//This method is general parse method
public String parseJSON(){
String json = null;
try{
InputStream istream = context.getAssets().open("restaurantopeninghours.json");
int size = istream.available();
byte[] buffer = new byte[size];
istream.read(buffer);
istream.close();
json = new String(buffer, "UTF-8");
}catch (IOException ex){
ex.printStackTrace();
return null;
}
return json;
}
}