Skip to content

Creating a controller

Jinyoung Jang edited this page Oct 27, 2016 · 10 revisions

Draw a class diagram for controllers and models

My first simple controller

package com.abc.insurance.controllers;

import com.abc.insurance.*;
import org.metaworks.annotation.ServiceMethod;

public class NameAndAddressTab {

    Customer model;
        public Customer getModel() {
            return model;
        }
        public void setModel(Customer model) {
            this.model = model;
        }

    public NameAndAddressTab() {
        setModel(new Customer());
	}

	@ServiceMethod(callByContent=true)
	public void startMyQuote() {

        //
        System.out.println("hello "  + getModel().getFirstName());

	}

}

rebuild the project and rerun the tomcat

\swm\auto-insurance>mvn tomcat7:run-war

go to

http://localhost:8090/auto-insurance/runner.html?classname=com.abc.insurance.controllers.NameAndAddressTab

to see the result.

Storing in database

	@ServiceMethod(callByContent=true)
	public void startMyQuote() throws Exception {

        //
        System.out.println("hello "  + getModel().getFirstName());

        PersistentTransaction t = com.abc.insurance.AutoinsurancePersistentManager.instance().getSession().beginTransaction();
        try {
            AutoinsurancePersistentManager.instance().getSession().save(getModel());

            t.commit();
        }
        catch (Exception e) {
            t.rollback();

            throw e;
        }
	}

you can check the data by querying in mysql workbench:

Customize User Interfaces

Adding ejs files for each model classes

/com/abc/insurance/Customer.ejs

<li> name: <%=fields.firstName.here()%>

/com/abc/insurance/controllers/NameAndAddressTab.ejs


<ul class="nav nav-tabs">
  <li role="presentation" class="active"><a href="#">Name & Address</a></li>
  <li role="presentation"><a href="#">Vehicles</a></li>
  <li role="presentation"><a href="#">Your Rates</a></li>
</ul>

Provide Policyholder Name & Address

<%=fields.model.here()%>

<%=methods.startMyQuote.here()%>

Adding Bootstrap Styles in runner.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		
		<title>Metaworks3 Tester</title>

        <!-- Add here -->

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
		

Result

Adding Vehicles Tab and YourRates Tab

Firstly draw all the control class diagram

Open generated VehiclesTab.java and add logic:

package com.abc.insurance.controllers;

import com.abc.insurance.*;
import org.metaworks.annotation.Hidden;
import org.metaworks.annotation.ServiceMethod;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;

import java.util.HashSet;

public class VehiclesTab {

 
    Vehicle model;
        public Vehicle getModel() {
            return model;
        }   
        public void setModel(Vehicle model) {
            this.model = model;
        }

    Customer customer;
    @Hidden
        public Customer getCustomer() {
            return customer;
        }
        public void setCustomer(Customer customer) {
            this.customer = customer;
        }

    public VehiclesTab() {}

    public VehiclesTab(Customer customer) {
        setCustomer(customer);
		setModel(new Vehicle());
        getModel().setOwner(customer);
	}

	@ServiceMethod(callByContent=true)
	public NameAndAddressTab back() {

        NameAndAddressTab nameAndAddressTab = new NameAndAddressTab();
        nameAndAddressTab.setModel(getCustomer());

		return nameAndAddressTab;
	}

    @ServiceMethod(callByContent=true)
	public YourRateTab next() throws Exception {

        PersistentTransaction t = com.abc.insurance.AutoinsurancePersistentManager.instance().getSession().beginTransaction();
        try {

            //getCustomer().setVehicles(new HashSet());
            //getCustomer().getVehicles().add(getModel());

            AutoinsurancePersistentManager.instance().getSession().save(getModel());
            t.commit();

            return new YourRateTab(getCustomer());

        }
        catch (Exception e) {
            t.rollback();

            throw e;
        }
	}

}

and write YourRateTab.java so that it can calculate the rate at the customer's age grade:

package com.abc.insurance.controllers;

import com.abc.insurance.*;
import org.metaworks.annotation.ServiceMethod;

public class YourRateTab {

    Customer customer;
        public Customer getCustomer() {
            return customer;
        }

        public void setCustomer(Customer customer) {
            this.customer = customer;
        }


    private double rate;

        public double getRate() {
            return this.rate;
        }

        /**
         *
         * @param rate
         */
        public void setRate(double rate) {
            this.rate = rate;
        }

	/**
	 *
	 * @param customer
	 */
	public YourRateTab(Customer customer) throws Exception {
		setCustomer(customer);
        
        if(getCustomer().getAge() > 30){
            setRate(100);
        }else if(getCustomer().getAge() > 19){
            setRate(200);
        }else{
            throw new Exception("Under 19 cannot subscribe this service.");
        }
	}

	@ServiceMethod
	public VehiclesTab back() {
        return new VehiclesTab(getCustomer());
	}

}

Result: