Skip to Content

Hi All,

Am going to write a blog post about adding a new item type and it’s service work flow in hybris.

Here, I have taken item type as customer.

After completing the hybris setup using recipe you have to follow the below steps.

Step1:

First you have to create an item type in (extension-name)-item.xml(in core extension)

 

Step2:

After that do ant clean all & hybrisserver.bat once successful those commands update in hac and checked whether the table created or not with attributes.

After creating table

 

 

Step3:

Created a new ConfigurecarForm pojo class  (Entity class with getters and setters) in the same package

package concerttours.controller;


public class ConfigurecarForm
{
	private String Firstname;
	private String Lastname;
	private String phone;
	private String Zipcode;
	private String Email;
	private boolean SubscribeNewsletter;

	/**
	 * @return the firstname
	 */
	public String getFirstname()
	{
		return Firstname;
	}

	/**
	 * @param firstname
	 *           the firstname to set
	 */
	public void setFirstname(final String firstname)
	{
		Firstname = firstname;
	}

	/**
	 * @return the lastname
	 */
	public String getLastname()
	{
		return Lastname;
	}

	/**
	 * @param lastname
	 *           the lastname to set
	 */
	public void setLastname(final String lastname)
	{
		Lastname = lastname;
	}

	/**
	 * @return the phone
	 */
	public String getPhone()
	{
		return phone;
	}

	/**
	 * @param phone
	 *           the phone to set
	 */
	public void setPhone(final String phone)
	{
		this.phone = phone;
	}

	/**
	 * @return the zipcode
	 */
	public String getZipcode()
	{
		return Zipcode;
	}

	/**
	 * @param zipcode
	 *           the zipcode to set
	 */
	public void setZipcode(final String zipcode)
	{
		Zipcode = zipcode;
	}

	/**
	 * @return the email
	 */
	public String getEmail()
	{
		return Email;
	}

	/**
	 * @param email
	 *           the email to set
	 */
	public void setEmail(final String email)
	{
		Email = email;
	}

	/**
	 * @return the subscribeNewsletter
	 */
	public boolean isSubscribeNewsletter()
	{
		return SubscribeNewsletter;
	}

	/**
	 * @param subscribeNewsletter
	 *           the subscribeNewsletter to set
	 */
	public void setSubscribeNewsletter(final boolean subscribeNewsletter)
	{
		SubscribeNewsletter = subscribeNewsletter;
	}

}

Step4:

created a new controller (OwnerDashboardController.java)with passing sample static data.

Note:in this controller you can send form data through ajax call into the method. so you can send data through postman or through ajax call.

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2018 SAP SE
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of SAP
 * Hybris ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the
 * terms of the license agreement you entered into with SAP Hybris.
 */
package concerttours.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException;
import concerttours.facades.ConfigurecarFacade;
import de.hybris.platform.commercefacades.user.data.AddressData;
import de.hybris.platform.commercefacades.user.data.CustomerData;



/**
 *
 */

@Controller
//@Scope("tenant")
//@RequestMapping("/")
public class OwnerDashboardController
{


	@Autowired
	private ConfigurecarFacade configurecarFacade;

	@RequestMapping(value = "/saveyourdetails", method = RequestMethod.GET)
	public String configureFormSubmit(final ConfigurecarForm form) throws CMSItemNotFoundException
	{


		configurecarFacade.saveModel(setDTO(form));
		return "welcome";
	}

	public CustomerData setDTO(final ConfigurecarForm form)
	{
		final AddressData addressData = new AddressData();
		final CustomerData customerData = new CustomerData();

		
		 customerData.setFirstName(form.getFirstname());
		 customerData.setLastName(form.getLastname());
		customerData.setEmail(form.getEmail()); 
		customerData.setNewsletter(form.isSubscribeNewsletter());
		customerData.setName(form.getFirstname()+ form.getLastname());
		  addressData.setPhone(form.getPhone()); 
		  addressData.setPostalCode(form.getZipcode());
		 customerData.setAddress(addressData);
		


		return customerData;
	}
}

 

Step 5:

created a new facade interface in wit the name of (ConfigurecarFacade).

package concerttours.facades;

import de.hybris.platform.commercefacades.user.data.CustomerData;


/**
 *
 */
public interface ConfigurecarFacade
{
	public void saveModel(final CustomerData customerData);
}

Step 6:

created implementation class for facade layer(ConfigurecarFacadeImp).

package concerttours.facades.impl;

import de.hybris.platform.commercefacades.user.data.CustomerData;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.user.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import concerttours.facades.ConfigurecarFacade;
import concerttours.facades.populators.ConfigurecarReversePopulator;
import concerttours.service.ConfigurecarService;


/**
 *
 */
public class ConfigurecarFacadeImp implements ConfigurecarFacade
{


	@Autowired
	private ConfigurecarService configurecarService;

	@Autowired
	public ConfigurecarReversePopulator configurecarPopulator;

	@Autowired
	private UserService user;

	@Autowired
	private ModelService modelService;


	@Override
	public void saveModel(final CustomerData customerData)
	{

		CustomerModel customerModel = new CustomerModel();

		// New User
		if (!user.isUserExisting(customerData.getEmail()))
		{
			System.out.println("New user::" + customerData.getEmail());
			customerModel.setFirstName(customerData.getFirstName());
			customerModel.setLastName(customerData.getLastName());
			customerModel.setUid(customerData.getEmail());
			customerModel.setSubscribeNewsLetter(Boolean.valueOf(customerData.isNewsletter()));
			customerModel.setName(customerData.getName());
			configurecarPopulator.populate(customerData, customerModel);
			configurecarService.saveModel(customerModel);
		}
		else
		//Existing user
		{
			System.out.println("Existing user::" + customerData.getEmail());
			customerModel = (CustomerModel) user.getUserForUID(customerData.getEmail());
			customerModel.setFirstName(customerData.getFirstName());
			customerModel.setLastName(customerData.getLastName());
			customerModel.setSubscribeNewsLetter(Boolean.valueOf(customerData.isNewsletter()));
			customerModel.setName(customerData.getName());
			configurecarPopulator.populate(customerData, customerModel);
			modelService.save(customerModel);
		}

	}
}

 

In this ConfigurecarFacadeImp class i have defined a method called saveModel it set the data into model class.

defined some condition like for new user and excisting user,

if customer not excisting in hybris we are calling custom service(configurecarService.saveModel(customerModel);)

if Existing user calling

modelService.save(customerModel);

 

Step 7:

created a new service interface ConfigurecarService.

package concerttours.service;

import de.hybris.platform.core.model.user.CustomerModel;


/**
 *
 */
public interface ConfigurecarService
{

	public void saveModel(final CustomerModel model);
}

 

Step 8:

Created a implementation class for service ConfigurecarServiceImpl

package concerttours.service.impl;

import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.servicelayer.event.EventService;
import de.hybris.platform.servicelayer.i18n.CommonI18NService;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.user.UserService;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import concerttours.service.ConfigurecarService;
import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException;

import de.hybris.platform.commerceservices.customer.DuplicateUidException;
import de.hybris.platform.commerceservices.customer.CustomerAccountService;
/**
 *
 */
public class ConfigurecarServiceImpl implements ConfigurecarService
{


	@Autowired
	public ModelService modelService;

	@Resource(name = "eventService")
	public EventService eventService;

	@Autowired
	private CommonI18NService commonI18NService;

	@Autowired
	private CustomerAccountService customerAccountService;

	@Autowired
	private UserService user;

	@Override
	public void saveModel(final CustomerModel model)
	{
		try
		{
			customerAccountService.register(model, "test123");
		
		}
		catch (final DuplicateUidException e)
		{
			System.out.println("Configurecar Service :User already exist");
		}
		
	}
}

 

Step 9:

created a new reverse populator class ConfigurecarReversePopulator.

 

package concerttours.facades.populators;


import de.hybris.platform.commercefacades.user.data.AddressData;
import de.hybris.platform.commercefacades.user.data.CustomerData;
import de.hybris.platform.converters.Populator;
import de.hybris.platform.core.model.user.AddressModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.servicelayer.dto.converter.ConversionException;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;



public class ConfigurecarReversePopulator implements Populator<CustomerData, CustomerModel>
{

	private static final Logger logger = LoggerFactory.getLogger(ConfigurecarReversePopulator.class);

	@Autowired
	private FlexibleSearchService flexibleSearchService;

	@Override
	public void populate(final CustomerData source, final CustomerModel target) throws ConversionException
	{

		final List<AddressModel> addressList = new ArrayList<AddressModel>();
		final AddressModel addressModel = new AddressModel();

		final AddressData address = source.getAddress();
		logger.info("Setting postal code {}", address.getPostalCode());
		addressModel.setPostalcode(address.getPostalCode());

		addressModel.setOwner(target);
		logger.info("Setting phone no. to {}", address.getPhone());
		addressModel.setPhone1(address.getPhone());

		addressList.add(addressModel);
		target.setAddresses(addressList);
	}

}

Step 10:

Define beans for that clases in resource folder concerttours-spring.xml(concerttours is my extension name)

 

<bean id="ConfigurecarService" 
		class="concerttours.service.impl.ConfigurecarServiceImpl">
	</bean> 
<bean id="ConfigurecarFacade" 
		class="concerttours.facades.impl.ConfigurecarFacadeImp">
	</bean> 
	<bean id="ConfigurecarReversePopulator" name="ConfigurecarReversePopulator"
		class="concerttours.facades.populators.ConfigurecarReversePopulator">
	</bean> 

Step 11:

After calling the service with payload as

{ "FirstName":"XXX",
  "LastName":"XXX",
  "Email":"XXXXXX@gmail.com",
  "Newsletter":"true",
  "Name":"XXXX",
  "Phone":"1234567890", 
  "PostalCode":"56435",
  "Address":"XXXXX"    
}

 

the static data inserted into the table as shown in below screenshot.

 

 

 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply