KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > CmsDbContext


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsDbContext.java,v $
3  * Date : $Date: 2005/09/12 09:57:12 $
4  * Version: $Revision: 1.14 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db;
33
34 import org.opencms.file.CmsProject;
35 import org.opencms.file.CmsRequestContext;
36 import org.opencms.file.CmsUser;
37 import org.opencms.file.CmsVfsException;
38 import org.opencms.flex.CmsFlexRequestContextInfo;
39 import org.opencms.i18n.CmsMessageContainer;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsRuntimeException;
42 import org.opencms.report.I_CmsReport;
43 import org.opencms.util.CmsRequestUtil;
44
45 /**
46  * Warps context information to access the OpenCms database.<p>
47  *
48  * @author Alexander Kandzior
49  *
50  * @version $Revision: 1.14 $
51  *
52  * @since 6.0.0
53  */

54 public class CmsDbContext {
55
56     /** The current Flex request context info (if available). */
57     protected CmsFlexRequestContextInfo m_flexRequestContextInfo;
58
59     /** The wrapped user request context. */
60     protected CmsRequestContext m_requestContext;
61
62     /**
63      * Creates a new, empty database context.<p>
64      */

65     public CmsDbContext() {
66
67         this(null);
68     }
69
70     /**
71      * Creates a new database context initialized with the given user request context.<p>
72      *
73      * @param context the current users request context
74      */

75     public CmsDbContext(CmsRequestContext context) {
76
77         m_requestContext = context;
78
79         if (m_requestContext != null) {
80             m_flexRequestContextInfo = (CmsFlexRequestContextInfo)m_requestContext.getAttribute(CmsRequestUtil.HEADER_LAST_MODIFIED);
81         }
82     }
83
84     /**
85      * Clears this database context.<p>
86      */

87     public void clear() {
88
89         m_requestContext = null;
90         m_flexRequestContextInfo = null;
91     }
92
93     /**
94      * Returns the current users project.<p>
95      *
96      * @return the current users project
97      */

98     public CmsProject currentProject() {
99
100         return m_requestContext.currentProject();
101     }
102
103     /**
104      * Returns the current user.<p>
105      *
106      * @return the current user
107      */

108     public CmsUser currentUser() {
109
110         return m_requestContext.currentUser();
111     }
112
113     /**
114      * Returns the current Flex request context info.<p>
115      *
116      * @return the current Flex request context info
117      */

118     public CmsFlexRequestContextInfo getFlexRequestContextInfo() {
119
120         return m_flexRequestContextInfo;
121     }
122
123     /**
124      * Returns the request context.<p>
125      *
126      * @return the request context
127      */

128     public CmsRequestContext getRequestContext() {
129
130         return m_requestContext;
131     }
132
133     /**
134      * Returns true if the database context uses the default implementation.<p>
135      *
136      * @return true if the database context uses the default implementation
137      */

138     public boolean isDefaultDbContext() {
139
140         return true;
141     }
142
143     /**
144      * Processes the current database context.<p>
145      *
146      * @throws CmsException if something goes wrong
147      */

148     public void pop() throws CmsException {
149
150         if (!isDefaultDbContext()) {
151             throw new CmsException(Messages.get().container(Messages.ERR_PROCESS_DB_CONTEXT_0));
152         }
153     }
154
155     /**
156      * Removes the current site root prefix from the absolute path in the resource name,
157      * that is adjusts the resource name for the current site root.<p>
158      *
159      * If no user request context is available, the given resource name is
160      * returned unchanged.<p>
161      *
162      * @param resourcename the resource name
163      *
164      * @return the resource name adjusted for the current site root
165      */

166     public String JavaDoc removeSiteRoot(String JavaDoc resourcename) {
167
168         if (m_requestContext != null) {
169             return m_requestContext.removeSiteRoot(resourcename);
170         }
171
172         return resourcename;
173     }
174
175     /**
176      * Reports an error to the given report (if available) and to the OpenCms log file.<p>
177      *
178      * @param report the report to write the error to
179      * @param message the message to write to the report / log
180      * @param throwable the exception to write to the report / log
181      *
182      * @throws CmsException if the throwable parameter is not null and a CmsException
183      * @throws CmsVfsException if the throwable parameter is not null and no CmsException
184      */

185     public void report(I_CmsReport report, CmsMessageContainer message, Throwable JavaDoc throwable)
186     throws CmsVfsException, CmsException {
187
188         if (report != null) {
189             if (message != null) {
190                 report.println(message, I_CmsReport.FORMAT_ERROR);
191             }
192             if (throwable != null) {
193                 report.println(throwable);
194             }
195         }
196
197         throwException(message, throwable);
198     }
199
200     /**
201      * Returns an Exception of the same type as throwable if throwable is an OpenCms Exception
202      * with the message as a MessageContainer and the throwable as a cause.<p>
203      *
204      * @param message the MessageContainer for the Exception to create
205      * @param throwable the cause of the exception
206      *
207      * @throws CmsVfsException if throwable is not an OpenCms Exception
208      * @throws CmsException of the same type as throwable if throwable is an OpenCms Exception
209      */

210     void throwException(CmsMessageContainer message, Throwable JavaDoc throwable) throws CmsVfsException, CmsException {
211
212         if (throwable instanceof CmsException) {
213             throw ((CmsException)throwable).createException(message, throwable);
214         } else if (throwable instanceof CmsRuntimeException) {
215             throw ((CmsRuntimeException)throwable).createException(message, throwable);
216         } else {
217             throw new CmsVfsException(message, throwable);
218         }
219     }
220 }
Popular Tags