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

📄 categorypersistaction.java

📁 Sample Hibernate Shopping Cart Application
💻 JAVA
字号:
/*
 * PersistCategory.java
 *
 * Created on February 12, 2005, 2:50 PM
 */

package org.simplecart.webapp.actions.administration;

import javax.servlet.http.*;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.*;

import org.apache.log4j.Category;

import org.apache.commons.beanutils.PropertyUtils;

import net.sf.hibernate.HibernateException;

import org.simplecart.persistence.HibernateUtility;
import org.simplecart.webapp.Constants;
import org.simplecart.webapp.forms.CatalogItemForm;
import org.simplecart.dao.InternetProductCategoryDAO;
import org.simplecart.shopcart.catalog.*;

/**
 *
 * @author Daniel Watrous
 */
public class CategoryPersistAction  extends DispatchAction {
    
    private InternetProductCategoryDAO dao;
    private static Category cat = Category.getInstance(CategoryPersistAction.class.getName());
    
    /**
     * Receives a populated CatalogItemForm and creates
     * a new <em>Stake</em> which it then persists
     */
    public ActionForward enter(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        // cast my form to a useful Type
        CatalogItemForm categoryForm = (CatalogItemForm) form;
        
        // copy form-bean values to new Stake and Address objects
        SearchUtility searchDetails = new SearchUtility();
        PropertyUtils.copyProperties(searchDetails, categoryForm);
        InternetProductCategory newCategory = new InternetProductCategory();
        PropertyUtils.copyProperties(newCategory, categoryForm);
        
        // attach the address to this new category
        newCategory.setSearchDetails(searchDetails);
        
        // get a DAO for the new Category
        dao = new InternetProductCategoryDAO();
        
        // store the new Stake
        dao.makePersistent(newCategory);
        
        // commit this transaction
        HibernateUtility.commitTransaction();
        HibernateUtility.closeSession();
        
        return mapping.findForward(Constants.SUCCESS_KEY);
    }
    
    /**
     * Receives a populated CatalogItemForm and loads an
     * existing <em>Stake</em> object which it then
     * updates and persists
     */
    public ActionForward edit(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        // cast my form to a useful Type
        CatalogItemForm categoryForm = (CatalogItemForm) form;
        
        // get a DAO for the new Stake
        dao = new InternetProductCategoryDAO();
        
        ProductCategory category = null;
        
        try {
            // find category objects and attach to request
            category = (ProductCategory) dao.findById(categoryForm.getId(), false);
            
            // copy form-bean values to new Stake and Address objects
            // NOTE: PropertyUtils will not work in this situation since there
            //       is an ID field which is different for Stake and Address
            category.setName(categoryForm.getName());
            category.setDescription(categoryForm.getDescription());
            category.getSearchDetails().setSearchMetaAuthor(categoryForm.getSearchMetaAuthor());
            category.getSearchDetails().setSearchMetaKeywords(categoryForm.getSearchMetaKeywords());
            category.getSearchDetails().setSearchMetaDescription(categoryForm.getSearchMetaDescription());
            
            // update the Stake
            dao.makePersistent(category);
            
            // close transaction and session
            HibernateUtility.commitTransaction();
            HibernateUtility.closeSession();
        } catch (HibernateException e) {
            cat.info("An exception occured: " + e.toString());
        }
        
        return mapping.findForward(Constants.SUCCESS_KEY);
    }
    
    /**
     * Receives a populated CatalogItemForm and uses the id
     * field to delete a <em>Stake</em> object and persist
     * the deletion.
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        // cast my form to a useful Type
        CatalogItemForm categoryForm = (CatalogItemForm) form;
        
        // copy form-bean values to new Stake objects
        InternetProductCategory category = new InternetProductCategory();
        PropertyUtils.copyProperties(category, categoryForm);
        
        // get a DAO for the new Stake
        dao = new InternetProductCategoryDAO();
        
        // store the new Stake
        dao.makeTransient(category);
        
        // commit this transaction
        HibernateUtility.commitTransaction();
        HibernateUtility.closeSession();
        
        return mapping.findForward(Constants.SUCCESS_KEY);
    }
}

⌨️ 快捷键说明

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