www.pudn.com > Community_open20061104.rar > AuctionMsgAction.java~8~


package com.redmoon.forum.plugin.auction; 
 
import javax.servlet.http.HttpServletRequest; 
import cn.js.fan.util.ErrMsgException; 
import javax.servlet.ServletContext; 
import com.redmoon.forum.plugin.base.IPluginMsgAction; 
import org.apache.log4j.Logger; 
import com.redmoon.forum.MsgDb; 
import cn.js.fan.util.StrUtil; 
import java.util.Calendar; 
import cn.js.fan.util.ResKeyException; 
import com.redmoon.kit.util.FileUpload; 
import cn.js.fan.util.DateUtil; 
import java.util.Iterator; 
import cn.js.fan.util.ImageUtil; 
import java.io.File; 
import cn.js.fan.util.file.FileUtil; 
import java.io.IOException; 
import java.util.Vector; 
 
public class AuctionMsgAction implements IPluginMsgAction { 
    Logger logger = Logger.getLogger(this.getClass().getName()); 
 
    public static final String ACTION_APPLY = "apply"; 
    public static final String ACTION_ACCEPT_APPLY = "acceptApply"; 
    public static final String ACTION_DECLINE_APPLY = "declineApply"; 
 
    public static final String ACTION_APPLY_MARRY = "applymarry"; 
    public static final String ACTION_ACCEPT_MARRY = "acceptApplyMarry"; 
    public static final String ACTION_DECLINE_MARRY = "acceptDeclineMarry"; 
 
    public AuctionMsgAction() { 
    } 
 
    /** 
     * 
     * @param application ServletContext 
     * @param request HttpServletRequest 
     * @param md MsgDb 所存储的是ReceiveData后得来的信息 
     * @return boolean 
     * @throws ErrMsgException 
     */ 
    public synchronized boolean editTopic(ServletContext application, 
                                          HttpServletRequest request, 
                                          MsgDb md, FileUpload fu) throws 
            ErrMsgException { 
        md = md.getMsgDb(md.getId()); // 注意参数md并非是对应于id的md 
        // 是交易贴 
        if (md.getReplyid() == -1) { 
            AuctionDb ad = new AuctionDb(); 
            ad = ad.getAuctionDb(md.getId()); 
            ad.setUserName(md.getName()); 
            String strcount = fu.getFieldValue("count"); 
            int count = 1; 
            try { 
                count = Integer.parseInt(strcount); 
            } 
            catch (Exception e) { 
                throw new ErrMsgException(AuctionSkin.LoadString(request, 
                        "err_count_must_string")); 
            } 
            String name = StrUtil.getNullStr(fu.getFieldValue("name")); 
            if (name.equals("")) 
                throw new ErrMsgException(AuctionSkin.LoadString(request, 
                        "err_want_name")); 
            /* 
                         // 销售类型不能更改 
                         int sellType = AuctionDb.SELL_TYPE_AUCTION; 
                         String strsellType = fu.getFieldValue("sellType"); 
                         if (StrUtil.isNumeric(strsellType)) { 
                sellType = Integer.parseInt(strsellType); 
                         } 
                         ad.setSellType(sellType); 
             */ 
            String moneyCode = StrUtil.getNullStr(fu.getFieldValue("moneyCode")); 
            String shopDir = StrUtil.getNullStr(fu.getFieldValue("shopDir")); 
            if (shopDir.equals("")) 
                shopDir = AuctionShopDirDb.DEFAULT; 
 
            ad.setCount(count); 
            // logger.info("name=" + name); 
            ad.setName(name); 
 
            // 如果该用户是商家,则判别其是否更改了目录 
            AuctionShopDb as = new AuctionShopDb(); 
            as = as.getAuctionShopDb(md.getName()); 
            if (as.isLoaded()) { // 用户是商家 
                String oldShopDir = ad.getShopDir(); 
                if (!oldShopDir.equals(shopDir)) { 
                    // 如果不是改至系统目录,则重置商品的catalogCode 
                    if (!shopDir.equals(AuctionShopDirDb.DEFAULT)) { 
                        // 取得新商铺目录对应的catalogCode 
                        AuctionShopDirDb asd2 = new AuctionShopDirDb(); 
                        asd2 = asd2.getAuctionShopDirDb(md.getName(), shopDir); 
                        ad.setCatalogCode(asd2.getCatalogCode()); 
                    } 
                    ad.setShopDir(shopDir); 
                } 
            } 
            else { // 用户为散户 
                String catalogCode = StrUtil.getNullString(fu.getFieldValue("catalogCode")); 
                if (catalogCode.equals(Leaf.TYPE_NONE_OPTION_VALUE)) 
                    throw new ErrMsgException(AuctionSkin.LoadString(request, "err_want_catalog")); 
                ad.setCatalogCode(catalogCode); 
            } 
 
            // 如果是拍卖 
            if (ad.getSellType() == ad.SELL_TYPE_AUCTION) { 
                String strbeginDate = fu.getFieldValue("beginDate"); 
                String strendDate = fu.getFieldValue("endDate"); 
                java.util.Date beginDate = null; 
                try { 
                    beginDate = DateUtil.parse(strbeginDate, 
                                   "yyyy-MM-dd"); 
                } 
                catch (Exception e) { 
                    logger.error("editTopic:" + e.getMessage()); 
                } 
                java.util.Date endDate = null; 
                try { 
                    endDate = DateUtil.parse(strendDate, 
                        "yyyy-MM-dd"); 
                } 
                catch (Exception e) { 
                    logger.error("editTopic:" + e.getMessage()); 
                } 
                if (beginDate == null || endDate == null) { 
                    throw new ErrMsgException(AuctionSkin.LoadString(request, 
                            "err_date_format")); 
                } 
 
                if (DateUtil.datediff(beginDate, 
                                      new java.util.Date(Calendar.getInstance(). 
                        getTimeInMillis())) < 0) 
                    throw new ErrMsgException(AuctionSkin.LoadString(request, 
                            "err_beginDate_early_than_curDate")); 
 
                if (DateUtil.compare(beginDate, endDate) == 1) 
                    throw new ErrMsgException(AuctionSkin.LoadString(request, 
                            "err_beginDate_late_than_endDate")); 
 
                AuctionConfig ac = new AuctionConfig(); 
                int max = ac.getIntProperty(ac.ExpireDayMax); 
                if (DateUtil.datediff(endDate, beginDate) > max) 
                    throw new ErrMsgException(AuctionSkin.LoadString(request, 
                            "err_expire_day_max")); 
 
                ad.setBeginDate(beginDate); 
                ad.setEndDate(endDate); 
            } 
 
            // 如果是一口价 
            boolean isShow = false; 
            if (ad.getSellType()==ad.SELL_TYPE_SELL) { 
                String sIsShow = fu.getFieldValue("isShow"); 
                // 仅供展示 
                if (sIsShow!=null && sIsShow.equals("true")) { 
                    ad.setShow(true); 
                    isShow = true; 
                } 
                else 
                    ad.setShow(false); 
            } 
 
            // 检查图片是否已被修改 
            boolean isImageChanged = false; 
            String image = StrUtil.getNullString(ad.getImage()); 
            if (!image.equals("")) { 
                // 原来有缩略图片,则从原来的缩略图片名称推断出文件名 
                String origFile = image.substring(0, image.length()-4) + "." + md.getFileName(); 
                if (!origFile.equals(md.getFileName())) { 
                    isImageChanged = true; 
                    // 删除原来的文件 
                    String realPath = application.getRealPath("/"); 
                    File imgFile = new File(realPath + "forum/" + ad.getImage()); 
                    imgFile.delete(); 
                } 
            } 
            if (isImageChanged) { 
                String ext = StrUtil.getNullStr(md.getExtName()); 
                if (ext.equals("gif") || ext.equals("jpg") || ext.equals("jpeg") || 
                    ext.equals("png")) { 
                    String fileName = md.getFileName(); 
                    ImageUtil iu = new ImageUtil(); 
                    String realPath = application.getRealPath("/"); 
                    File srcFile = new File(realPath + "forum/" + fileName); 
                    // 根据文件名 
                    image = FileUtil.getFileNameWithoutExt(fileName) + 
                                   "_s.jpg"; 
                    logger.info("fileName=" + fileName); 
                    logger.info(realPath + "forum/" + image); 
                    File desFile = new File(realPath + "forum/" + image); 
                    try { 
                        iu.Image2Thumb(srcFile, desFile, 80); 
                    } catch (IOException e) { 
                        logger.error("EditTopic:" + e.getMessage()); 
                    } 
                    ad.setImage(image); 
                } 
            } 
 
            // 保存修改 
            boolean re = ad.save(); 
 
            if (re) { 
                AuctionWorthDb aw = new AuctionWorthDb(); 
                Vector awv = aw.list(md.getId()); 
 
                aw = (AuctionWorthDb)awv.get(0); 
                // 如果是拍卖 
                if (ad.getSellType() == AuctionDb.SELL_TYPE_AUCTION) { 
                    String strPrice = fu.getFieldValue("price"); 
                    String strDlt = fu.getFieldValue("dlt"); 
                    String strReferPrice = fu.getFieldValue("referPrice"); 
                    double price = StrUtil.toDouble(strPrice); 
                    double dlt = StrUtil.toDouble(strDlt); 
                    double referPrice = StrUtil.toDouble(strReferPrice); 
 
                    aw.setPrice(price); 
                    aw.setDlt(dlt); 
                    aw.setReferPrice(referPrice); 
                    aw.setMoneyCode(moneyCode); 
                    re = aw.save(); 
                } 
 
                // 如果是一口价 
                if (ad.getSellType() == AuctionDb.SELL_TYPE_SELL) { 
                    Iterator awir = awv.iterator(); 
 
                    // 删除原来的AuctionWorthDb 
                    while (awir.hasNext()) { 
                        AuctionWorthDb awd = (AuctionWorthDb) awir.next(); 
                        awd.del(); 
                    } 
 
                    String[] moneyTypes = fu.getFieldValues("moneyType"); 
                    if (moneyTypes != null) { 
                        int len = moneyTypes.length; 
                        for (int i = 0; i < len; i++) { 
                            String strprice = fu.getFieldValue("price_" + 
                                    moneyTypes[i]); 
                            double price = 0.0; 
                            logger.info("moneyTypes[" + i + "]=" + moneyTypes[i] + " price=" + strprice); 
                            try { 
                                price = Double.parseDouble(strprice); 
                            } catch (Exception e) { 
                                logger.error("EditTopic: Double.parseDouble " + 
                                             e.getMessage()); 
                                throw new ErrMsgException(AuctionSkin. 
                                        LoadString(request, "err_price_format")); 
                            } 
                            aw.setMsgRootId(md.getId()); 
                            aw.setMoneyCode(moneyTypes[i]); 
                            aw.setPrice(price); 
                            try { 
                                aw.create(); 
                            } 
                            catch (ResKeyException e) { 
                                throw new ErrMsgException(e.getMessage(request)); 
                            } 
                        } 
                    } else { 
                        // 如果不是仅供展示,则报缺少币种错 
                        if (!isShow) { 
                            throw new ErrMsgException(AuctionSkin.LoadString( 
                                    request, "err_want_money")); 
                        } 
                    } 
                } 
            } 
        } 
        // refreshWorthOfAuction 
        return true; 
    } 
 
    public boolean AddNew(ServletContext application, 
                          HttpServletRequest request, MsgDb md, FileUpload fu) throws 
            ErrMsgException { 
        // logger.info("AddNew:msgRootId=" + md.getId()); 
        md = md.getMsgDb(md.getId()); 
 
        AuctionDb ad = new AuctionDb(); 
        ad.setmsgRootId(md.getId()); 
        ad.setState(ad.STATE_SELLING); 
        ad.setUserName(md.getName()); 
        String strUserType = fu.getFieldValue("userType"); 
        String strcount = fu.getFieldValue("count"); 
        int userType = 0; 
        if (StrUtil.isNumeric(strUserType)) 
            userType = Integer.parseInt(strUserType); 
        int count = 1; 
        if (StrUtil.isNumeric(strcount)) 
            count = Integer.parseInt(strcount); 
        else 
            throw new ErrMsgException(AuctionSkin.LoadString(request, 
                    "err_count_must_string")); 
        String name = StrUtil.getNullStr(fu.getFieldValue("name")); 
        if (name.equals("")) 
            throw new ErrMsgException(AuctionSkin.LoadString(request, 
                    "err_want_name")); 
 
        int sellType = AuctionDb.SELL_TYPE_AUCTION; 
        String strsellType = fu.getFieldValue("sellType"); 
        if (StrUtil.isNumeric(strsellType)) { 
            sellType = Integer.parseInt(strsellType); 
        } 
        String moneyCode = StrUtil.getNullStr(fu.getFieldValue("moneyCode")); 
 
        String shopDir = StrUtil.getNullStr(fu.getFieldValue("shopDir")); 
        if (shopDir.equals("")) { 
            shopDir = AuctionShopDirDb.DEFAULT; 
            String catalogCode = StrUtil.getNullStr(fu.getFieldValue("catalogCode")); 
            // 验证是商品目录是否合法 
            if (catalogCode.equals("") || catalogCode.equals(Leaf.TYPE_NONE_OPTION_VALUE)) { 
                throw new ErrMsgException(AuctionSkin.LoadString(request, "err_want_catalog")); 
            } 
            else 
                ad.setCatalogCode(catalogCode); 
        } 
        else { 
            // 根据shopDir得到catalogCode 
            AuctionShopDirDb asd = new AuctionShopDirDb(); 
            asd = asd.getAuctionShopDirDb(md.getName(), shopDir); 
            ad.setCatalogCode(asd.getCatalogCode()); 
        } 
        ad.setSellType(sellType); 
        ad.setCount(count); 
        // logger.info("name=" + name); 
        ad.setName(name); 
        ad.setUserType(userType); 
        ad.setShopDir(shopDir); 
 
        // 如果销售类型为拍卖 
        if (sellType == ad.SELL_TYPE_AUCTION) { 
            String strbeginDate = fu.getFieldValue("beginDate"); 
            String strendDate = fu.getFieldValue("endDate"); 
            java.util.Date beginDate = null; 
            try { 
                beginDate = DateUtil.parse(strbeginDate, 
                                           "yyyy-MM-dd"); 
            } catch (Exception e) { 
                logger.error("AddNew:" + e.getMessage()); 
            } 
            java.util.Date endDate = null; 
            try { 
                endDate = DateUtil.parse(strendDate, "yyyy-MM-dd"); 
            } catch (Exception e) { 
                logger.error("AddNew:" + e.getMessage()); 
            } 
 
            if (beginDate == null || endDate == null) { 
                throw new ErrMsgException(AuctionSkin.LoadString(request, 
                        "err_date_format")); 
            } 
 
            if (DateUtil.datediff(beginDate, 
                                  new java.util.Date(Calendar.getInstance(). 
                    getTimeInMillis())) < 0) 
                throw new ErrMsgException(AuctionSkin.LoadString(request, 
                        "err_beginDate_early_than_curDate")); 
 
            if (DateUtil.compare(beginDate, endDate) == 1) 
                throw new ErrMsgException(AuctionSkin.LoadString(request, 
                        "err_beginDate_late_than_endDate")); 
 
            AuctionConfig ac = new AuctionConfig(); 
            int max = ac.getIntProperty(ac.ExpireDayMax); 
            if (DateUtil.datediff(endDate, beginDate) > max) 
                throw new ErrMsgException(AuctionSkin.LoadString(request, 
                        "err_expire_day_max")); 
 
            ad.setBeginDate(beginDate); 
            ad.setEndDate(endDate); 
        } 
 
        boolean isShow = false; 
        // 如果是一口价 
        if (sellType == AuctionDb.SELL_TYPE_SELL) { 
            // 检查是否仅供展示 
            String sisShow = fu.getFieldValue("isShow"); 
            // logger.info("isShow=" + sisShow); 
            if (sisShow!=null && sisShow.equals("true")) { 
                isShow = true; 
                ad.setShow(true); 
            } 
        } 
        boolean re = false; 
 
        try { 
            AuctionWorthDb aw = new AuctionWorthDb(); 
            aw.setMsgRootId(md.getId()); 
            // 如果是拍卖 
            if (sellType == AuctionDb.SELL_TYPE_AUCTION) { 
                String strPrice = fu.getFieldValue("price"); 
                String strDlt = fu.getFieldValue("dlt"); 
                String strReferPrice = fu.getFieldValue("referPrice"); 
 
                try { 
                    double price = StrUtil.toDouble(strPrice); 
                    double dlt = StrUtil.toDouble(strDlt); 
                    double referPrice = StrUtil.toDouble(strReferPrice); 
 
                    aw.setPrice(price); 
                    aw.setDlt(dlt); 
                    aw.setReferPrice(referPrice); 
                    aw.setMoneyCode(moneyCode); 
                    re = aw.create(); 
                } 
                catch (ErrMsgException e) { 
                    throw new ErrMsgException(AuctionSkin.LoadString( 
                            request, "err_price_format")); 
                } 
            } 
 
            // 如果是一口价 
            if (sellType == AuctionDb.SELL_TYPE_SELL) { 
                String[] moneyTypes = fu.getFieldValues("moneyType"); 
                if (moneyTypes != null) { 
                    int len = moneyTypes.length; 
                    for (int i = 0; i < len; i++) { 
                        String strprice = fu.getFieldValue("price_" + 
                                moneyTypes[i]); 
                        double price = 0.0; 
                        // logger.info("moneyTypes[" + i + "]=" + moneyTypes[i] + " price=" + strprice); 
                        try { 
                            price = Double.parseDouble(strprice); 
                        } catch (Exception e) { 
                            re = false; 
                            logger.error("AddNew: Double.parseDouble " + 
                                         e.getMessage()); 
                            throw new ErrMsgException(AuctionSkin. 
                                    LoadString(request, "err_price_format")); 
                        } 
                        aw.setMoneyCode(moneyTypes[i]); 
                        aw.setPrice(price); 
                        re = aw.create(); 
                    } 
                } else { 
                    // 如果不是为了展示,则价格必须要填 
                    if (!isShow) { 
                        re = false; 
                        throw new ErrMsgException(AuctionSkin.LoadString( 
                                request, "err_want_money")); 
                    } 
                    else 
                        re = true; 
                } 
            } 
        } catch (ResKeyException e) { 
            re = false; 
            throw new ErrMsgException(e.getMessage(request)); 
        } 
 
        if (re) { 
            try { 
                re = ad.create(); 
                if (re) { 
                    // 生成缩略图 
                    ad = ad.getAuctionDb(md.getId()); 
                    String ext = StrUtil.getNullStr(md.getExtName()); 
                    if (ext.equals("gif") || ext.equals("jpg") || ext.equals("jpeg") || ext.equals("png")) { 
                        String fileName = md.getFileName(); 
                        ImageUtil iu = new ImageUtil(); 
                        String realPath = application.getRealPath("/"); 
                        File srcFile = new File(realPath + "forum/" + fileName); 
                        // 根据文件名 
                        String image = FileUtil.getFileNameWithoutExt(fileName) + "_s.jpg"; 
                        // logger.info(realPath + "forum/" + image); 
                        File desFile = new File(realPath + "forum/" + image); 
                        try { 
                            iu.Image2Thumb(srcFile, desFile, 80); 
                        } 
                        catch (IOException e) { 
                            logger.error("AddNew:" + e.getMessage()); 
                        } 
                        ad.setImage(image); 
                        ad.save(); 
                    } 
                } 
            } catch (ResKeyException e) { 
                throw new ErrMsgException(e.getMessage(request)); 
            } 
        } 
        return re; 
    } 
 
    public boolean AddQuickReply(ServletContext application, 
                                 HttpServletRequest request, long replyMsgId) throws 
            ErrMsgException { 
        return true; 
    } 
 
    /** 
     * 本方法置于MsgMgr中delTopic真正删除贴子之前,使在删除插件相应内容后,再删除贴子本身 
     * @param application ServletContext 
     * @param request HttpServletRequest 
     * @param md MsgDb 
     * @return boolean 
     * @throws ErrMsgException 
     */ 
    public boolean delTopic(ServletContext application, 
                            HttpServletRequest request, MsgDb md) throws 
            ErrMsgException { 
        // 如果是删除根贴 
        if (md.getReplyid() == -1) { 
            // 删除plugin_auction中对应的内容,所有plugin_auction_sq_message中对应于msgRootId的内容 
            // 及所有plugin_auction_user中对应的msgRootId中的内容 
            AuctionDb ad = new AuctionDb(); 
            ad = ad.getAuctionDb(md.getId()); 
            String image = StrUtil.getNullString(ad.getImage()); 
            ad.del(); 
            if (!image.equals("")) { 
                image = application.getRealPath("/") + "forum/" + image; 
                File f = new File(image); 
                f.delete(); 
            } 
        } 
        return true; 
    } 
 
    /** 
     * 此函数置于MsgDb delSingleMsg中真正删除贴子之前,以便于递归删除贴子 
     * @param delId int 
     * @return boolean 
     * @throws ErrMsgException 
     */ 
    public boolean delSingleMsg(long delId) throws 
            ErrMsgException { 
        //AuctionMsgDb sm = new AuctionMsgDb(); 
        //sm = sm.getAuctionMsgDb(delId); 
        //return sm.del(); 
        return true; 
    } 
 
    public boolean AddReply(ServletContext application, 
                            HttpServletRequest request, MsgDb md, FileUpload fu) throws 
            ErrMsgException { 
        String auctionAction; 
 
        auctionAction = StrUtil.getNullString(fu.getFieldValue("auctionAction")); 
        // logger.info("auctionAction=" + auctionAction); 
        if (auctionAction.equals(ACTION_APPLY)) { 
            // 登记该贴为申请贴 
 
        } else if (auctionAction.equals(this.ACTION_ACCEPT_APPLY)) { // 同意申请成为追求者 
 
            // 发送短消息至被回复者,它已被置为追求者 
        } 
        //else if (auctionAction.equals(this.ACTION_DECLINE_APPLY)) { 
 
        //} 
        else if (auctionAction.equals(ACTION_APPLY_MARRY)) { // 登记申请结婚 
            // 登记该贴为申请结婚贴 
 
        } else if (auctionAction.equals(this.ACTION_ACCEPT_MARRY)) { // 接受求婚 
 
            // 发送短消息至被回复者,它已被置为追求者 
        } 
        //else if (auctionAction.equals(this.ACTION_DECLINE_MARRY)) { 
 
        //} 
        else { 
            // 登记密级 
 
        } 
        return true; 
    } 
}