1 package action; 2 3 import java.util.List ; 4 5 import javax.servlet.http.HttpServletRequest ; 6 import javax.servlet.http.HttpServletResponse ; 7 8 import org.apache.struts.action.Action; 9 import org.apache.struts.action.ActionForm; 10 import org.apache.struts.action.ActionForward; 11 import org.apache.struts.action.ActionMapping; 12 import org.objectstyle.cayenne.access.DataContext; 13 import org.objectstyle.cayenne.conf.ServletUtil; 14 import org.objectstyle.cayenne.exp.Expression; 15 import org.objectstyle.cayenne.exp.ExpressionFactory; 16 import org.objectstyle.cayenne.query.SelectQuery; 17 18 import webtest.Artist; 19 import webtest.Painting; 20 import formbean.PaintingForm; 21 22 public class SavePaintingAction extends Action { 23 24 public ActionForward execute( 25 ActionMapping mapping, 26 ActionForm form, 27 HttpServletRequest request, 28 HttpServletResponse response) 29 throws Exception { 30 31 PaintingForm paintingForm = (PaintingForm) form; 32 33 DataContext ctxt = 34 ServletUtil.getSessionContext(request.getSession()); 35 36 String anArtistName = paintingForm.getArtistName(); 37 38 Expression qual = ExpressionFactory.matchExp("artistName", anArtistName); 39 40 SelectQuery query = new SelectQuery(Artist.class, qual); 41 42 List artists = ctxt.performQuery(query); 43 System.err.println("artists: " + artists); 44 Artist artist = (Artist) artists.get(0); 45 46 Painting aPainting = (Painting) ctxt.createAndRegisterNewObject(Painting.class); 47 aPainting.setPaintingTitle(paintingForm.getPaintingTitle()); 48 aPainting.setEstimatedPrice(paintingForm.getEstimatedPrice()); 49 50 artist.addToPaintingArray(aPainting); 51 52 ctxt.commitChanges(); 54 55 return (mapping.findForward("success")); 56 } 57 } 58 | Popular Tags |