Professional Documents
Culture Documents
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
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
Credentials Table
Creating the credentials table
Food Table
Creating the food table
Consumption Table
Creating the consumption table
Reports Table
Creating the reports table
b. This section deals with the population of data in the tables. Required “INSERT”
commands are written for adding data into the tables.
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);
c. Entity-Relationship Diagram
(This screenshot shows the Entity-Relationship diagram made in for the Calorie Tracker Server Database.)
(This screenshot shows the Restful Web Service created for the Calorie Tracker Application.)
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();
}
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");
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.
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.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);
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.setParameter("dateOfConsumption", date);
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");
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.setParameter("dateOfReportGeneration", date);
return query.getResultList();
}
@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();
}
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.setParameter("name", name);
query.setParameter("signupDate", date);
return query.getResultList();
}
@GET
@Path("findCaloriesBurnedPerStep/{userId}")
@Produces(MediaType.TEXT_PLAIN)
public double findCaloriesBurnedPerStep(@PathParam("userId") Integer userId) {
int stepsPerMile;
int caloriesLostPerMile;
double userWeight;
double caloriesBurnedPerStep;
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;
}
@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;
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
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) {
birthdayArray = formattedBirthDate.split("/");
year = Integer.parseInt(birthdayArray[0]);
month = Integer.parseInt(birthdayArray[1]);
day = Integer.parseInt(birthdayArray[2]);
age = period.getYears();
return age;
}
@GET
@Path("findCaloriesBurnedAtRest/{userId}")
@Produces(MediaType.TEXT_PLAIN)
public double findCaloriesBurnedAtRest(@PathParam("userId") Integer userId) {
double baseMetabolicRate = 0;
double caloriesBurnedAtRest = 0;
int levelOfActivity = 1;
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;
}
}
@GET
@Path("findTotalCaloriesConsumed/{userId}/{dateOfConsumption}")
@Produces(MediaType.TEXT_PLAIN)
public int findTotalCaloriesConsumed(@PathParam("userId") int userId,
@PathParam("dateOfConsumption") String dateOfConsumption) {
int totalCaloriesConsumed = 0;
query.setParameter("userId", userId);
query.setParameter("dateOfConsumption", date);
Mobile and Distributed Computing Systems Pooja Pancholi ID: 29984939
Calorie Tracker | Assignment 1
return totalCaloriesConsumed;
}
@GET
@Path("findReportByUserIdAndDate/{userId}/{dateOfReportGeneration}")
@Produces({"application/json"})
double remainingCalories;
@GET
@Path("findReportByTimePeriod/{userId}/{startDate}/{endDate}")
@Produces({"application/json"})
public Object findReportByTimePeriod(@PathParam("userId") int userId,
@PathParam("startDate") String startDate, @PathParam("endDate") String endDate) {