1 43 package net.jforum.view.forum.common; 44 45 import java.util.List ; 46 import java.util.Random ; 47 48 import net.jforum.dao.BannerDAO; 49 import net.jforum.dao.DataAccessDriver; 50 import net.jforum.entities.Banner; 51 52 56 public class BannerCommon 57 { 58 private BannerDAO dao; 59 private List banners; 60 61 public BannerCommon() 62 { 63 this.dao = DataAccessDriver.getInstance().newBannerDAO(); 64 } 65 66 72 public boolean canBannerDisplay(int bannerId) throws Exception 73 { 74 boolean result = true; 75 76 78 return result; 79 } 80 81 87 public boolean isActiveBannerExist(int placement) throws Exception 88 { 89 banners = dao.selectActiveBannerByPlacement(placement); 90 if (banners == null || banners.isEmpty()) 91 { 92 return false; 93 } 94 95 return true; 96 } 97 98 112 public Banner getBanner() throws Exception 113 { 114 Banner result = null; 115 116 if (banners == null || banners.isEmpty()) 117 { 118 return null; 119 } 120 121 int r = (new Random ().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 result.setViews(result.getViews() + 1); 138 dao.update(result); 139 140 return result; 141 } 142 } 143 | Popular Tags |