Wednesday, September 30, 2015

ATG Droplet 101

First read the following:
  1. What is a Droplet? - http://learnoracleatg.blogspot.com/2014/10/art109-what-are-droplets-or-dynamo.html
  2. How to pass parameters to a Droplet from JSP? http://learnoracleatg.blogspot.in/2014/10/art110-how-to-pass-parameters-to.html
Here is what I did:
Step 1: I created a HelloWorldDroplet in modules.base 
/modules.base/src/atg/repository/HelloWorldDroplet.java
package atg.repository;

import java.io.IOException;
import javax.servlet.ServletException;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import atg.servlet.DynamoServlet;

public class HelloWorldDroplet extends DynamoServlet {

 public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response) throws IOException, ServletException{
  
  String myInput = request.getParameter("input");
  response.setContentType("text/html");
  
  myInput = myInput.concat(" appended from droplet");
  request.setParameter("concatenatedString", myInput);

  request.serviceParameter("CUSTOMOUTPUT", request, response);
  
  response.getOutputStream().println("Hello World\n");
 }
}

Step 2: A droplet is a component. So we need to create a configuration file. Here is the HelloWorldComponent.properties

/modules.base/config/atg/hello/HelloWorldComponent.properties


$class=wildcats.droplets.HelloWorldDroplet
Step 3: How to invoke a droplet from JSP?

/modules.store/j2ee-apps/Web/Web.war/hello/helloworld.jsp

<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<dsp:page>
    <dsp:importbean bean="/atg/hello/HelloWorldComponent" />
    <html>
        <body>
        Here is the text from droplet<br>
            <dsp:droplet name="HelloWorldComponent">
            <dsp:param name="input" value="passed from jsp" />
 
            <dsp:oparam name="CUSTOMOUTPUT">
            <dsp:valueof param="concatenatedString" />
            </dsp:oparam>
            </dsp:droplet>
        </body>
    </html>
</dsp:page>

Step 4: Look at the modules.store's web.xml and find the context-root. If you do not have any add the following: /modules.store/j2ee-apps/Web/Web.war/WEB-INF/web.xml


<context-param>
  <param-name>context-root</param-name>
  <param-value>sams</param-value>
</context-param>

Step 5: From the above web.xml, we know how to access the jsp file. Type http://localhost:8080/myapp/hello/helloworld.jsp in your browser and see the following output in your browser:



More about Droplets: Out Of the box droplets:
  • I tried out how to use "ForEach" droplet. Here is what I did:
    Step 1: Create an employee component i.e a java file and a properties file.
/modules.base/src/atg/repository/EmployeeBean.java

package atg.repository;
/**
 *
 *
 * @version  : 1.0
 */
import java.util.ArrayList;
import java.util.List;
public class EmployeeBean {
        List<String> carsList = new ArrayList<String>();
        public List<String> getCarsList() {
            return carsList;
        }
        public void setCarsList(List<String> carsList) {
            this.carsList = carsList;
        }
}
/modules.base/config/atg/hello/EmployeeComponent.properties
$class=atg.repository.EmployeeBean
carsList=\
        Honda,\
        Skoda,\
        Mercedes,\
        VW

Step 2: Write the JSP which invokes the droplet:

/modules.store/j2ee-apps/Web/Web.war/hello/helloemployee.jsp


<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<dsp:page>
    <dsp:droplet name="/atg/dynamo/droplet/ForEach">
    <dsp:param name ="array" bean="/atg/hello/EmployeeComponent.carsList" />
    <dsp:oparam name="outputStart">
        <p> The employee has following cars: <br/></p>
    </dsp:oparam>
     
    <ul>
        <dsp:oparam name = "output">
        <li><dsp:valueof param="element"></dsp:valueof></li>
        </dsp:oparam>
    </ul>
     
    <dsp:oparam name ="outputEnd">
        Thats all!
    </dsp:oparam>
 
    </dsp:droplet>
</dsp:page>

Step 3: To access this JSP type, http://localhost:8080/myapp/hello/helloemployee.jsp in the browser and see the output as following:

Saturday, August 1, 2015

ATG Scheduler 101

Writing a Scheduler
This is a simple scheduler that prints "Hello World" to the console. See the steps to write and debug a scheduler in ATG.

package com.wilcats.schedulers;

import atg.nucleus.GenericService;
import atg.nucleus.ServiceException;
import atg.service.scheduler.Schedulable;
import atg.service.scheduler.Schedule;
import atg.service.scheduler.ScheduledJob;
import atg.service.scheduler.Scheduler;
/**
 *
 *
 * @version  : 1.0
 */
public class ExampleScheduler extends GenericService implements Schedulable{
    // Scheduler property
    Scheduler scheduler;
    public Scheduler getScheduler() {
        return scheduler;
    }
    public void setScheduler(Scheduler scheduler){
        this.scheduler = scheduler;
    }
    // Schedule property
    Schedule schedule;
    public Schedule getSchedule() {
        return schedule;
    }
    public void setSchedule(Schedule schedule){
        this.schedule = schedule;
    }
     
    int jobId;
    // Schedulable method
    public void performScheduledTask(Scheduler scheduler,
            ScheduledJob job){
        logDebug("Hello World!!!");
        System.out.println("Hello World");
    }
    // Start method
     
    public void doStartService () throws ServiceException{
        ScheduledJob job = new ScheduledJob("hello",
                "Prints Hello World",
                getAbsoluteName(),
                getSchedule(),
                this,
                ScheduledJob.SCHEDULER_THREAD);
        jobId = getScheduler().addScheduledJob(job);
    }
    // Stop method
    public void doStopService() throws ServiceException{
        getScheduler().removeScheduledJob(jobId);
    }
}

ExampleScheduler.properties

$class=com.wildcats.schedulers.ExampleScheduler
$scope=global
scheduler=/atg/dynamo/service/Scheduler
schedule=every 30 seconds
threadMethod=2
loggingDebug=true

Initialize this scheduler in initial.properties only if you want this scheduler to initialize/run on start up of the server. For more details on initial.properties read http://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0204startinganucleuscomponent01.html
Intialize the ExampleScheduler in initial.properties like below:
/com/wilcats/schedulers/ExampleScheduler

Steps to execute a Scheduler
  1. Write the above classes to your ATG code base
  2. Build your code.
  3. Start your server. For eg: Jboss server using startJboss.sh
  4. Go to ATG Dynamo Server Admin. See details in the next section. 
  5. It will show you the component browser. Search your scheduler - ExampleScheduler
  6. Once you reach the ExampleScheduler page - go to the bottom of the page and you will see a set of methods listed. Click on doStartService. It will ask you to confirm to invoke this call. 
  7. Confirm you want to invoke this method call.
  8. Check your server-log/console and you will see "Hello World" printed there.

ATG Dynamo Server Admin

The ATG Dynamo Server Admin gives you quick access to a number of useful features. For example, you can modify the configuration for ATG server instances, browse the Nucleus component hierarchy, change administrator passwords, and so on.
To start the ATG Dynamo Server Admin:

Debugging Schedulers

  1. In eclipse, go to Debug configurations -> Remote Java Applications -> right click create new  -> change the port to 8787. 
  2. Once you have started your server, you can add breakpoints in your code and start debugging
  3. Hint: Change the schedule of your scheduler to run every 4 hours. Put breakpoint on doStartService(),  performScheduledTask(), doScheduledTask()

References


Thursday, July 23, 2015

Glossary ~

*** Work In progress ***
  • ATG - Art Technology Group. ATG provides all the necessary functionalities required to create an e-commerce web application. It is currently owned by Oracle. 
  • Maven - Maven is a build automation tool used primarily for Java projects. The word maven means 'accumulator of knowledge' in Yiddish. Maven addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies. Contrary to preceding tools like Apache Ant, it uses conventions for the build procedure, and only exceptions need to be written down. An XMLfile describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging. Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories such as the Maven 2 Central Repository, and stores them in a local cache.[4] This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated.
  • POM.xml - A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/test/java; and so on. 
    The POM was renamed from project.xml in Maven 1 to pom.xml in Maven 2. Instead of having a maven.xml file that contains the goals that can be executed, the goals or plugins are now configured in thepom.xml. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal. 
    Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.
  • PEBL/Jenkins - Jenkins is an open source continuous integration tool written in Java. Builds can be started by various means, including being triggered by commit in a version control system, by scheduling via a cron-like mechanism, by building when other builds have completed, and by requesting a specific build URL. We have stopped using Jenkins directly since it overwhelms users with lot of options that is not needed for a simple jar / war project. We have started using PEBL which is an easy to use in-house developed wrapper UI tool. Read more here: https://confluence.walmart.com/x/7k88Bw
  • JSON - JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data between a server and web application, as an alternative to XML. Code for parsing and generating JSON data is readily available in many programming languages. The official Internet media type for JSON is application/json. The JSON filename extension is .json.
  • POJO - Plain Old Java Object, is a normal Java object class (that is, not a JavaBean, EntityBean etc.) and does not serve any other special role nor does it implement any special interfaces of any of the Java frameworks.
  • Jackson - Jackson Api contains a lot of functionalities to read and build json using java. It has very powerful data binding capabilities and provides a framework to serialize custom java objects to json string and deserialize json string back to java objects. 
  • JAXB  - JAXB stands for Java architecture for XML binding.It is used to convert XML to java object and java object to XML. JAXB defines an API for reading and writing Java objects to and from XML documents. There are two operations you can perform using JAXB 1. MarshallingConverting a java object to XML 2. UnMarshallingConverting a XML to java object.
  • GIT -  Git is a distributed revision control system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows.
  • Apache-camel - 
  • Liquibase - 
  • CXF - 
  • Logmon -  Logging and monitoring tool
  • Mockito - Mockito is an open source Java mocking framework used for unit testing of Java code. Mockito library enables mocks creation, verification and stubbing.
  • TestNG - TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:
    • Annotations.
    • Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...).
    • Test that your code is multithread safe.
    • Flexible test configuration.
    • Support for data-driven testing (with @DataProvider).
    • Support for parameters.
    • Powerful execution model (no more TestSuite).
    • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
    • Embeds BeanShell for further flexibility.
    • Default JDK functions for runtime and logging (no dependencies).
    • Dependent methods for application server testing.
    TestNG is designed to cover all categories of tests:  unit, functional, end-to-end, integration, etc...
  • DTO - Data Transfer Objects don't have any logic. They only have fields (state). They are used when transferring data from one layer/subsystem to another. One use case is DTO can be sent to UI.
  • DO - Domain objects can have logic and they are usually related to the database structure. One use case is DO are used to persist in DB
  • XML - Extensible Markup Language is a markup language that defines a set of rules for encoding documents in a format which is both human-readable and machine-readable. It is used as a data carrier and to write WSDL and SOAP documents. Services are defined by WSDL, exchanged using SOAP, and validated with schemas documents (XSD)
  • XSD - XML Schema Document. Validates XML data and structure.
  • UDDI - Universal Description, Discovery and Integration is a platform-independentExtensible Markup Language (XML)-based registry by which businesses worldwide can list themselves on the Internet, and a mechanism to register and locate web service applications.
  • WSDL - The Web Services Description Language (WSDL) is an XML-based interface definition language that is used for describing the functionality offered by a web service. The acronym is also used for any specific WSDL description of a web service, which provides a machine-readable description of how the service can be called, what parameters it expects, and what data structures it returns. It thus serves a purpose that corresponds roughly to that of a method signature in a programming language. In other words, WSDL defines the interaction or contract between set service consumers and web service. It is similar to a Java interface but written in XML. The six major sections of a WSDL document are: 
  • <definitions> Root element
        <types> Data types to be transmitted
        <message> Messages to be transmitted
        <portType> Operations / functions supported
        <binding> How the messages will be transmitted, SOAP specific details
        <service> URL/Location of the service
    </definitions>
    
  • SOAP - Simple Object Access protocol, is a protocol specification for exchanging structured information in the implementation of web services in computer networks. It uses XML Information Set for its message format, and relies on other application layer protocols, most notably Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.
  • JSP - JavaServer Pages is a technology that helps software developers create dynamically generated web pages based on HTMLXML, or other document types. To deploy and run JavaServer Pages, a compatible web server with a servlet container, such as Apache Tomcat or Jetty, is required.
  • JSTL - The JSP syntax adds additional tags, called JSP actions, to invoke built-in functionality. Additionally, the technology allows for the creation of custom JSP tag libraries that act as extensions to the standard JSP syntax.  One such library is the JSTL, with support for common tasks such as iteration and conditionals (the equivalent of "for" and "if" statements in Java.) The JavaServer Pages Standard Tag Library (JSTL) is a component of the Java EE Web application development platform. It extends the JSP specification by adding a tag library of JSP tags for common tasks, such as XML data processing, conditional execution, database access, loops and internationalization
  •  REST - REST stands for Representational State Transfer. It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used. REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines. In many ways, the World Wide Web itself, based on HTTP, can be viewed as a REST-based architecture. RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.
  • HTTP - HTTP stands for Hyper Text Transfer Protocol. Protocol means a language or mechanism for communication. So, HTTP is a way to exchange and communicate information online. The stuff you exchange and transfer in HTTP is called hypertext. (Hence the name). Hypertext is a structured form of text that has one interesting property: it contains logical links to other text. These links are called hyperlinks. A common and popular way to write hypertext is using a language called HyperText Markup Language, or HTML. HTTP has what are called methods or verbs that you can use to interact with URLs. The most common HTTP methods are GET, PUT, POST, DELETE.

Sunday, April 12, 2015

Database - Normalization and De-normalization concepts

*In progress* 

Database normalization:
Database normalization is the process of organizing the attributes and tables of a relational database to minimize data redundancy.

Primarily, purpose of normalization is the following:
1.     Arranging data into logical groupings such that each group describes a small part of the whole system. In other words, refactoring a table into smaller tables without losing information.
2.     Eliminating/minimizing redundant data stored in the database.
3.     Organizing the data such that when you modify it, you make change only at one place.
4.     Building a database in which you can access and manipulate the data quickly and efficiently without compromising the integrity of the data in storage.

A typical example of normalization is that an entity’s unique ID is stored everywhere in the system but its name is held only in one table. The name can be updated more easily in one row of one table.  A typical update in such an example would be Symantec Company changing its CEO to Michael Brown. The update would be done in one place and immediately the correct CEO name would be displayed throughout the system.

Why do we need Normalization?
Without normalization database systems can be inaccurate, slow, inefficient and they might not produce the data you expect. Insertion, Updation and Deletion anomalies are very frequent if database is not normalized.
Lets take an example of student and courses he/she is enrolled in.

StudentID
StudentName
StudentAddress
StudentCity
Courses_enrolled
101
Alex
12 Ellis st
Milpitas
Python
102
Adam
310 Yosemite ct
Palo Alto
Web services
103
Kat
99 Buena vista ave
Cupertino
Java
101
Alex
12 Ellis st
Milpitas
Java

Update Anomaly: The same information is expressed on multiple rows on the above table. A change of address for a particular student will potentially need to be applied to multiple records (one for each course). If the update doesn’t happen successfully, we could leave the data in inconsistent state.

Insertion Anomaly: Suppose for a new admission, we have a studentID, name, address, city of the student but if the student has not yet enrolled for any courses then we have to insert null there, leading to insertion anomaly.

Deletion Anomaly: If student ID:102 who has enrolled only in one course and temporarily he drops it, when we delete the row, entire student information will be deleted along with it.

Normalization Rule:

Normalization rules are divided into the following normal form:
1.     First Normal Form (1NF)
2.     Second Normal Form (2NF)
3.     Third Normal Form (3NF)
4.     Boyce and Codd (BCNF)
5.     Forth Normal Form (4NF)
6.     Fifth Normal Form (5NF)

1. First Normal Form – No repeating elements or group of elements.
A. It contains only atomic values – An atomic value is a value, which cannot be divided. For eg in the table below studentid 101 has enrolled in two courses namely Python and Java. Hence the table below is not in 1NF.

StudentID
StudentName
StudentAddress
StudentCity
Courses_enrolled
101
Alex
12 Ellis st
Milpitas
Python, Java
102
Adam
310 Yosemite ct
Palo Alto
Web services
103
Kat
99 Buena vista ave
Cupertino
Java

In the first normal form, the previous table can be represented in the following way:

StudentID
StudentName
StudentAddress
StudentCity
Courses_enrolled
101
Alex
12 Ellis st
Milpitas
Python
102
Adam
310 Yosemite ct
Palo Alto
Web services
101
Alex
12 Ellis st
Milpitas
Java
103
Kat
99 Buena vista ave
Cupertino
Java

Using the first normal form, data redundancy increases, as there will be many columns with same data in multiple rows but each row as a whole will be unique.
A primary key is a column (or group of columns) that uniquely identifies each row. In the above table, there is no single column that uniquely identifies each row. However, if we put a number of columns together we can satisfy this requirement. The two columns that together uniquely identify each row are StudentID and Courses_enrolled. Therefore, together they qualify to be used as the table’s primary key. However, this database design does not meet the more stringent requirements of the second normal form.

B. There are no repeating groups - A repeating group means that a table contains two or more columns that are closely related. For eg the table below records a student who has enrolled in two courses in course1 and course2. This table is not in 1NF because course1, course2 are all repeating the same attribute.

StudentID
StudentName
StudentAddress
StudentCity
Course1
Course2
101
Alex
12 Ellis st
Milpitas
Python
 Java
102
Adam
310 Yosemite ct
Palo Alto
Web services
Null
103
Kat
99 Buena vista ave
Cupertino
Java
Null


In the first normal form, the previous table can be represented in the following way:

StudentID
StudentName
StudentAddress
StudentCity
Courses_enrolled
101
Alex
12 Ellis st
Milpitas
Python
102
Adam
310 Yosemite ct
Palo Alto
Web services
101
Alex
12 Ellis st
Milpitas
Java
103
Kat
99 Buena vista ave
Cupertino
Java

The First Normal Form addresses two issues:
A.     A row of data cannot contain repeating groups of similar data (atomicity) and
B.     Each row of data must have a unique identifier (Primary key)

2. Second Normal Form: No partial dependency on a concatenated key.

For second normal form, we test the table for partial dependencies on a concatenated key. This means that for a table that has a concatenated primary key, each column in the table that is not part of the primary key must depend upon the entire concatenated key for its existence. If any column only depends upon one part of the concatenated key, then we say that the entire table has failed Second Normal Form and we must create another table to rectify the failure.


StudentID
StudentName
StudentAddress
StudentCity
Courses_enrolled
101
Alex
12 Ellis st
Milpitas
Python
102
Adam
310 Yosemite ct
Palo Alto
Web services
101
Alex
12 Ellis st
Milpitas
Java
103
Kat
99 Buena vista ave
Cupertino
Java

In the above table, StudentID and Courses_enrolled is the concatenated primary key.
The table has two rows for StudentID 101 to include multiple courses the student has enrolled for. While this is searchable and follows 1NF, it is inefficient use of space.  Also, attributes like Student Name, Address, City depends on Student ID and not on the Courses he has enrolled for. To achieve 2NF, we will have to split the tables as follows:

Student table
StudentID
StudentName
StudentAddress
StudentCity
101
Alex
12 Ellis st
Milpitas
102
Adam
310 Yosemite ct
Palo Alto
103
Kat
99 Buena vista ave
Cupertino

Courses table
StudentID
Courses_enrolled
101
Python
101
Java
103
Java
102
Webservices

Now, both the tables qualify for the Second normal form and will not suffer from update anomalies.