KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > rdbms > RdbmsUserContextAssociation


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.repository.rdbms;
31
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 import com.genimen.djeneric.repository.DjContext;
36 import com.genimen.djeneric.repository.DjSession;
37 import com.genimen.djeneric.repository.DjUser;
38 import com.genimen.djeneric.repository.DjUserContextAssociation;
39 import com.genimen.djeneric.repository.exceptions.DjenericException;
40
41 public class RdbmsUserContextAssociation extends DjUserContextAssociation
42 {
43   // Use this constructor to create a new DevCtx association
44
protected RdbmsUserContextAssociation()
45   {
46   }
47
48   protected RdbmsUserContextAssociation(DjUser dev, DjContext ctxt, boolean canCreate, boolean canModify,
49                                         boolean canDelete, boolean canQuery)
50   {
51     super(dev, ctxt, canCreate, canModify, canDelete, canQuery);
52   }
53
54   public void delete(DjSession session) throws DjenericException
55   {
56     try
57     {
58       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
59                                                                            + RdbmsPersistenceManager.USER_CTX_TABLE
60                                                                            + " where user_id = :devId "
61                                                                            + "and context_id = :ctxtId ");
62       stmt.setLong("devId", getUser().getId());
63       stmt.setLong("ctxtId", getContext().getId());
64       stmt.executeUpdate();
65     }
66     catch (SQLException JavaDoc x)
67     {
68       throw new DjenericException(x);
69     }
70   }
71
72   public void reload(DjSession session) throws DjenericException
73   {
74     try
75     {
76       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select cre_yn, mod_yn, del_yn, qry_yn "
77                                                                            + "from "
78                                                                            + RdbmsPersistenceManager.USER_CTX_TABLE
79                                                                            + " where user_id = :devId "
80                                                                            + "and context_id = :ctxtId ");
81       stmt.setLong("devId", getUser().getId());
82       stmt.setLong("ctxtId", getContext().getId());
83       ResultSet JavaDoc rs = stmt.executeQuery();
84       if (rs.next())
85       {
86         setCreate(rs.getString("cre_yn").equalsIgnoreCase("Y"));
87         setModify(rs.getString("mod_yn").equalsIgnoreCase("Y"));
88         setDelete(rs.getString("del_yn").equalsIgnoreCase("Y"));
89         setQuery(rs.getString("qry_yn").equalsIgnoreCase("Y"));
90       }
91       rs.close();
92       stmt.close();
93     }
94     catch (SQLException JavaDoc x)
95     {
96       throw new DjenericException(x);
97     }
98   }
99
100   public void persist(DjSession session) throws DjenericException
101   {
102     try
103     {
104       SqlStatement stmt = ((RdbmsSession) session)
105           .getInternalSqlStatement("update " + RdbmsPersistenceManager.USER_CTX_TABLE
106                                    + " set cre_yn = :cre_yn, mod_yn = :mod_yn, del_yn = :del_yn, qry_yn = :qry_yn "
107                                    + "where user_id = :devId " + "and context_id = :ctxtId ");
108
109       stmt.setLong("devId", getUser().getId());
110       stmt.setLong("ctxtId", getContext().getId());
111       stmt.setString("cre_yn", isCreate() ? "Y" : "N");
112       stmt.setString("mod_yn", isModify() ? "Y" : "N");
113       stmt.setString("del_yn", isDelete() ? "Y" : "N");
114       stmt.setString("qry_yn", isQuery() ? "Y" : "N");
115       if (stmt.executeUpdate() == 0)
116       {
117         // Deleted / not yet persisted?
118
insert(session);
119       }
120     }
121     catch (SQLException JavaDoc x)
122     {
123       throw new DjenericException(x);
124     }
125   }
126
127   public void insert(DjSession session) throws DjenericException
128   {
129     try
130     {
131       SqlStatement stmt = ((RdbmsSession) session)
132           .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.USER_CTX_TABLE
133                                    + "(user_id, context_id, cre_yn, mod_yn, del_yn, qry_yn) "
134                                    + "values (:user_id, :context_id, :cre_yn, :mod_yn, :del_yn, :qry_yn)");
135       stmt.setLong("user_id", getUser().getId());
136       stmt.setLong("context_id", getContext().getId());
137       stmt.setString("cre_yn", isCreate() ? "Y" : "N");
138       stmt.setString("mod_yn", isModify() ? "Y" : "N");
139       stmt.setString("del_yn", isDelete() ? "Y" : "N");
140       stmt.setString("qry_yn", isQuery() ? "Y" : "N");
141       stmt.executeUpdate();
142       session.commit();
143     }
144     catch (SQLException JavaDoc x)
145     {
146       throw new DjenericException(x);
147     }
148   }
149 }
Popular Tags