Java tutorial
/** * PureInfo TGirls * @(#)GetWelcomeMsg.java 1.0 2009-3-9 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.tgirls.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Random; import javax.servlet.ServletException; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.time.DateUtils; import org.apache.log4j.Logger; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.force.exception.PureException; import com.pureinfo.tfr.cache.core.CacheManager; import com.pureinfo.tfr.cache.core.IReadCache; import com.pureinfo.tfr.cache.core.IWriteCache; import com.pureinfo.tgirls.domain.IRandomEventMgr; import com.pureinfo.tgirls.model.Information; import com.pureinfo.tgirls.model.RandomEvent; import com.pureinfo.tgirls.model.User; import com.pureinfo.tgirls.model.helper.InformationConstants; import com.pureinfo.tgirls.model.helper.RandomEventConstants; import com.pureinfo.tgirls.model.helper.UserConstants; import com.pureinfo.tgirls.model.helper.UserMoneySaveEntry; import com.pureinfo.tgirls.utils.script.UserMoneySaveCache; public class NormalRandomEventMsg { private Logger logger = Logger.getLogger(NormalRandomEventMsg.class); private final String NORMAL_EVENT_KEY = "normal.event"; private IReadCache cache = CacheManager.getReadFromDBCache("random.event"); private static int todayTotalScale = 0; private static NormalRandomEventMsg instance = new NormalRandomEventMsg(); private IWriteCache informationWriteCache = CacheManager.getWrite2DBCache("informationSaveCache"); private NormalRandomEventMsg() { } public static NormalRandomEventMsg getInstance() { return instance; } public void createEvent(User loginUser) throws ServletException, IOException { if (loginUser == null) { return; } Calendar now = Calendar.getInstance(); int m = now.get(Calendar.MONTH); int d = now.get(Calendar.DAY_OF_MONTH); List<RandomEvent> todayEvents = (List<RandomEvent>) cache.get(NORMAL_EVENT_KEY + "." + m + "." + d); if (todayEvents == null) { todayEvents = new ArrayList<RandomEvent>(); logger.debug("today events is empty. reload."); List<RandomEvent> allNormalEvents = null; try { allNormalEvents = getNormalEvents(); // cache.put(WELCOME_EVENT_KEY, allWelcomeRandomEvents); } catch (PureException e) { logger.error("error when get normal events.", e); } if (allNormalEvents == null || allNormalEvents.isEmpty()) { logger.debug("all normal events is empty."); return; } List<RandomEvent> noDayEvents = new ArrayList<RandomEvent>();// Calendar c1 = Calendar.getInstance();// Calendar.getInstance(); Calendar today = Calendar.getInstance(); for (Iterator iterator = allNormalEvents.iterator(); iterator.hasNext();) { RandomEvent randomEvent = (RandomEvent) iterator.next(); String month_day = randomEvent.getEventMonthDay(); if (StringUtils.isNotEmpty(month_day)) { try { int i = month_day.indexOf("-"); int month = NumberUtils.toInt(month_day.substring(0, i), -1); int day = NumberUtils.toInt(month_day.substring(i + 1), -1); c1.clear(); c1 = Calendar.getInstance(); // logger.debug("the random event [" + randomEvent.getEventInfo() + "] month:" + month + " day:" // + day); if (month != -1) { c1.set(Calendar.MONTH, month - 1); } if (day != -1) { c1.set(Calendar.DAY_OF_MONTH, day); } if (DateUtils.isSameDay(c1, today)) { logger.debug("today event:" + randomEvent.getEventInfo()); todayEvents.add(randomEvent); } } catch (Exception e) { } } else { noDayEvents.add(randomEvent); } } if (todayEvents == null || todayEvents.isEmpty()) { if (noDayEvents.isEmpty()) { logger.debug("today events is empty, and noDay events is empty too."); return; } } // ////////////////////////////// // ////////////////////////////// todayEvents.addAll(noDayEvents); // ////////////////////// // ////////////////////// Collections.sort(todayEvents, new BeanComparator("eventScale")); todayTotalScale = 0; for (Iterator iterator = todayEvents.iterator(); iterator.hasNext();) { RandomEvent randomEvent2 = (RandomEvent) iterator.next(); todayTotalScale += randomEvent2.getEventScale(); } cache.put(NORMAL_EVENT_KEY + "." + m + "." + d, todayEvents); } int random = new Random().nextInt(todayTotalScale); RandomEvent todayEvent = null; int start = 0; int end = 0; for (int i = 0; i < todayEvents.size(); i++) { if (i == 0) { start = 0; end = todayEvents.get(i).getEventScale(); } else { start = end; end = end + todayEvents.get(i).getEventScale(); } if (random >= start && random < end) { todayEvent = todayEvents.get(i); break; } } if (todayEvent == null) { return; } try { logger.info("normal event[" + todayEvent.getEventInfo() + "]"); // loginUser.setFunds(loginUser.getFunds() + todayEvent.getEventMoney()); // loginUser.setAssets(loginUser.getAssets() + todayEvent.getEventMoney()); createInformation(loginUser, todayEvent); UserMoneySaveEntry userMoney = new UserMoneySaveEntry(); userMoney.setTaobaoId(loginUser.getTaobaoID()); userMoney.setFunds(todayEvent.getEventMoney()); userMoney.setAssets(todayEvent.getEventMoney()); UserMoneySaveCache.getInstance().add(userMoney); // UserInfoSaveCache.getInstance().add(loginUser); } catch (Exception e) { logger.error(e); return; } return; } private void createInformation(User _loginUser, RandomEvent _todayEvent) { try { logger.debug("to create information."); Information info = new Information(); info.setUserTaobaoId(_loginUser.getTaobaoID()); info.setFromUserName(UserConstants.SYSTEM_SUPER_ADMIN_NAME); info.setFromUserNickName(UserConstants.SYSTEM_SUPER_ADMIN_NAME); info.setFromUserTaobaoId("-1"); info.setInformation(_todayEvent.getEventInfo()); info.setType(InformationConstants.Type.RANDOM_EVENT); info.setCreateTime(); informationWriteCache.put(info); } catch (Exception e) { logger.error("error when create information.", e); } } private List<RandomEvent> getNormalEvents() throws PureException { IRandomEventMgr mgr = (IRandomEventMgr) ArkContentHelper.getContentMgrOf(RandomEvent.class); return mgr.getEventByType(RandomEventConstants.EventType.NORMAL_EVENT); } }