Monday 7 March 2016

Create new store (website) using Hybris

In Hybris yaccelerator is a default out-of-box store but we want to create our own  by using Hybris multi-channel suite.Required to follow below steps for the same

Step1:- Go to cmd and hybris bin plateform directory and type >ant modulegen

Step2:- Choose extension template accelerator

Step 3:- Enter store name .Here given store name  "azmat"

Step 4:-  Enter package name.Here given package name "com.azmat"


After performing all above step , Below seven extension gets
(1)  azmatcockpits
(2)  azmatcore
(3)  azmatfacades
(4)  azmatfulfilmentprocess
(5)  azmatinitialdata
(6)  azmatstorefront
(7)  azmattest




Step 5:- Add all newly created extension to C:\dv\hybris\config/localextensions.xml

Step 6:- Do all the below step
              (a) Run ant clean all
              (b) Start the Hybris Server
              (c) Initialize extension from Hybris management console
              (d) Hit the URL http://localhost:9001/azmatstorefront/?site=apparel-uk



Some analysis from code perspective:-

Here are example:-
 Example 1:-
When we hit the URL http://localhost:9001/azmatstorefront/?site=apparel-uk , above given page gets displayed(Refer # home page). If we want to figure out which controller gets called

                When we hit this URL. 

   Step A:-   Refer blow entry in azmatstorefront\web\webroot\WEB-INF\config\spring-mvc-config.xml

                   <util:map id="defaultPreviewUrlResolverPageMappings">
                                <entry key="homepage" value="/"/>
                                <entry key="cartPage" value="/cart"/>
                                <entry key="search" value="/search"/>
                                <entry key="searchEmpty" value="/search"/>
                                <entry key="account" value="/my-account"/>
                  </util:map>

           Step B:-  Search text (@RequestMapping("/"))for the home page , By this way we  can get                               controller name. In this case coming  HomePageController.java
            From the controller, we can get information about page view .

Example 2:-
On  click of button “ADD TO BAG”, URL /azmatstorefront/en/cart/add gets called . For the same search for the text (“/cart/add”). In this case controller name is coming AddToCartController.java




Thursday 18 February 2016

Exercise how to display a Product on JSP page

This exercise describe detail about how to display a product on a frontend JSP using ProductService.
The main difference to the How to Display a User is that the product type is managed by catalogs which implies additional logic for setting the correct catalog versions at the session context.
     High level activity required to achieve this exercise are 
    (a) Create a Frontend Controller and a View
    (b) Inject the ProductService
    (c) Look Up the Product
    (d) Display the Product
    (e) First Attempt to Run
    (f) Set Up the Catalog Version
    (g) Second Attempt to Run
   Detail step are given below:-
 Step 1:-
    (a) Create a Frontend Controller and a View  (ProductController.java)

ProductController.java             
      package org.training.web.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ProductController implements Controller
{
@Override
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception
{
return new ModelAndView("product.jsp");
}
}



(b)    Create a new file named product.jsp in the hybristraining/web/webroot directory
            <html>
                   <head>
                      <title>Product</title>
                   </head>
                <body>
                     Product goes here
              </body>
      </html>

           (c) Create entry of controller in springmvc-servlet.xml   
                                    <bean
                                               name="/product.html"
                                                    class="org.training.web.controllers.ProductController"/>


(d) Call ant clean all and start the hybris server                                  

(e)     Invoke the URL http://localhost:9001/hybristraining/product.html.        


     Step 2:- Inject the ProductService                                                              
                                                To display a product with a certain code, passed through an URL
                                                such as http://localhost:9001/hybristraining/product.html?code=somproductcode

       (a)    Add an instance variable and a setter method to the ProductController.java
         
private ProductService productService;

                               public void setProductService(final ProductService productService)
                               {
                                this.productService = productService;
                               }


     (b) To obtain the ProductService, use a dependency injection, modify spring bean

                  <bean name="/product.html" class="org.training.web.controllers.ProductController">
                                       <property name="productService" ref="productService"/>
<                          /bean>
         
             
Step3:- Lookup the product                                                                                          
                   (a) To extract the product code from the URL, add the following piece of code to the handleRequest(...) method of our ProductController.java                                                         

private ProductModel product = null;
                     final String code = request.getParameter("code");

if (code != null)
{
                                                        product = productService.getProductForCode(code);
}
final Map<String, Object> model = new HashMap<String, Object>();
model.put("product", product);



                           Step 4:  Display the Product , Add below code to JSP product.jsp    
<html>
    <head>
                                     <title>Product</title>
    </head>
    <body>
                                       <h1>${product.name}</h1>
                                      ${product.description}
    </body>
           </html>   

Step 5:Call  ant clean all  and start the hybris server 
 Step 6:- Open HMC and collect the test data                   








          Step 8:- Set Up the Catalog Version
                        (a)    Add an instance variable and a setter method to the ProductController of CatalogService
                                       private CatalogService catalogService;
                               public void setCatalogService(final CatalogService catalogService)
                              {
                                     this.catalogService = catalogService;
                              }

                              catalogService.setSessionCatalogVersion("apparelProductCatalog", "Online");


                                  (b)    Add the dependency of the CatalogService to the springmvc-servlet.xml file  

                     <bean name="/product.html" class="org.training.web.controllers.ProductController">
    <property name="productService" ref="productService"/>
    <property name="catalogService" ref="catalogService"/>
</bean>      

(c) Stop server, call ant clean all and start the hybris server                 
                               (d)   Invoke URL  http://localhost:9001/hybristraining/product.html?code=100124            

              















Exercise to how display user information on JSP page

This exercise will give the idea how to  display an user account on a frontend JSP using UserService. For the same , required to create our own Spring MVC controller which will use UserService for gathering an user information and will display it at a JSP view.

High level below activity would be required to achieve this exercise

 Detail step are given below:-
                   Put the Spring MVC library JAR into the web/webroot/WEB-INF/lib folder of the hybristraining extension
           Copy jar  /acceleratorstorefrontcommons/commonweb/webroot/WEB-INF/lib/spring-webmvc-3.2.5.RELEASE    and put inside folder web/webroot/WEB-INF/lib 




Display some message (User goes here) on JSP first before displaying user information
(1)   Add a servlet definition to the extension's web.xml file located in the hybristraining/web/webroot/WEB-INF directory
    <servlet>
         <servlet-name>springmvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

Run ant clean all , build should be successful


(2) Create front controller UserController.java 
package org.training.web.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class UserController implements Controller
{
@Override
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception
{
return new ModelAndView("user.jsp");
}
}


(3) Create springmvc-servlet.xml to path web/webroot/WEB-INF/   and add below highlighted line

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    <bean
    name="/user.html"
    class="org.training.web.controllers.UserController">
  
    </bean>
                
</beans>

(4) Create a new file named user.jsp in the hybristraining/web/webroot

<html>
    <head>
        <title>User</title>
    </head>
    <body>
        User goes here
    </body>
</html>

(5) Call ant clean all
(6) Start the hybris Server

(8) Inject the UserService to get user information .For the add below highlighted code inside UserController.java


 (9)   To obtain the UserService, use a dependency injection, for the same add below highlighted code Snippet to  web/webroot/WEB-INF/springmvc-servlet.xml

(10)   Lookup the current user, add below highlighted code snippet to  UserController.java

               final UserModel user = userService.getCurrentUser();
final Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
return new ModelAndView("user.jsp", model);

(11)  To display the Current User, modify user.jsp with below code                         

                       <html>
                           <head>
                            <title>User</title>
                           </head>
                             <body>
                                 <h1>${user.uid}</h1>
                                <b>Name:</b> ${user.name}
                               <br/>
                              <b>Description:</b> ${user.description}
                        </body>
                    </html>   

(12)       Call ant clean all
(13)    Start the hybris Server
           (14)    Hit the URL http://localhost:9001/hybristraining/user.html 



Look Up and Display a Custom User
To display a specific user instead of the current one, use an URL parameter to pass the desired UID, such as: http://localhost:9001/training/user.html?uid=admin

For the same Modify UserController.java and add below highlighted code

        uid = request.getParameter("uid");
UserModel user = null;
if (uid == null)
{
user = userService.getCurrentUser();
}
else
{
user = userService.getUser(uid);
}


-> Call ant  clean all                                                     
    -> Start the hybris Server                                                 
                                     -> http://localhost:9001/hybristraining/user.html?uid=admin