Coverage Report - org.trails.callback.CollectionCallback
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionCallback
89% 
N/A 
1.333
 
 1  
 /*
 2  
  * Created on Feb 28, 2005
 3  
  *
 4  
  * Copyright 2004 Chris Nelson
 5  
  * 
 6  
  * Licensed under the Apache License, Version 2.0 (the "License"); 
 7  
  * you may not use this file except in compliance with the License. 
 8  
  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 9  
  * Unless required by applicable law or agreed to in writing, 
 10  
  * software distributed under the License is distributed on an "AS IS" BASIS, 
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 12  
  * See the License for the specific language governing permissions and limitations under the License.
 13  
  */
 14  
 package org.trails.callback;
 15  
 
 16  
 import java.util.HashMap;
 17  
 
 18  
 import ognl.Ognl;
 19  
 import ognl.OgnlException;
 20  
 import org.trails.TrailsRuntimeException;
 21  
 import org.trails.descriptor.CollectionDescriptor;
 22  
 import org.trails.persistence.PersistenceService;
 23  
 
 24  
 /**
 25  
  * This guy is responsible for returning from an add or remove on a
 26  
  * collection.
 27  
  */
 28  
 public class CollectionCallback extends EditCallback
 29  
 {
 30  
         private CollectionDescriptor collectionDescriptor;
 31  
 
 32  
         private boolean childRelationship;
 33  
 
 34  
         /**
 35  
          * @param pageName
 36  
          * @param model
 37  
          */
 38  
         public CollectionCallback(String pageName, Object model, CollectionDescriptor collectionDescriptor)
 39  
         {
 40  10
                 super(pageName, model);
 41  10
                 this.collectionDescriptor = collectionDescriptor;
 42  10
         }
 43  
 
 44  
         public void save(PersistenceService persistenceService, Object newObject)
 45  
         {
 46  4
                 executeOgnlExpression(collectionDescriptor.findAddExpression(), newObject);
 47  4
                 persistenceService.save(getModel());
 48  4
         }
 49  
 
 50  
         public void remove(PersistenceService persistenceService, Object object)
 51  
         {
 52  2
                 executeOgnlExpression(collectionDescriptor.findRemoveExpression(), object);
 53  2
                 persistenceService.save(getModel());
 54  2
         }
 55  
 
 56  
         /**
 57  
          * @param previousModel
 58  
          */
 59  
         private void executeOgnlExpression(String ognlExpression, Object newObject)
 60  
         {
 61  6
                 HashMap context = new HashMap();
 62  6
                 context.put("member", newObject);
 63  
 
 64  
                 try
 65  
                 {
 66  6
                         Ognl.getValue(ognlExpression + "(#member)", context, model);
 67  0
                 } catch (OgnlException e)
 68  
                 {
 69  0
                         throw new TrailsRuntimeException(e, model.getClass());
 70  
                 }
 71  6
         }
 72  
 
 73  
         public boolean isChildRelationship()
 74  
         {
 75  1
                 return childRelationship;
 76  
         }
 77  
 
 78  
 
 79  
         public void setChildRelationship(boolean child)
 80  
         {
 81  3
                 this.childRelationship = child;
 82  3
         }
 83  
 
 84  
 }