1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 #include <com/sun/star/container/XNameAccess.hpp>
35 #include <com/sun/star/embed/XStorage.hpp>
36 #include "hsqldb/StorageFileAccess.h"
37 #include "hsqldb/HStorageMap.hxx"
38 
39 #include <rtl/logfile.hxx>
40 
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::embed;
44 using namespace ::com::sun::star::io;
45 using namespace ::com::sun::star::lang;
46 using namespace ::connectivity::hsqldb;
47 
48 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
49 /*****************************************************************************/
50 /* exception macros */
51 
52 #define ThrowException(env, type, msg) { \
53 	env->ThrowNew(env->FindClass(type), msg); }
54 
55 /*
56  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess
57  * Method:    isStreamElement
58  * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
59  */
60 SAL_DLLPUBLIC_EXPORT jboolean JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_isStreamElement
61   (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
62 {
63 	TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(StorageContainer::jstring2ustring(env,key));
64 	if ( aStoragePair.first.first.is() )
65 	{
66 		try
67 		{
68             ::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
69             try
70 		    {
71                 ::rtl::OUString sOldName = StorageContainer::removeOldURLPrefix(sName);
72                 if ( aStoragePair.first.first->isStreamElement(sOldName) )
73                 {
74                     try
75                     {
76                         aStoragePair.first.first->renameElement(sOldName,StorageContainer::removeURLPrefix(sName,aStoragePair.first.second));
77                     }
78                     catch(Exception&)
79                     {
80                     }
81                 }
82             }
83 		    catch(NoSuchElementException&)
84 		    {
85             }
86 			return aStoragePair.first.first->isStreamElement(StorageContainer::removeURLPrefix(sName,aStoragePair.first.second));
87 		}
88 		catch(NoSuchElementException&)
89 		{
90 		}
91 		catch(Exception& e)
92 		{
93             OSL_ENSURE(0,"Exception catched! : Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_isStreamElement");
94 			if (JNI_FALSE != env->ExceptionCheck())
95 				env->ExceptionClear();
96 			::rtl::OString cstr( ::rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
97 			OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
98 		}
99 	}
100 	return JNI_FALSE;
101 }
102 // -----------------------------------------------------------------------------
103 
104 /*
105  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess
106  * Method:    removeElement
107  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
108  */
109 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_removeElement
110   (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
111 {
112 #ifdef HSQLDB_DBG
113     {
114         ::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
115         ::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
116     }
117 #endif
118 	TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(StorageContainer::jstring2ustring(env,key));
119 	if ( aStoragePair.first.first.is() )
120 	{
121 		try
122 		{
123 			aStoragePair.first.first->removeElement(StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,name),aStoragePair.first.second));
124 		}
125 		catch(NoSuchElementException&)
126 		{
127 			if (JNI_FALSE != env->ExceptionCheck())
128 				env->ExceptionClear();
129 		}
130 		catch(Exception& e)
131 		{
132             OSL_ENSURE(0,"Exception catched! : Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_removeElement");
133             StorageContainer::throwJavaException(e,env);
134 		}
135 	}
136 }
137 // -----------------------------------------------------------------------------
138 
139 /*
140  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess
141  * Method:    renameElement
142  * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
143  */
144 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_renameElement
145   (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring oldname, jstring newname)
146 {
147 #ifdef HSQLDB_DBG
148     {
149         ::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
150         ::rtl::OUString sNewName = StorageContainer::jstring2ustring(env,newname);
151         ::rtl::OUString sOldName = StorageContainer::jstring2ustring(env,oldname);
152     }
153 #endif
154 	TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(StorageContainer::jstring2ustring(env,key));
155 	if ( aStoragePair.first.first.is() )
156 	{
157 		try
158 		{
159             aStoragePair.first.first->renameElement(
160                 StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,oldname),aStoragePair.first.second),
161                 StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,newname),aStoragePair.first.second)
162             );
163 #ifdef HSQLDB_DBG
164             {
165                 ::rtl::OUString sNewName = StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,newname),aStoragePair.first.second);
166                 OSL_ENSURE(aStoragePair.first.first->isStreamElement(sNewName),"Stream could not be renamed");
167             }
168 #endif
169 		}
170 		catch(NoSuchElementException&)
171 		{
172 		}
173 		catch(Exception& e)
174 		{
175 			OSL_ENSURE(0,"Exception catched! : Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_renameElement");
176             StorageContainer::throwJavaException(e,env);
177 		}
178 	}
179 }
180 // -----------------------------------------------------------------------------
181