KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > common > BannerCommon


1 /*
2  * Copyright (c) 2003, Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on Apr 2, 2005 / 9:15:13 AM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum.common;
44
45 import java.util.List JavaDoc;
46 import java.util.Random JavaDoc;
47
48 import net.jforum.dao.BannerDAO;
49 import net.jforum.dao.DataAccessDriver;
50 import net.jforum.entities.Banner;
51
52 /**
53  * @author Samuel Yung
54  * @version $Id: BannerCommon.java,v 1.5 2005/07/26 04:01:19 diegopires Exp $
55  */

56 public class BannerCommon
57 {
58     private BannerDAO dao;
59     private List JavaDoc banners;
60
61     public BannerCommon()
62     {
63         this.dao = DataAccessDriver.getInstance().newBannerDAO();
64     }
65
66     /**
67      * Check whether the banner will be displayed based on user rights and
68      * banner filter settings.
69      * @return boolean
70      * @throws Exception
71      */

72     public boolean canBannerDisplay(int bannerId) throws Exception JavaDoc
73     {
74         boolean result = true;
75
76                 // todo
77

78         return result;
79     }
80
81     /**
82      * Test whether any active banner exist at the placement indicated.
83      * @param placement int
84      * @return boolean
85      * @throws Exception
86      */

87     public boolean isActiveBannerExist(int placement) throws Exception JavaDoc
88     {
89         banners = dao.selectActiveBannerByPlacement(placement);
90         if (banners == null || banners.isEmpty())
91         {
92             return false;
93         }
94         
95         return true;
96     }
97
98     /**
99      * Retrieves the correct banner based on weight. Before calling this
100      * function the isBannerExist(int placement) must be called. The total
101      * weight for all the same position banners should be equal to 99. If
102      * the total weight is smaller than 99 and the random number is larger
103      * than the total weight of all the same position banners, the highest
104      * weight's banner will be chosen. After a correct banner is found, its
105      * views variable will be incremented by 1.
106      *
107      * @param placement int
108      * @return Banner
109      * @throws Exception
110      * @see #isBannerExist(int)
111      */

112     public Banner getBanner() throws Exception JavaDoc
113     {
114         Banner result = null;
115
116         if (banners == null || banners.isEmpty())
117         {
118             return null;
119         }
120
121         // get correct banner based on weight
122
int r = (new Random JavaDoc().nextInt(99));
123         int weightFrom = 0;
124         int weightTo = 0;
125         for(int i = 0; i < banners.size(); i++)
126         {
127             result = (Banner)banners.get(i);
128             weightTo += result.getWeight();
129             if (r >= weightFrom && r < weightTo)
130             {
131                 break;
132             }
133             weightFrom = weightTo;
134         }
135
136         // increment views by 1
137
result.setViews(result.getViews() + 1);
138         dao.update(result);
139
140         return result;
141     }
142 }
143
Popular Tags