3. INTERAFACE
finnaly, show time :)…
let’s build the main class 🙂

/**
 * @author Fendi Yuniawan Aditya
 * @authorEmail fendi.yuniawan.a@gmail.com
 */
package Test;

import org.apache.log4j.Logger;
import org.fendi.pm.entities.Product;
import org.fendi.pm.entities.Subscriber;
import org.fendi.pm.service.ProductService;
import org.fendi.pm.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PmTest {
	static Logger log = Logger.getLogger(PmTest.class);
	static ApplicationContext context= new ClassPathXmlApplicationContext( new String[] {"repository-context.xml"});
	static UserService userService = (UserService) context.getBean("userService");
	static ProductService productService = (ProductService) context.getBean("productService");

	public static Product addProduct(){
		Product product = new Product();
		product.setProductId("100MB");
		product.setProductName("Paket Data 100 MB");
		product.setProductNormalSpeed(512);
		product.setProductThrottledSpeed(248);
		product.setProductQuota(100);
		productService.addNewProduct(product);
		return product;
	}

	public static Subscriber subscriberInfo(String id) {
		Subscriber subscriber = userService.getSubscriberInfo(id);
		log.info("nomor : " +subscriber.getSubscriberNumber());
		return subscriber;

	}

	public static void removeProduct(){
		productService.removeProduct(productService.getProductInfo("100MB"));
	}

	public static Subscriber activateNewSubscriber(){
		Subscriber subscriber = new Subscriber();
		Product product = productService.getProductInfo("100MB");
		subscriber.setSubscriberId("11115");
		subscriber.setSubscriberNumber("081111115");
		subscriber.setProduct(product);
		userService.activateSubscriber(subscriber);

		return subscriber;
	}

	public static void terminateSubscriber() {
		userService.terminateSubscriber(userService.getSubscriberInfo("11115"));
	}

	public static void activateProduct() {
		userService.activateProduct(userService.getSubscriberInfo("11115"), productService.getProductInfo("100MB"));
	}

	public static void terminateProduct() {
		userService.terminateProduct(userService.getSubscriberInfo("11191"));
	}

	public static void speedRequest() {
		int consumption = 0;
		userService.speedRequest(userService.getSubscriberInfo("11115"), consumption);
	}

	public static void main (String[] args){
		addProduct();
		activateNewSubscriber();
		activateProduct();
		speedRequest();
//		subscriberInfo("11115");
//		removeProduct();
//		terminateSubscriber();

	}
}

this is the repository-context.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
			           	http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:annotation-config />
	<tx:annotation-driven />
	<context:component-scan base-package="org.fendi.pm.service" />
  	<context:property-placeholder location="classpath:database.properties"/>

	<beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.user}" />
		<property name="password" value="${jdbc.password}" />
	</beans:bean>
	<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" 		ref="dataSource" />
		<property name="configLocation" 	value="classpath:hibernate.cfg.xml" />
		<property name="packagesToScan" value="org.fendi.pm.entities"></property>
	</beans:bean>

	<beans:bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</beans:bean>

	<beans:bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</beans:bean>

	<beans:bean id="baseDAO" class="org.fendi.pm.dao.BaseDAO" abstract="true">
		<property name="hibernateTemplate" ref="hibernateTemplate" />
	</beans:bean>

	<beans:bean id="dao" class="org.fendi.pm.dao.impl.DAOImpl" parent="baseDAO" autowire="byName"/>
</beans:beans>

you can run the main class now :)… it will give you output like below :

2013-06-14 09:41:32 INFO - [addNewProduct] Start ...
2013-06-14 09:41:33 INFO - New Product has been succesfully added
2013-06-14 09:41:33 INFO - [addNewProduct] Finish ...
2013-06-14 09:41:33 INFO - [activateSubscriber] Start ...
2013-06-14 09:41:33 INFO - Subscriber has been succesfully activated
2013-06-14 09:41:33 INFO - [activateSubscriber] Finish ...
2013-06-14 09:41:33 INFO - [activateProduct] Start ...
2013-06-14 09:41:33 INFO - Product has been successfuly added to subscriber
2013-06-14 09:41:33 INFO - [activateProduct] Finish ...
2013-06-14 09:41:33 INFO - [speedRequest] Start ...
2013-06-14 09:41:33 INFO - Your remaining product quota is : 102400
2013-06-14 09:41:33 INFO - Your Speed is : 512 Kbps
2013-06-14 09:41:33 INFO - [speedRequest] Finish ...

its working man …. now try to remove product and terminate the subscriber, by uncomment the removeProduct(), and terminateSubscriber() in the main method, then comment the rest. It will give you output like below:

2013-06-14 09:39:35 INFO - [removeProduct] Start ...
2013-06-14 09:39:35 INFO - Product has been succesfully removed
2013-06-14 09:39:35 INFO - [removeProduct] Finish ...
2013-06-14 09:39:35 INFO - [terminateSubscriber] Start ...
2013-06-14 09:39:35 INFO - Subscriber has been successfully terminated
2013-06-14 09:39:35 INFO - [terminateSubscriber] Finish ...

this is the happy ending :D….,

Advertisements