001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Apr 2, 2005 / 9:15:13 AM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum.common;
044:
045: import java.util.List;
046: import java.util.Random;
047:
048: import net.jforum.dao.BannerDAO;
049: import net.jforum.dao.DataAccessDriver;
050: import net.jforum.entities.Banner;
051:
052: /**
053: * @author Samuel Yung
054: * @version $Id: BannerCommon.java,v 1.9 2007/08/01 22:30:05 rafaelsteil Exp $
055: */
056: public class BannerCommon {
057: private BannerDAO dao;
058: private List banners;
059:
060: public BannerCommon() {
061: this .dao = DataAccessDriver.getInstance().newBannerDAO();
062: }
063:
064: /**
065: * Check whether the banner will be displayed based on user rights and
066: * banner filter settings.
067: * @return boolean
068: * @param bannerId int
069: */
070: public boolean canBannerDisplay(int bannerId) {
071: return true;
072: }
073:
074: /**
075: * Test whether any active banner exist at the placement indicated.
076: * @param placement int
077: * @return boolean
078: */
079: public boolean isActiveBannerExist(int placement) {
080: banners = dao.selectActiveBannerByPlacement(placement);
081: if (banners == null || banners.isEmpty()) {
082: return false;
083: }
084:
085: return true;
086: }
087:
088: /**
089: * Retrieves the correct banner based on weight. Before calling this
090: * function the isBannerExist(int placement) must be called. The total
091: * weight for all the same position banners should be equal to 99. If
092: * the total weight is smaller than 99 and the random number is larger
093: * than the total weight of all the same position banners, the highest
094: * weight's banner will be chosen. After a correct banner is found, its
095: * views variable will be incremented by 1.
096: *
097: * @return Banner
098: */
099: public Banner getBanner() {
100: Banner result = null;
101:
102: if (banners == null || banners.isEmpty()) {
103: return null;
104: }
105:
106: // get correct banner based on weight
107: int r = (new Random().nextInt(99));
108: int weightFrom = 0;
109: int weightTo = 0;
110: for (int i = 0; i < banners.size(); i++) {
111: result = (Banner) banners.get(i);
112: weightTo += result.getWeight();
113: if (r >= weightFrom && r < weightTo) {
114: break;
115: }
116: weightFrom = weightTo;
117: }
118:
119: // increment views by 1
120: result.setViews(result.getViews() + 1);
121: dao.update(result);
122:
123: return result;
124: }
125: }
|