1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the BSD license.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Sun Microsystems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*************************************************************************/
import com.sun.star.uno.*;
import com.sun.star.beans.*;
import com.sun.star.form.*;
import com.sun.star.lang.*;
import com.sun.star.sdb.*;
import com.sun.star.sdbc.*;
import com.sun.star.sdbcx.*;
import com.sun.star.container.*;
import com.sun.star.awt.*;
/**************************************************************************/
/** base class for helpers dealing with unique column values
*/
class UniqueColumnValue
{
/* ------------------------------------------------------------------ */
/** extracts the name of the table a form is based on.
<p>This method works for forms based directly on tables, and for forms based on statements, which
themself are based on one table.<br/>
Everything else (especially forms based on queries) is not yet implemented.</p>
*/
protected String extractTableName( XPropertySet xForm ) throws com.sun.star.uno.Exception
{
String sReturn;
Integer aCommandType = (Integer)xForm.getPropertyValue( "CommandType" );
String sCommand = (String)xForm.getPropertyValue( "Command" );
if ( CommandType.COMMAND == aCommandType.intValue() )
{
// get the connection from the form
XConnection xFormConn = (XConnection)UnoRuntime.queryInterface( XConnection.class,
xForm.getPropertyValue( "ActiveConnection" ) );
// and let it create a composer for us
XSQLQueryComposerFactory xComposerFac =
(XSQLQueryComposerFactory)UnoRuntime.queryInterface(
XSQLQueryComposerFactory.class, xFormConn );
XSQLQueryComposer xComposer = xComposerFac.createQueryComposer( );
// let this composer analyze the command
xComposer.setQuery( sCommand );
// and ask it for the table(s)
XTablesSupplier xSuppTables = (XTablesSupplier)UnoRuntime.queryInterface(
XTablesSupplier.class, xComposer );
XNameAccess xTables = xSuppTables.getTables();
// simply take the first table name
String[] aNames = xTables.getElementNames( );
sCommand = aNames[0];
}
return sCommand;
}
/* ------------------------------------------------------------------ */
/** generates a statement which can be used to create a unique (in all conscience) value
for the column given.
<p>Currently, the implementation uses a very simple approach - it just determines the maximum of currently
existing values in the column. If your concrete data source supports a more sophisticated approach of generating
unique values, you probably want to adjust the <code>SELECT</code> statement below accordingly.</p>
@returns
a String which can be used as statement to retrieve a unique value for the given column.
The result set resulting from such a execution contains the value in it's first column.
*/
protected String composeUniqueyKeyStatement( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
{
String sStatement = new String( "SELECT MAX( " );
sStatement += sFieldName;
sStatement += new String( ") + 1 FROM " );
// the table name is a property of the form
sStatement += extractTableName( xForm );
// note that the implementation is imperfect (besides the problem that MAX is not a really good solution
// for a database with more that one client):
// It does not quote the field and the table name. This needs to be done if the database is intolerant
// against such things - the XDatabaseMetaData, obtained from the connection, would be needed then
// Unfortunately, there is no UNO service doing this - it would need to be implemented manually.
return sStatement;
}
/* ------------------------------------------------------------------ */
/** generates a unique (in all conscience) key into the column given
@param xForm
the form which contains the column in question
@param sFieldName
the name of the column
*/
protected int generatePrimaryKey( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
{
// get the current connection of the form
XConnection xConn = (XConnection)UnoRuntime.queryInterface(
XConnection.class, xForm.getPropertyValue( "ActiveConnection" ) );
// let it create a new statement
XStatement xStatement = xConn.createStatement();
// build the query string to determine a free value
String sStatement = composeUniqueyKeyStatement( xForm, sFieldName );
// execute the query
XResultSet xResults = xStatement.executeQuery( sStatement );
// move the result set to the first record
xResults.next( );
// get the value
XRow xRow = (XRow)UnoRuntime.queryInterface( XRow.class, xResults );
int nFreeValue = xRow.getInt( 1 );
// dispose the temporary objects
FLTools.disposeComponent( xStatement );
// this should get rid of the result set, too
return nFreeValue;
}
/* ------------------------------------------------------------------ */
/** inserts a unique (in all conscience) key into the column given
@param xForm
the form which contains the column in question
@param sFieldName
the name of the column
*/
public void insertPrimaryKey( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
{
// check the privileges
Integer aConcurrency = (Integer)xForm.getPropertyValue( "ResultSetConcurrency" );
if ( ResultSetConcurrency.READ_ONLY != aConcurrency.intValue() )
{
// get the column object
XColumnsSupplier xSuppCols = (XColumnsSupplier)UnoRuntime.queryInterface(
XColumnsSupplier.class, xForm );
XNameAccess xCols = xSuppCols.getColumns();
XColumnUpdate xCol = (XColumnUpdate)UnoRuntime.queryInterface(
XColumnUpdate.class, xCols.getByName( sFieldName ) );
xCol.updateInt( generatePrimaryKey( xForm, sFieldName ) );
}
}
};
/**************************************************************************/
/** base class for helpers dealing with unique column values
*/
class KeyGeneratorForReset extends UniqueColumnValue implements XResetListener
{
/* ------------------------------------------------------------------ */
private DocumentViewHelper m_aView;
private String m_sFieldName;
/* ------------------------------------------------------------------ */
/** ctor
@param aView
the view which shall be used to focus controls
@param sFieldName
the name of the field for which keys should be generated
*/
public KeyGeneratorForReset( String sFieldName, DocumentViewHelper aView )
{
m_sFieldName = sFieldName;
m_aView = aView;
}
/* ------------------------------------------------------------------ */
/** sets the focus to the first control which is no fixed text, and not the
one we're defaulting
*/
public void defaultNewRecordFocus( XPropertySet xForm ) throws com.sun.star.uno.Exception
{
XIndexAccess xFormAsContainer = (XIndexAccess)UnoRuntime.queryInterface(
XIndexAccess.class, xForm );
for ( int i = 0; i<xFormAsContainer.getCount(); ++i )
{
// the model
XPropertySet xModel = UNO.queryPropertySet( xFormAsContainer.getByIndex( i ) );
// check if it's a valid leaf (no sub form or such)
XPropertySetInfo xPSI = xModel.getPropertySetInfo( );
if ( ( null == xPSI ) || !xPSI.hasPropertyByName( "ClassId" ) )
continue;
// check if it's a fixed text
Short nClassId = (Short)xModel.getPropertyValue( "ClassId" );
if ( FormComponentType.FIXEDTEXT == nClassId.shortValue() )
continue;
// check if it is bound to the field we are responsible for
if ( !xPSI.hasPropertyByName( "DataField" ) )
continue;
String sFieldDataSource = (String)xModel.getPropertyValue( "DataField" );
if ( sFieldDataSource.equals( m_sFieldName ) )
continue;
// both conditions do not apply
// -> set the focus into the respective control
XControlModel xCM = UNO.queryControlModel( xModel );
m_aView.grabControlFocus( xCM);
break;
}
}
/* ------------------------------------------------------------------ */
// XResetListener overridables
/* ------------------------------------------------------------------ */
public boolean approveReset( com.sun.star.lang.EventObject rEvent ) throws com.sun.star.uno.RuntimeException
{
// not interested in vetoing this
return true;
}
/* ------------------------------------------------------------------ */
public void resetted( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
{
// check if this reset occurred becase we're on a new record
XPropertySet xFormProps = UNO.queryPropertySet( aEvent.Source );
try
{
Boolean aIsNew = (Boolean)xFormProps.getPropertyValue( "IsNew" );
if ( aIsNew.booleanValue() )
{ // yepp
// we're going to modify the record, though after that, to the user, it should look
// like it has not been modified
// So we need to ensure that we do not change the IsModified property with whatever we do
Object aModifiedFlag = xFormProps.getPropertyValue( "IsModified" );
// now set the value
insertPrimaryKey( xFormProps, m_sFieldName );
// then restore the flag
xFormProps.setPropertyValue( "IsModified", aModifiedFlag );
// still one thing ... would be nice to have the focus in a control which is
// the one which's value we just defaulted
defaultNewRecordFocus( xFormProps );
}
}
catch( com.sun.star.uno.Exception e )
{
System.out.println(e);
e.printStackTrace();
}
}
/* ------------------------------------------------------------------ */
// XEventListener overridables
/* ------------------------------------------------------------------ */
public void disposing( EventObject aEvent )
{
// not interested in
}
};
/**************************************************************************/
/** base class for helpers dealing with unique column values
*/
class KeyGeneratorForUpdate extends UniqueColumnValue implements XRowSetApproveListener
{
/* ------------------------------------------------------------------ */
private String m_sFieldName;
/* ------------------------------------------------------------------ */
public KeyGeneratorForUpdate( String sFieldName )
{
m_sFieldName = sFieldName;
}
/* ------------------------------------------------------------------ */
// XRowSetApproveListener overridables
/* ------------------------------------------------------------------ */
public boolean approveCursorMove( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
{
// not interested in vetoing moves
return true;
}
/* ------------------------------------------------------------------ */
public boolean approveRowChange( RowChangeEvent aEvent ) throws com.sun.star.uno.RuntimeException
{
if ( RowChangeAction.INSERT == aEvent.Action )
{
try
{
// the affected form
XPropertySet xFormProps = UNO.queryPropertySet( aEvent.Source );
// insert a new unique value
insertPrimaryKey( xFormProps, m_sFieldName );
}
catch( com.sun.star.uno.Exception e )
{
System.out.println(e);
e.printStackTrace();
}
}
return true;
}
/* ------------------------------------------------------------------ */
public boolean approveRowSetChange( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
{
// not interested in vetoing executions of the row set
return true;
}
/* ------------------------------------------------------------------ */
// XEventListener overridables
/* ------------------------------------------------------------------ */
public void disposing( EventObject aEvent )
{
// not interested in
}
};
/**************************************************************************/
/** allows to generate unique keys for a field of a Form
*/
public class KeyGenerator
{
/* ------------------------------------------------------------------ */
private KeyGeneratorForReset m_aResetKeyGenerator;
private KeyGeneratorForUpdate m_aUpdateKeyGenerator;
private boolean m_bResetListening;
private boolean m_bUpdateListening;
private DocumentHelper m_aDocument;
private XPropertySet m_xForm;
/* ------------------------------------------------------------------ */
/** ctor
@param xForm
specified the form to operate on
@param sFieldName
specifies the field which's value should be manipulated
*/
public KeyGenerator( XPropertySet xForm, String sFieldName,
XComponentContext xCtx )
{
m_xForm = xForm;
DocumentHelper aDocument = DocumentHelper.getDocumentForComponent( xForm, xCtx );
m_aResetKeyGenerator = new KeyGeneratorForReset( sFieldName, aDocument.getCurrentView() );
m_aUpdateKeyGenerator = new KeyGeneratorForUpdate( sFieldName );
m_bResetListening = m_bUpdateListening = false;
}
/* ------------------------------------------------------------------ */
/** stops any actions on the form
*/
public void stopGenerator( )
{
XReset xFormReset = UNO.queryReset( m_xForm );
xFormReset.removeResetListener( m_aResetKeyGenerator );
XRowSetApproveBroadcaster xFormBroadcaster = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(
XRowSetApproveBroadcaster.class, m_xForm );
xFormBroadcaster.removeRowSetApproveListener( m_aUpdateKeyGenerator );
m_bUpdateListening = m_bResetListening = false;
}
/* ------------------------------------------------------------------ */
/** activates one of our two key generators
*/
public void activateKeyGenerator( boolean bGenerateOnReset )
{
// for resets
XReset xFormReset = UNO.queryReset( m_xForm );
// for approving actions
XRowSetApproveBroadcaster xFormBroadcaster = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(
XRowSetApproveBroadcaster.class, m_xForm );
if ( bGenerateOnReset )
{
if ( !m_bResetListening )
xFormReset.addResetListener( m_aResetKeyGenerator );
if ( m_bUpdateListening )
xFormBroadcaster.removeRowSetApproveListener( m_aUpdateKeyGenerator );
m_bUpdateListening = false;
m_bResetListening = true;
}
else
{
if ( m_bResetListening )
xFormReset.removeResetListener( m_aResetKeyGenerator );
if ( !m_bUpdateListening )
xFormBroadcaster.addRowSetApproveListener( m_aUpdateKeyGenerator );
m_bResetListening = false;
m_bUpdateListening = true;
}
}
};
|