Register   Login   About   Study   Enterprise   Share
Internet / AI Technology University (ITU/AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 4. Web Apps Frameworks >> 4.4. Spring with Apache Maven and Data Service Frameworks >> 4.4.4.Data Service Framework and Troubleshooting
Current Topic: 4.4.4.1.Data Service Framework basic
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
4.4.4.1.Data Service Framework
There are several frameworks, including Hybernate, Spring, and DataService, that recognize and solve common data processing issues and allow us to minimize our coding efforts and focus on specific business cases.

The Hybernate and Spring frameworks provide object-relational mappings and offer creating SQL statements on-the-fly. Developers don’t need to be familiar with the database structures; they just create initial configuration files instead.

The DataService framework is the smallest one. The framework requires developers to know data structure and use this knowledge to create good SQL statements. The framework allows developers keep SQL statements in separate files that can be directly used for testing SQL with the TOAD or SQLPlus tools. The DataService takes care of data source initializations, data connections, and processing result sets.

The DataService class is the façade of the com.util.jar library. This is a single jar file with a very small footprint.

The library includes the ItsBaseServlet class that reads initialization parameters for a web application from the web.xml file, and makes data sources available. The initialization procedure is called automatically when application is started.

There are several pieces of data that we pass to the framework via the web.xml file

1. Provide the name of the project, which is actually the name of the application, so it goes as appName.

The application will also need to work with the file system.
Let the application know where it is deployed to make this work a bit easier.
Note two values below separated by comma: one for Windows deployment and another for Linux deployment, for example in the AWS environment.
DataService will recognize Operating System and will use a proper pathToDeploy value.


appName
MyApp


pathToDeploy
c:/aws/yourAppBase/,/home/ec2-user/yourAppBase/





2. Provide the description of the data sources in the file located in the deploy directory named as applicationName-ds.xml, for example MyApp-ds.xml.
Map data sources to the application in the web.xml file using context-param elements. Use the names for data source that end with “DataSource”, for example, “MyAppDataSource”.
 

MyAppDataSource
MyAppDataSource


In the case when the number of databases and tables is not very significant, directly describe in the web.xml all metadata.
For example, if you have just one database, list the tables as comma separated parameters.


tables
UserProfile,Accounts,Roles,Teams,Articles,Comments


For each table above provide the column names (table keys).


UserProfileTableKeys
userID,firstName,lastName,mobileNumber,lastUpdate,domain


DataService framework will create necessary tables if do not exist and provide default data types, such as varchar(60).

It is easy to specify data types as in the example below with the Roles table.


RolesTableKeys
roleID,roleName,lastUpdate


RolesDataTypes
number,varchar(60),varchar(120)



3. Use the listener-class element to point to the ItsBaseServlet class in the web.xml file.


com.its.util.ItsBaseServlet
Was it clear so far?

4. Similar to Struts or Spring frameworks there is a single entry in the web.xml for the servlet class and servlet mapping.


com.its.util.ItsBaseServlet
com.its.util.ItsBaseServlet


com.its.util.ItsBaseServlet
/Lookup



It is expected that the request will always include url-pattern /Lookup and the following parameters will point to a real action or a page.

Briefly about the ItsBaseServlet class. This class extends HttpServlet and implements the following interfaces:
- HttpSessionListener, // allows to listen to session events, such as createSession and destroySession and insert in these methods a specific behavior
- ServletContextListener // allows to listen to the servlet context events, such as initialization. Implement the initialize() method to provide specific to your application initialization procedures not related to database sources.

The ItsBaseServlet class includes the doPost() and doGet() methods that respond to a request from a browser. Similar to the Dispatcher class in the Spring this class will delegate control to an action class or to a Java Server Page.

This can be done with the simple URL, such as:
/BASE/Lookup?action=content
Or in a more formal REST presentation
/BASE/Lookup/content

How to use the Data Service framework:
a) Copy the com.util.jar library to the WEB-INF/lib directory and make it a part of the Java Build Path.
b) Handle data by using the DataService API (see examples below and DataService API)

Example:

Store the select statement below in the file WEB-INF/sql/getUser.sql

You can use Statement or PreparedStatement in the DataService framework
Prefered way is using PreparedStatement, which besides performance benefits also provides more security and has embedded work around symbols, such as a single quote and more, which may appear in SQL statements.

Example with the Statement

getUser.sql – file in the Eclipse project in WebContent/config/sql – directory

Select * from users where loginName = ‘:loginName’

Provide this code in your action class to execute this statement in the MyAppDataSource.

// See recommendations on the action class and find more details on the HashMap keys

keys.put(“loginName”, form.getLoginName()); // common HashMap keys
List records = DataService.getData(“getUser”, keys, User.class, “MyAppDataSource”);
If(records.size() == 0) {
// no users found
return “error”; // forward to the Error page
}
User user = (User) records.get(0);
request.getSession().setAttribute(“user”, user);
return “success”; // forward to the next page

For PreparedStatement:

insertUser.sql – file in the Eclipse project in WebContent/config/sql – directory

INSERT into User values(? , ?)

/* parameters:
- name of the sqlFile - insertUser refers to insertUser.sql file
- String array of run-time parameters to replace "?"
- the name of data source - in this case "myDB"
*/
DataService.setPrepDataBySqlName("insertUser", new String[] {login, psw}, "myDB");
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/><context-param>
<br/>    <param-name>appName</param-name>
<br/>    <param-value>MyApp</param-value>
<br/> </context-param>
<br/><context-param>
<br/>    <param-name>pathToDeploy</param-name>
<br/>    <param-value>c:/aws/yourAppBase/,/home/ec2-user/yourAppBase/</param-value>
<br/> </context-param>
<br/>




2. Provide the description of the data sources in the file located in the deploy directory named as applicationName-ds.xml, for example MyApp-ds.xml.
Map data sources to the application in the web.xml file using context-param elements. Use the names for data source that end with “DataSource”, for example, “MyAppDataSource”.
 <context-param>
<br/>        <param-name>MyAppDataSource</param-name>
<br/>        <param-value>MyAppDataSource</param-value>
<br/>  </context-param>
<br/>

In the case when the number of databases and tables is not very significant, directly describe in the web.xml all metadata.
For example, if you have just one database, list the tables as comma separated parameters.
<br/>   <context-param>
<br/>    <param-name>tables</param-name>
<br/>    <param-value>UserProfile,Accounts,Roles,Teams,Articles,Comments</param-value>
<br/>   </context-param>   
<br/>

For each table above provide the column names (table keys).
<br/>   <context-param>
<br/>    <param-name>UserProfileTableKeys</param-name>
<br/>    <param-value>userID,firstName,lastName,mobileNumber,lastUpdate,domain</param-value>
<br/>   </context-param> 


DataService framework will create necessary tables if do not exist and provide default data types, such as varchar(60).

It is easy to specify data types as in the example below with the Roles table.
<br/>   <context-param>
<br/>    <param-name>RolesTableKeys</param-name>
<br/>    <param-value>roleID,roleName,lastUpdate</param-value>
<br/>   </context-param>  
<br/>   <context-param>
<br/>    <param-name>RolesDataTypes</param-name>
<br/>    <param-value>number,varchar(60),varchar(120)</param-value>
<br/> </context-param>   
<br/>


3. Use the listener-class element to point to the ItsBaseServlet class in the web.xml file.
<br/><listener>
<br/>    <listener-class>com.its.util.ItsBaseServlet</listener-class>
<br/></listener>






Was it clear so far?


4. Similar to Struts or Spring frameworks there is a single entry in the web.xml for the servlet class and servlet mapping.
<br/><servlet>
<br/>    <servlet-name>com.its.util.ItsBaseServlet</servlet-name>
<br/>    <servlet-class> com.its.util.ItsBaseServlet</servlet-class>
<br/>  </servlet>
<br/>  <servlet-mapping>
<br/>    <servlet-name>com.its.util.ItsBaseServlet</servlet-name>
<br/>    <url-pattern>/Lookup</url-pattern>
<br/>  </servlet-mapping>
<br/>


It is expected that the request will always include url-pattern /Lookup and the following parameters will point to a real action or a page.

Briefly about the ItsBaseServlet class. This class extends HttpServlet and implements the following interfaces:
- HttpSessionListener, // allows to listen to session events, such as createSession and destroySession and insert in these methods a specific behavior
- ServletContextListener // allows to listen to the servlet context events, such as initialization. Implement the initialize() method to provide specific to your application initialization procedures not related to database sources.

The ItsBaseServlet class includes the doPost() and doGet() methods that respond to a request from a browser. Similar to the Dispatcher class in the Spring this class will delegate control to an action class or to a Java Server Page.

This can be done with the simple URL, such as:
/BASE/Lookup?action=content
Or in a more formal REST presentation
/BASE/Lookup/content

How to use the Data Service framework:
a) Copy the com.util.jar library to the WEB-INF/lib directory and make it a part of the Java Build Path.
b) Handle data by using the DataService API (see examples below and DataService API)

Example:

Store the select statement below in the file WEB-INF/sql/getUser.sql

You can use Statement or PreparedStatement in the DataService framework
Prefered way is using PreparedStatement, which besides performance benefits also provides more security and has embedded work around symbols, such as a single quote and more, which may appear in SQL statements.

Example with the Statement

getUser.sql – file in the Eclipse project in WebContent/config/sql – directory

Select * from users where loginName = ‘:loginName’

Provide this code in your action class to execute this statement in the MyAppDataSource.

// See recommendations on the action class and find more details on the HashMap keys

keys.put(“loginName”, form.getLoginName()); // common HashMap keys
List records = DataService.getData(“getUser”, keys, User.class, “MyAppDataSource”);
If(records.size() == 0) {
// no users found
return “error”; // forward to the Error page
}
User user = (User) records.get(0);
request.getSession().setAttribute(“user”, user);
return “success”; // forward to the next page

For PreparedStatement:

insertUser.sql – file in the Eclipse project in WebContent/config/sql – directory

INSERT into User values(? , ?)

/* parameters:
- name of the sqlFile - insertUser refers to insertUser.sql file
- String array of run-time parameters to replace "?"
- the name of data source - in this case "myDB"
*/
DataService.setPrepDataBySqlName("insertUser", new String[] {login, psw}, "myDB");

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy