KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > campware > cream > modules > actions > ProjectSQL


1 package org.campware.cream.modules.actions;
2
3 /* ====================================================================
4  * Copyright (C) 2003-2005 Media Development Loan Fund
5  *
6  * * contact: contact@campware.org - http://www.campware.org
7  * Campware encourages further development. Please let us know.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
27  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  * ====================================================================
36  *
37  * This software consists of voluntary contributions made by many
38  * individuals on behalf of the Apache Software Foundation. For more
39  * information on the Apache Software Foundation, please see
40  * <http://www.apache.org/>.
41  */

42
43 import java.util.Date JavaDoc;
44 import org.apache.velocity.context.Context;
45
46 import org.apache.turbine.util.RunData;
47 import org.apache.torque.util.Criteria;
48 import org.apache.torque.util.Transaction;
49 import java.sql.Connection JavaDoc;
50
51 import org.campware.cream.om.Project;
52 import org.campware.cream.om.ProjectPeer;
53
54 /**
55  * This class provides a simple set of methods to
56  * insert/update/delete records in a database.
57  */

58 public class ProjectSQL extends CreamAction
59 {
60     protected void initScreen()
61     {
62         setModuleType(ENTITY);
63         setModuleName("PROJECT");
64     }
65     /**
66      * This simply takes an entry from the web form and
67      * inserts it directly into the database.
68      *
69      * This would not be good in practice as the
70      * data should be verified before being allowed
71      * into the database. This is merely an
72      * example of how to use peers, this certainly
73      * wouldn't be secure.
74      */

75     public void doInsert(RunData data, Context context)
76         throws Exception JavaDoc
77     {
78         Project entry = new Project();
79         data.getParameters().setProperties(entry);
80
81
82         String JavaDoc myCode=data.getParameters().getString("projectcode");
83
84         entry.setStartDate(parseDate(data.getParameters().getString("startdate")));
85         entry.setEndDate(parseDate(data.getParameters().getString("enddate")));
86         entry.setCreatedBy(data.getUser().getName());
87         entry.setCreated(new Date JavaDoc());
88         entry.setModifiedBy(data.getUser().getName());
89         entry.setModified(new Date JavaDoc());
90         
91         if (myCode.equals("AUTO"))
92         {
93             entry.setProjectCode(getTempCode());
94
95             Connection JavaDoc conn = Transaction.begin(ProjectPeer.DATABASE_NAME);
96             boolean success = false;
97             try {
98                 entry.save(conn);
99                 entry.setProjectCode(getRowCode("CP", entry.getProjectId()));
100                 entry.save(conn);
101                 Transaction.commit(conn);
102                 success = true;
103     
104             } finally {
105                 if (!success) Transaction.safeRollback(conn);
106             }
107         }
108         else
109         {
110             entry.save();
111         }
112
113     }
114
115     /**
116      * Update a record in the database with the
117      * information present in the web form.
118      *
119      * Again, this is merely an example. The data
120      * should be checked before being allowed
121      * into the database.
122      */

123     public void doUpdate(RunData data, Context context)
124         throws Exception JavaDoc
125     {
126         Project entry = new Project();
127         data.getParameters().setProperties(entry);
128
129         String JavaDoc myCode=data.getParameters().getString("projectcode");
130         if (myCode.equals("AUTO"))
131         {
132             entry.setProjectCode(getRowCode("CP", entry.getProjectId()));
133         }
134
135         entry.setStartDate(parseDate(data.getParameters().getString("startdate")));
136         entry.setEndDate(parseDate(data.getParameters().getString("enddate")));
137         entry.setCreated(parseDateTime(data.getParameters().getString("created")));
138         entry.setModifiedBy(data.getUser().getName());
139         entry.setModified(new Date JavaDoc());
140
141         entry.setModified(true);
142         entry.setNew(false);
143         entry.save();
144
145     }
146
147     /**
148      * Delete a record from the database using
149      * the unique id gleaned from the web form.
150      */

151     public void doDelete(RunData data, Context context)
152         throws Exception JavaDoc
153     {
154         Criteria criteria = new Criteria();
155         criteria.add(ProjectPeer.PROJECT_ID, data.getParameters().getInt("projectid"));
156         ProjectPeer.doDelete(criteria);
157     }
158
159     /**
160      * Delete selected records from the database using
161      * the unique ids gleaned from the web form.
162      */

163     public void doDeleteselected(RunData data, Context context)
164         throws Exception JavaDoc
165     {
166         int[] delIds= data.getParameters().getInts("rowid");
167         Criteria criteria = new Criteria();
168         criteria.addIn(ProjectPeer.PROJECT_ID, delIds);
169         ProjectPeer.doDelete(criteria);
170     }
171
172 }
173
Popular Tags