⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customerfunctioncontroller.java

📁 一个购房管理系统,JSF+Hibernate+Mssql2
💻 JAVA
字号:
/*
 * CustomerFunctionController.java
 *
 * Created on 2007��6��2��, ����11:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.housesale.jsfbean;

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

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import org.hibernate.Query;

import com.housesale.hibernate.Customer;
import com.housesale.hibernate.CustomerFunction;
import com.housesale.hibernate.dao.CustomerFunctionDAO;

/**
 * 
 * @author msq
 */
public class CustomerFunctionController {

	/** Creates a new instance of CustomerFunctionController */
	public CustomerFunctionController() {
	}

	private CustomerFunction customerFunction;

	private DataModel model;

	private CustomerFunctionDAO getEntityManager() {

		CustomerFunctionDAO.initialize();

		return new CustomerFunctionDAO();
	}

	private int batchSize = 10;

	private int firstItem = 0;

	private String queryProperty;

	private String queryValue;

	private int itemCount = 0;

	public DataModel getModel() {
		return model;
	}

	public void setModel(DataModel model) {
		this.model = model;
	}

	public String getQueryProperty() {
		return queryProperty;
	}

	public void setQueryProperty(String queryProperty) {
		this.queryProperty = queryProperty;
	}

	public String getQueryValue() {
		return queryValue;
	}

	public void setQueryValue(String queryValue) {
		this.queryValue = queryValue;
	}

	public void setItemCount(int itemCount) {
		this.itemCount = itemCount;
	}

	public CustomerFunction getCustomerFunction() {
		return customerFunction;
	}

	public void setCustomerFunction(CustomerFunction customerFunction) {
		this.customerFunction = customerFunction;
	}

	public DataModel getDetailCustomerFunctions() {
		return model;
	}

	public void setDetailCustomerFunctions(Collection<CustomerFunction> m) {
		model = new ListDataModel(new ArrayList(m));
	}

	public String createSetup() {
		this.customerFunction = new CustomerFunction();
		return "customerFunction_create";
	}

	public String create() {
		CustomerFunctionDAO em = getEntityManager();
		try {

			em.save(customerFunction);

			addSuccessMessage("CustomerFunction was successfully created.");
		} catch (Exception ex) {
			try {
				addErrorMessage(ex.getLocalizedMessage());

			} catch (Exception e) {
				addErrorMessage(e.getLocalizedMessage());
			}
		}
		return "customerFunction_list";
	}

	public String detailSetup() {
		setCustomerFunctionFromRequestParam();
		return "customerFunction_detail";
	}

	public String editSetup() {
		setCustomerFunctionFromRequestParam();
		return "customerFunction_edit";
	}

	public String edit() {
		CustomerFunctionDAO em = getEntityManager();
		try {

			em.update(customerFunction);

			addSuccessMessage("CustomerFunction was successfully updated.");
		} catch (Exception ex) {
			try {
				addErrorMessage(ex.getLocalizedMessage());

			} catch (Exception e) {
				addErrorMessage(e.getLocalizedMessage());
			}
		}
		return "customerFunction_list";
	}

	public String destroy() {
		CustomerFunctionDAO em = getEntityManager();
		try {

			CustomerFunction customerFunction = getCustomerFunctionFromRequestParam();
			em.delete(customerFunction);

			addSuccessMessage("CustomerFunction was successfully deleted.");
		} catch (Exception ex) {
			try {
				addErrorMessage(ex.getLocalizedMessage());

			} catch (Exception e) {
				addErrorMessage(e.getLocalizedMessage());
			}
		}
		return "customerFunction_list";
	}

	public CustomerFunction getCustomerFunctionFromRequestParam() {
		CustomerFunctionDAO em = getEntityManager();
		CustomerFunction o = null;
		try {
			o = (CustomerFunction) model.getRowData();
			o = em.get(o.getId());
		} finally {
			return o;
		}
	}

	public void setCustomerFunctionFromRequestParam() {
		CustomerFunction customerFunction = getCustomerFunctionFromRequestParam();
		setCustomerFunction(customerFunction);
	}

	public String findCustomerFunctions() {

		Query q = null;
		if (this.queryProperty.equals("id")
				|| this.queryProperty.equals("customer.customerId")) {
			try {
				Integer.valueOf(getQueryValue());
			} catch (NumberFormatException e) {
				addErrorMessage(e.getLocalizedMessage());
				return "customerFunction_list";
			}
			q = this.getEntityManager().findByProperty(getQueryProperty(),
					Integer.valueOf(getQueryValue()));
		} else if (this.queryProperty.equals("all")) {
			q = this.getEntityManager().getQuery("from CustomerFunction as o");
		} else {
			q = this.getEntityManager().findByProperty(getQueryProperty(),
					getQueryValue());
		}
		List list = q.list();
		this.setItemCount(list.size());
		q.setMaxResults(batchSize);
		q.setFirstResult(firstItem);		
		model = new ListDataModel(q.list());
		this.getEntityManager().closeCurrentSession();
		return "customerFunction_list";
	}

	public static void addErrorMessage(String msg) {
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				msg, msg);
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.addMessage(null, facesMsg);
	}

	public static void addSuccessMessage(String msg) {
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO,
				msg, msg);
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.addMessage("successInfo", facesMsg);
	}

	public CustomerFunction findCustomerFunction(Integer id) {
		CustomerFunctionDAO em = getEntityManager();
		CustomerFunction o = null;
		try {
			o = (CustomerFunction) em.get(id);

		} finally {
			return o;
		}
	}

	public javax.faces.model.SelectItem[] getCustomerIDs() {
		CustomerFunctionDAO em = getEntityManager();
		SelectItem select[] = new SelectItem[0];
		try {
			List<Customer> l = (List<Customer>) em.getQuery(
					"from Customer as o").list();
			select = new SelectItem[l.size()];
			int i = 0;
			for (Customer x : l) {
				select[i++] = new SelectItem(x,x.getCustomerName());
			}

		} finally {
			return select;
		}
	}

	public int getItemCount() {

		return this.itemCount;

	}

	public int getFirstItem() {
		return firstItem;
	}

	public int getLastItem() {
		int size = getItemCount();
		return firstItem + batchSize > size ? size : firstItem + batchSize;
	}

	public int getBatchSize() {
		return batchSize;
	}

	public String next() {
		if (firstItem + batchSize < getItemCount()) {
			firstItem += batchSize;
		}
		return findCustomerFunctions();
	}

	public String prev() {
		firstItem -= batchSize;
		if (firstItem < 0) {
			firstItem = 0;
		}
		return findCustomerFunctions();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -