1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com
.sun
.star
.uno
.*;
37 import com
.sun
.star
.beans
.*;
38 import com
.sun
.star
.form
.*;
39 import com
.sun
.star
.lang
.*;
40 import com
.sun
.star
.sdb
.*;
41 import com
.sun
.star
.sdbc
.*;
42 import com
.sun
.star
.sdbcx
.*;
43 import com
.sun
.star
.container
.*;
44 import com
.sun
.star
.awt
.*;
46 /**************************************************************************/
47 /** base class for helpers dealing with unique column values
49 class UniqueColumnValue
51 /* ------------------------------------------------------------------ */
52 /** extracts the name of the table a form is based on.
54 <p>This method works for forms based directly on tables, and for forms based on statements, which
55 themself are based on one table.<br>
56 Everything else (especially forms based on queries) is not yet implemented.</p>
58 private String
extractTableName( XPropertySet xForm
) throws com
.sun
.star
.uno
.Exception
62 Integer aCommandType
= (Integer
)xForm
.getPropertyValue( "CommandType" );
63 String sCommand
= (String
)xForm
.getPropertyValue( "Command" );
65 if ( CommandType
.COMMAND
== aCommandType
.intValue() )
67 // get the connection from the form
68 XConnection xFormConn
= UnoRuntime
.queryInterface( XConnection
.class,
69 xForm
.getPropertyValue( "ActiveConnection" ) );
70 // and let it create a composer for us
71 XSQLQueryComposerFactory xComposerFac
=
72 UnoRuntime
.queryInterface(
73 XSQLQueryComposerFactory
.class, xFormConn
);
74 XSQLQueryComposer xComposer
= xComposerFac
.createQueryComposer( );
76 // let this composer analyze the command
77 xComposer
.setQuery( sCommand
);
79 // and ask it for the table(s)
80 XTablesSupplier xSuppTables
= UnoRuntime
.queryInterface(
81 XTablesSupplier
.class, xComposer
);
82 XNameAccess xTables
= xSuppTables
.getTables();
84 // simply take the first table name
85 String
[] aNames
= xTables
.getElementNames( );
92 /* ------------------------------------------------------------------ */
93 /** generates a statement which can be used to create a unique (in all conscience) value
95 <p>Currently, the implementation uses a very simple approach - it just determines the maximum of currently
96 existing values in the column. If your concrete data source supports a more sophisticated approach of generating
97 unique values, you probably want to adjust the <code>SELECT</code> statement below accordingly.</p>
100 a String which can be used as statement to retrieve a unique value for the given column.
101 The result set resulting from such a execution contains the value in its first column.
103 private String
composeUniqueyKeyStatement( XPropertySet xForm
, String sFieldName
) throws com
.sun
.star
.uno
.Exception
105 String sStatement
= "SELECT MAX( ";
106 sStatement
+= sFieldName
;
107 sStatement
+= ") + 1 FROM ";
108 // the table name is a property of the form
109 sStatement
+= extractTableName( xForm
);
111 // note that the implementation is imperfect (besides the problem that MAX is not a really good solution
112 // for a database with more that one client):
113 // It does not quote the field and the table name. This needs to be done if the database is intolerant
114 // against such things - the XDatabaseMetaData, obtained from the connection, would be needed then
115 // Unfortunately, there is no UNO service doing this - it would need to be implemented manually.
120 /* ------------------------------------------------------------------ */
121 /** generates a unique (in all conscience) key into the column given
123 the form which contains the column in question
125 the name of the column
127 private int generatePrimaryKey( XPropertySet xForm
, String sFieldName
) throws com
.sun
.star
.uno
.Exception
129 // get the current connection of the form
130 XConnection xConn
= UnoRuntime
.queryInterface(
131 XConnection
.class, xForm
.getPropertyValue( "ActiveConnection" ) );
132 // let it create a new statement
133 XStatement xStatement
= xConn
.createStatement();
135 // build the query string to determine a free value
136 String sStatement
= composeUniqueyKeyStatement( xForm
, sFieldName
);
139 XResultSet xResults
= xStatement
.executeQuery( sStatement
);
141 // move the result set to the first record
145 XRow xRow
= UnoRuntime
.queryInterface( XRow
.class, xResults
);
146 int nFreeValue
= xRow
.getInt( 1 );
148 // dispose the temporary objects
149 FLTools
.disposeComponent( xStatement
);
150 // this should get rid of the result set, too
155 /* ------------------------------------------------------------------ */
156 /** inserts a unique (in all conscience) key into the column given
158 the form which contains the column in question
160 the name of the column
162 public void insertPrimaryKey( XPropertySet xForm
, String sFieldName
) throws com
.sun
.star
.uno
.Exception
164 // check the privileges
165 Integer aConcurrency
= (Integer
)xForm
.getPropertyValue( "ResultSetConcurrency" );
166 if ( ResultSetConcurrency
.READ_ONLY
!= aConcurrency
.intValue() )
168 // get the column object
169 XColumnsSupplier xSuppCols
= UnoRuntime
.queryInterface(
170 XColumnsSupplier
.class, xForm
);
171 XNameAccess xCols
= xSuppCols
.getColumns();
172 XColumnUpdate xCol
= UnoRuntime
.queryInterface(
173 XColumnUpdate
.class, xCols
.getByName( sFieldName
) );
175 xCol
.updateInt( generatePrimaryKey( xForm
, sFieldName
) );
180 /**************************************************************************/
181 /** base class for helpers dealing with unique column values
183 class KeyGeneratorForReset
extends UniqueColumnValue
implements XResetListener
185 /* ------------------------------------------------------------------ */
186 private DocumentViewHelper m_aView
;
187 private String m_sFieldName
;
189 /* ------------------------------------------------------------------ */
192 the view which shall be used to focus controls
194 the name of the field for which keys should be generated
196 public KeyGeneratorForReset( String sFieldName
, DocumentViewHelper aView
)
198 m_sFieldName
= sFieldName
;
202 /* ------------------------------------------------------------------ */
203 /** sets the focus to the first control which is no fixed text, and not the
206 private void defaultNewRecordFocus( XPropertySet xForm
) throws com
.sun
.star
.uno
.Exception
208 XIndexAccess xFormAsContainer
= UnoRuntime
.queryInterface(
209 XIndexAccess
.class, xForm
);
210 for ( int i
= 0; i
<xFormAsContainer
.getCount(); ++i
)
213 XPropertySet xModel
= UNO
.queryPropertySet( xFormAsContainer
.getByIndex( i
) );
215 // check if it's a valid leaf (no sub form or such)
216 XPropertySetInfo xPSI
= xModel
.getPropertySetInfo( );
217 if ( ( null == xPSI
) || !xPSI
.hasPropertyByName( "ClassId" ) )
220 // check if it's a fixed text
221 Short nClassId
= (Short
)xModel
.getPropertyValue( "ClassId" );
222 if ( FormComponentType
.FIXEDTEXT
== nClassId
.shortValue() )
225 // check if it is bound to the field we are responsible for
226 if ( !xPSI
.hasPropertyByName( "DataField" ) )
229 String sFieldDataSource
= (String
)xModel
.getPropertyValue( "DataField" );
230 if ( sFieldDataSource
.equals( m_sFieldName
) )
233 // both conditions do not apply
234 // -> set the focus into the respective control
235 XControlModel xCM
= UNO
.queryControlModel( xModel
);
236 m_aView
.grabControlFocus( xCM
);
241 /* ------------------------------------------------------------------ */
242 // XResetListener overridables
243 /* ------------------------------------------------------------------ */
244 public boolean approveReset( com
.sun
.star
.lang
.EventObject rEvent
) throws com
.sun
.star
.uno
.RuntimeException
246 // not interested in vetoing this
250 /* ------------------------------------------------------------------ */
251 public void resetted( com
.sun
.star
.lang
.EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
253 // check if this reset occurred because we're on a new record
254 XPropertySet xFormProps
= UNO
.queryPropertySet( aEvent
.Source
);
257 Boolean aIsNew
= (Boolean
)xFormProps
.getPropertyValue( "IsNew" );
258 if ( aIsNew
.booleanValue() )
261 // we're going to modify the record, though after that, to the user, it should look
262 // like it has not been modified
263 // So we need to ensure that we do not change the IsModified property with whatever we do
264 Object aModifiedFlag
= xFormProps
.getPropertyValue( "IsModified" );
267 insertPrimaryKey( xFormProps
, m_sFieldName
);
269 // then restore the flag
270 xFormProps
.setPropertyValue( "IsModified", aModifiedFlag
);
272 // still one thing ... would be nice to have the focus in a control which is
273 // the one which's value we just defaulted
274 defaultNewRecordFocus( xFormProps
);
277 catch( com
.sun
.star
.uno
.Exception e
)
279 System
.out
.println(e
);
283 /* ------------------------------------------------------------------ */
284 // XEventListener overridables
285 /* ------------------------------------------------------------------ */
286 public void disposing( EventObject aEvent
)
293 /**************************************************************************/
294 /** base class for helpers dealing with unique column values
296 class KeyGeneratorForUpdate
extends UniqueColumnValue
implements XRowSetApproveListener
298 /* ------------------------------------------------------------------ */
299 private String m_sFieldName
;
301 /* ------------------------------------------------------------------ */
302 public KeyGeneratorForUpdate( String sFieldName
)
304 m_sFieldName
= sFieldName
;
307 /* ------------------------------------------------------------------ */
308 // XRowSetApproveListener overridables
309 /* ------------------------------------------------------------------ */
310 public boolean approveCursorMove( com
.sun
.star
.lang
.EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
312 // not interested in vetoing moves
316 /* ------------------------------------------------------------------ */
317 public boolean approveRowChange( RowChangeEvent aEvent
) throws com
.sun
.star
.uno
.RuntimeException
319 if ( RowChangeAction
.INSERT
== aEvent
.Action
)
324 XPropertySet xFormProps
= UNO
.queryPropertySet( aEvent
.Source
);
325 // insert a new unique value
326 insertPrimaryKey( xFormProps
, m_sFieldName
);
328 catch( com
.sun
.star
.uno
.Exception e
)
330 System
.out
.println(e
);
337 /* ------------------------------------------------------------------ */
338 public boolean approveRowSetChange( com
.sun
.star
.lang
.EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
340 // not interested in vetoing executions of the row set
343 /* ------------------------------------------------------------------ */
344 // XEventListener overridables
345 /* ------------------------------------------------------------------ */
346 public void disposing( EventObject aEvent
)
352 /**************************************************************************/
353 /** allows to generate unique keys for a field of a Form
355 public class KeyGenerator
357 /* ------------------------------------------------------------------ */
358 private KeyGeneratorForReset m_aResetKeyGenerator
;
359 private KeyGeneratorForUpdate m_aUpdateKeyGenerator
;
360 private boolean m_bResetListening
;
361 private boolean m_bUpdateListening
;
363 private XPropertySet m_xForm
;
365 /* ------------------------------------------------------------------ */
368 specified the form to operate on
370 specifies the field which's value should be manipulated
372 public KeyGenerator( XPropertySet xForm
, String sFieldName
,
373 XComponentContext xCtx
)
377 DocumentHelper aDocument
= DocumentHelper
.getDocumentForComponent( xForm
, xCtx
);
379 m_aResetKeyGenerator
= new KeyGeneratorForReset( sFieldName
, aDocument
.getCurrentView() );
380 m_aUpdateKeyGenerator
= new KeyGeneratorForUpdate( sFieldName
);
382 m_bResetListening
= m_bUpdateListening
= false;
385 /* ------------------------------------------------------------------ */
386 /** stops any actions on the form
388 public void stopGenerator( )
390 XReset xFormReset
= UNO
.queryReset( m_xForm
);
391 xFormReset
.removeResetListener( m_aResetKeyGenerator
);
393 XRowSetApproveBroadcaster xFormBroadcaster
= UnoRuntime
.queryInterface(
394 XRowSetApproveBroadcaster
.class, m_xForm
);
395 xFormBroadcaster
.removeRowSetApproveListener( m_aUpdateKeyGenerator
);
397 m_bUpdateListening
= m_bResetListening
= false;
400 /* ------------------------------------------------------------------ */
401 /** activates one of our two key generators
403 public void activateKeyGenerator( boolean bGenerateOnReset
)
406 XReset xFormReset
= UNO
.queryReset( m_xForm
);
407 // for approving actions
408 XRowSetApproveBroadcaster xFormBroadcaster
= UnoRuntime
.queryInterface(
409 XRowSetApproveBroadcaster
.class, m_xForm
);
411 if ( bGenerateOnReset
)
413 if ( !m_bResetListening
)
414 xFormReset
.addResetListener( m_aResetKeyGenerator
);
415 if ( m_bUpdateListening
)
416 xFormBroadcaster
.removeRowSetApproveListener( m_aUpdateKeyGenerator
);
418 m_bUpdateListening
= false;
419 m_bResetListening
= true;
423 if ( m_bResetListening
)
424 xFormReset
.removeResetListener( m_aResetKeyGenerator
);
425 if ( !m_bUpdateListening
)
426 xFormBroadcaster
.addRowSetApproveListener( m_aUpdateKeyGenerator
);
428 m_bResetListening
= false;
429 m_bUpdateListening
= true;
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */