Coverage Report - org.trails.page.EditPage
 
Classes in this File Line Coverage Branch Coverage Complexity
EditPage
84% 
89% 
0
 
 1  
 /*
 2  
  * Copyright 2004 Chris Nelson
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 7  
  * Unless required by applicable law or agreed to in writing,
 8  
  * software distributed under the License is distributed on an "AS IS" BASIS,
 9  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 10  
  * See the License for the specific language governing permissions and limitations under the License.
 11  
  */
 12  
 package org.trails.page;
 13  
 
 14  
 import org.apache.tapestry.IExternalPage;
 15  
 import org.apache.tapestry.IRequestCycle;
 16  
 import org.apache.tapestry.annotations.Bean;
 17  
 import org.apache.tapestry.annotations.Lifecycle;
 18  
 import org.apache.tapestry.callback.ICallback;
 19  
 import org.trails.callback.AssociationCallback;
 20  
 import org.trails.callback.CollectionCallback;
 21  
 import org.trails.callback.EditCallback;
 22  
 import org.trails.persistence.PersistenceException;
 23  
 import org.trails.validation.TrailsValidationDelegate;
 24  
 
 25  
 /**
 26  
  * @author Chris Nelson
 27  
  *         <p/>
 28  
  *         This page will edit an instance contained in the model property
 29  
  */
 30  52
 public abstract class EditPage extends ModelPage implements IExternalPage
 31  
 {
 32  
         @Bean(lifecycle = Lifecycle.REQUEST)
 33  
         public abstract TrailsValidationDelegate getDelegate();
 34  
 
 35  
         public void activateExternalPage(Object[] parameters, IRequestCycle cycle)
 36  
         {
 37  1
                 setModel(parameters[0]);
 38  1
         }
 39  
 
 40  
         /**
 41  
          * This property allows components to change the page during the middle of
 42  
          * the rewind without causing a StaleLinkException
 43  
          *
 44  
          * @return
 45  
          */
 46  
         public abstract ICallback getNextPage();
 47  
 
 48  
         public abstract void setNextPage(ICallback NextPage);
 49  
 
 50  
         public void save(IRequestCycle cycle)
 51  
         {
 52  2
                 save();
 53  
 
 54  2
         }
 55  
 
 56  
         /*
 57  
                  * To avoid duplicate callbacks on the stack, we need to replace the top
 58  
                  * callback if it has an unsaved model object. (non-Javadoc)
 59  
                  *
 60  
                  * @see org.trails.page.TrailsPage#pushCallback()
 61  
                  */
 62  
         public void pushCallback()
 63  
         {
 64  32
                 getCallbackStack().push(
 65  16
                         new EditCallback(getPageName(), getModel(), isModelNew()));
 66  16
         }
 67  
 
 68  
         protected boolean save()
 69  
         {
 70  4
                 if (!getDelegate().getHasErrors())
 71  
                 {
 72  
                         // We get hibernate errors when the cascade tries to
 73  
                         // put the same object on the session twice
 74  
                         // if (!cameFromChildCollection())
 75  
                         // {
 76  
                         try
 77  
                         {
 78  4
                                 setModel(getPersistenceService().save(getModel()));
 79  1
                         } catch (PersistenceException pe)
 80  
                         {
 81  1
                                 getDelegate().record(pe);
 82  1
                                 return false;
 83  
                         }
 84  
                         // }
 85  3
                         return true;
 86  
                 }
 87  0
                 return false;
 88  
         }
 89  
 
 90  
         public void cancel(IRequestCycle cycle)
 91  
         {
 92  2
                 ICallback callback = (ICallback) getCallbackStack()
 93  1
                         .popPreviousCallback();
 94  1
                 callback.performCallback(cycle);
 95  1
         }
 96  
 
 97  
         /*
 98  
                  * This is here so that if a component needs to go to a new page it won't do
 99  
                  * so in the middle of a rewind and generate a StaleLink
 100  
                  */
 101  
         public void onFormSubmit(IRequestCycle cycle)
 102  
         {
 103  1
                 if (getNextPage() != null)
 104  
                 {
 105  1
                         getNextPage().performCallback(cycle);
 106  
                 }
 107  1
         }
 108  
 
 109  
         /**
 110  
          * @param cycle
 111  
          */
 112  
         public void saveAndReturn(IRequestCycle cycle)
 113  
         {
 114  2
                 if (save())
 115  
                 {
 116  2
                         ICallback callback = getCallbackStack().popPreviousCallback();
 117  2
                         if (callback instanceof CollectionCallback)
 118  
                         {
 119  2
                                 ((CollectionCallback) callback).save(getPersistenceService(),
 120  1
                                         getModel());
 121  1
                         } else if (callback instanceof AssociationCallback)
 122  
                         {
 123  0
                                 ((AssociationCallback) callback).save(getPersistenceService(),
 124  0
                                         getModel());
 125  
                         }
 126  
 
 127  2
                         callback.performCallback(cycle);
 128  
                 }
 129  2
         }
 130  
 
 131  
         public void remove(IRequestCycle cycle)
 132  
         {
 133  
 
 134  
                 try
 135  
                 {
 136  3
                         getPersistenceService().remove(getModel());
 137  1
                 } catch (PersistenceException pe)
 138  
                 {
 139  1
                         getDelegate().record(pe);
 140  1
                         return;
 141  
                 }
 142  
 
 143  2
                 ICallback callback = getCallbackStack().popPreviousCallback();
 144  2
                 if (callback instanceof CollectionCallback)
 145  
                 {
 146  2
                         ((CollectionCallback) callback).remove(getPersistenceService(),
 147  1
                                 getModel());
 148  1
                 } else if (callback instanceof AssociationCallback)
 149  
                 {
 150  0
                         ((AssociationCallback) callback).remove(getPersistenceService(),
 151  0
                                 getModel());
 152  
                 }
 153  2
                 callback.performCallback(cycle);
 154  2
         }
 155  
 
 156  
         /**
 157  
          * @return
 158  
          */
 159  
         public String getTitle()
 160  
         {
 161  2
                 Object[] params = new Object[]{getClassDescriptor().getDisplayName()};
 162  2
                 if (cameFromCollection() && isModelNew())
 163  
                 {
 164  2
                         return getResourceBundleMessageSource().getMessageWithDefaultValue(
 165  1
                                 "org.trails.i18n.add", params, getLocale(),
 166  1
                                 "[TRAILS][ORG.TRAILS.I18N.ADD]");
 167  
                 } else
 168  
                 {
 169  2
                         return getResourceBundleMessageSource().getMessageWithDefaultValue(
 170  1
                                 "org.trails.i18n.edit", params, getLocale(),
 171  1
                                 "[TRAILS][ORG.TRAILS.I18N.EDIT]");
 172  
                 }
 173  
         }
 174  
 
 175  
         public boolean cameFromCollection()
 176  
         {
 177  
 
 178  2
                 return getCallbackStack().getPreviousCallback() instanceof CollectionCallback;
 179  
         }
 180  
 
 181  
         public boolean cameFromChildCollection()
 182  
         {
 183  
 
 184  0
                 if (getCallbackStack().getPreviousCallback() instanceof CollectionCallback)
 185  
                 {
 186  0
                         return ((CollectionCallback) getCallbackStack()
 187  0
                                 .getPreviousCallback()).isChildRelationship();
 188  
                 } else
 189  0
                         return false;
 190  
         }
 191  
 }