001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.survey.ejb;
034:
035: import org.libresource.LibresourceResourceBase;
036:
037: import org.libresource.survey.Option;
038: import org.libresource.survey.util.SurveyResourceUtil;
039:
040: import java.util.Date;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.Vector;
044:
045: import javax.ejb.CreateException;
046:
047: /**
048: * A libresource Survey
049: *
050: * @libresource.resource name="Survey" service="LibresourceSurvey"
051: */
052: public abstract class SurveyResourceBean extends
053: LibresourceResourceBase {
054: /**
055: * @ejb.create-method
056: */
057: public String ejbCreate(String question, String votersList,
058: String state, String displayMode, Date creationDate,
059: Date lastVoteDate) throws CreateException {
060: setId(SurveyResourceUtil.generateGUID(this ));
061: setQuestion(question);
062: setState(state);
063: setCreationDate(creationDate);
064: setLastVoteDate(lastVoteDate);
065: setDisplayMode(displayMode);
066: setVotersList(votersList);
067: setOptions(new HashMap());
068:
069: return null;
070: }
071:
072: // persistents fields
073:
074: /**
075: * @ejb.persistent-field
076: * @ejb.interface-method
077: * @ejb.value-object match="libresource"
078: * @ejb.transaction type="Supports"
079: */
080: public abstract String getId();
081:
082: /**
083: * @ejb.persistent-field
084: * @ejb.interface-method
085: * @ejb.transaction type="Mandatory"
086: */
087: public abstract void setId(String id);
088:
089: /**
090: * @ejb.interface-method
091: * @ejb.value-object match="libresource"
092: * @ejb.transaction type="Supports"
093: */
094: public String getShortResourceName() {
095: return getQuestion();
096: }
097:
098: /**
099: * @ejb.persistent-field
100: * @ejb.interface-method
101: * @ejb.value-object match="libresource"
102: * @ejb.transaction type="Supports"
103: */
104: public abstract String getQuestion();
105:
106: /**
107: * @ejb.persistent-field
108: * @ejb.interface-method
109: * @ejb.transaction type="Mandatory"
110: */
111: public abstract void setQuestion(String question);
112:
113: /**
114: * @ejb.persistent-field
115: * @ejb.interface-method
116: * @ejb.value-object match="libresource"
117: * @ejb.transaction type="Supports"
118: */
119: public abstract String getState();
120:
121: /**
122: * @ejb.persistent-field
123: * @ejb.interface-method
124: * @ejb.transaction type="Mandatory"
125: */
126: public abstract void setState(String state);
127:
128: /**
129: * @ejb.persistent-field
130: * @ejb.interface-method
131: * @ejb.value-object match="libresource"
132: * @ejb.transaction type="Supports"
133: */
134: public abstract Date getCreationDate();
135:
136: /**
137: * @ejb.persistent-field
138: * @ejb.interface-method
139: * @ejb.transaction type="Mandatory"
140: */
141: public abstract void setCreationDate(Date creationDate);
142:
143: /**
144: * @ejb.persistent-field
145: * @ejb.interface-method
146: * @ejb.value-object match="libresource"
147: * @ejb.transaction type="Supports"
148: */
149: public abstract Date getLastVoteDate();
150:
151: /**
152: * @ejb.persistent-field
153: * @ejb.interface-method
154: * @ejb.transaction type="Mandatory"
155: */
156: public abstract void setLastVoteDate(Date lastVoteDate);
157:
158: /**
159: * @ejb.persistent-field
160: * @ejb.interface-method
161: * @ejb.value-object match="libresource"
162: * @ejb.transaction type="Supports"
163: */
164: public abstract String getVotersList();
165:
166: /**
167: * @ejb.persistent-field
168: * @ejb.interface-method
169: * @ejb.transaction type="Mandatory"
170: */
171: public abstract void setVotersList(String voters);
172:
173: /**
174: * @ejb.persistent-field
175: * @ejb.interface-method
176: * @ejb.value-object match="libresource"
177: * @ejb.transaction type="Supports"
178: */
179: public abstract String getDisplayMode();
180:
181: /**
182: * @ejb.persistent-field
183: * @ejb.interface-method
184: * @ejb.transaction type="Mandatory"
185: */
186: public abstract void setDisplayMode(String displayMode);
187:
188: /**
189: * @ejb.persistent-field
190: * @ejb.interface-method
191: * @ejb.value-object match="libresource"
192: * @ejb.transaction type="Supports"
193: */
194: public abstract HashMap getOptions();
195:
196: /**
197: * @ejb.persistent-field
198: * @ejb.interface-method
199: * @ejb.transaction type="Mandatory"
200: */
201: public abstract void setOptions(HashMap options);
202:
203: // business methods
204:
205: /**
206: * @ejb.interface-method
207: * @ejb.value-object match="libresource"
208: * @ejb.transaction type="Supports"
209: */
210: public Vector getOptionsList() throws Exception {
211: HashMap options = getOptions();
212: Vector result = new Vector();
213:
214: for (Iterator i = options.keySet().iterator(); i.hasNext();) {
215: String id = (String) i.next();
216: result.add(options.get(id));
217: }
218:
219: return result;
220: }
221:
222: // voters management
223:
224: /**
225: * @ejb.interface-method
226: * @ejb.value-object match="libresource"
227: * @ejb.transaction type="Supports"
228: */
229: public String[] getVoters() throws Exception {
230: if ((getVotersList() == null)
231: || getVotersList().trim().equals("")) {
232: return new String[0];
233: }
234:
235: return getVotersList().split(",");
236: }
237:
238: /**
239: * @ejb.interface-method
240: * @ejb.transaction type="Mandatory"
241: */
242: public void setVoters(String[] voters) throws Exception {
243: StringBuffer buffer = new StringBuffer();
244:
245: for (int i = 0; i < voters.length; i++) {
246: buffer.append(voters[i]);
247:
248: if (i != (voters.length - 1)) {
249: buffer.append(",");
250: }
251: }
252:
253: setVotersList(buffer.toString());
254: }
255:
256: /**
257: * @ejb.interface-method
258: * @ejb.transaction type="Mandatory"
259: */
260: public void addVoter(String voter) throws Exception {
261: if (!isVoter(voter)) {
262: String[] voters = getVoters();
263: String[] newVoters = new String[voters.length + 1];
264: System.arraycopy(voters, 0, newVoters, 0, voters.length);
265: newVoters[voters.length] = voter;
266: setVoters(newVoters);
267: }
268: }
269:
270: /**
271: * @ejb.interface-method
272: * @ejb.transaction type="Supports"
273: */
274: public boolean isVoter(String voter) throws Exception {
275: String[] voters = getVoters();
276:
277: for (int i = 0; i < voters.length; i++) {
278: if (voters[i].equals(voter)) {
279: return true;
280: }
281: }
282:
283: return false;
284: }
285:
286: // options management
287:
288: /**
289: * @ejb.interface-method
290: * @ejb.transaction type="Mandatory"
291: */
292: public void addOption(String choice) throws Exception {
293: addOption(choice, 0);
294: }
295:
296: /**
297: * @ejb.interface-method
298: * @ejb.transaction type="Mandatory"
299: */
300: public void addOption(String choice, int nbVotes) throws Exception {
301: String id = SurveyResourceUtil.generateGUID(choice);
302: Option option = new Option(id, choice, nbVotes);
303: HashMap options = getOptions();
304:
305: if (options == null) {
306: options = new HashMap();
307: }
308:
309: options.put(id, option);
310: setOptions(options);
311: }
312:
313: /**
314: * @ejb.interface-method
315: * @ejb.transaction type="Mandatory"
316: */
317: public void editOption(String id, String choice) throws Exception {
318: HashMap options = getOptions();
319:
320: if (options == null) {
321: options = new HashMap();
322: }
323:
324: Option option = (Option) options.get(id);
325: option.setChoice(choice);
326: options.put(id, option);
327: setOptions(options);
328: }
329:
330: /**
331: * @ejb.interface-method
332: * @ejb.transaction type="Mandatory"
333: */
334: public void incNbVote(String optionId) throws Exception {
335: HashMap options = getOptions();
336:
337: if (options == null) {
338: options = new HashMap();
339: }
340:
341: Option option = (Option) options.get(optionId);
342: option.incNbVote();
343: options.put(optionId, option);
344: setOptions(options);
345: }
346:
347: /**
348: * @ejb.interface-method
349: * @ejb.value-object match="libresource"
350: * @ejb.transaction type="Supports"
351: */
352: public Option getOption(String optionId) throws Exception {
353: HashMap options = getOptions();
354:
355: if (options != null) {
356: return (Option) options.get(optionId);
357: }
358:
359: return null;
360: }
361:
362: /**
363: * @ejb.interface-method
364: * @ejb.transaction type="Mandatory"
365: */
366: public void deleteOption(String optionId) throws Exception {
367: HashMap options = getOptions();
368:
369: if (options != null) {
370: options.remove(optionId);
371: setOptions(options);
372: }
373: }
374: }
|