View Javadoc

1   /*
2    * Copyright 2004 Anite - Central Government Division
3    * http://www.anite.com/publicsector
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License. You may obtain a copy of
7    * the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  package com.anite.antelope.modules.actions.formSample;
19  
20  import net.sf.hibernate.Session;
21  import net.sf.hibernate.Transaction;
22  
23  import org.apache.turbine.modules.actions.VelocityAction;
24  import org.apache.turbine.util.RunData;
25  import org.apache.velocity.context.Context;
26  
27  import com.anite.antelope.modules.screens.formSample.AnimalScreen;
28  import com.anite.antelope.om.Animal;
29  import com.anite.meercat.PersistenceLocator;
30  import com.anite.penguin.form.Field;
31  import com.anite.penguin.modules.tools.FormTool;
32  
33  /***
34   * @author Ben
35   *  
36   */
37  public class UpdateAnimalAction extends VelocityAction {
38  
39      /***
40       * Save the updated animal fields
41       */
42      public void doPerform(RunData data, Context context) throws Exception {
43  
44          FormTool form = (FormTool) context.get(FormTool.DEFAULT_TOOL_NAME);
45  
46          if (form.isAllValid()) {
47              // Passed all validation
48              String age = ((Field) form.getFields().get(AnimalScreen.AGE))
49                      .getValue().toString();
50              Animal animal = (Animal) data.getSession().getAttribute(
51                      WhichAnimal.ANIMAL);
52              animal.setAge(Integer.parseInt(age));
53  
54              Session session = PersistenceLocator.getInstance()
55                      .getCurrentSession();
56              Transaction t = session.beginTransaction();
57              session.saveOrUpdate(animal);
58              t.commit();
59  
60              // Go to success URL
61              data.setScreenTemplate("formSample,TheMenagerie.vm");
62          } else {
63              // Failed some validation
64  
65              // Return to previous screen the previous screen must have set
66              // itself as setPage
67  
68          }
69      }
70  }