Consider the UML class diagram shown below. Create the corresponding Java Data Model that implements the equivalent relational model, fulfills the use cases, implements the relations, and enforces the constraints. Implement Data Access Objects (DAOs) that encapsulate access to the database applying best practices discussed in class. βA link to this assignment can be found hereβ.
UML Class Diagram
Disregard the attributes for the User class in this diagram. See the below diagram for updated user attributes.
Β
Β
Create Java Data Model (5pts.)
Use Java to implement the data model. Make use of best practices discussed in class. Each instance variables must be declared as private and have public setters and getters.
Β
| 1.Β Person.java
2.Β User.java 3.Β Developer.java 4.Β Website.java 5.Β Page.java 6.Β Widget.java |
7.Β Β HeadingWidget.java
8.Β Β HtmlWidget.java 9.Β Β ImageWidget.java 10.Β YouTubeWidget.java 11.Β Role.java 12.Β Priviledge.java |
Constructor Requirements: (5Pts)
There are no constructor requirements for classes not specified below. You are free to implement constructors as you see fit.
1. Developers.java
- Should create an instance of developer when developer Key, Developer ID, Developer First Name, Developer Last Name, with rest of the fields with default values specified by you.
- Should also create an instance of developer when developer key, developer ID, developer first name, developer last name, developer user-name, developer password, email and dob are passed as parameters.
2. User.java
- Should create an instance of user when user ID, First Name, Last Name. All new users cannot be approved users.
3. Websites.java
- Should create an instance of website when id, website name, description, created, updated, and views are passed as parameters.
4. Page.java
- Should create an instance of Page when id, title, description, created, updated and views are passed as parameters.
5. Widget.java
- Should create an instance of Widget when ID, name, width, height, cssStyle, cssCLass, text, order are passed as parameters.
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
UML Diagram
The UML diagram defines several associations between various classes using lines and symbols. These are implemented in SQL using primary and foreign key fields. In Java youβll also capture the primary key fields id, but instead of the foreign key fields, youβll capture some collection of instances of the child class. As an example, consider the relation between entities Website and Page illustrated below. The UML class diagram captures the association using a line and the cardinality of how many of each participate in relation. The diagram says that each Website instance has zero or more Page instance.
Β
| UML | SQL | Java |
CREATE TABLE Website ( Β Β Β Β class Website {
id int, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β int id;
PRIMARY KEY (id) Β Β Β Β Β Β Β Β Β Β Collection<Page> pages;
); Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β addPage(Page) { }
CREATE TABLE Page ( Β Β Β Β Β Β Β Β Β removePage(Page) { }
id int, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }
websiteId int, Β Β Β Β Β Β Β Β Β Β class Page {
FOREIGN KEY (websiteId)Β Β Website website;
REFERENCES Website(id) }
);
Β
Β
SQL implements the relation using primary and foreign keys. In the example, the Pageβ table declares foreign key βwebsiteId which references to the βid field in the βWebsite table. If we consider the relation between websites and pages as a whole/parts relation, where pages are the various parts that make a whole Website, then the relation implementation states that each part (page) know who the whole is (website). The Java implementation captures the relation between websites and pages by declaring a collection of pages in the βWebsite class. That is, each βWebsite instance has a complete list of references to its Pageβ instance. Notice the difference in the direction of the relation between the SQL and the Java relation implementation. Whereas in the SQL implementation the relation is from the part to the whole, e.g., page records have foreign key references to websites, in the Java implementation, the relation goes from the whole to the parts, e.g., the websites have a collection of references to page instances. Additionally, the Java implementation also can declare a reference from the page to the website which matches the SQL implementation, but this is less common in object oriented solutions.
Β
A good naming convention for primary and foreign keys is to name all foreign keys βidβ ββ (or βIDβ ββ) and name the foreign keys that refer to them as the table name followed by βIdβ β.β For instance, from the example above, the foreign key from table βPage to table βWebsite should be named websiteIdβ and should refer to the primary key field βidβ in table βWebsiteβ.
Naming Conventions (5pts)
These are the naming conventions for the project and they must be followed. Else your JUnit grader will fail and would not grade your assignments.
Β
- Your Projects must be named cs5200_fall2018_lastname_firstname_jdbc
- All Packages must be created in src->main->java->edu->northeastern->cs5200
- Inside your edu.northeastern.cs5200 package,
- All DAOs must be in package called β
- All models must be in package called modelsβ β.
- If other packages are created, please specify their names in README.txt.
Implementation(5Pts)
**All applications should be deployed in AWS, unless otherwise excused.**
Β
In src->main->java>edu>northeastern>cs5200 package, create a public Singleton class called Connection.java.Β
Β
public class Connection {
Β
private static final String DRIVER = βcom.mysql.jdbc.Driverβ;
private static final String URL = βjdbc:mysql://<AWS ENDPOINT>/<SCHEMA NAME>β; private static final String USER = <AWS RDS USERNAME>; private static final String PASSWORD = <AWS RDS PASSWORD>;
Β
public static Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName(DRIVER);
return DriverManager.getConnection(URL, USER, PASSWORD);
}
Β
public static void closeConnection(Connection conn) {
try {Β Β Β Β conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch blockΒ Β Β Β Β Β Β Β Β Β Β e.printStackTrace();
}
}
}
Β
So, everytime the test method calls the static method called getInstance() of the
Connection.java class, it should return the same instance of the connection rather than creating a new instance.
The class should have method called closeConnection(). It must be a static method and should not return anything. It closes the above said connection.
Create Data Access Objects (40pts.)
A good practice is to encapsulate all data access in a set of classes whose only responsibility is to provide an API to the database. These types of classes are commonly referred to as Data Access Objects, or DAOs. Implement the following DAOs that encapsulate the access to the database for each of the entities and relations in the UML class diagram. Β
- DeveloperDao.java 4. WidgetDao.java 2. WebsiteDao.java 5. RoleDao.java 3. PageDao.java Β 6. PriviledgeDao.java
Implement the Developer Data Access Object
In a file called βDeveloperImpl.javaβ, create an interface shown below. Please do not make any modification to the interface.
Β
1. void createDeveloper(Developer developer)
inserts properties in βdeveloperβ instance parameter in tables βDeveloperβ and Personβ
- Collection<Developer> findAllDevelopers() returns all joined records from βDeveloper and βPerson tables as a βCollection of Developerβ instances.
3. Developer findDeveloperById(int developerId)
returns a joined record from βDeveloper and βPerson tables whose βid field is equal to the developerIdβ parameter
- Developer findDeveloperByUsername(String username) returns a joined record from βDeveloper and βPerson tables whose βusername field matches the parameter.
5. Developer findDeveloperByCredentials(String username, String password) returns a joined record from βDeveloper and βPerson tables whose βusername and password fields match the parametersβ
- int updateDeveloper(int developerId, Developer developer) updates records in βDeveloper and βPerson tables whose idβ field is equal to developerId New record field values are set to the values in the βdeveloper instance parameter.
- int deleteDeveloper(int developerId) deletes records from βDeveloper and βPerson tables whose βid field is equal to developerId β βDo not make any modifications to the instance provided.
Β
In a file called DeveloperDao.javaβ ,β develop a class which implements the Developer interface and encapsulates all database interaction between the βDeveloper and βPerson entities and relations.
Β
When returning the developer instance, create a new developer instance and do not modify the existing developer instance provided in the parameters.
Implement Website Data Access Object
In a file called βWebsiteImpl.javaβ, create an interface as shown below. Do not make any modifications to the interface.
Β
1. void createWebsiteForDeveloper(int developerId, Website website)
inserts properties in βwebsite instance parameter into the βWebsite table. The websiteβs developerId foreign key refer to βDeveloper βtable primary key βid whose value is equal to the developerIdβ parameter. You can use the ownerβs user id as the foreign keyβ
- Collection<Website> findAllWebsites()
returns all records from βWebsiteβ table as a βCollectionβ of βWebsiteβ instances
- Collection<Website> findWebsitesForDeveloper(int developerId) returns all records from βWebsite table as a βCollection of βWebsite instances whose developerIdβ is equal to the βdeveloperIdβ parameter Website findWebsiteById(int websiteId)
returns a record from βWebsiteβ table whose βidβ field is equal to the βwebsiteIdβ parameter
5. int updateWebsite(int websiteId, Website website)
updates record in βWebsite table whose βid field is equal to βwebsiteId parameter. New record field values are set to the values in the βwebsiteβ instance parameter
6. int deleteWebsite(int websiteId)
deletes record from βWebsiteβ table whose βidβ field is equal to βwebsiteId parameterβ
Β
In a file called βWebsiteDao.javaβ, implement a class that encapsulates all database interaction between the βWebsiteβ and βDeveloperβ entities and relations.
Β
When returning the website instance, create a new website instance and do not modify the existing developer instance provided in the parameters.
Implement Page Data Access Object
In a file called βPageImpl.java, βcreate an interface with the methods given below. Do not make any modifications.
Β
1. void createPageForWebsite(int websiteId, Page page)
inserts properties in pageβΒ Β Β instance parameter into the PageβΒ Β Β Β Β table. The pageβs websiteIdβΒ Β Β Β Β Β Β Β Β Β Β foreign key refer to WebsiteβΒ Β Β Β Β Β βtable primary key idβ whose value is equal to the βwebsiteId parameter
- Collection<Page> findAllPages()
returns all records from βPageβ table as a βCollectionβ of βPageβ instances
- Page findPageById(int pageId)
returns a record from βPageβ table whose βidβ field is equal to the βpageIdβ parameter
4. Collection<Page> findPagesForWebsite(int websiteId)
returns all records from βPage table as a βCollection of βPage instances whose βwebsiteId is equal to the βwebsiteIdβ parameter
5. int updatePage(int pageId, Page page)
updates record in βPage table whose βid field is equal to βpageId parameter. New record field values are set to the values in the βpageβ instance parameter
6. int deletePage(int pageId)
deletes record from βPageβ table whose βidβ field is equal to βpageIdβ parameter
Β
In a file called βPageDao.javaβ, implement a class that encapsulates all database interaction between the βWebsiteβ and βPageβ entities and relations. Use the interface shown below.
Β
When returning the page instance, create a new page instance and do not modify the existing developer instance provided in the parameters.
Implement Widget Data Access Object
In a file called βWidgetImpl.java, βcreate an interface with the methods given below. Do not make any modifications.
Β
1. int createWidgetForPage(int pageId, Widget widget)
inserts properties in βwidget instance parameter into the βWidget table. The widgetβs pageId foreign key refer to βPage βtable primary key βid whose value is equal to the pageIdβ parameter
- Collection<Widget> findAllWidgets()
returns all records from βWidgetβ table as a βCollectionβ of βWidget instancesβ
3. Widget findWidgetById(int widgetId)
returns a record from βWidgetβ table whose βidβ field is equal to the βwidgetIdβ parameter
- Collection<Widget> findWidgetsForPage(int pageId) returns all records from βWidget table as a βCollection of βWidget instances whose pageIdβ is equal to the βpageIdβ parameter
- int updateWidget(int widgetId, Widget widget) updates record in βWidget table whose βid field is equal to βwidgetId New record field values are set to the values in the βwidgetβ instance parameter
6. int deleteWidget(int widgetId)
deletes record from βWidgetβ table whose βidβ field is equal to βwidgetIdβ parameter
Β
In a file called βWidgetDao.javaβ, implement a class that encapsulates all database interaction between the βWidget and β βPage entities and relations.β
Β
When returning the widget instance, create a new widget instance and do not modify the existing developer instance provided in the parameters.
Implement Role Data Access Object
In a file called βRoleDao.javaβ, implement a class that encapsulates all database access that manage website and page roles assigned to βDeveloperβ. Use the interface shown below. Feel free to implement additional or improved versions of the API
Β
- assignWebsiteRole(int developerId, int websiteId, int roleId)
inserts into table Role a record that assigns a developer whose id is developerId, the role with roleId, to the website with websiteId
- assignPageRole(int developerId, int pageId, int roleId)
inserts into table Role a record that assigns a developer whose id is developerId, the role with roleId, to the page with pageId
- deleteWebsiteRole(int developerId, int websiteId, int roleId)
deletes from table Role a record that removes roleId from developerId, on websiteId
- deletePageRole(int developerId, int pageId, int roleId)
deletes from table Role a record that removes roleId from developerId, on pageId
Implement Priviledge Data Access Object
In a file called PriviledgeDao.javaβ ,β implement a class that encapsulates all database access that manage website and page priviledges assigned to βDeveloperβ. Use the interface shown below. Feel free to implement additional or improved versions of the API
Β
1. assignWebsitePriviledge(int developerId, int websiteId, String priviledge)
inserts into table Priviledge a record that assigns a developer whose id is developerId, the priviledge with priviledgeId, to the website with websiteId
2. assignPagePriviledge(int developerId, int pageId, String priviledge)
inserts into table Priviledge a record that assigns a developer whose id is developerId, the priviledge with priviledgeId, to the page with pageId
- deleteWebsitePriviledge(int developerId, int websiteId, String priviledge)
deletes from table Priviledge a record that removes priviledgeId from developerId, on websiteId
4. deletePagePriviledge(int developerId, int pageId, String priviledge)
deletes from table priviledge a record that removes priviledgeId from developerId, on pageId
Β
Exercise Your Data Model and DAOs (20pts.)
In a file called hw_jdbc_last_first.java, use the Data Model and Data Access Objects implemented earlier, to create the data shown below
Β
- (5pts.) Create the following developers and users. Insert into the correct tables depending on the type
Β
| id | Username | Password | First | Last | Type | Key | |
| 12 | alice | alice | Alice | Wonder | Developer | [email protected] | 4321rewq |
| 23 | bob | bob | Bob | Marley | Developer | [email protected] | 5432trew |
| 34 | charlie | charlie | Charles | Garcia | Developer | [email protected] | 6543ytre |
| 45 | dan | dan | Dan | Martin | Use | [email protected] | 7654fda |
| 56 | ed | ed | Ed | Karaz | User | [email protected] | 5678dfgh |
Β
- (5pts.) Create the following web sites for the developers above. For both the created field and updated field, use the date your assignment will be graded, e.g., do not hardcode it
Β
| id | Name | Description | Owner | Editor | Admin | Visits |
| 123 | an online social media and social networking service | alice | bob | charlie | 1234234 | |
| 234 | an Β Β Β Β online Β Β Β news Β Β Β and Β Β Β Β social
networking service |
bob | charlie | alice | 4321543 | |
| 345 | Wikipedia | a free online encyclopedia | charlie | alice | bob | 3456654 |
| 456 | CNN | an American basic cable and
satellite television news channel |
alice | bob | charlie | 6543345 |
| 567 | CNET | an American media website that publishes reviews, news, articles, blogs, podcasts and videos on
technology Β Β Β Β and Β Β Β Β consumer electronics |
bob | charlie | alice | 5433455 |
| 678 | Gizmodo | a design, technology, science and science fiction website that also | charlie | alice | bob | 4322345 |
| writes articles on politics |
Β
- (5pts.) Create the following pages for the web sites above. Use the semesterβs start date for the created field. Use the assignmentβs due date for the updated field.
Β
| id | Name | Description | Website | Editor | Reviewer | Writer | Views |
| 123 | Home | Landing page | CNET | alice | bob | charlie | 12343
4 |
| 234 | About | Website description | Gizmodo | bob | charlie | alice | 23454
5 |
| 345 | Contact | Addresses, phones, and contact info | Wikipedia | charlie | alice | bob | 34565
6 |
| 456 | Preferences | Where users can configure their
preferences |
CNN | alice | bob | charlie | 45677
6 |
| 567 | Profile | Users can configure their personal
information |
CNET | bob | charlie | alice | 56787
8 |
Β
- (5pts.) Create the following widgets for the pages shown.
Β
| Name | Type | Text | Order | Width/Height | Url | Page |
| head123 | heading | Welcome | 0 | null | null | Home |
| post234 | html | <p>Lorem</p> | 0 | null | null | About |
| head345 | heading | Hi | 1 | null | null | Contact |
| intro456 | html | <h1>Hi</h1> | 2 | null | null | Contact |
| image345 | image | null | 3 | 50Γ100 | /img/567.png | Contact |
| video456 | youtube | null | 0 | 400Γ300 | https://youtu.be/
h67VX51QXiQ |
Preference s |
Β
Implement Updates (15pts.)
Using the DAOs implemented earlier, do the following updates
- Update developer β Update Charlieβs primary phone number to 333-444-5555
- Update widget β Update the relative order of widget head345 on the page so that itβs new order is 3. Note that the other widgetβs order needs to update as well
- Update page β Append βCNET β β to the beginning of all CNETβs page titles
- Update roles β Swap Charlieβs and Bobβs role in CNETβs Home page
Implement Deletes (15pts.)
Using the DAOs implemented earlier, do the following deletes
- Delete developer β Delete Aliceβs primary address
- Delete widget β Remove the last widget in the Contact page. The last widget is the one with the highest value in the order field
- Delete page β Remove the last updated page in Wikipedia
- Delete website β Remove the CNET web site, as well as all related roles and privileges relating developers to the Website and Pages
Stored Procedures (10pts.)
Using the DAOs implemented, create stored procedures for the following cases.
- Create a Stored Procedure βgetUnasnweredQuestionsβ which retrieves the question with most number of answers and no correct answer. Group the questions by module and print the question text and numbers of answer for the question. (5M)
- Create a Stored Procedure, βendorsedUsersforWeekβ, which retrieves the 5 most endorsed users (having the most number of correct answers for the questions posted in the given week). If there multiple endorsed Users per week, sort by their First Name. (5M)
Deliverables
In the folder provided to you, submit your entire code into the directory on Github. And also make a submission on Blackboard with the submission text as your Github URL.
Β




