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

📄 communitycontroller.java

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

package com.housesale.jsfbean;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.myfaces.custom.fileupload.UploadedFile;
import org.hibernate.Query;

import com.housesale.hibernate.Community;
import com.housesale.hibernate.dao.CommunityDAO;

/**
 * 
 * @author MSQ
 */
public class CommunityController {

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

	private Community community;

	private DataModel model;

	private int itemCount = 0;

	private int batchSize = 10;

	private int firstItem = 0;

	private String detailUrl;

	private String queryProperty;

	private String queryValue;

	private UploadedFile image1;

	private UploadedFile image2;

	private UploadedFile image3;

	private UploadedFile image4;

	private UploadedFile image5;

	private UploadedFile image6;

	public Community getCommunity() {
		return community;
	}

	public void setCommunity(Community community) {
		this.community = community;
	}

	public DataModel getDetailCommunitys() {
		return model;
	}

	private CommunityDAO getEntityManager() {
		CommunityDAO.initialize();
		return new CommunityDAO();
	}

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

	public String createSetup() {
		this.community = new Community();
		this.community.setCommunityId(0);
		return "community_create";
	}

	public String create() {

		try {

			getEntityManager().save(community);

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

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

	public String detailSetup() {
		
		FacesContext facesContext=FacesContext.getCurrentInstance();
		StringBuffer host=((HttpServletRequest)facesContext.getExternalContext().getRequest()).getRequestURL();	
		String str=host.substring(0, host.lastIndexOf("/"));
		str=str.substring(0, str.lastIndexOf("/")+1);
		this.setDetailUrl(str);
		
		setCommunityFromRequestParam();
		return "community_detail";
	}

	public String back() {
		return "community_list";
	}

	public String editSetup() {
		setCommunityFromRequestParam();
		return "community_edit";
	}

	public String findImages() {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse) fc
				.getExternalContext().getResponse();
		response.setContentType("image/jpg");
		try {
			OutputStream toClient = response.getOutputStream();
			this.community = this.getEntityManager().get(3);
			toClient.write(community.getImage1());
			toClient.close();
		} catch (IOException e) {
			PrintWriter toClient;
			try {
				toClient = response.getWriter();

				response.setContentType("text/html;charset=utf-8");
				toClient.write("error!");
				toClient.close();
			} catch (IOException e1) {

				e1.printStackTrace();
			}
		}

		return "image_detail";

	}

	public String edit() {

		try {

			getEntityManager().update(community);

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

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

	public String destroy() {

		try {

			Community community = getCommunityFromRequestParam();
			getEntityManager().delete(community);
			addSuccessMessage("Community was successfully deleted.");
		} catch (Exception ex) {
			try {
				addErrorMessage(ex.getLocalizedMessage());
			} catch (Exception e) {
				addErrorMessage(e.getLocalizedMessage());
			}
		}
		return "community_list";
	}

	public Community getCommunityFromRequestParam() {

		Community o = null;
		try {
			o = (Community) model.getRowData();
			o = getEntityManager().get(o.getCommunityId());

		} finally {
			return o;
		}
	}

	public void setCommunityFromRequestParam() {
		Community community = getCommunityFromRequestParam();
		setCommunity(community);
	}

	public String findCommunitys() {

		Query q = null;
		if (this.queryProperty.equals("communityId")) {
			try {
				Integer.valueOf(getQueryValue());
			} catch (NumberFormatException e) {
				addErrorMessage(e.getLocalizedMessage());
				return "community_list";
			}
			q = this.getEntityManager().findByProperty(getQueryProperty(),
					Integer.valueOf(getQueryValue()));
		} else if (this.queryProperty.equals("all")) {
			q = this.getEntityManager().getQuery("from Community 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 "community_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 Community findCommunity(Integer id) {
		Community o = null;
		try {
			o = (Community) this.getEntityManager().get(id);

		} finally {
			return o;
		}
	}

	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 findCommunitys();
	}

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

	public UploadedFile getImage1() {
		return image1;
	}

	public void setImage1(UploadedFile image1) {
		this.image1 = image1;
		if (image1 != null)
			try {
				this.community.setImage1(image1.getBytes());
			} catch (IOException e) {
				addErrorMessage(e.getLocalizedMessage());
			}
	}

	public UploadedFile getImage2() {
		return image2;
	}

	public void setImage2(UploadedFile image2) {
		this.image2 = image2;
		if (image2 != null)
			try {
				this.community.setImage2(image2.getBytes());
			} catch (IOException e) {
				addErrorMessage(e.getLocalizedMessage());
			}
	}

	public UploadedFile getImage3() {
		return image3;
	}

	public void setImage3(UploadedFile image3) {
		this.image3 = image3;
		if (image3 != null)
			try {
				this.community.setImage3(image3.getBytes());
			} catch (IOException e) {
				addErrorMessage(e.getLocalizedMessage());
			}
	}

	public UploadedFile getImage4() {
		return image4;
	}

	public void setImage4(UploadedFile image4) {
		this.image4 = image4;
		if (image4 != null)
			try {
				this.community.setImage4(image4.getBytes());
			} catch (IOException e) {
				addErrorMessage(e.getLocalizedMessage());
			}
	}

	public UploadedFile getImage5() {
		return image5;
	}

	public void setImage5(UploadedFile image5) {

		this.image5 = image5;
		if (image5 != null)
			try {
				this.community.setImage5(image5.getBytes());
			} catch (IOException e) {
				addErrorMessage(e.getLocalizedMessage());
			}
	}

	public UploadedFile getImage6() {
		return image6;
	}

	public void setImage6(UploadedFile image6) {
		this.image6 = image6;
		if (image6 != null)
			try {
				this.community.setImage6(image6.getBytes());
			} catch (IOException e) {
				addErrorMessage(e.getLocalizedMessage());
			}
	}

	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 DataModel getModel() {
		return model;
	}

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

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

	public String getDetailUrl() {
		return detailUrl;
	}

	public void setDetailUrl(String detailUrl) {
		this.detailUrl = detailUrl;
	}
}

⌨️ 快捷键说明

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