KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > module > survey > SurveyUtil


1 /*
2 -- GeiNuke --
3 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
4 http://www.hostingjava.it/-geinuke/
5
6 This file is part of GeiNuke.
7
8    GeiNuke is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GeiNuke is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GeiNuke; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */

22 package com.geinuke.module.survey;
23
24 import javax.servlet.http.Cookie JavaDoc;
25
26 import com.geinuke.common.UserI;
27 import com.geinuke.util.CookieHandler;
28 import com.geinuke.vo.SurveyAnswerVO;
29 import com.geinuke.vo.SurveyVO;
30
31
32 public class SurveyUtil {
33     
34     protected UserI user=null;
35     protected CookieHandler ch=null;
36     
37     public SurveyUtil(UserI user,CookieHandler ch){
38         this.user=user;
39         this.ch=ch;
40     }
41     
42     public static SurveyAnswerVO getSurveyAnswerByAID(SurveyVO su,int aid){
43         SurveyAnswerVO sa=null;
44         int t=-1;
45         boolean flag=false;
46         for(int i=0;i<su.getAnswers().size() && ! flag;i++){
47             sa=(SurveyAnswerVO)su.getAnswers().get(i);
48             if(sa.getAid()==aid)
49                 flag=true;
50         }
51         return sa;
52     }
53     
54     
55     
56     
57     public String JavaDoc getKey(SurveyVO su){
58         String JavaDoc k=null;
59         k="SURVEY"+su.getSid()+"-"+this.user.getId();
60         return k;
61     }
62     
63     public String JavaDoc getAnonymousKey(SurveyVO su){
64         String JavaDoc k=null;
65         k="SURVEY"+su.getSid()+"-"+UserI.ANONYMOUS_ID;
66         return k;
67     }
68     
69     public boolean hasVoted(SurveyVO su){
70         boolean flag=false;
71         if(this.ch.getReq().getSession(true).getAttribute(this.getAnonymousKey(su))!=null){
72             return true;
73         }
74         
75         Cookie JavaDoc co=ch.getCookie(this.getKey(su));
76         if(co!=null)
77             flag=true;
78         return flag;
79     }
80     
81     public void storeVote(SurveyVO su){
82         if(this.user.getId()==UserI.ANONYMOUS_ID){
83             this.ch.getReq().getSession(true).setAttribute(this.getKey(su),System.currentTimeMillis()+"");
84         }else{
85             this.ch.addCookie(this.getKey(su),System.currentTimeMillis()+"");
86         }
87     }
88     
89 }
Popular Tags