KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > action > SaveArtistAction


1 package action;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5
6 import org.apache.struts.action.Action;
7 import org.apache.struts.action.ActionErrors;
8 import org.apache.struts.action.ActionForm;
9 import org.apache.struts.action.ActionForward;
10 import org.apache.struts.action.ActionMapping;
11 import org.objectstyle.cayenne.access.DataContext;
12 import org.objectstyle.cayenne.conf.ServletUtil;
13
14 import webtest.Artist;
15 import formbean.ArtistForm;
16
17 public class SaveArtistAction extends Action {
18
19     public ActionForward execute(
20         ActionMapping mapping,
21         ActionForm form,
22         HttpServletRequest JavaDoc request,
23         HttpServletResponse JavaDoc response)
24         throws Exception JavaDoc {
25
26         ArtistForm artistForm = (ArtistForm) form;
27
28         // Validate the user form information
29
ActionErrors errors = new ActionErrors();
30         errors = artistForm.validate(mapping, request);
31
32         // Report any errors we have discovered back to the original form
33
if (!errors.isEmpty()) {
34             saveErrors(request, errors);
35             saveToken(request);
36             return (new ActionForward(mapping.getInput()));
37         }
38
39         DataContext ctxt =
40             ServletUtil.getSessionContext(request.getSession());
41
42         Artist anArtist = (Artist) ctxt.createAndRegisterNewObject(Artist.class);
43         anArtist.setArtistName(artistForm.getArtistName());
44         anArtist.setDateOfBirth(new java.sql.Date JavaDoc(System.currentTimeMillis()));
45
46         // commit to the database
47
ctxt.commitChanges();
48
49         return (mapping.findForward("success"));
50     }
51 }
52
Popular Tags