Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / KeyGenerator.java
blob08beae57c63d83b5aa86cecf2252fd331310f190
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
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
60 String sReturn;
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( );
86 sCommand = aNames[0];
89 return sCommand;
92 /* ------------------------------------------------------------------ */
93 /** generates a statement which can be used to create a unique (in all conscience) value
94 for the column given.
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>
99 @returns
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.
117 return sStatement;
120 /* ------------------------------------------------------------------ */
121 /** generates a unique (in all conscience) key into the column given
122 @param xForm
123 the form which contains the column in question
124 @param sFieldName
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 );
138 // execute the query
139 XResultSet xResults = xStatement.executeQuery( sStatement );
141 // move the result set to the first record
142 xResults.next( );
144 // get the value
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
152 return nFreeValue;
155 /* ------------------------------------------------------------------ */
156 /** inserts a unique (in all conscience) key into the column given
157 @param xForm
158 the form which contains the column in question
159 @param sFieldName
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 /* ------------------------------------------------------------------ */
190 /** ctor
191 @param aView
192 the view which shall be used to focus controls
193 @param sFieldName
194 the name of the field for which keys should be generated
196 public KeyGeneratorForReset( String sFieldName, DocumentViewHelper aView )
198 m_sFieldName = sFieldName;
199 m_aView = aView;
202 /* ------------------------------------------------------------------ */
203 /** sets the focus to the first control which is no fixed text, and not the
204 one we're defaulting
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 )
212 // the model
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" ) )
218 continue;
220 // check if it's a fixed text
221 Short nClassId = (Short)xModel.getPropertyValue( "ClassId" );
222 if ( FormComponentType.FIXEDTEXT == nClassId.shortValue() )
223 continue;
225 // check if it is bound to the field we are responsible for
226 if ( !xPSI.hasPropertyByName( "DataField" ) )
227 continue;
229 String sFieldDataSource = (String)xModel.getPropertyValue( "DataField" );
230 if ( sFieldDataSource.equals( m_sFieldName ) )
231 continue;
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);
237 break;
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
247 return true;
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() )
259 { // yepp
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" );
266 // now set the value
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);
280 e.printStackTrace();
283 /* ------------------------------------------------------------------ */
284 // XEventListener overridables
285 /* ------------------------------------------------------------------ */
286 public void disposing( EventObject aEvent )
288 // not interested in
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
313 return true;
316 /* ------------------------------------------------------------------ */
317 public boolean approveRowChange( RowChangeEvent aEvent ) throws com.sun.star.uno.RuntimeException
319 if ( RowChangeAction.INSERT == aEvent.Action )
323 // the affected form
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);
331 e.printStackTrace();
334 return true;
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
341 return true;
343 /* ------------------------------------------------------------------ */
344 // XEventListener overridables
345 /* ------------------------------------------------------------------ */
346 public void disposing( EventObject aEvent )
348 // not interested in
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 /* ------------------------------------------------------------------ */
366 /** ctor
367 @param xForm
368 specified the form to operate on
369 @param sFieldName
370 specifies the field which's value should be manipulated
372 public KeyGenerator( XPropertySet xForm, String sFieldName,
373 XComponentContext xCtx )
375 m_xForm = xForm;
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 )
405 // for resets
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;
421 else
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: */