You are on page 1of 35

CALORIE TRACKER

FIT5046 : Mobile and Distributed Computing Systems

ASSIGNMENT 1
NAME : POOJA VISHAL PANCHOLI
STUDENT ID : 29984939
Calorie Tracker | Assignment 1

TABLE OF CONTENTS

Contents
TABLE OF CONTENTS .....................................................................................................................................................1
Task 1. Creating a Database ...........................................................................................................................................3
a. Creation of the Database ..................................................................................................................................3
b. Populating the Database. .................................................................................................................................7
 Insertion in Users table: ...............................................................................................................................7
 Insertion in Credentials table: ......................................................................................................................7
 Insertion in Food table: ................................................................................................................................8
 Insertion in Consumption table: ................................................................................................................10
 Insertion in Reports table:..........................................................................................................................10
c. Entity-Relationship Diagram ...........................................................................................................................11
Task 2. Creating a Restful Web Service........................................................................................................................12
Task 3. Dynamic and Static Queries .............................................................................................................................12
a. Querying the table attributes. ........................................................................................................................12
findByName Method: ..........................................................................................................................................12
findBySurname Method: .....................................................................................................................................13
findByEmail Method: ...........................................................................................................................................13
findByHeight Method: .........................................................................................................................................13
findByWeight Method: ........................................................................................................................................14
findByDateOfBirth Method: ................................................................................................................................14
findByGender Method: ........................................................................................................................................14
findByAddress Method: .......................................................................................................................................14
findByPostcode Method: .....................................................................................................................................15
findByLevelOfActivity Method: ............................................................................................................................15
findByStepsPerMile Method: ..............................................................................................................................15
findByUsername Method: ...................................................................................................................................16
findByPasswordHash Method: ............................................................................................................................16
findBySignupDate Method: .................................................................................................................................16
findByFoodName Method: ..................................................................................................................................17
findByFoodCategory Method: .............................................................................................................................17
findByAmountOfCalories Method: ......................................................................................................................18
findByServingUnit Method: .................................................................................................................................18
findByServingAmount Method: ...........................................................................................................................18
findByFatAmount Method: ..................................................................................................................................18
findByFoodServingQuantity Method: ..................................................................................................................19

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

findByDateOfConsumption Method: ...................................................................................................................19


findByCaloriesConsumed Method: ......................................................................................................................20
findByTotalCaloriesBurned Method: ...................................................................................................................20
findByTotalSteps Method: ...................................................................................................................................20
findByCalorieGoal Method: .................................................................................................................................20
findByDateOfReportGeneration Method: ...........................................................................................................21
b. Dynamic query with two attributes from the same table. .............................................................................21
c. Dynamic query showing IMPLICIT JOIN with two attributes from different tables. ......................................22
d. Static query showing IMPLICIT JOIN with two attributes from different tables. ...........................................23
Task 4. Calculations with REST Methods .....................................................................................................................25
a. Calculate calories burned per step. ................................................................................................................25
b. Calculate the Base Metabolic Rate. ................................................................................................................26
c. Calculate the total calories burned at rest. ....................................................................................................28
d. Calculate the total calories consumed. ..........................................................................................................30
Task 5. Creating the Report .........................................................................................................................................31
a. Return Single Record data calculation. ...........................................................................................................31
b. Return Multiple Record data calculation. .......................................................................................................33

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

Task 1. Creating a Database

Database Name : CalorieTrackerServer


Database Username : ppan0011

a. This section deals with creation of the tables and making an entity relationship
diagram using the primary and foreign keys of each table.
 Users Table
 Creating the users table

CREATE TABLE PPAN0011.users


(
user_id INTEGER NOT NULL,
name VARCHAR(50) NOT NULL,
surname VARCHAR(50),
email VARCHAR(255),
date_of_birth DATE,
height DOUBLE,
weight DOUBLE,
gender VARCHAR(10),
address VARCHAR(200),
postcode CHAR(10),
level_of_activity INTEGER,
steps_per_mile INTEGER,
PRIMARY KEY(user_id)
);

 Check Constraints for users table


For Level of activity to fall between 1 and 5:

ALTER TABLE PPAN0011.users ADD CONSTRAINT CHK_LevelOfActivity CHECK


(level_of_activity BETWEEN 1 AND 5);

For Gender to fall in one of the three categories:

ALTER TABLE PPAN0011.users ADD CONSTRAINT CHK_Gender CHECK


(gender IN ('Male', 'Female', 'Other'));

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

 Credentials Table
 Creating the credentials table

CREATE TABLE PPAN0011.credentials


(
credential_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
username VARCHAR(50) NOT NULL,
password_hash VARCHAR(40) NOT NULL,
signup_date DATE NOT NULL,
PRIMARY KEY(credential_id)
);

 Foreign Keys for credentials table


Foreign key for user_id:

ALTER TABLE PPAN0011.credentials ADD FOREIGN KEY (user_id)


REFERENCES PPAN0011.users (user_id);

 Food Table
 Creating the food table

CREATE TABLE PPAN0011.food


(
food_id INTEGER NOT NULL,
food_name VARCHAR(50) NOT NULL,
food_category VARCHAR(50) NOT NULL,
amount_of_calories INTEGER NOT NULL,
serving_unit VARCHAR(10) NOT NULL,
serving_amount DOUBLE NOT NULL,
fat_amount DOUBLE NOT NULL,
PRIMARY KEY(food_id)
);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

 Consumption Table
 Creating the consumption table

CREATE TABLE PPAN0011.consumption


(
consumption_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
food_id INTEGER NOT NULL,
date_of_consumption DATE NOT NULL,
food_serving_quantity INTEGER NOT NULL,
PRIMARY KEY(consumption_id)
);

 Foreign Keys for consumption table


Foreign key for user_id in users table:

ALTER TABLE PPAN0011.consumption ADD FOREIGN KEY (user_id)


REFERENCES PPAN0011.users (user_id);

Foreign key for food_id in food table:

ALTER TABLE PPAN0011.consumption ADD FOREIGN KEY (food_id)


REFERENCES PPAN0011.food (food_id);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

 Reports Table
 Creating the reports table

CREATE TABLE PPAN0011.reports


(
report_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
date_of_report_generation DATE NOT NULL,
calories_consumed DOUBLE NOT NULL,
total_calories_burned DOUBLE NOT NULL,
total_steps INTEGER NOT NULL,
calorie_goal INTEGER NOT NULL,
PRIMARY KEY(report_id)
);

 Foreign keys for reports table


Foreign key for user_id in users table:

ALTER TABLE PPAN0011.reports ADD FOREIGN KEY (user_id) REFERENCES


PPAN0011.users (user_id);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

b. This section deals with the population of data in the tables. Required “INSERT”
commands are written for adding data into the tables.

 Insertion in Users table:

Note: The height is stored in centimeters and the weight is stored in pounds (lbs)
INSERT INTO PPAN0011.USERS (USER_ID, "NAME", SURNAME, EMAIL, DATE_OF_BIRTH, HEIGHT,
WEIGHT, GENDER, ADDRESS, POSTCODE, LEVEL_OF_ACTIVITY, STEPS_PER_MILE) VALUES (1, 'Pooja',
'Pancholi', 'ppan0011@gmail.com', '1996-01-11', 173.736, 60.4, 'Female', 'Rowville', '3162', 2, 1024);

INSERT INTO PPAN0011.USERS (USER_ID, "NAME", SURNAME, EMAIL, DATE_OF_BIRTH, HEIGHT,


WEIGHT, GENDER, ADDRESS, POSTCODE, LEVEL_OF_ACTIVITY, STEPS_PER_MILE) VALUES (2, 'Varun',
'Mathur', 'vmat0002@gmail.com', '1994-07-31', 195.072, 90.4, 'Male', 'Caulfield', '3145', 1, 2024);
INSERT INTO PPAN0011.USERS (USER_ID, "NAME", SURNAME, EMAIL, DATE_OF_BIRTH, HEIGHT,
WEIGHT, GENDER, ADDRESS, POSTCODE, LEVEL_OF_ACTIVITY, STEPS_PER_MILE) VALUES (3,
'Shama', 'Jariwala', 'sam1101@gmail.com', '1995-03-28', 142.044, 50.05, 'Female', 'Tarneit', '3091',
4, 890);

INSERT INTO PPAN0011.USERS (USER_ID, "NAME", SURNAME, EMAIL, DATE_OF_BIRTH, HEIGHT,


WEIGHT, GENDER, ADDRESS, POSTCODE, LEVEL_OF_ACTIVITY, STEPS_PER_MILE) VALUES (4,
'Spencer', 'Hastings', 'shast1@gmail.com', '1996-09-25', 160.10, 57.3, 'Female', 'Rosewood', '1124',
3, 2010);

INSERT INTO PPAN0011.USERS (USER_ID, "NAME", SURNAME, EMAIL, DATE_OF_BIRTH, HEIGHT,


WEIGHT, GENDER, ADDRESS, POSTCODE, LEVEL_OF_ACTIVITY, STEPS_PER_MILE) VALUES (5, 'Caleb',
'Fitzgerald', 'cfit1198@gmail.com', '1995-12-01', 119.484, 80.54, 'Male', 'Flinders Street', '3143', 4,
1194);

 Insertion in Credentials table:


Note: The password_hash is stored using the MD5 message digest
INSERT INTO PPAN0011.CREDENTIALS (CREDENTIAL_ID, USER_ID, USERNAME, PASSWORD_HASH,
SIGNUP_DATE) VALUES (1,1, 'ppan3162', '2755A342C21AFDC7524426E0525432B3', '2014-11-
20');
INSERT INTO PPAN0011.CREDENTIALS (CREDENTIAL_ID, USER_ID, USERNAME, PASSWORD_HASH,
SIGNUP_DATE) VALUES (2, 2, 'vmat3145', '67DECAE4C63A349F59518B55AA793084', '2017-03-
18');
INSERT INTO PPAN0011.CREDENTIALS (CREDENTIAL_ID, USER_ID, USERNAME, PASSWORD_HASH,
SIGNUP_DATE) VALUES (3,3, 'sjar3091', 'C8235AA39B4ED38F60CAE55F63177B6E', '2018-03-31');
INSERT INTO PPAN0011.CREDENTIALS (CREDENTIAL_ID, USER_ID, USERNAME, PASSWORD_HASH,
SIGNUP_DATE) VALUES (4, 4, 'shas1124', 'E3A38C7E453269F4E78D05A35D16C2B3', '2018-08-
16');

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

INSERT INTO PPAN0011.CREDENTIALS (CREDENTIAL_ID, USER_ID, USERNAME, PASSWORD_HASH,


SIGNUP_DATE) VALUES (5, 5, 'cfit3143', '05E4017CC8BE498836F0389159A222DC', '2018-12-04');

 Insertion in Food table:


INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (1, 'Apples,
dried', 'Fruits', 52, 'cup', 0.25, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (2, 'chicken,
plain', 'Protein', 190, 'each', 1, 5.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (3, 'pork',
'Protein', 36, 'slice', 1, 3.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (4, 'walnuts',
'Nuts', 161, 'cup', 0.25, 15.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (5, 'Wendy’s®
Jr. cheeseburger', 'Fast Food', 319, 'each', 1, 12.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (6, 'almonds,
mixed nuts', 'Nuts', 211, 'cup', 0.25, 19.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (7, 'Papaya,
diced', 'Fruits', 27, 'cup', 0.5, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (8, 'Peas,
green', 'Vegetables', 62, 'cup', 0.5, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (9, 'Bread
pudding, with raisins', 'Desserts', 217, 'cup', 0.5, 9.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (10, 'Parsley,
fresh', 'Herbs', 1, 'tbsp', 1, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (11,
'Cauliflower', 'Vegetables', 17, 'cup', 0.5, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (12, 'Big
Mac®', 'Fast Food', 515, 'each', 1, 24.0);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,


AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (13, 'Crepe, 6"
diam', 'Desserts', 80, 'pc', 1, 3.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (14,
'Strawberries, fresh', 'Fruits', 23, 'cup', 0.5, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (15,
'Macadamia nuts, raw', 'Nuts', 235, 'cup', 0.25, 25.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (16, 'Lox,
smoked salmon', 'Protein', 33, 'oz', 1, 1.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (17, 'Mango,
diced', 'Fruits', 54, 'cup', 0.5, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (18, 'Fish,
fillets', 'SeaFood', 235, 'oz', 3, 15.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (19, 'Sev
(fried noodle, snack)', 'Snacks', 107, 'cup', 0.5, 6.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (20, 'Samosa,
fried', 'Snacks', 114, 'med', 1, 5.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (21, 'Peppers,
green or red', 'Vegetables', 19, 'cup', 0.5, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (22,
'Macaroni', 'Fast Food', 99, 'cup', 0.5, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (23, 'Sweets,
Indian', 'Desserts', 105, 'each', 1, 4.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (24, 'Orange,
fresh 2 5/8" diam', 'Fruits', 62, 'each', 1, 0.0);
INSERT INTO PPAN0011.FOOD (FOOD_ID, FOOD_NAME, FOOD_CATEGORY,
AMOUNT_OF_CALORIES, SERVING_UNIT, SERVING_AMOUNT, FAT_AMOUNT) VALUES (25, 'Honey',
'Dressings', 64, 'tbsp', 1, 0.0);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

 Insertion in Consumption table:


Note: The fat_amount is stored in grams
- Varun Mathur consumes walnuts twice a day and Cauliflower in the evening.
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (1, 2, 4, '2018-10-24', 2);
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (2, 2, 11, '2018-10-24', 1);
- Caleb Fitzgerald ate strawberries and diced mangoes in breakfast.
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (3, 5, 14, '2018-12-14', 1);
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (4, 5, 17, '2018-12-14', 1);
- Pooja Pancholi ate one serve of diced papayas and two serves of fried samosas at the party.
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (5, 1, 7, '2019-03-14', 1);
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (6, 1, 20, '2019-03-14', 2);
- Shama Jariwala consumed 3 serves of dried apple.
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (7, 3, 1, '2019-03-15', 3);
- Spencer Hastings consumed bread pudding with raisins topped with strawberries and honey.

INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,


DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (8, 4, 9, '2019-03-19', 1);
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (9, 4, 14, '2019-03-19', 1);
INSERT INTO PPAN0011.CONSUMPTION (CONSUMPTION_ID, USER_ID, FOOD_ID,
DATE_OF_CONSUMPTION, FOOD_SERVING_QUANTITY) VALUES (10, 4, 25, '2019-03-19', 1);

 Insertion in Reports table:


INSERT INTO PPAN0011.REPORTS (REPORT_ID, USER_ID, DATE_OF_REPORT_GENERATION,
CALORIES_CONSUMED, TOTAL_CALORIES_BURNED, TOTAL_STEPS, CALORIE_GOAL) VALUES (1, 1,
'2019-03-30', 12.0, 124.4, 2404, 124);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

INSERT INTO PPAN0011.REPORTS (REPORT_ID, USER_ID, DATE_OF_REPORT_GENERATION,


CALORIES_CONSUMED, TOTAL_CALORIES_BURNED, TOTAL_STEPS, CALORIE_GOAL) VALUES (2, 2,
'2019-01-01', 1041.0, 1190.0, 100, 2000);
INSERT INTO PPAN0011.REPORTS (REPORT_ID, USER_ID, DATE_OF_REPORT_GENERATION,
CALORIES_CONSUMED, TOTAL_CALORIES_BURNED, TOTAL_STEPS, CALORIE_GOAL) VALUES (3, 3,
'2019-02-13', 2103.0, 1503.0, 3460, 1500);
INSERT INTO PPAN0011.REPORTS (REPORT_ID, USER_ID, DATE_OF_REPORT_GENERATION,
CALORIES_CONSUMED, TOTAL_CALORIES_BURNED, TOTAL_STEPS, CALORIE_GOAL) VALUES (4, 1,
'2019-03-06', 3000.0, 3500.0, 1245, 3200);
INSERT INTO PPAN0011.REPORTS (REPORT_ID, USER_ID, DATE_OF_REPORT_GENERATION,
CALORIES_CONSUMED, TOTAL_CALORIES_BURNED, TOTAL_STEPS, CALORIE_GOAL) VALUES (5, 4,
'2019-01-01', 2450.0, 2000.0, 1100, 3000);

c. Entity-Relationship Diagram

(This screenshot shows the Entity-Relationship diagram made in for the Calorie Tracker Server Database.)

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

Task 2. Creating a Restful Web Service

Restful Web Service’s Name : calorietracker

(This screenshot shows the Restful Web Service created for the Calorie Tracker Application.)

Task 3. Dynamic and Static Queries

a. This section deals with the usage of Java Persistence Query Language for building
static and dynamic queries.
 Users Table
The given code for all user methods is added in UsersFacadeREST.java file. All the method
names are same as in the Entity class Users.java file.
findByName Method:
@GET
@Path("findByName/{name}")
@Produces({"application/json"})
public List<Users> findByName(@PathParam("name") String name) {
Query query = em.createNamedQuery("Users.findByName");
query.setParameter("name", name);
return query.getResultList();
}

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

findBySurname Method:
@GET
@Path("findBySurname/{surname}")
@Produces({"application/json"})
public List<Users> findBySurname(@PathParam("surname") String surname) {
Query query = em.createNamedQuery("Users.findBySurname");
query.setParameter("surname", surname);
return query.getResultList();
}

findByEmail Method:
@GET
@Path("findByEmail/{email}")
@Produces({"application/json"})
public List<Users> findByEmail(@PathParam("email") String email) {
Query query = em.createNamedQuery("Users.findByEmail");
query.setParameter("email", email);
return query.getResultList();
}

findByHeight Method:
@GET
@Path("findByHeight/{height}")
@Produces({"application/json"})
public List<Users> findByHeight(@PathParam("height") Double height) {
Query query = em.createNamedQuery("Users.findByHeight");

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

query.setParameter("height", height);
return query.getResultList();
}

findByWeight Method:
@GET
@Path("findByWeight/{weight}")
@Produces({"application/json"})
public List<Users> findByWeight(@PathParam("weight") Double weight) {
Query query = em.createNamedQuery("Users.findByWeight");
query.setParameter("weight", weight);
return query.getResultList();
}

findByDateOfBirth Method:
@GET
@Path("findByDateOfBirth/{dateOfBirth}")
@Produces({"application/json"})
public List<Users> findByDateOfBirth(@PathParam("dateOfBirth") String dateOfBirth) {
Query query = em.createNamedQuery("Users.findByDateOfBirth");
Date date = Date.valueOf(dateOfBirth);

query.setParameter("dateOfBirth", date);
return query.getResultList();
}

findByGender Method:
@GET
@Path("findByGender/{gender}")
@Produces({"application/json"})
public List<Users> findByGender(@PathParam("gender") String gender) {
Query query = em.createNamedQuery("Users.findByGender");
query.setParameter("gender", gender);
return query.getResultList();
}

findByAddress Method:
@GET
@Path("findByAddress/{address}")
@Produces({"application/json"})
public List<Users> findByAddress(@PathParam("address") String address) {
Query query = em.createNamedQuery("Users.findByAddress");
Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939
Calorie Tracker | Assignment 1

query.setParameter("address", address);
return query.getResultList();
}

findByPostcode Method:
@GET
@Path("findByPostcode/{postcode}")
@Produces({"application/json"})
public List<Users> findByPostcode(@PathParam("postcode") String postcode) {
Query query = em.createNamedQuery("Users.findByPostcode");
query.setParameter("postcode", postcode);
return query.getResultList();
}

findByLevelOfActivity Method:
@GET
@Path("findByLevelOfActivity/{levelOfActivity}")
@Produces({"application/json"})
public List<Users> findByLevelOfActivity(@PathParam("levelOfActivity") Integer levelOfActivity)
{
Query query = em.createNamedQuery("Users.findByLevelOfActivity");
query.setParameter("levelOfActivity", levelOfActivity);
return query.getResultList();
}

findByStepsPerMile Method:
@GET
@Path("findByStepsPerMile/{stepsPerMile}")
@Produces({"application/json"})
public List<Users> findByStepsPerMile(@PathParam("stepsPerMile") Integer stepsPerMile) {
Query query = em.createNamedQuery("Users.findByStepsPerMile");
query.setParameter("stepsPerMile", stepsPerMile);
return query.getResultList();
}

 Credentials Table
The given code for all credentials methods is added in CredentialsFacadeREST.java file. All
the method names are same as in the Entity class Credentials.java file.

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

findByUsername Method:
@GET
@Path("findByUsername/{username}")
@Produces({"application/json"})
public List<Credentials> findByUsername(@PathParam("username") String username) {
Query query = em.createNamedQuery("Credentials.findByUsername");
query.setParameter("username", username);
return query.getResultList();
}

findByPasswordHash Method:
@GET
@Path("findByPasswordHash/{passwordHash}")
@Produces({"application/json"})
public List<Credentials> findByPasswordHash(@PathParam("passwordHash") String
passwordHash) {
Query query = em.createNamedQuery("Credentials.findByPasswordHash");
query.setParameter("passwordHash", passwordHash);
return query.getResultList();
}

findBySignupDate Method:
@GET
@Path("findBySignupDate/{signupDate}")
@Produces({"application/json"})
public List<Credentials> findBySignupDate(@PathParam("signupDate") String signupDate) {
Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939
Calorie Tracker | Assignment 1

Query query = em.createNamedQuery("Credentials.findBySignupDate");


Date date = Date.valueOf(signupDate);

query.setParameter("signupDate", date);
return query.getResultList();
}

 Food Table
The given code for all food methods is added in FoodFacadeREST.java file. All the method
names are same as in the Entity class Food.java file.
findByFoodName Method:
@GET
@Path("findByFoodName/{foodName}")
@Produces({"application/json"})
public List<Food> findByFoodName(@PathParam("foodName") String foodName) {
Query query = em.createNamedQuery("Food.findByFoodName");
query.setParameter("foodName", foodName);
return query.getResultList();
}

findByFoodCategory Method:
@GET
@Path("findByFoodCategory/{foodCategory}")
@Produces({"application/json"})
public List<Food> findByFoodCategory(@PathParam("foodCategory") String foodCategory)
{
Query query = em.createNamedQuery("Food.findByFoodCategory");
query.setParameter("foodCategory", foodCategory);
return query.getResultList();
Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939
Calorie Tracker | Assignment 1

findByAmountOfCalories Method:
@GET
@Path("findByAmountOfCalories/{amountOfCalories}")
@Produces({"application/json"})
public List<Food> findByAmountOfCalories(@PathParam("amountOfCalories") Integer
amountOfCalories) {
Query query = em.createNamedQuery("Food.findByAmountOfCalories");
query.setParameter("amountOfCalories", amountOfCalories);
return query.getResultList();
}

findByServingUnit Method:
@GET
@Path("findByServingUnit/{servingUnit}")
@Produces({"application/json"})
public List<Food> findByServingUnit(@PathParam("servingUnit") String servingUnit) {
Query query = em.createNamedQuery("Food.findByServingUnit");
query.setParameter("servingUnit", servingUnit);
return query.getResultList();
}

findByServingAmount Method:
@GET
@Path("findByServingAmount/{servingAmount}")
@Produces({"application/json"})
public List<Food> findByServingAmount(@PathParam("servingAmount") String
servingAmount) {
Query query = em.createNamedQuery("Food.findByServingAmount");
query.setParameter("servingAmount", servingAmount);
return query.getResultList();
}

findByFatAmount Method:
@GET
@Path("findByFatAmount/{fatAmount}")
@Produces({"application/json"})
public List<Food> findByFatAmount(@PathParam("fatAmount") Double fatAmount) {
Query query = em.createNamedQuery("Food.findByFatAmount");
query.setParameter("fatAmount", fatAmount);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

return query.getResultList();
}

 Consumption Table
The given code for all consumption methods is added in ConsumptionFacadeREST.java file. All
the method names are same as in the Entity class Consumption.java file.
findByFoodServingQuantity Method:
@GET
@Path("findByFoodServingQuantity/{foodServingQuantity}")
@Produces({"application/json"})
public List<Consumption> findByFoodServingQuantity(@PathParam("foodServingQuantity")
Integer foodServingQuantity) {
Query query = em.createNamedQuery("Consumption.findByFoodServingQuantity");
query.setParameter("foodServingQuantity", foodServingQuantity);
return query.getResultList();
}

findByDateOfConsumption Method:
@GET
@Path("findByDateOfConsumption/{dateOfConsumption}")
@Produces({"application/json"})
public List<Consumption> findByDateOfConsumption(@PathParam("dateOfConsumption")
String dateOfConsumption) {

Query query = em.createNamedQuery("Consumption.findByDateOfConsumption");


Date date = Date.valueOf(dateOfConsumption);

query.setParameter("dateOfConsumption", date);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

return query.getResultList();
}

 Reports Table
The given code for all report methods is added in ReportsFacadeREST.java file. All the method
names are same as in the Entity class Reports.java file.
findByCaloriesConsumed Method:
@GET
@Path("findByCaloriesConsumed/{caloriesConsumed}")
@Produces({"application/json"})
public List<Reports> findByCaloriesConsumed(@PathParam("caloriesConsumed") double
caloriesConsumed) {
Query query = em.createNamedQuery("Reports.findByCaloriesConsumed");
query.setParameter("caloriesConsumed", caloriesConsumed);
return query.getResultList();
}

findByTotalCaloriesBurned Method:
@GET
@Path("findByTotalCaloriesBurned/{totalCaloriesBurned}")
@Produces({"application/json"})
public List<Reports> findByTotalCaloriesBurned(@PathParam("totalCaloriesBurned") double
totalCaloriesBurned) {
Query query = em.createNamedQuery("Reports.findByTotalCaloriesBurned");
query.setParameter("totalCaloriesBurned", totalCaloriesBurned);
return query.getResultList();
}

findByTotalSteps Method:
@GET
@Path("findByTotalSteps/{totalSteps}")
@Produces({"application/json"})
public List<Reports> findByTotalSteps(@PathParam("totalSteps") Integer totalSteps) {
Query query = em.createNamedQuery("Reports.findByTotalSteps");
query.setParameter("totalSteps", totalSteps);
return query.getResultList();
}

findByCalorieGoal Method:
@GET
@Path("findByCalorieGoal/{calorieGoal}")
@Produces({"application/json"})
public List<Reports> findByCalorieGoal(@PathParam("calorieGoal") Integer calorieGoal) {
Query query = em.createNamedQuery("Reports.findByCalorieGoal");

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

query.setParameter("calorieGoal", calorieGoal);
return query.getResultList();
}

findByDateOfReportGeneration Method:
@GET
@Path("findByDateOfReportGeneration/{dateOfReportGeneration}")
@Produces({"application/json"})
public List<Reports> findByDateOfReportGeneration(@PathParam("dateOfReportGeneration")
String dateOfReportGeneration) {

Query query = em.createNamedQuery("Reports.findByDateOfReportGeneration");


Date date = Date.valueOf(dateOfReportGeneration);

query.setParameter("dateOfReportGeneration", date);
return query.getResultList();
}

b. Dynamic query with two attributes.


The given code for the method is added in UsersFacadeREST.java file. It uses
javax.persistence.TypedQuery in order to query the Users table.

@GET
@Path("findByFullName/{name}/{surname}")
@Produces({"application/json"})
public List<Users> findByFullName(@PathParam("name") String name,
@PathParam("surname") String surname) {
TypedQuery<Users> q = em.createQuery("SELECT u FROM Users u WHERE u.name = :name
AND u.surname = :surname", Users.class);
q.setParameter("name", name);
q.setParameter("surname", surname);
return q.getResultList();
Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939
Calorie Tracker | Assignment 1

c. Dynamic query showing IMPLICIT JOIN with two attributes from different tables.
The given code for the method is added in ConsumptionFacadeREST.java file. It uses
javax.persistence.TypedQuery in order to query the Consumption table.

@GET
@Path("findConsumptionByNameAndItem/{name}/{food_name}")
@Produces({"application/json"})
public List<Consumption> findConsumptionByNameAndItem(@PathParam("name") String
name, @PathParam("food_name") String food_name) {
TypedQuery<Consumption> q = em.createQuery("SELECT c FROM Consumption c WHERE
c.userId.name = :name AND c.foodId.foodName LIKE CONCAT('%',:food_name ,'%')",
Consumption.class);
q.setParameter("name", name);
q.setParameter("food_name", food_name);
return q.getResultList();
}

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

d. Static query showing IMPLICIT JOIN with two attributes from different tables.
The given code for the method is added in CredentialsFacadeREST.java file. It uses
javax.persistence.Query in order to query the Credentials table.

Credentials.java
@NamedQueries({
@NamedQuery(name = "Credentials.findAll", query = "SELECT c FROM Credentials c")
, @NamedQuery(name = "Credentials.findByCredentialId", query = "SELECT c FROM
Credentials c WHERE c.credentialId = :credentialId")
, @NamedQuery(name = "Credentials.findByUsername", query = "SELECT c FROM Credentials
c WHERE c.username = :username")
, @NamedQuery(name = "Credentials.findByPasswordHash", query = "SELECT c FROM
Credentials c WHERE c.passwordHash = :passwordHash")
, @NamedQuery(name = "Credentials.findBySignupDate", query = "SELECT c FROM Credentials
c WHERE c.signupDate = :signupDate")
, @NamedQuery(name = "Credentials.findByNameAndSignupDate", query = "SELECT c FROM
Credentials c WHERE c.userId.name = :name AND c.signupDate = :signupDate")});

CredentialsFacadeRest.java
@GET
@Path("findByNameAndSignupDate/{name}/{signupDate}")
@Produces({"application/json"})
public List<Credentials> findByNameAndSignupDate(@PathParam("name") String name,
@PathParam("signupDate") String signupDate) {

Query query = em.createNamedQuery("Credentials.findByNameAndSignupDate");

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

Date date = Date.valueOf(signupDate);

query.setParameter("name", name);
query.setParameter("signupDate", date);
return query.getResultList();
}

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

Task 4. Calculations with REST Methods

a. Calculate calories burned per step.


The given code for the method is added in UsersFacadeREST.java file. It uses
javax.persistence.TypedQuery in order to query the Users table. Then the result is used to fetch
data and perform calculations.

@GET
@Path("findCaloriesBurnedPerStep/{userId}")
@Produces(MediaType.TEXT_PLAIN)
public double findCaloriesBurnedPerStep(@PathParam("userId") Integer userId) {

int stepsPerMile;
int caloriesLostPerMile;
double userWeight;
double caloriesBurnedPerStep;

TypedQuery<Users> q = em.createQuery("SELECT u FROM Users u WHERE u.userId =


:userId", Users.class);
q.setParameter("userId", userId);
List<Users> results = q.getResultList();

if (results.size() == 0)
caloriesBurnedPerStep = 0;
else
{
stepsPerMile = results.get(0).getStepsPerMile();
userWeight = results.get(0).getWeight();
caloriesLostPerMile = (int) (userWeight * 0.49); // calories burned per mile
caloriesBurnedPerStep = (double) caloriesLostPerMile / stepsPerMile;
}

return caloriesBurnedPerStep;
}

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

b. Calculate the Base Metabolic Rate.


The given code for the method is added in UsersFacadeREST.java file. It uses
javax.persistence.TypedQuery in order to query the Users table. Then the result is used to fetch
data and perform calculations. The findAge method imports classes like Period and LocalDate in
order to calculate the age which is necessary for performing calculations in
findBaseMetabolicRate method.

@GET
@Path("findBaseMetabolicRate/{userId}")
@Produces(MediaType.TEXT_PLAIN)
public double findBaseMetabolicRate(@PathParam("userId") Integer userId) {

int userAge = 0;
String userGender = "";
double userHeight = 0;
double userWeight = 0;
double baseMetabolicRate = 0;

TypedQuery<Users> q = em.createQuery("SELECT u FROM Users u WHERE u.userId =


:userId", Users.class);
Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939
Calorie Tracker | Assignment 1

q.setParameter("userId", userId);
List<Users> results = q.getResultList();

if (results.isEmpty())
return baseMetabolicRate;
else
{
userGender = results.get(0).getGender();
userHeight = results.get(0).getHeight();
userWeight = results.get(0).getWeight() * 0.45; // considering 1 pound = 0.45
kilogram

java.util.Date birthDate = results.get(0).getDateOfBirth();


java.sql.Date formattedDate = new java.sql.Date(birthDate.getTime());

userAge = findAge(formattedDate);

if (userGender.equals("Male"))
baseMetabolicRate = (13.75 * userWeight) + (5.003 * userHeight) - (6.755 * userAge) +
66.5;
else if (userGender.equals("Female"))
baseMetabolicRate = (9.563 * userWeight) + (1.85 * userHeight) - (4.676 * userAge) +
655.1;

return baseMetabolicRate;
}
}

@GET
@Path("findAge/{dob}")
@Produces(MediaType.TEXT_PLAIN)
public int findAge(@PathParam("dob") Date dob) {

int age, month, year, day;


String formattedBirthDate;
String[] birthdayArray = new String[3];

LocalDate today = LocalDate.now();


LocalDate localBirthdate;

DateFormat dateForm = new SimpleDateFormat("yyyy/MM/dd");


formattedBirthDate = dateForm.format(dob);

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

birthdayArray = formattedBirthDate.split("/");
year = Integer.parseInt(birthdayArray[0]);
month = Integer.parseInt(birthdayArray[1]);
day = Integer.parseInt(birthdayArray[2]);

localBirthdate = LocalDate.of(year, Month.of(month), day);


Period period = Period.between(localBirthdate, today);

age = period.getYears();

return age;
}

c. Calculate the total calories burned at rest.


The given code for the method is added in UsersFacadeREST.java file. It uses
javax.persistence.TypedQuery in order to query the Users table. Then the result is used to fetch
data and perform calculations. The method makes use of switch case for different level of activity
to get the calories burned at rest.

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

@GET
@Path("findCaloriesBurnedAtRest/{userId}")
@Produces(MediaType.TEXT_PLAIN)
public double findCaloriesBurnedAtRest(@PathParam("userId") Integer userId) {

double baseMetabolicRate = 0;
double caloriesBurnedAtRest = 0;
int levelOfActivity = 1;

TypedQuery<Users> q = em.createQuery("SELECT u FROM Users u WHERE u.userId = :userId",


Users.class);
q.setParameter("userId", userId);
List<Users> results = q.getResultList();

if (results.isEmpty())
return caloriesBurnedAtRest;
else
{
levelOfActivity = results.get(0).getLevelOfActivity();
baseMetabolicRate = findBaseMetabolicRate(userId);

switch (levelOfActivity) {
case 1:
caloriesBurnedAtRest = baseMetabolicRate * 1.2;
break;
case 2:
caloriesBurnedAtRest = baseMetabolicRate * 1.375;
break;
case 3:
caloriesBurnedAtRest = baseMetabolicRate * 1.55;
break;
case 4:
caloriesBurnedAtRest = baseMetabolicRate * 1.725;
break;
case 5:
caloriesBurnedAtRest = baseMetabolicRate * 1.9;
break;
default:
break;
}

return caloriesBurnedAtRest;
}
}

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

d. Calculate the total calories consumed.


The given code for the method is added in ConsumptionFacadeREST.java file. It uses
javax.persistence.TypedQuery in order to query the Consumption table. Then the result is used to
fetch data and perform calculations. The for loop is used in order to calculate the total calories
consumed from the data fetched.

@GET
@Path("findTotalCaloriesConsumed/{userId}/{dateOfConsumption}")
@Produces(MediaType.TEXT_PLAIN)
public int findTotalCaloriesConsumed(@PathParam("userId") int userId,
@PathParam("dateOfConsumption") String dateOfConsumption) {

int totalCaloriesConsumed = 0;

TypedQuery<Consumption> query = em.createQuery("SELECT c FROM Consumption c


WHERE c.userId.userId = :userId AND c.dateOfConsumption = :dateOfConsumption",
Consumption.class);
Date date = Date.valueOf(dateOfConsumption);

query.setParameter("userId", userId);
query.setParameter("dateOfConsumption", date);
Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939
Calorie Tracker | Assignment 1

List<Consumption> results = query.getResultList();

for (Consumption consume : results)


{
totalCaloriesConsumed += consume.getFoodId().getAmountOfCalories();
}

return totalCaloriesConsumed;
}

Task 5. Creating the Report

a. Return Single Record data calculation.


The given code for the method is added in ReportsFacadeREST.java file. It uses JsonArrayBuilder
in order to form the JSON response. The for loop is used to add the data to Json object.

@GET
@Path("findReportByUserIdAndDate/{userId}/{dateOfReportGeneration}")
@Produces({"application/json"})

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

public Object findReportByUserIdAndDate(@PathParam("userId") int userId,


@PathParam("dateOfReportGeneration") String dateOfReportGeneration) {

double remainingCalories;

Date date = Date.valueOf(dateOfReportGeneration);


List<Object[]> queryList = em.createQuery("SELECT r.caloriesConsumed, r.totalCaloriesBurned
FROM Reports AS r WHERE r.userId.userId = :userId AND r.dateOfReportGeneration =
:dateOfReportGeneration",Object[].class).setParameter("userId",userId).setParameter("dateOf
ReportGeneration",date).getResultList();

JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();


for (Object[] row : queryList)
{
remainingCalories = ((double) row[0] - (double) row[1]);

JsonObject caloriesObject = Json.createObjectBuilder()


.add("caloriesConsumed", (double)row[0])
.add("totalCaloriesBurned", (double)row[1])
.add("remainingCalorie",remainingCalories)
.build();
arrayBuilder.add(caloriesObject);
}

JsonArray jArray = arrayBuilder.build();


return jArray;
}

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

b. Return Multiple Record data calculation.


The given code for the method is added in ReportsFacadeREST.java file. It uses JsonArrayBuilder
in order to form the JSON response. The for loop is used to perform the calculations and then add
the data to Json object.

@GET
@Path("findReportByTimePeriod/{userId}/{startDate}/{endDate}")
@Produces({"application/json"})
public Object findReportByTimePeriod(@PathParam("userId") int userId,
@PathParam("startDate") String startDate, @PathParam("endDate") String endDate) {

double caloriesConsumed = 0, caloriesBurned = 0;


int totalStepsTaken = 0;

Date startDateFormatted = Date.valueOf(startDate);


Date endDateFormatted = Date.valueOf(endDate);

List<Object[]> queryList = em.createQuery("SELECT r.caloriesConsumed,


r.totalCaloriesBurned, r.totalSteps FROM Reports AS r WHERE r.userId.userId = :userId AND
r.dateOfReportGeneration BETWEEN :startDate AND
:endDate",Object[].class).setParameter("userId",userId).setParameter("startDate",startDateFor
matted).setParameter("endDate",endDateFormatted).getResultList();

JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();


for (Object[] row : queryList)
{
caloriesConsumed += (double)row[0];
caloriesBurned += (double)row[1];
totalStepsTaken += (int)row[2];
}

JsonObject caloriesObject = Json.createObjectBuilder()


.add("caloriesConsumed", caloriesConsumed)
.add("totalCaloriesBurned", caloriesBurned)
.add("remainingCalorie",totalStepsTaken)
.build();
arrayBuilder.add(caloriesObject);

JsonArray jArray = arrayBuilder.build();


return jArray;
}

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939


Calorie Tracker | Assignment 1

Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939

You might also like