Skip to content

Commit c2bb51e

Browse files
authored
Merge pull request #9 from mohammed6688/mohammed_ashraf
Mohammed ashraf
2 parents 1e1121b + 90ccbc6 commit c2bb51e

File tree

9 files changed

+204
-46
lines changed

9 files changed

+204
-46
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
"1","01147964655","https://facebook.com","2015-03-31","20","0","1","3"
2+

Cdr_Processor/src/main/java/Main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
public class Main {
77
public static void main(String[] args) {
88
connectToDB();
9-
// String filename = "cdr_1830208061";
10-
String filename = GetLastCDR.getLastModified();
9+
String filename = "cdr_1830208061";
10+
//String filename = GetLastCDR.getLastModified();
1111
System.out.println(filename);
1212
CDR cdrData = CDRParser.parseCDR(filename);
1313

@@ -19,7 +19,7 @@ public static void main(String[] args) {
1919

2020
}
2121
private static void connectToDB() {
22-
String DB_NAME = "billing project";
22+
String DB_NAME = "Billing";
2323
String USER = "postgres";
2424
String PASS = "1502654";
2525
// String PASS = "0000"; //omar pass

Cdr_Processor/src/main/java/Rating.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,21 @@ public static void RIH(CDR cdr, String typeOfService) throws SQLException {
5050
//1- Check the contract table and check units based on service
5151
//2- rate the service (Units based on service) & LE
5252

53+
int overUnits=0;
5354
RatePlane uRatePlane = null;
5455
List<RatePlane> ratePlanes =SiteDAO.instanceData.getRatePlane(cdr.getRatePlan_id());
55-
Contract contract = SiteDAO.instanceData.getContract(cdr.getTerminated_msisdn());
56+
Contract contract = SiteDAO.instanceData.getContract(cdr.getSource_msisdn());
5657
for (RatePlane ratePlane:ratePlanes){
5758
if (ratePlane.getId()==cdr.getRatePlan_id()){
5859
uRatePlane = ratePlane;
5960
}
6061
}
6162

63+
if (contract==null){
64+
System.out.println("the contract not found");
65+
return;
66+
}
67+
6268
if (uRatePlane==null){
6369
System.out.println("the ratePlane id is wrong");
6470
return;
@@ -72,20 +78,41 @@ public static void RIH(CDR cdr, String typeOfService) throws SQLException {
7278
break;
7379
case "sms":
7480
int smsCount = cdr.getDuration();
75-
int availableSms = uRatePlane.getSms_service();
81+
int availableSms = contract.getCurrent_sms();
7682
int restSms = availableSms - smsCount;
77-
if (contract==null){
78-
System.out.println("the contract not found");
79-
return;
80-
}
81-
if (restSms>0 || restSms == 0){
83+
if (restSms>0 || restSms == 0){ //the user consumed service inside his bundle
8284
cdr.setRate(0);
83-
}else {
84-
85+
}else { //the user exceeded his bundle
86+
int additionalRate;
87+
if (availableSms!=0){ //there is remained sms for that user
88+
overUnits=availableSms;
89+
additionalRate = (smsCount - availableSms) * uRatePlane.getAdditional_sms_service();
90+
}else { // there is no sms available for that user
91+
additionalRate = smsCount * uRatePlane.getAdditional_sms_service();
92+
}
93+
cdr.setRate(additionalRate);
8594
}
86-
CCH(cdr);
95+
System.out.println("(RIH)the rate in sms is: "+cdr.getRate());
96+
CCH(cdr,overUnits);
8797
break;
8898
case "roaming":
99+
int consumedRoamingMinutes = cdr.getDuration();
100+
int availableRoamingMinutes = contract.getCurrent_roaming();
101+
int restRoamingMinutes = availableRoamingMinutes - consumedRoamingMinutes;
102+
if (restRoamingMinutes>0 || restRoamingMinutes == 0){ //the user consumed service inside his bundle
103+
cdr.setRate(0);
104+
}else { //the user exceeded his bundle
105+
int additionalRate;
106+
if (availableRoamingMinutes!=0){ //there is remained sms for that user
107+
overUnits=availableRoamingMinutes;
108+
additionalRate = (consumedRoamingMinutes - availableRoamingMinutes) * uRatePlane.getAdditional_roaming_service();
109+
}else { // there is no sms available for that user
110+
additionalRate = consumedRoamingMinutes * uRatePlane.getAdditional_roaming_service();
111+
}
112+
cdr.setRate(additionalRate);
113+
}
114+
System.out.println("(RIH)the rate in roaming is: "+cdr.getRate());
115+
CCH(cdr,overUnits);
89116
break;
90117
default:
91118
break;
@@ -94,7 +121,7 @@ public static void RIH(CDR cdr, String typeOfService) throws SQLException {
94121
//CCH(cdr,typeOfVoice);
95122
}
96123

97-
public static void CCH(CDR cdr) throws SQLException {
124+
public static void CCH(CDR cdr, int overUnits) throws SQLException {
98125
Integer discount = SiteDAO.instanceData.getDiscount(cdr.getSource_msisdn());
99126

100127
int OldRate = cdr.getRate();
@@ -113,7 +140,8 @@ public static void CCH(CDR cdr) throws SQLException {
113140
}
114141
System.out.println("CCH newDuration"+NewDuration+" rating :"+cdr.getRate());
115142
RLH(cdr, (int)NewDuration);
116-
// System.out.println("CCH newDuration"+NewDuration+" rating :"+cdr.getRate());
143+
// System.out.println("CCH newDuration"+NewDuration+" rating :"+cdr.getRate());
144+
117145
}
118146
}
119147

Cdr_Processor/src/main/java/modules/SiteDAO.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class SiteDAO {
1010
public static SiteDAO instanceData;
1111
PreparedStatement stmt;
1212
Statement NStmt;
13+
1314
public SiteDAO(String dbname, String user, String pass) throws SQLException {
1415
this.connect(dbname, user, pass);
1516
}
@@ -156,37 +157,37 @@ public String getService(int id) throws SQLException {
156157
return null;
157158
}
158159

159-
public int getUnits(String msisdn, String str) throws SQLException {
160+
public int getUnits(String msisdn, String str) throws SQLException {
160161
System.out.println(str);
161-
// stmt = this.con.prepareStatement("select current_voice from bscs.contract where msisdn = ? ");
162-
String SQL = "select "+str+" from bscs.contract where msisdn ='"+msisdn+"' ;";
162+
// stmt = this.con.prepareStatement("select current_voice from bscs.contract where msisdn = ? ");
163+
String SQL = "select "+str+" from bscs.contract where msisdn ='"+msisdn+"' ;";
163164

164-
ResultSet rs = NStmt.executeQuery(SQL);
165-
// stmt.setString(1,str);
165+
ResultSet rs = NStmt.executeQuery(SQL);
166+
// stmt.setString(1,str);
166167
// stmt.setString(1,msisdn);
167168
// stmt.setc
168-
// ResultSet rs = stmt.executeQuery();
169+
// ResultSet rs = stmt.executeQuery();
169170

170-
while (rs.next()) {
171+
while (rs.next()) {
171172

172-
return rs.getInt(str);
173-
}
174-
return -1;
175-
}
176-
public void setUnits(String msisdn,String str,int nduration,int nfree) throws SQLException {
173+
return rs.getInt(str);
174+
}
175+
return -1;
176+
}
177+
public void setUnits(String msisdn,String str,int nduration,int nfree) throws SQLException {
177178
// stmt = this.con.prepareStatement("update bscs.contracts SET ?=?,current_additional_sp=? where msisdn=?");
178179
// stmt.setString(1,str);
179180
// stmt.setInt(2, nduration);
180181
// stmt.setInt(3,nfree);
181182
// stmt.setString(4, msisdn);
182183

183-
NStmt.executeUpdate("update bscs.contract SET "+str+" = "+nduration+",current_additional_sp= "+nfree+" where msisdn ='"+msisdn+"' ;");
184-
// stmt.executeUpdate();
185-
ResultSet rs = NStmt.getGeneratedKeys();
184+
NStmt.executeUpdate("update bscs.contract SET "+str+" = "+nduration+",current_additional_sp= "+nfree+" where msisdn ='"+msisdn+"' ;");
185+
// stmt.executeUpdate();
186+
ResultSet rs = NStmt.getGeneratedKeys();
186187

187-
System.out.println(rs);
188-
if (rs != null) {
189-
System.out.println("Rating added");
188+
System.out.println(rs);
189+
if (rs != null) {
190+
System.out.println("Rating added");
190191

191192
// return 1;
192193
} else {
@@ -215,9 +216,9 @@ public void setRTX(CDR cdr) throws SQLException {
215216
}
216217
}
217218

218-
public Contract getContract(String terminated_msisdn) throws SQLException {
219+
public Contract getContract(String source_msisdn) throws SQLException {
219220
stmt = this.con.prepareStatement("select * from bscs.contract where msisdn = ?");
220-
stmt.setInt(1,Integer.parseInt(terminated_msisdn));
221+
stmt.setString(1,source_msisdn);
221222
ResultSet rs = stmt.executeQuery();
222223
List<Contract> contract = new ArrayList<>();
223224

Customer Care Site/src/main/java/com/example/customer_care_app/modules/SiteDAO.java

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public int addUser(String national_id, String name, String age, String address)
3737
}
3838
}
3939

40-
public List<RatePlane> getRatePlane() throws SQLException {
40+
public List<RatePlane> getRatePlanes() throws SQLException {
4141
stmt = this.con.prepareStatement("select * from bscs.rateplanes");
4242
ResultSet rs = stmt.executeQuery();
4343
List<RatePlane> ratePlanes = new ArrayList<>();
@@ -62,23 +62,103 @@ public List<RatePlane> getRatePlane() throws SQLException {
6262
return ratePlanes;
6363
}
6464

65-
public int addContract(String national_id, String rateplane, String msisdn) throws SQLException {
66-
stmt = this.con.prepareStatement("insert into bscs.contract(msisdn,rateplane_id,userid) values(?,?,?)");
67-
stmt.setInt(1, Integer.parseInt(msisdn));
65+
public RatePlane getRatePlane(String id) throws SQLException {
66+
stmt = this.con.prepareStatement("select * from bscs.rateplanes where id = ?");
67+
stmt.setInt(1,Integer.parseInt(id));
68+
ResultSet rs = stmt.executeQuery();
69+
List<RatePlane> ratePlanes = new ArrayList<>();
70+
71+
while (rs.next()) {
72+
ratePlanes.add(new RatePlane(
73+
rs.getInt("id"),
74+
rs.getString("commercial_name"),
75+
rs.getInt("voice_service"),
76+
rs.getInt("cross_voice_service"),
77+
rs.getInt("data_service"),
78+
rs.getInt("sms_service"),
79+
rs.getInt("roaming_service"),
80+
rs.getInt("additional_minutes_service"),
81+
rs.getInt("additional_sms_service"),
82+
rs.getInt("additional_data_service"),
83+
rs.getInt("additional_roaming_service"),
84+
rs.getInt("fee")));
85+
}
86+
87+
if (ratePlanes.size()!=0){
88+
return ratePlanes.get(0);
89+
}else {
90+
return null;
91+
}
92+
}
93+
94+
public int addContract(String national_id, String rateplane, String msisdn,String discount, String freeUnits) throws SQLException {
95+
stmt = this.con.prepareStatement("insert into bscs.contract(msisdn,rateplane_id,userid,discount,additional_sp) values(?,?,?,?,?)");
96+
stmt.setString(1, msisdn);
6897
stmt.setInt(2, Integer.parseInt(rateplane));
6998
stmt.setInt(3, Integer.parseInt(national_id));
99+
stmt.setInt(4, Integer.parseInt(discount));
100+
stmt.setInt(5, Integer.parseInt(freeUnits));
70101

71102
stmt.executeUpdate();
72103
ResultSet rs = stmt.getGeneratedKeys();
73104
System.out.println(rs);
74105
if (rs != null) {
75106
System.out.println("contract added");
107+
RatePlane uRatePlane=getRatePlane(rateplane);
108+
updateUsage(freeUnits,uRatePlane,national_id);
76109
return 1;
77110
} else {
78111
return -1;
79112
}
80113
}
81114

115+
private void updateUsage(String freeUnits, RatePlane rateplane,String uid) throws SQLException {
116+
List<ServicePackage> servicePackages =getServicePackage();
117+
int freeUnit=getFreeUnit(freeUnits);
118+
stmt = this.con.prepareStatement("update bscs.contract set current_voice = ? , current_cross_voice = ? ,current_data = ? , current_sms = ? ,current_roaming=?,current_additional_sp=? where userid = ?;");
119+
stmt.setInt(1, getServiceUnits(rateplane.getVoice_service(),servicePackages));
120+
stmt.setInt(2, getServiceUnits(rateplane.getCross_voice_service(),servicePackages));
121+
stmt.setInt(3, getServiceUnits(rateplane.getData_service(),servicePackages));
122+
stmt.setInt(4, getServiceUnits(rateplane.getSms_service(),servicePackages));
123+
stmt.setInt(5, getServiceUnits(rateplane.getRoaming_service(),servicePackages));
124+
stmt.setInt(6, freeUnit);
125+
stmt.setInt(7, Integer.parseInt(uid));
126+
127+
stmt.executeUpdate();
128+
ResultSet rs = stmt.getGeneratedKeys();
129+
System.out.println(rs);
130+
if (rs != null) {
131+
System.out.println("usage updated");
132+
} else {
133+
System.out.println("error while updating usage");
134+
}
135+
}
136+
137+
private int getFreeUnit(String freeUnits) throws SQLException {
138+
stmt = this.con.prepareStatement("select * from bscs.service_package where id = ?");
139+
stmt.setInt(1,Integer.parseInt(freeUnits));
140+
ResultSet rs = stmt.executeQuery();
141+
List<ServicePackage> servicePackage = new ArrayList<>();
142+
143+
while (rs.next()) {
144+
servicePackage.add(new ServicePackage(
145+
rs.getInt("id"),
146+
rs.getString("service_type"),
147+
rs.getInt("units")
148+
));
149+
}
150+
return servicePackage.get(0).getUnits();
151+
}
152+
153+
private int getServiceUnits(int voice_service,List<ServicePackage> servicePackages) throws SQLException {
154+
for (ServicePackage servicePackage : servicePackages){
155+
if (servicePackage.getId()==voice_service){
156+
return servicePackage.getUnits();
157+
}
158+
}
159+
return 0;
160+
}
161+
82162
public List<Users> getUsers() throws SQLException {
83163
stmt = this.con.prepareStatement("select * from bscs.users");
84164
ResultSet rs = stmt.executeQuery();
@@ -184,4 +264,20 @@ public int addRatePlan (RatePlane ratePlane) throws SQLException {
184264
return -1;
185265
}
186266
}
267+
268+
public List<ServicePackage> getFreeUnits() throws SQLException {
269+
stmt = this.con.prepareStatement("select * from bscs.service_package where service_type = ?");
270+
stmt.setString(1,"free");
271+
ResultSet rs = stmt.executeQuery();
272+
List<ServicePackage> servicePackage = new ArrayList<>();
273+
274+
while (rs.next()) {
275+
servicePackage.add(new ServicePackage(
276+
rs.getInt("id"),
277+
rs.getString("service_type"),
278+
rs.getInt("units")
279+
));
280+
}
281+
return servicePackage;
282+
}
187283
}

Customer Care Site/src/main/webapp/WEB-INF/web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</listener>
99
<context-param>
1010
<param-name>DB_NAME</param-name>
11-
<param-value>billing project</param-value>
11+
<param-value>Billing</param-value>
1212
</context-param>
1313
<context-param>
1414
<param-name>USER_NAME</param-name>

0 commit comments

Comments
 (0)