| 1 |
|
package org.trails.callback; |
| 2 |
|
|
| 3 |
|
import ognl.Ognl; |
| 4 |
|
import ognl.OgnlException; |
| 5 |
|
import org.trails.TrailsRuntimeException; |
| 6 |
|
import org.trails.descriptor.ObjectReferenceDescriptor; |
| 7 |
|
import org.trails.descriptor.OwningObjectReferenceDescriptor; |
| 8 |
|
import org.trails.persistence.PersistenceService; |
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
public class AssociationCallback extends EditCallback |
| 14 |
|
{ |
| 15 |
|
private ObjectReferenceDescriptor od; |
| 16 |
|
|
| 17 |
|
private boolean oneToOne; |
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
public AssociationCallback(String pageName, Object model, ObjectReferenceDescriptor od) |
| 24 |
|
{ |
| 25 |
0 |
super(pageName, model); |
| 26 |
0 |
this.od = od; |
| 27 |
0 |
this.setOneToOne(od.supportsExtension(OwningObjectReferenceDescriptor.class.getName())); |
| 28 |
0 |
} |
| 29 |
|
|
| 30 |
|
public void save(PersistenceService persistenceService, Object newObject) |
| 31 |
|
{ |
| 32 |
0 |
executeOgnlExpression(findAddExpression(), newObject); |
| 33 |
0 |
persistenceService.save(getModel()); |
| 34 |
0 |
} |
| 35 |
|
|
| 36 |
|
public void remove(PersistenceService persistenceService, Object object) |
| 37 |
|
{ |
| 38 |
|
try |
| 39 |
|
{ |
| 40 |
0 |
Ognl.setValue(findAddExpression(), model, null); |
| 41 |
0 |
Ognl.getValue(findAddExpression(), model); |
| 42 |
|
|
| 43 |
0 |
persistenceService.save(getModel()); |
| 44 |
0 |
persistenceService.remove(object); |
| 45 |
0 |
} catch (OgnlException e) |
| 46 |
|
{ |
| 47 |
0 |
throw new TrailsRuntimeException(e, getModel().getClass()); |
| 48 |
|
} |
| 49 |
0 |
} |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
private void executeOgnlExpression(String ognlExpression, Object newObject) |
| 55 |
|
{ |
| 56 |
|
try |
| 57 |
|
{ |
| 58 |
0 |
Ognl.setValue(ognlExpression, model, newObject); |
| 59 |
0 |
Ognl.getValue(ognlExpression, model); |
| 60 |
0 |
} catch (OgnlException e) |
| 61 |
|
{ |
| 62 |
0 |
throw new TrailsRuntimeException(e, model.getClass()); |
| 63 |
|
} |
| 64 |
0 |
} |
| 65 |
|
|
| 66 |
|
public boolean isOneToOne() |
| 67 |
|
{ |
| 68 |
0 |
return oneToOne; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
public void setOneToOne(boolean oneToOne) |
| 72 |
|
{ |
| 73 |
0 |
this.oneToOne = oneToOne; |
| 74 |
0 |
} |
| 75 |
|
|
| 76 |
|
private String findAddExpression() |
| 77 |
|
{ |
| 78 |
0 |
return od.getName(); |
| 79 |
|
} |
| 80 |
|
} |