Hi Warner,

I have approached it from a similar angle.  It is based on the assumption
that if the records primarykey id present in the form then it will be
updated otherwise it should be inserted.

The previous action sets up the $SearchRequest in the context.  (You could
pass id as a parameter instead)

vm template : 

## Set up the Intake tool and the get the searchrequest from the server.
#if($SearchRequest.Searchrequestid)#set($requestGroup =
$intake.SearchRequest.mapTo($SearchRequest))
#else #set($requestGroup = $intake.SearchRequest.Default)#end

  #set ( $action =
$link.setPage("SearchRequest.vm").setAction("ProcessSearch").addPathInfo("ne
xtTemplate", "BarcosIndex.vm") )

<form method="post" action="$action">

#if( $SearchRequest.Searchrequestid )
<input type="hidden" name="$requestGroup.Searchrequestid.Key"
value="$SearchRequest.Searchrequestid">
#end

Enter additional keywords to search on here.
<textarea name="$requestGroup.Keywords.Key"
rows="5">$!requestGroup.Keywords</textarea></font>

<input type="submit" name="eventSubmit_doUpdatesearchrequest"
value="$local.getString("CONTINUE")">

$intake.declareGroups()

</form>


Then the action does as follows

    public void doUpdatesearchrequest(RunData data, TemplateContext context)
throws Exception
    {

        String template = data.getParameters().getString(
Constants.TEMPLATE, null);
        String nextTemplate = data.getParameters().getString(
Constants.NEXT_TEMPLATE, template );

        IntakeTool intake = (IntakeTool)context.get(Constants.INTAKE_TOOL);

        Searchrequest srchreq = null;

        //Get the group from intake (default or primary key referenced
record)
        String requestIndex = data.getParameters().getString(
"searchrequest", null );
        Group requestgroup = intake.get("SearchRequest", requestIndex );

        // if all the rules checked then save
        if ( intake.isAllValid() )
        {

            NumberKey searchrequestid = (NumberKey)
requestgroup.get("Searchrequestid").getValue();

            try
            {

                try
                {
                    // if searchrequest exists retrieve it from the db
                    if( searchrequestid != null )
                        srchreq = SearchrequestPeer.retrieveByPK(
searchrequestid );
                    else
                        srchreq = new Searchrequest();

                }
                // Assume it failed to find the record, possibly deleted
while it wasn't looking
                catch ( Exception e )
                {
                    srchreq = new Searchrequest();
                }

                // Set the properties in the OM to those in intake
                requestgroup.setProperties( srchreq );

                // Update or Inserts the object.  Torque will know if the
record isNew()
                srchreq.save();

                // Clean up this group from the intake tool.
                intake.remove( requestgroup );    

                data.setMessage( local.getString( "SEARCHREQUEST_SAVED" ) );
                setTarget(data, nextTemplate);

            }
            catch ( Exception e )
            {
                log.error( "Failed to save searchrequestid ", e );
                data.setMessage( local.getString( "SEARCHREQUEST_SAVEFAILED"
) + " " + e.getMessage() );

                context.put( "SearchRequest", srchreq );

            }

        }

    }


The important thing to note is the 

        String requestIndex = data.getParameters().getString(
"searchrequest", null );
        Group requestgroup = intake.get("SearchRequest", requestIndex );

"searchrequest" in statement one is the intake group's Key 
and "SearchRequest" in statement two is the intake group's Name 

Hope this helps, if you see anything tragic about the above code I would
welcome comments.

Dave

> -----Original Message-----
> From: Warner Onstine [mailto:[EMAIL PROTECTED]]
> Sent: 09 July 2001 17:20
> To: [EMAIL PROTECTED]
> Subject: Re: another intake question
> 
> 
> 
> ----- Original Message -----
> From: "John McNally" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, July 09, 2001 8:54 AM
> Subject: Re: another intake question
> 
> 
> >
> >
> > Warner Onstine wrote:
> > >
> > > Hi all,
> > > Just wondering if there is a better way to do this:
> > >
> > > Currently I have one form and whether an id is passed or 
> not it either
> > > reflects a record in the database or a brand new record. 
> There are also
> two
> > > possible button presses doUpdate and doInsert - only one 
> shows depending
> on
> > > the id value.
> > >
> > > What currently happens is that I grab the group from the 
> intake tool
> via:
> > > get("Project", IntakeTool.DEFAULT_KEY, false) for the 
> insert action, and
> > >
> >
> > This looks fine.
> >
> >
> > > get("Project", queryKey, false) for the update action
> > >
> > > Now queryKey I have to pull manually from the data 
> submitted from the
> form
> > > via
> > > String queryKey = data.getParameters().get("proj"); - 
> where proj is the
> > > Intake value that is pre-defined on ViewProject.vm page.
> > >
> > > So, is there a better way to do this?
> >
> > Since you are updating, I assume there is a torque object 
> that is being
> > updated.  In this case I would
> >
> > in template
> > #set ($project = torque object)
> > #set ($group = $intake.Project.mapTo($project))
> >
> > then in the action you can do
> >
> > Project project = torque object
> > Group group = intake.get("Project", project.getQueryKey(), false);
> > ... validate etc ...
> > group.setProperties(project);
> > project.save();
> 
> I tried this initially, but project.queryKey() wasn't 
> returning anything
> because the project object hasn't been initialized. The only 
> way I can see
> to initialize it would be to grab a variable from the form which was
> submitted.
> 
> Unless I'm missing something.
> 
> -warner
> 
> > john mcnally
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: 
> [EMAIL PROTECTED]
> >
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to