001: /*
002: * SurveyModel.java
003: *
004: * Created on May 17, 2005, 2:26 PM
005: */
006:
007: package com.sun.portal.app.collab.survey.model;
008:
009: import java.beans.*;
010: import java.io.Serializable;
011: import java.util.Calendar;
012: import java.util.Date;
013: import java.util.ArrayList;
014:
015: /**
016: * @author jerry
017: */
018: public class SurveyModel extends Object implements Serializable {
019:
020: private PropertyChangeSupport propertySupport;
021:
022: public SurveyModel() {
023: propertySupport = new PropertyChangeSupport(this );
024: }
025:
026: public void addPropertyChangeListener(
027: PropertyChangeListener listener) {
028: propertySupport.addPropertyChangeListener(listener);
029: }
030:
031: public void removePropertyChangeListener(
032: PropertyChangeListener listener) {
033: propertySupport.removePropertyChangeListener(listener);
034: }
035:
036: /**
037: * Holds value of property surveyId.
038: */
039: private int surveyId;
040:
041: /**
042: * Getter for property surveyId.
043: * @return Value of property surveyId.
044: */
045: public int getSurveyId() {
046:
047: return this .surveyId;
048: }
049:
050: /**
051: * Setter for property surveyId.
052: * @param surveyId New value of property surveyId.
053: */
054: public void setSurveyId(int surveyId) {
055:
056: this .surveyId = surveyId;
057: }
058:
059: /**
060: * Holds value of property surveyName.
061: */
062: private String surveyName;
063:
064: /**
065: * Getter for property surveyName.
066: * @return Value of property surveyName.
067: */
068: public String getSurveyName() {
069:
070: return this .surveyName;
071: }
072:
073: /**
074: * Setter for property surveyName.
075: * @param surveyName New value of property surveyName.
076: */
077: public void setSurveyName(String surveyName) {
078:
079: this .surveyName = surveyName;
080: }
081:
082: /**
083: * Holds value of property surveyDescription.
084: */
085: private String surveyDescription;
086:
087: /**
088: * Getter for property surveyDescription.
089: * @return Value of property surveyDescription.
090: */
091: public String getSurveyDescription() {
092:
093: return this .surveyDescription;
094: }
095:
096: /**
097: * Setter for property surveyDescription.
098: * @param surveyDescription New value of property surveyDescription.
099: */
100: public void setSurveyDescription(String surveyDescription) {
101:
102: this .surveyDescription = surveyDescription;
103: }
104:
105: /**
106: * Holds value of property communityId.
107: */
108: private String communityId;
109:
110: /**
111: * Getter for property communityId.
112: * @return Value of property communityId.
113: */
114: public String getCommunityId() {
115:
116: return this .communityId;
117: }
118:
119: /**
120: * Setter for property communityId.
121: * @param communityId New value of property communityId.
122: */
123: public void setCommunityId(String communityId) {
124:
125: this .communityId = communityId;
126: }
127:
128: /**
129: * Holds value of property startDate.
130: */
131: private Date startDate;
132:
133: /**
134: * Getter for property startDate.
135: * @return Value of property startDate.
136: */
137: public Date getStartDate() {
138:
139: return this .startDate;
140: }
141:
142: /**
143: * Setter for property startDate.
144: * @param startDate New value of property startDate.
145: */
146: public void setStartDate(Date startDate) {
147: // taking argument date from enduser
148: // storing it as time 00:00:00 of current day
149: Calendar cal = Calendar.getInstance();
150: cal.setLenient(true);
151: cal.setTime(startDate);
152: cal.set(Calendar.HOUR, 0);
153: cal.set(Calendar.MINUTE, 0);
154: cal.set(Calendar.SECOND, 0);
155: cal.set(Calendar.MILLISECOND, 0);
156: this .startDate = cal.getTime();
157: }
158:
159: /**
160: * Holds value of property endDate.
161: */
162: private Date endDate;
163:
164: /**
165: * Getter for property endDate.
166: * @return Value of property endDate.
167: */
168: public Date getEndDate() {
169: Calendar cal = Calendar.getInstance();
170: cal.setLenient(true);
171: cal.setTime(this .endDate);
172: cal.set(Calendar.HOUR, 0);
173: cal.set(Calendar.MINUTE, 0);
174: cal.set(Calendar.SECOND, 0);
175: cal.set(Calendar.MILLISECOND, 0);
176: this .endDate = cal.getTime();
177: return this .endDate;
178: }
179:
180: /**
181: * Setter for property endDate.
182: * @param endDate New value of property endDate.
183: */
184: public void setEndDate(Date endDate) {
185: // taking argument date from enduser
186: // storing it as time 00:00:00 of current date
187: // in surveyDB we'll add 1 day to be 00:00:00 on the next day
188: Calendar cal = Calendar.getInstance();
189: cal.setLenient(true);
190: cal.setTime(endDate);
191: cal.set(Calendar.HOUR, 0);
192: cal.set(Calendar.MINUTE, 0);
193: cal.set(Calendar.SECOND, 0);
194: cal.set(Calendar.MILLISECOND, 0);
195: this .endDate = cal.getTime();
196: }
197:
198: /**
199: * Holds value of property surveyOwner.
200: */
201: private String surveyOwner;
202:
203: /**
204: * Getter for property surveyOwner.
205: * @return Value of property surveyOwner.
206: */
207: public String getSurveyOwner() {
208:
209: return this .surveyOwner;
210: }
211:
212: /**
213: * Setter for property surveyOwner.
214: * @param surveyOwner New value of property surveyOwner.
215: */
216: public void setSurveyOwner(String surveyOwner) {
217:
218: this .surveyOwner = surveyOwner;
219: }
220:
221: /**
222: * Holds value of property poll.
223: */
224: private boolean poll;
225:
226: /**
227: * Getter for property poll.
228: * @return Value of property poll.
229: */
230: public boolean isPoll() {
231:
232: return this .poll;
233: }
234:
235: /**
236: * Setter for property poll.
237: * @param poll New value of property poll.
238: */
239: public void setPoll(boolean poll) {
240:
241: this .poll = poll;
242: }
243:
244: /**
245: * Holds value of property questions.
246: */
247: private QuestionModel[] questions;
248:
249: /**
250: * Getter for property questions.
251: * @return Value of property questions.
252: */
253: public QuestionModel[] getQuestions() throws SurveyException {
254: if (questions == null) {
255: // Fetch questions collection from database
256: SurveyDB db = new SurveyDB();
257: ArrayList l = db.getQuestions(surveyId);
258: if (l != null) {
259: questions = (QuestionModel[]) l
260: .toArray(new QuestionModel[1]);
261: if (questions.length == 1 && questions[0] == null) {
262: questions = null;
263: }
264: }
265: }
266: return this .questions;
267: }
268:
269: /**
270: * Setter for property questions.
271: * @param questions New value of property questions.
272: */
273: public void setQuestions(QuestionModel[] questions) {
274:
275: this .questions = questions;
276: }
277:
278: public String toString() {
279: StringBuffer str = new StringBuffer();
280: str.append("SurveyModel class:");
281: str.append("SurveyID=" + surveyId);
282: str.append(",SurveyName=" + surveyName);
283: str.append(",SurveyOwner=" + surveyOwner);
284: str.append(",startDate=" + startDate);
285: str.append(",endDate=" + endDate);
286: return str.toString();
287:
288: }
289:
290: public int getResponseCount() throws SurveyException {
291: SurveyDB db = new SurveyDB();
292: int count = db.getSurveyResponseCount(surveyId);
293: return count;
294: }
295:
296: public boolean isOpen() {
297: Date today = new Date();
298: if ((startDate.compareTo(today) <= 0)
299: && (endDate.compareTo(today) > 0)) {
300: return true;
301: } else {
302: return false;
303: }
304: }
305:
306: public void addResponse(String userId, int questionId, int answerId)
307: throws SurveyException {
308: SurveyDB db = new SurveyDB();
309: db.addResponse(surveyId, userId, questionId, answerId);
310:
311: }
312:
313: public void addResponse(String userId, int questionId,
314: String open_answer) throws SurveyException {
315: SurveyDB db = new SurveyDB();
316: db.addResponse(surveyId, userId, questionId, open_answer);
317:
318: }
319:
320: public boolean hasResponded(String userId) throws SurveyException {
321: SurveyDB db = new SurveyDB();
322: return db.hasResponded(surveyId, userId);
323: }
324:
325: }
|