1 package com.fc.taglibs.castor;
2
3 import java.io.*;
4 import java.util.ArrayList;
5 import javax.naming.*;
6 import javax.servlet.jsp.*;
7 import javax.servlet.jsp.tagext.*;
8
9 import org.exolab.castor.jdo.DataObjects;
10 import org.exolab.castor.jdo.Database;
11 import org.exolab.castor.jdo.ClassNotPersistenceCapableException;
12 import org.exolab.castor.jdo.TransactionNotInProgressException;
13 import org.exolab.castor.jdo.PersistenceException;
14
15 public class CastorCreateTag extends BodyTagSupport
16 {
17
18 /***
19 * The name of this tag
20 */
21 private static String _tagName = "CastorUpdateTag";
22
23 /***
24 * The object to create
25 */
26 private Object data;
27
28 /***
29 * The database to perform the update
30 */
31 private Database db = null;
32
33 /***
34 * The JDO object
35 */
36 private DataObjects jdo = null;
37
38 /***
39 * The name of the JDO object
40 * to be retrieved from page scope
41 */
42 private String jdoName = null;
43
44 /***
45 * The name of the data object
46 * to be retrieved from page scope
47 */
48 private String name = null;
49
50 /***
51 * Whether we are starting
52 * a new transaction
53 */
54 private boolean newTransaction = false;
55
56 /***
57 * Sets the jdo name
58 */
59 public void setJdoName(String _jdoName)
60 {
61 this.jdoName = _jdoName;
62 }
63
64 /***
65 * Sets the name of the page scoped attribute
66 * that is the bean we are deleting/updating
67 */
68 public void setName(String _name)
69 {
70 this.name = _name;
71 }
72
73 /***
74 * Do the start tag bit
75 */
76 public int doStartTag() throws JspException
77 {
78
79 try
80 {
81 /*
82 * Get the data object
83 */
84 if (pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE)!=null)
85 data = pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE);
86 else if (pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE)!=null)
87 data = pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE);
88 else if (pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE)!=null)
89 data = pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE);
90 else if (pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE)!=null)
91 data = pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);
92 else
93 {
94 String msg = "Data object " + this.name + " could not be found in any scope.";
95 log(msg,null);
96 throw new Exception(msg);
97 }
98
99 /*
100 * Check to see if this tag is nested within a transactional tag
101 * Get the database from the parent tag and then create the data
102 */
103 CastorTransactionInitiator t = (CastorTransactionInitiator)findAncestorWithClass(this, CastorTransactionInitiator.class);
104 if (t!=null)
105 {
106 db = t.getDatabase();
107 log ("Got transaction from parent tag", null);
108 db.create(data);
109 }
110 else
111 {
112 String msg = "Tag not nested within a CastorTransactionTag.";
113 log(msg,null);
114 throw new Exception(msg);
115 }
116
117
118 }
119 catch(ClassNotPersistenceCapableException e)
120 {
121 log(e.getMessage() , e);
122 throw new JspTagException(e.getMessage());
123 }
124 catch(TransactionNotInProgressException e)
125 {
126 log(e.getMessage() , e);
127 throw new JspTagException(e.getMessage());
128 }
129 catch(PersistenceException e)
130 {
131 log(e.getMessage() , e);
132 throw new JspTagException(e.getMessage());
133 }
134 catch(Exception e)
135 {
136 log(e.getMessage() , e);
137 throw new JspTagException(e.getMessage());
138 }
139 return EVAL_PAGE;
140 }
141
142 private void log(String msg,Exception e)
143 {
144 if (e!=null)
145 {
146 System.err.println(_tagName + ": " + msg);
147 e.printStackTrace();
148 }
149 else
150 {
151 System.out.println(_tagName + ": " + msg);
152 }
153 }
154 }
This page was automatically generated by Maven