Added Junits
[portal.git] / docs / tutorials / portal-sdk / creating.rst
1 Creating a new application
2 ==========================
3  
4 Our tutorial application will consist of two parts:
5
6 1. Java code: We'll create a controller (in reality you might need to create many, depending on how complex your application is) that will handle many of our web requests. Most importantly, it will handle the top-level access request.
7 2. Web code: The web code consists of all the files that Tomcat serves when requests are made for our application. Files like HTML, CSS, JavaScript, images, and so on.
8
9 If you're working with code command-line only (e.g. vim), you'll want to create symlinks to both parts of the code, :code:`java` and :code:`web`. While debugging, you'll frequently move between the two halves.
10
11 Java code location
12 ------------------
13
14 Our Java code will reside here:
15
16 ::
17
18     sdk/ecomp-sdk/epsdk-app-os/src/main/java/org/openecomp/myapp
19
20
21 Create the :code:`myapp` directory.
22
23 Web code location
24 -----------------
25
26 Our web code will reside here:
27
28 ::
29
30     sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/app/fusion/scripts/myapp
31
32 Create the :code:`myapp` directory.
33
34 Inside this directory, we'll add our first subpage:
35
36 ::
37
38     sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/app/fusion/scripts/myapp/myfirstpage
39
40 Create the :code:`myfirstpage` directory.
41
42 Spring controllers
43 ------------------
44
45 We need a controller to handle the top-level access requests (e.g. :code:`localhost:8080/epsdk-app-os/somepage`). For every :code:`somepage` our app needs, we'll need to either designate an existing controller to handle the request or create a new one.
46
47 Why would you need to create a new one?
48
49 The controller is where you'll set up your database access, read from database tables, update database tables, and so on. It may make sense to use different controllers for different :code:`somepage` functions to increase readability and make things easier when others need to maintain the code.
50
51 A basic controller
52 ~~~~~~~~~~~~~~~~~~
53
54 Copy the following basic controller code into your Java code location as :code:`MyAppController.java`
55
56 .. code-block:: java
57
58     package org.onap.myapp;
59
60     import java.util.HashMap;
61     import java.util.ArrayList;
62     import java.util.Map;
63     import java.util.List;
64     import java.io.IOException;
65
66     import java.beans.PropertyVetoException;
67     
68     import org.springframework.stereotype.Controller;
69     import org.springframework.web.bind.annotation.RequestMapping;
70     import org.springframework.web.bind.annotation.RequestMethod;
71     import org.springframework.web.bind.annotation.PathVariable;
72     import org.springframework.stereotype.Repository;
73     import org.springframework.beans.factory.annotation.Autowired;
74     import org.springframework.web.servlet.ModelAndView;
75     import org.springframework.jdbc.core.JdbcTemplate;
76     
77     import javax.servlet.http.HttpSession;
78     import javax.servlet.http.HttpServletRequest;
79     import javax.servlet.http.HttpServletResponse;
80     import javax.sql.DataSource;
81     import javax.annotation.PostConstruct;
82     
83     import com.mchange.v2.c3p0.ComboPooledDataSource;
84     
85     import org.json.JSONObject;
86     import org.json.JSONArray;
87     
88     import org.onap.portalsdk.core.controller.RestrictedBaseController;
89     import org.onap.portalsdk.core.util.SystemProperties;
90     import org.onap.portalsdk.core.domain.User;
91     
92     @Controller
93     @Repository
94     @RequestMapping("/")
95     public class MyAppController extends RestrictedBaseController {
96       ///////////////////////////////////////////////////////////////////////////////
97       // Constructor
98       //
99       public MyAppController() {
100         super();
101       }
102       
103       ///////////////////////////////////////////////////////////////////////////////
104       // Handle 'myfirstpage' requests
105       //
106       @RequestMapping(value = { "/myfirstpage" }, method = RequestMethod.GET)
107       public ModelAndView myfirstpage(HttpServletRequest request) {
108         final String defaultViewName = null;
109         return new ModelAndView(defaultViewName);
110       }
111     }
112
113 .. _definitions.xml:
114
115 Request routing via definitions.xml
116 -----------------------------------
117
118 In order for the framework to route requests for :code:`myfirstpage` correctly, we'll need to create an entry in :code:`sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/WEB-INF/defs/definitions.xml` that looks like this:
119
120 ::
121
122     <definition name="myfirstpage" template="/app/fusion/scripts/myapp/myfirstpage/myfirstpage.html" />
123
124 Then, add the following to :code:`sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/app/fusion/scripts/myapp/myfirstpage/myfirstpage.html`:
125
126 .. code-block:: html
127
128     <html>
129       <body>
130         <p>It worked!</p>
131       </body>
132     </html>
133
134 Now, build and install your application as before. If everything worked, you should see `It worked!` in your browser window when you visit myfirstpage_ after logging in. 
135
136 When the request from the browser comes in, the framework creates a mapping from :code:`myfirstpage` to the MyAppController, which in turn maps your definition name to a particular template. Soon, we'll fill in that template to do more interesting things.
137
138 .. _myfirstpage: http://localhost:8080/epsdk-app-os/myfirstpage