Shopping Basket

Shopping Basket is a fictional online grocery and food product provider. It sells fresh fruits, vegetables, groceries, beverages, dairy, branded food and etc..

In a coming video series, I will be explaining how this beautiful user interface can be developed using Angular and spring boot services with step by step instructions.

Shopping basket home page lists a variety of products. The users can browse through these items and can add to items to shopping cart. Items can be added, quantity can be increased or decreased. Selected item get added to users shopping cart in real-time.

Shopping basket can be managed by admin users using 2 options- manage orders and manage inventory.

Manage orders – Lists orders placed using shopping basket. Admin user can check the delivery status of the orders.

Manage Inventory – Items inventory can be managed using this option. This provides an option to add the new item to inventory, add/update/delete item etc.

Mulesoft – Excel download using Apache POI

Excel can be created in Mulesoft using Apache POI library and allow for download

  • Create Maven enabled Mulesoft project
  • Add the Apache POI as a dependency in the pom.xml
 	   <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
          </dependency>
          
  • Create RAML file having endpoint to access excel download URL
/excel:
    get:
          description: fetch worklist details.
          is: [pagination, accept-required, content-type-required, client-credentials-required, standard-error-responses]
          responses:
            200:
              body:
                '*/*':
  •  Create Apache POI utility Implementation which generates XML file
package com.pwn.download;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelDownload {

	public InputStream downloadFile() {

		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("Cities");

		Object[][] cities = { { "USA", "Dublin", 94568 }, { "USA", "San Ramon", 94568 },
				{ "USA", "Livermore", 97856 }, { "USA", "Pleasanton", 54689 }, };

		int rowCount = 0;

		for (Object[] city : cities) {
			Row row = sheet.createRow(++rowCount);
			int columnCount = 0;

			for (Object field : city) {
				Cell cell = row.createCell(++columnCount);
				if (field instanceof String) {
					cell.setCellValue((String) field);
				} else if (field instanceof Integer) {
					cell.setCellValue((Integer) field);
				}
			}

		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			workbook.write(bos);
			bos.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return new ByteArrayInputStream(bos.toByteArray());
	}
}

One important point is xls data should be sent as inputStream(as opposite to  outputStream) to download as mentioned in the below code

ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			workbook.write(bos);
			bos.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return new ByteArrayInputStream(bos.toByteArray());

    Register this as a bean

  • Create the flow using API definition file

  • Run the application and test the endpoint using following parameters
Headers – 
	accept:application/json
	client_id:xx
	client_secret:xx
	content-type:application/json
URL - http://localhost:8081/download/v1/excel

 

  • Use the option Send and Download option to execute the endpoint
  • Output looks like as below

Source Code – https://github.com/programmingwithnaveen/excel-download

Java – Load Properties from a XML file

Properties from XML file can be read using loadFromXML() option

 
package com.pwn.property;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class LoadPropertyFromXML {

	public static void main(String[] args) {
		// make new properties instance to load the file into
		Properties prop = new Properties();
		// check to make sure the file exists
		File file = new File("c://install//userdetails.xml");
		if (file.exists()) {
			// load the file
			try {
				prop.loadFromXML(new FileInputStream(file));
			} catch (InvalidPropertiesFormatException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			// print out all the properties
			for (String name : prop.stringPropertyNames()) {
				System.out
						.println(name + "=" + prop.getProperty(name));
			}
		} else {
			System.err.println("Error: No file found : ");
		}

	}

}

name=Naveen
country=US

Java – Saving Properties as XML

Using storeToXML() option Property files can be stored as XML.

 
package com.pwn.property;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SavePropertyAsXML {

	public static void main(String[] args) {
		// make new instance of properties
		Properties prop = new Properties();
		// set the property values
		prop.setProperty("name", "Naveen");
		prop.setProperty("country", "US");

		// check to see if the file already exists
		File file = new File("c://install//userdetails.xml");
		if (!file.exists()) {
			// save the properties
			try {
				file.createNewFile();
				prop.storeToXML(new FileOutputStream(file),
						"testing properties with xml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>testing properties with xml</comment>
<entry key="name">Naveen</entry>
<entry key="country">US</entry>
</properties>

Java – Loading Property File

The properties object contains key and value pair both as a string. The java.util.Properties class is the subclass of
Hashtable.
It can be used to get property value based on the property key. The Properties class provides methods to get data
from properties file and store data into properties file.

Use following sample applicaiton to load a properties file bundled :

 
package com.pwn.property;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class LoadingProperty {
	
	/*user.properties
	name=Naveen
	country=US*/

	public static void main(String[] args) {
		try (InputStream bundledResource = LoadingProperty.class
				.getResourceAsStream("user.properties")) {
			Properties userProperties = new Properties();
			userProperties.load(bundledResource);
			System.out.println("username from property file "
					+ userProperties.get("name"));

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
username from property file Naveen