Assignment title: Information


SWE80005 Ent. Dev. Lab 04 Stateless Session Bean ãSwinburne University of Technology 2017 Page 1 of 7 Note: This document is developed based on the NetBeans IDE 8.2. Modification history Created on: 14th Feb 2017 Purpose: Develop a Java EE application for accessing MYUSER database Modified on: 4th Mar 2017 Purpose: Make sure lines in the code segment break nicely Software To finish the lab, you may need the following software: 1. NetBeans IDE version 8.2 or later (or, NetBeans) 2. JDK version 1.8.0 update 121 or later (or, JDK) 3. GlassFish Server Open Source Edition version 4.1.1 or later (or, GlassFish) Aim Programming the EJB Stateless Session Bean "MyuserBean" and its client application. Figure 1 shows a rough design of the "MyuserAppClient" application and its related classes, including their roles. The "MyuserAppClient.java" acting as a client will handle the input from users to perform the CRUD operations of the MYUSER database table via the Myuser class (ORM). As mentioned in Lab 03, we need a JPA controller for a better design. In Lab 03, we did this in a normal Java class. However, as this normal Java class is not managed by any server, we could not achieve scalability. In this lab, we will be programming a StateLess Session Bean (SLSB) to take on this role. As the SLSB is managed by the GlassFish server, scalability is achieved. You need to read about the "theory" stuff (in lecture notes) on how GlassFish server (or other Java EE compliant server) does all these to achieve "scalability". When doing this lab using the "auto-code generation features" provided by NetBeans, it has its own naming convention. NetBeans called this SLSB, "MyuserFacade", ("…Façade") because it follows the famous Façade design pattern. Also, the naming conventions for the remote interface of the Façade is "…FacadeRemote" whereas the local interface is "…FacadeLocal". Figure 1 A rough design of MyuserAppClient, a ED-JEE-DTO project Overview of Lab Tasks In this Lab, you will learn to perform the following tasks in programming a stateless session bean: LT1. Create a Java Class Library for the Remote Interface of the stateless session bean LT2. Program a DTO class and put it in the Remote Interface project LT3. Create an Enterprise Application project LT4. Add an Entity class to the Enterprise Application's EJB project LT5. Create the Stateless Session Bean LT6. Program the Remote business method in the Stateless Session Bean LT7. Program the Remote Interface class of an EJB Stateless Session Bean LT8. Program a client application that calls an EJB Stateless Session Bean Myuser AppClient MyuserFacade Myuser DB4server MYUSER4 (table) Thin4client4(UI) A Get4input4from4users A Display4output4to4users MyuserFacade (SB4as4Controller) A Emulate4DB4function A ORM4APIs4here Myuser (Entity4class) A Map4to4MYUSER4table DB4server A Actual4DB4functions MYUSER4(database4table) A Store4information MyuserDTO (DTO) A For4remote4communications MyuserDTO (DTO) ED#JEE#DTO'projectSWE80005 Ent. Dev. Lab 04 Stateless Session Bean ãSwinburne University of Technology 2017 Page 2 of 7 LT9. Deploy the Enterprise Application to the GlassFish server LT10. Run the Enterprise Application Client Lab Tasks This Lab should be run on MS Windows Platform LT1. Create a new Java Class Library Project in NetBeans called "ED-JEE-DTO-RI" to hold the remote interface of the stateless session bean LT1.1 Select "File" > "New Project" LT1.2 Choose "Java" > "Java Class Library", then click "Next" LT1.3 Enter "ED-JEE-DTO-RI" in the "Project Name" field LT1.4 Click "Finish" *Note: The project has been created. We will just leave it as is. NetBeans will automatically put the required information in this project when necessary as we perform the learning task in this lab. LT2. Program the Data Transfer Object (DTO) class for "Myuser.java" LT2.1 Create a new Java class "MyuserDTO" in the package "entity" in the "ED-JEE-DTO-RI" project LT2.2 Make sure that "MyuserDTO" implements the "java.io.Serializable" interface Hint: Add "implements Serializable" in between "public class MyuserDTO" and "{". Remember to fix imports. LT2.3 Copy and paste the following code segments into "MyuserDTO" class private final String userid; private final String name; private final String password; private final String email; private final String phone; private final String address; private final String secQn; private final String secAns; LT2.4 Generate a constructor a. Right click within the "MyuserDTO" class, select "Insert Code…" > "Constructor…" b. In the "Generate Constructor" window, click the "Select All" button > "Generate" Note: NetBeans generates a constructor that sets all the instance variables from its parameters. LT2.5 Generate the getters of these instance variables (Only getters are required. No setters.) Note: NetBeans generates all the getters. LT3. Create a new Enterprise Application Project in NetBeans called "ED-JEE-DTO" LT3.1 Select "File" > "New Project" LT3.2 Choose "Java EE" > "Enterprise Application", then click "Next" LT3.3 Enter "ED-JEE-DTO" in the Project Name field, then click "Next" LT3.4 Use the default server "GlassFish" and Java EE version "Java EE 7" LT3.5 Make sure the following check boxes are checked a. "Create EJB Module" – "ED-JEE-DTO-ejb" b. "Create Web Application Module" – "ED-JEE-DTO-war" LT3.6 Click "Finish" *Note: There are three new "projects" in the "Projects" Windows. They are a. ED-JEE-DTO (Enterprise Application Project) b. ED-JEE-DTO-ejb (Enterprise Application's EJB Project) where our SLSB will sit c. ED-JEE-DTO-war (Enterprise Application's Web Application Project) where our Web App will sit (see Lab 5) LT4. Add an Entity class to the EJB Project LT4.1 Select the "ED-JEE-DTO-ejb" project LT4.2 Right click the mouse and select "New" > "Entity Class from Database…"SWE80005 Ent. Dev. Lab 04 Stateless Session Bean ãSwinburne University of Technology 2017 Page 3 of 7 LT4.3 In the "New Entity Classes from Database" window, select "jdbc/__default" in the Data Source field and put the "MYUSER" table in the "Selected Tables" text area (Make sure the "Include Related Tables" check box is checked) Note: If you cannot find the "MYUSER" table in the "Available Tables:" field, make sure your database connection string is right and you have set up the database properly with the proper username "APP" and password "APP". You may want to remove the database and recreate one with the right settings. See Lab_01a_Setup_JavaEE. Note: When asked about the database username and password, enter "APP" for both (or, whatever you put during the setting up of your own databases). LT4.4 Click "Next" Note: A new dialog box appears with "MYUSER" in the "Class Names" field and "ED-JEE-DTOejb" in the Project field LT4.5 Enter "entity" in the "Package" field LT4.6 Make sure that the following check boxes are checked a. "Generate Named Query Annotations for Persistent Fields" Note: By checking the box, NetBeans will create some standard named queries like "Myuser.findAll" to find all myusers in the database table "Myuser.findByUserid" to find a particular myuser using userid (userid is the primary key of the table) … (others omitted) … b. "Generate JAXB Annotations" Note: By checking the box, NetBeans will create some standard annotations that allow the entity class to be used via JAXB web services. We do not need this actually. c. "Create Persistence Unit" LT4.7 Click "Finish" Note: NetBeans will automatically generate the entity POJO "Myuser.java" for you, including some named queries. Note: If you do not have any methods to update the data, this entity class is now complete. Note: This entity class is a Data Access Object (DAO) that is responsible for holding the required information for the records in the MYUSER database table. Note: In the source folder, you can see a "package" called "META-INF". The file "persistence.xml" is the persistence unit of Myuser, in xml format and act as a config file. LT4.8 This task will fix some "unknown" bug in NetBeans a. Expand on "Configuration Files…" b. Double click on "persistence.xml" c. In the "persistence.xml" pane, uncheck "Include All Entity …" d. Click on "Add Classes…" e. In the "Add Entity Class" window, select "entity.Myuser" and click "OK" f. Click "Save" LT5. Create the Stateless Session Bean class, called "MyuserFacade" LT5.1 Select the "ED-JEE-DTO-ejb" project LT5.2 Right click the mouse and select "New" > "Session Beans…"1 Note: If you cannot find "Session Beans…", select "Other…". Then, in the "New File" window, choose "Enterprise JavaBeans" in the "Categories:" selection box, then choose "Session Bean" in the "File Types:" selection box. Then, click "Next". LT5.3 In the "New Session Bean" window, a. Enter "MyuserFacade" in "EJB Name" text field b. Enter "session" in the "Package" text field c. Select "Stateless" for "Session Type" d. Check the "Remote" checkbox in "Create Interface" 1 We do not use "Session Beans from Entity Classes…" option due to some unknown NetBeans issues. Produced with a Trial Version of PDF Annotator - www.PDFAnnotator.comSWE80005 Ent. Dev. Lab 04 Stateless Session Bean ãSwinburne University of Technology 2017 Page 4 of 7 e. Select "ED-JEE-DTO-RI" in the "Remote in project" selection box Note: When "Remote" is checked, NetBeans asks for a java class library project (in this case, select "ED-JEE-DTO-RI", the one we just created in LT1) f. Click "Finish" Note: NetBeans now generates the "MyuserFacade" class and put "MyuserFacadeRemote" class in the "ED-JEE-DTO-RI" project under "session" package. LT5.4 Copy and Paste the following code segment into the MyuserFacade class @PersistenceContext(unitName = "ED-JEE-DTO-ejbPU") private EntityManager em; protected EntityManager getEntityManager() { return em; } private void create(Myuser myuser) { em.persist(myuser); } private void edit(Myuser myuser) { em.merge(myuser); } private void remove(Myuser myuser) { em.remove(em.merge(myuser)); } private Myuser find(Object id) { return em.find(Myuser.class, id); } Remember to fix imports and format the code. Note: The line with "@PersistenceContext(unitName = "…") …" tells the GlassFish server to create the required entity manager when creating the SLSB object, most likely using the "EntityManagerFactory" API. As a developer, you are not required to program this part. Note: We have all these methods declared as "private" because we do not want the actual DAO (Myuser) to be passed to the actual client. The DTO done early is responsible for such purposes. Now, we are going to program the business methods for "MyuserFacade" LT6. Program the remote business method, "public boolean createRecord(MyuserDTO)", in the "MyuserFacade" class. It accepts a MyuserDTO object and tries to store the relevant information into the database. If a record with the same userid (the primary key) exists in the database, it does nothing and returns false. Otherwise, it creates the record in the database and return true. LT6.1 Put your mouse inside the "MyuserFacade" class (that is, mouse cursor within the braces "{" and "}") in the editor pane LT6.2 Right click the mouse and Select "Insert Code…" > "Add Business Method…" Note: If you put the mouse in the wrong place in LT6.1, you will get some other options but lacking the "Add Business Method…" LT6.3 In the "Add Business Method…" window, a. Enter "createRecord" in the "Name" field (This specifies the name of the method) b. Enter "boolean" in the "Return Type" field c. Select the "Parameters" tag and click "Add" 1. Enter "myuserDTO" under the "Name" column 2. Enter "MyuserDTO" under the "Type" column d. Click "OK" Note: NetBeans creates the method in the "MyuserFacade" class and a declaration in the "MyuserFacadeRemote" interface class. Remember to fix the imports of "MyuserDTO" in these two classes.SWE80005 Ent. Dev. Lab 04 Stateless Session Bean ãSwinburne University of Technology 2017 Page 5 of 7 LT6.4 Copy and paste the following code segment into the "createRecord" method if (find(myuserDTO.getUserid()) != null) { // user whose userid can be found return false; } // user whose userid could not be found try { Myuser myuser = this.myDTO2DAO(myuserDTO); this.create(myuser); // add to database return true; } catch (Exception ex) { return false; // something is wrong, should not be here though } LT6.5 Copy and paste the following code segment into "MyuserFacade" private Myuser myDTO2DAO(MyuserDTO myuserDTO) { Myuser myuser = new Myuser(); myuser.setUserid(myuserDTO.getUserid()); myuser.setName(myuserDTO.getName()); myuser.setPassword(myuserDTO.getPassword()); myuser.setEmail(myuserDTO.getEmail()); myuser.setPhone(myuserDTO.getPhone()); myuser.setAddress(myuserDTO.getAddress()); myuser.setSecqn(myuserDTO.getSecQn()); myuser.setSecans(myuserDTO.getSecAns()); return myuser; } LT6.6 Remember to save both files LT7. In this Lab task, you will learn how to write the Remote interface class If you used "Insert Code…" > "Add Business Method…" in LT6.2, all business methods' signatures will be added to the corresponding Remote Interface class. Hence, no need to do anything. Otherwise, you have to manually type in the relevant method signatures in the Remote interface class. Note: Even though the description is for Remote interface class, the same is also true for "writing the Local interface class". LT8. In this Lab task, you will learn how to program the application client "MyuserAppClient" to access the Stateless Session Bean "MyuserFacade" LT8.1 Create the "ED-JEE-DTO-appclient" Enterprise Application Client project a. Select "File" > "New Project" b. Choose "Java EE" > "Enterprise Application Client", then click "Next" c. Enter "ED-JEE-DTO-appclient" in the Project Name field, then click "Next" d. Use the default server "GlassFish" and Java EE version "Java EE 7" Note: There is no need to add the project to an enterprise application. e. Change the Main class to "edjee.MyuserAppClient" f. Click "Finish" Note: NetBeans now creates the ED-JEE-DTO-appclient (Enterprise Application Client Project) project. LT8.2 Now, we need to add a reference to the Stateless session bean "MyuserFacade" in the EJB project in the application client "MyuserAppClient.java" class a. In the Projects window, select "ED-JEE-DTO-appclient" project b. Expand "ED-JEE-DTO-appclient" project > "Source Packages" > "edjee" > "MyuserAppClient.java" c. Open "MyuserAppClient.java" in the editor window d. Locate the mouse within the "MyuserAppClient" class e. Right click the mouse and select "Insert Code..." > "Call Enterprise Bean…" Note: If your mouse is in the wrong position, you won't be able to see the right option.SWE80005 Ent. Dev. Lab 04 Stateless Session Bean ãSwinburne University of Technology 2017 Page 6 of 7 f. In the "Call Enterprise Bean" window, expand on "ED-JEE-DTO-ejb" node and select "MyuserFacade" (remember this is an EJB) g. Select "Remote" in the "Referenced Interface" (or, it has been selected for you) h. Click "OK" Note 1: NetBeans will create the following annotation for you "@EJB private static MyuserFacadeRemote myuserFacade;" (This actually creates a remote reference to the actual EJB SLSB.) Note 2: NetBeans also adds the "ED-JEE-DTO-RI.jar" class library file as a project library. LT8.3 Copy and paste the following segment of code to "MyuserAppClient.java" public MyuserAppClient() { } public static void main(String[] args) { MyuserAppClient client = new MyuserAppClient(); // assuming inputs from keyboard or any GUI MyuserDTO myuserDTO = new MyuserDTO("000001", "Edmonds Lau", "123456", "[email protected]", "9876543210", "Swinburne EN510a", "What is my name?", "Edmonds"); boolean result = client.createRecord(myuserDTO); client.showCreateResult(result, myuserDTO); // assuming inputs from keyboard or any GUI MyuserDTO myuserDTO2 = new MyuserDTO("000006", "Man Lau", "654321", "[email protected]", "9876543210", "Swinburne EN510a", "What is my name?", "Edmonds"); result = client.createRecord(myuserDTO2); client.showCreateResult(result, myuserDTO2); } public void showCreateResult(boolean result, MyuserDTO myuserDTO) { if (result) { System.out.println("Record with primary key " + myuserDTO.getUserid() + " has been created in the database table."); } else { System.out.println("Record with primary key " + myuserDTO.getUserid() + " could not be created in the database table!"); } } public Boolean createRecord(MyuserDTO myuserDTO) { return myuserFacade.createRecord(myuserDTO); } Remember to "Fix Imports", format the code, and finally, save the file LT9. In this lab task, you will learn how to deploy the enterprise application "ED-JEE-DTO" project LT9.1 Select "ED-JEE-DTO" enterprise application project in the "Projects" window LT9.2 Right click your mouse and select "Deploy" (You may want to select "Clean and Build" first to clean up any mess that has been done.) Note: In case you encounter problems in deploying and you are very sure that everything is right (that is you follow the lab instructions to the letter), you may want to try "Clean and Build" on the Enterprise Application EJB project first and then "Deploy" on the Enterprise Application project. [This is how I overcome the deployment problem while preparing this lab sheet. :-) Reason is unknown to me. Once I did that, I have no further deployment issues. Good Luck!] LT10. Run the enterprise application client "ED-JEE-DTO-appclient" project LT10.1 Select "ED-JEE-DTO-appclient" enterprise application client project in the "Projects" window LT10.2 Right click the mouse and select "Run"SWE80005 Ent. Dev. Lab 04 Stateless Session Bean ãSwinburne University of Technology 2017 Page 7 of 7 Now, observe the outputs of "MyuserAppClient" in the "ED-JEE-DTO-appclient (run)" tab of the "Output" pane. It should show something like the following (assuming you have not changed the content in the database after you completed Labs 02 and 03) "Record with primary key 000001 could not be created …" "Record with primary key 000007 has been created …"Faculty of Science, Engineering and Technology SWE80005 Enterprise Development [Java EE] Pass Task 4.1 Stateless Business Logic Time Frame: Weeks 4 – 5 Suggested to start and complete in Week 4 Submission Due: Week 5, Fri, 5:00pm Overview In this task, you are required to program business layer objects that can handle business logics on the server using related technologies. You are also required to demonstrate your work is of good quality. Purpose To demonstrate your ability to use relevant technologies to develop quality application that handles the business logics on the server Tasks 1. Learn to develop an enterprise application that uses stateless session bean to perform the ORM of an entity class 2. Extend the stateless session bean so that it can handle all CRUD operations 3. Extend the enterprise application client program developed in 1 above to request the actual CRUD operations provided by the stateless session bean 4. Prepare your test cases and test your application thoroughly by using appropriate input values and database contents 5. Describe (with justification) and document your design 6. Answer questions related to the design of the project Pre-req Task1 Pass Task 2.1 Follow-up Task2 Pass Task 5.1 Suggested Time 2 hours if you know the stuff well 6 – 8 hours if you need to read the concepts and learn how to program class on the Business Logic Layer 10+ hours even if you "know" what to do and just make ONE simple mistake during the programming Resources Lecture 04a Business Logics 1 Lecture 04b EJB Lecture 04c Stateless Session Bean Java EE – Stateless Session EJBs Feedback Ask your tutor for feedback Next task Pass Task 5.1 Pass Task 4.1 Submission Details and Assessment Criteria You must create your own document (pdf) in portrait mode3, which you will upload to Doubtfire, with the following details: • Your name and student id • Your tutor's name • Your own responses to the tasks according to the corresponding instructions (see below) 1 You need to complete the pre-requisite task before doing this task. 2 You need to complete this task in order to do the follow-up task because the follow-up task depends on your answer in this one. 3 Landscape mode pdf does not work properly in Doubtfire.Faculty of Science, Engineering and Technology SWE80005 Ent. Dev. Pass Task 4.1 Page 2 of 3 Tasks and Instructions Assumption: You have implemented the Entity Class, Myuser, in Pass Task 2.1 Task 1. Complete Lab_04_SLSB_MyuserFacade_DTO Task 2. Programming [Assume you have completed Task 1 above] Add the following methods in the "MyuserFacade.java" class (Lab_04_SLSB_MyuserFacade_DTO) that supports the remaining CRUD operations of MYUSER 1. "private MyuserDTO myDAO2DTO(Myuser myuser)" – accepts a Myuser object and returns a MyuserDTO object 2. "MyuserDTO getRecord(String userId)" – accepts a String object whose value is the userId of a record to be searched. If the record can be found, it returns a MyuserDTO object that stores the information of the actual database record. Otherwise, it returns a "null" object. Hint: It may call the "myDAO2DTO" method. 3. "boolean updateRecord(MyuserDTO myuserDTO)" – accepts a MyuserDTO object and checks whether the actual record exists in the database. If it does, it will update the information of the record with the current information stored in the MyuserDTO object and return true. Otherwise, it returns false without doing anything. Hint: Use the EntityManager's instance method called merge() 4. "boolean deleteRecord(String userId)" – accepts a String object whose value is the userId of a record to be deleted. If the record can be found, it removes the record in the database and return true. Otherwise, it returns false. Hint: Use the EntityManager's instance method called remove() 5. "ArrayList getRecordsByAddress(String address)" – accepts a String object whose value is the address of a user. It then searches the MYUSER table and returns all records having the same address as an "ArrayList". Otherwise, it returns a "null" object. Hint: The following code segment may help. But, you need to read the Java EE API on how to use them properly. For your other tasks in the future. javax.persistence.Query query; query = em.createNamedQuery("Myuser.findByAddress").setParameter("address", address); ArrayList daoList = (ArrayList) query.getResultList(); Note: When reading the Java EE API, you will be amazed by how many different ways you can achieve the same thing. However, due to time limitation, please stick to the current one. You are strongly encouraged to explore other possibilities on your own time. Task 3. Programming [Assume you have completed Task 2 above] Modify the MyuserAppClient.java class so that it acts as a test harness of the methods you implemented in Task 2 above. Note: Although this is a test harness, there is no need to use JUnit Tests. Task 4. Testing Write your test cases (including the database content and input values) and test your work thoroughly. Remember to collect the relevant screen dump. Console output in the Output tab of NetBeans is fine. Note: For this task, organize the test cases in "MyuserAppClient.java" properly will do. For example, when testing whether a record could be added or not, we need "yes" and "no" response. The "MyuserAppClient.java" in Lab 04 demonstrates these two situations via one simple execution.Faculty of Science, Engineering and Technology SWE80005 Ent. Dev. Pass Task 4.1 Page 3 of 3 Task 5. Design Justification 5.1. Draw an architecture diagram, using UML notations, of your entire set of programs in all previous Tasks (i.e., Tasks 1 – 4). In your diagram, you need to annotate the names and types of each software component, put them in the right tiers, and indicate how they interact with each other. Note: My diagram in the Lab 04 is not in UML notation. 5.2. Describe and discuss the role of each of the components in your diagram. Task 6. Answer the following questions: 6.1. Which class is responsible for doing all the ORM work? MyuserFacade / Myuser? Justify your answer. 6.2. The "MyuserFacade" class provides all the business methods, it is basically a business object. Why we choose to program it as a "stateless session bean" rather than a "stateful session bean"? Why not using "singleton session bean"? Justify your answer. 6.3. Explain (in your own words) the concept of bean instance pooling in the context of stateless session bean using "MyuserFacade" as an example. Also explain how it can achieve scalability. Submission Task Once completed, you need to submit a pdf file that contains all your work (e.g. selected code segments – show me the key stuff and some screen dumps of your testing) Demonstration You may be asked to demonstrate your assignment in the lab. You should be able to do this and explain your code when asked in the practical session.