Assignment 2/2021-SEM2-CSE3OAD-ASSIGNMENT 2 updated (1).pdf CSE3OAD Assignment 2, Semester 2, 2021 Page 1 of 6 CSE3OAD Assignment 2 Due Date: 15 October 2021, Friday, 10:00 AM Assessment: This...

1 answer below »
This is the question



Assignment 2/2021-SEM2-CSE3OAD-ASSIGNMENT 2 updated (1).pdf CSE3OAD Assignment 2, Semester 2, 2021 Page 1 of 6 CSE3OAD Assignment 2 Due Date: 15 October 2021, Friday, 10:00 AM Assessment: This assignment 2 is worth 25% of the f inal mark for CSE3OAD. This is an individual assignment. Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your own. The Department of Computer Science and Information Technology treats plagiarism very seriously. When it is detected, penalties are strictly imposed. Students are referred to the Department of Computer Science and Information Technology’s Handbook and policy documents with regard to plagiarism. No extensions will be given: Penalties are applied to late assignments (5% of your total assignment mark given is deducted per day, accepted up to 5 days af ter the due date only). If there are circumstances that prevent the assignment being submitted on time, an application for special consideration may be made. See the departmental Student Handbook for details. Note that delays caused by computer downtime cannot be accepted as a valid reason for a late submission without penalty. Students must plan their work to allow for both scheduled and unscheduled downtime. Assignments submitted more than 5 days late (i.e. af ter 10:00 AM, 22 October 2021) will receive the mark of 0. Return of Assignments: Students are referred to the departmental Student Handbook for details. Objectives: To design and implement a RESTful API that will provide an access endpoint to a data source controller similar to the one you implemented using JDBC in Assignment 1. CSE3OAD Assignment 2, Semester 2, 2021 Page 2 of 6 Introduction The aims of the assignment are: • To implement web services for making available resources regarding courses and subjects which are those that you have worked with in Assignment 1. o Using Java Ref lection and Servlet API • To apply the relevant validation strategies to the course, using the Validation Framework we built in Lab 07. Files Provided: The following are provided in the course directory (zip f ile) • The servlet descriptor f ile web.xml (directory course/WEB-INF) • The jar f iles for JSON conversion and JDBC (MySQL) driver (directory course/WEB-INF/lib) • The following in the directory course/WEB-INF/classes o The Java f iles for the Validation framework (complete f iles): ▪ Min.java, MinValidator.java ▪ Max.java, MaValidator.java ▪ NotNull.java, NotNullValidator.java ▪ CharCount.java, CharCountValidator.java ▪ Validator.java, ValidationException.java o The Data Source Controller, CourseDSC.java ▪ Complete (NOTE: do not use your solution from Assignment 1) o The Models (complete) ▪ Course.java (excluding the validation annotations required for task XXX) ▪ Subject.java o The Controllers (incomplete) ▪ CourseController.java ▪ SubjectController.java o The Router Servlet (incomplete) ▪ CourseRouterServlet.java o A few custom Exception sub-classes to be used throughout this Assignment 2 (complete) ▪ MissingArgumentException.java ▪ ResourceNotFoundException.java ▪ UpdateNotAllowedException.java • The SQL Script to create and populate your database (Need to make changes if not on localhost but on latcs7.cs.latrobe.du.au MySQL) o CreateDBScript.sql CSE3OAD Assignment 2, Semester 2, 2021 Page 3 of 6 Task 1 1. Study the Validation Framework provided (complete code provided) as well as the lecture and lab materials that covered Java Ref lection, Annotations and the Validation Framework. 2. TODO: Clearly annotate the relevant f ields of the Course.java model. o You may want to test if your validation on the model is working (see how we did that in the labs) – a static main has been provided in Course.java for you to add your testing code. Task 2 The controllers are the bridge between the CourseDSC class and the CourseRouterServlet class. • Study the complete CourseDSC.java f ile provided, paying attention to its constructor (some changes have been made, which dif fers from Assignment 1) o NOTE: the constructor requires database host, username and password as arguments For each controller (CourseController.java and SubjectController.java) • TODO: Complete each of the method stubs provided. Each method stub needs to make a call to a relevant CourseDSC.java method. Identify which one and code it in the controllers. o You may want to test if the controllers are responding properly when calling each CourseDSC method – a static main method has been provided in each class for you to add your testing code. CSE3OAD Assignment 2, Semester 2, 2021 Page 4 of 6 Task 3 Implement the servlet (CourseRouterServlet class) - The purpose of this servlet class is to: 1. Identify which resource the URL is requesting, using HttpServletRequest • example, the resource f rom a browser call to http://localhost:8080/course/api/subject will then be “course” (the same applies for a call to http://localhost:8080/course/api/SubjEct) • the HTTP method can also be found here (in this example, a browser call means using HTTP GET method) 2. Using the Servlet API, • retrieve your database host, username and password f rom the servlet descriptor f ile web.xml. (add your MySQL username & password where needed in f ile web.xml) 3. Using the Ref lection API, a. Find if the resource controller exists - f rom the above example, resource “course” should have a matching controller class CourseController [hint: using Class.forName(…)] b. Create an instance of such controller class NOTE: • HTTP GET (with no parameters identif ied) maps to controller method get() • HTTP GET (with parameter identif ied) maps to controller method get(id) • HTTP POST (with parameter identif ied) maps to controller method add(…) • HTTP PUT (with parameter identif ied) maps to controller method update(id) • HTTP DELETE (with parameter identif ied) maps to controller method delete(id) c. If we have an HTTP GET method, with no identif iable parameters (f rom the above example), f ind if CourseController class has a no-argument method named get() • then, make a call to this method, and store the returned data d. Do the same (a, b, c above) if the resource identif ied is “subject” • NOTE: clearly distinguish the dif ference between controller classes CourseController and SubjectController 4. Using the Gson package provided and HttpServletResponse a. Convert any returned data (f rom controller method call) to JSON data (using Gson) b. Convert any valid case messages (if any) to JSON data (using Gson) c. Send the any JSON data back to the browser http://localhost:8080/course/api/subject http://localhost:8080/course/api/SubjEct CSE3OAD Assignment 2, Semester 2, 2021 Page 5 of 6 d. Making sure any potential errors (throughout steps 1-4 described for this Task 3) are caught, identif ied and send back to the browser (using GSON to convert messages to JSON) and using appropriate HTTP Status Codes and adequate (descriptive) error messages. You will use Postman to test your API (https://www.getpostman.com/) – More on Postman will be covered in your lab sessions. Testing your API (Using Postman) The resources to access (using Postman for example) for the assignment 2 are: • http://localhost:8080/course/api/course on the above you can ▪ get all (GET http method with the above URL) ▪ get one (GET http method with id 10: http://localhost:8080/course/api/course/10 ) ▪ add one (POST http method with above URL, with additional course info in JSON format sent as POST data) ▪ delete one (DELETE http method with id 11: http://localhost:8080/course/api/course/11) ▪ update one (PUT http method with id 9: http://localhost:8080/course/api/course/9) • http://localhost:8080/course/api/subject ▪ get all subjects (GET http method with the above url) NOTE: The update course requirement is different from usual updates (only one field is updated, that is, number of students) – so the action of reducing student quantity is done on the course found (if any) by the provided id. You still need to provide a full json format/data for the updated course, only one field (numberOfStudents) get updated. the id (10, 11, 9) provided above are examples - courses with those id should exist in your database for the relevant action to be possible. (Else it should return a well-defined and meaningful error message) https://www.restapitutorial.com/httpstatuscodes.html https://www.getpostman.com/ http://localhost:8080/course/api/course http://localhost:8080/course/api/course/10 http://localhost:8080/course/api/course/11 http://localhost:8080/course/api/course/9 http://localhost:8080/course/api/subject CSE3OAD Assignment 2, Semester 2, 2021 Page 6 of 6 Consultation and other resources 1. Consultation sessions will be announced (LMS) prior to the submission date of this Assignment 2. 2. You are strongly advised to regularly check the Assignment 2 LMS section for any hints, clarif ication or corrections regarding Assignment 2. What to submit Electronic copy of all of the classes required to run the application, including those that are provided, are to be submitted through LMS. All of your classes must be able to be compiled f rom their current directory. This means, they must not be contained in any package. As for the database, you can use the one on latcs7 or on your local machine. The only requirement is that your program should work on the database tables course and subject which must have the same structure as the one in the provided MySQL script (CreateDBScript.sql). For each class that you submit, you must include, as part of the comments, at the beginning of each f ile, o your full name (with surname in capital), o your student ID, o your username (if dif ferent f rom student ID), and o the subject code (CSE3OAD) Assignment submissions without any of these will have 5% of the mark deducted. Assignment 2/Provided Files (1).zip Provided Files/course/WEB-INF/classes/CharCount.java Provided Files/course/WEB-INF/classes/CharCount.java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** Define annotation interface CharCount.  *  If both params gets defaults, Validator should through error.  *  Only applies to String; If not String, Validator should through error.  */ @Retention(RetentionPolicy.RUNTIME) // Process this annotation at runtime @Target(ElementType.FIELD) // This is an annotation on a field (attribute) public @interface CharCount {     public int min() default 0;     public int max() default 0; } Provided Files/course/WEB-INF/classes/CharCountValidator.java Provided Files/course/WEB-INF/classes/CharCountValidator.java import java.lang.reflect.*; import java.lang.annotation.Annotation; public class CharCountValidator extends Validator {     private static CharCount charCount;     public void applyRule(Annotation annotation, Object fieldValue, Class fieldType) throws Exception {         charCount = (CharCount) annotation;         /**          * developer/coder throws for potential annotating errors          */         if (!fieldType.equals(String.class))             throw new ValidationException(" is not of type String; Annotation @CharCount can only be applied to fields of type String.");         if (charCount.min() <>< 0)             throw new validationexception(" annotation @charcount parameters cannot be less than zero.");=""         if (charcount.min() ="= 0 && charCount.max() == 0)"             throw new validationexception(" annotation @charcount parameters min() and max() cannot both be zero (their default values). at least one of these parameters has to set to an integer greater than zero.");=""         string fvalue =" fieldValue.toString();"         int fieldlength =" fValue.length();"         boolean valid =" true;"         string message =" "";"         /**=""          * user relevant error messages=""          */=""         if (charcount.min() =""> 0 && charCount.max() > 0) {             if (fieldLength < charcount.min() || fieldlength > charCount.max()) {                 valid = false;                 if (charCount.min() == charCount.max())                     message = " character count (length) has to be exactly "  + charCount.min();                 else {                     message = " character count (length) has to be between "  + charCount
Answered 27 days AfterOct 03, 2021

Answer To: Assignment 2/2021-SEM2-CSE3OAD-ASSIGNMENT 2 updated (1).pdf CSE3OAD Assignment 2, Semester 2, 2021...

Swapnil answered on Oct 10 2021
119 Votes
92705/Course/bin/CharCount.class
92705/Course/bin/CharCountValidator.class
92705/Course/bin/Course.class
92705/Course/bin/CourseController.class
92705/Course/bin/CourseDSC.clas
s
92705/Course/bin/CourseRouterServlet.class
92705/Course/bin/CreateDBScript.sql
DROP DATABASE IF EXISTS `coursedb`;
CREATE DATABASE `coursedb` ;
USE `coursedb`;
DROP TABLE IF EXISTS `subject`;
CREATE TABLE `subject` (
`code` varchar(20) NOT NULL,
`name` varchar(50) NOT NULL,
`hasPrerequisites` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`code`),
UNIQUE KEY `name_UNIQUE` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOCK TABLES `subject` WRITE;
INSERT INTO `subject` VALUES ('CSE3OAD','OBJECT-ORIENTED APPLICATION DEVELOPMENT',1),('CSE1IOO','INTERMEDIATE OBJECT-ORIENTED PROGRAMMING',1),('CSE1OOF','OBJECT-ORIENTED PROGRAMMING FUNDAMENTALS',0),('CSE2DES' ,'SYSTEM DESIGN ENGINEERING FUNDAMENTALS',1),('CSE2DBF','DATABASE FUNDAMENTALS',1),('CSE1PE','PROGRAMMING ENVIRONMENT',0),('MAT1DM','DISCRETE MATHEMATICS',0),('CSE5DMI','DATA MINING',1);
UNLOCK TABLES;
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`subjectCode` varchar(20) NOT NULL,
`startingDate` varchar(10) DEFAULT NULL,
`numberOfStudents` int(11) DEFAULT NULL,
`semester` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `subjectCode_idx` (`subjectCode`),
CONSTRAINT `fk_course_subject` FOREIGN KEY (`subjectCode`) REFERENCES `subject` (`code`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8;
LOCK TABLES `course` WRITE;
INSERT INTO `course` VALUES (1,'CSE1IOO','17/07/2021',20,'SE2'),(2,'CSE1OOF','20/03/2021',21,'SE1'),(3,'CSE2DBF','24/11/2021',23,'SUM');
UNLOCK...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here