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

📄 producteditaction.java

📁 工厂版本管理系统,STRUTS2框架,用于管理商品的版本,便于有效的控制版本
💻 JAVA
字号:
package com.bluesky.elecall.web.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.bluesky.elecall.domain.CategoryItem;
import com.bluesky.elecall.domain.DiscountPolicy;
import com.bluesky.elecall.domain.Product;
import com.bluesky.elecall.domain.ProductAttribute;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class ProductEditAction extends BaseAction implements Preparable,
		PreResultListener {

	// form bean
	private Product product = new Product();

	private List attributes = new ArrayList();
	private List discountPolicies = new ArrayList();

	private File image;
	private String imageContentType;
	private String imageFileName;

	private File smallImage;
	private String smallImageContentType;
	private String smallImageFileName;

	// for next page
	private List families;
	private List manufactories;

	public ProductEditAction() {

		product.setCategoryItem(new CategoryItem());

		for (int i = 0; i < 5; i++) {
			attributes.add(new ProductAttribute());
		}

		for (int i = 0; i < 5; i++) {
			discountPolicies.add(new DiscountPolicy());
		}

	}

	public void prepare() throws Exception {
		//load manufactories and families
		
		manufactories = manufactoryDao.getAll();

		Product p=null;
		CategoryItem ci=null;
		if (product.getId() != null) {
			p = productCategory.getProduct(product.getId());
			
			ci = productCategory.getCategoryItem(p
					.getCategoryItem().getId());
		}
		else
			ci = productCategory.getCategoryItem(product
				.getCategoryItem().getId());
		
		families = new ArrayList(productCategory
				.getChildrenCategoryItems(ci));

	}

	@Override
	public String input() throws Exception {

		if (product.getId() != null) {
			product = productCategory.getProduct(product.getId());

			// clear formbean attributes, filling it below
			attributes.clear();
			discountPolicies.clear();
			if (product != null) {

				// copy 'product.attributes' to formbean 'attributes', replace
				Iterator it = product.getAttributes().iterator();
				int i = 0;
				while (it.hasNext()) {
					ProductAttribute pa = (ProductAttribute) it.next();

					attributes.add(pa);

					i++;
				}

				Iterator it2 = product.getDiscountPolicies().keySet()
						.iterator();
				while (it2.hasNext()) {
					long key = (Long) it2.next();
					BigDecimal unitPrice = (BigDecimal) product
							.getDiscountPolicies().get(key);

					DiscountPolicy dp = new DiscountPolicy();
					dp.setBaseQuantity(key);
					dp.setUnitPrice(unitPrice);

					discountPolicies.add(dp);

				}

			}
		}

		// to reach the min_size
		while (attributes.size() < 6)
			attributes.add(new ProductAttribute());

		while (discountPolicies.size() < 5)
			discountPolicies.add(new DiscountPolicy());

		return super.input();
	}

	public String save() throws Exception {
		if (product.getManufactory().getId() < 0) {
			addActionError("制造商是必选的");
			return INPUT;
		}

		if (product.getFamily().getId() < 0)
			product.setFamily(null);

		product.getAttributes().clear();
		Iterator it = attributes.iterator();
		while (it.hasNext()) {
			ProductAttribute pa = (ProductAttribute) it.next();
			if (pa.validate())
				product.addAttribute(pa);
		}

		product.getDiscountPolicies().clear();
		it = discountPolicies.iterator();
		while (it.hasNext()) {
			DiscountPolicy dp = (DiscountPolicy) it.next();
			if (dp.getBaseQuantity() > 1)
				product.getDiscountPolicies().put(dp.getBaseQuantity(),
						dp.getUnitPrice());
		}

		// save image to db
		if (image != null) {
			InputStream is = new FileInputStream(image);
			byte[] buffer = new byte[1023 * 1024 * 10];
			int len = is.read(buffer);
			is.close();

			byte[] data = new byte[len];
			for (int i = 0; i < len; i++)
				data[i] = buffer[i];

			com.bluesky.system.File file = new com.bluesky.system.File();
			file.setContentType(imageContentType);
			file.setData(data);

			reposity.save(file);

			if (product.getImageFileId() != null)
				reposity.delete(com.bluesky.system.File.class, product
						.getImageFileId());

			product.setImageFileId(file.getId());
		}
		
		// save small image to db
		if (smallImage != null) {
			InputStream is = new FileInputStream(smallImage);
			byte[] buffer = new byte[1023 * 1024 * 10];
			int len = is.read(buffer);
			is.close();

			byte[] data = new byte[len];
			for (int i = 0; i < len; i++)
				data[i] = buffer[i];

			com.bluesky.system.File file = new com.bluesky.system.File();
			file.setContentType(smallImageContentType);
			file.setData(data);

			reposity.save(file);

			if (product.getSmallImageFileId() != null)
				reposity.delete(com.bluesky.system.File.class, product
						.getSmallImageFileId());

			product.setSmallImageFileId(file.getId());
		}

		if (product.getId().equals(""))
			productCategory.save(product);
		else
			productCategory.merge(product);

		return SUCCESS;
	}

	public void beforeResult(ActionInvocation arg0, String arg1) {

	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public File getSmallImage() {
		return smallImage;
	}

	public void setSmallImage(File smallImage) {
		this.smallImage = smallImage;
	}

	public String getSmallImageContentType() {
		return smallImageContentType;
	}

	public void setSmallImageContentType(String smallImageContentType) {
		this.smallImageContentType = smallImageContentType;
	}

	public String getSmallImageFileName() {
		return smallImageFileName;
	}

	public void setSmallImageFileName(String smallImageFileName) {
		this.smallImageFileName = smallImageFileName;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List getAttributes() {
		return attributes;
	}

	public void setAttributes(List attributes) {
		this.attributes = attributes;
	}

	public List getDiscountPolicies() {
		return discountPolicies;
	}

	public void setDiscountPolicies(List discountPolicies) {
		this.discountPolicies = discountPolicies;
	}

	public List getFamilies() {
		return families;
	}

	public void setFamilies(List families) {
		this.families = families;
	}

	public List getManufactories() {
		return manufactories;
	}

	public void setManufactories(List manufactories) {
		this.manufactories = manufactories;
	}

}

⌨️ 快捷键说明

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