Grails 1.3.7 Map to sql server 2k8 Date Column - sql-server

We have a legacy grails app running 1.3.7 and having trouble mapping to a mssql server 2k8 table with a date data type.
I've got it mapped with sqlType:'date' it still get the following error when it tries to load the entity ERROR util.JDBCExceptionReporter - Can't convert '2013-07-24' to Timestamp.
Added a custom Dialect extending the SQLServerDialect with the following no difference
registerColumnType( Types.DATE, "date" );

Grails 1.3.7 comes bundles with hibernate 3.3.1 GA. I think there is no SQLServer2008Dialect in this version of Hibernate, only a `SQLServerDialect̀ which is good for 2k and 2k5 versions
You'll have to borrow this class from a later version, as explained in this blog post
I reproduce the class here for archive sakes (from robert-reiz.com)
import org.hibernate.Hibernate;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.function.*;
import org.hibernate.type.StandardBasicTypes;
import java.sql.Types;
public class SqlServer2008Dialect extends SQLServerDialect {
public SqlServer2008Dialect(){
super();
registerColumnType( Types.DATE, "date" );
registerColumnType( Types.TIME, "time" );
registerColumnType( Types.TIMESTAMP, "datetime2" );
registerFunction( "current_timestamp", new NoArgSQLFunction("current_timestamp", Hibernate.TIMESTAMP,false) );
registerColumnType( Types.BIT, "tinyint" ); //Sybase BIT type does not support null values
registerColumnType( Types.BIGINT, "bigint" );//changed
registerColumnType( Types.SMALLINT, "smallint" );
registerColumnType( Types.TINYINT, "tinyint" );
registerColumnType( Types.INTEGER, "int" );
registerColumnType( Types.CHAR, "char(1)" );
registerColumnType( Types.VARCHAR, "varchar($l)" );
registerColumnType( Types.FLOAT, "float" );
registerColumnType( Types.DOUBLE, "double precision" );
registerColumnType( Types.VARBINARY, "varbinary($l)" );
registerColumnType( Types.NUMERIC, "numeric($p,$s)" );
registerColumnType( Types.BLOB, "image" );
registerColumnType( Types.CLOB, "text" );
registerColumnType( Types.ROWID, "bigint");
registerFunction( "ascii", new StandardSQLFunction("ascii", StandardBasicTypes.INTEGER));
registerFunction( "char", new StandardSQLFunction("char", StandardBasicTypes.CHARACTER));
registerFunction( "len", new StandardSQLFunction("len", StandardBasicTypes.LONG) );
registerFunction( "lower", new StandardSQLFunction("lower") );
registerFunction( "upper", new StandardSQLFunction("upper") );
registerFunction( "str", new StandardSQLFunction("str", StandardBasicTypes.STRING) );
registerFunction( "ltrim", new StandardSQLFunction("ltrim") );
registerFunction( "rtrim", new StandardSQLFunction("rtrim") );
registerFunction( "reverse", new StandardSQLFunction("reverse") );
registerFunction( "space", new StandardSQLFunction("space", StandardBasicTypes.STRING));
registerFunction( "user", new NoArgSQLFunction("user", StandardBasicTypes.STRING) );
registerFunction( "current_timestamp", new NoArgSQLFunction("getdate", StandardBasicTypes.TIMESTAMP) );
registerFunction( "current_time", new NoArgSQLFunction("getdate", StandardBasicTypes. TIME) );
registerFunction( "current_date", new NoArgSQLFunction("getdate", StandardBasicTypes. DATE) );
registerFunction( "getdate", new NoArgSQLFunction("getdate", StandardBasicTypes. TIMESTAMP) );
registerFunction( "getutcdate", new NoArgSQLFunction("getutcdate", StandardBasicTypes. TIMESTAMP) );
registerFunction( "day", new StandardSQLFunction("day", StandardBasicTypes.INTEGER) );
registerFunction( "month", new StandardSQLFunction("month", StandardBasicTypes.INTEGER) );
registerFunction( "year", new StandardSQLFunction("year", StandardBasicTypes.INTEGER) ) ;
registerFunction( "datename", new StandardSQLFunction("datename", StandardBasicTypes. STRING) );
registerFunction( "abs", new StandardSQLFunction("abs") );
registerFunction( "sign", new StandardSQLFunction("sign", StandardBasicTypes.INTEGER) ) ;
registerFunction( "acos", new StandardSQLFunction("acos", StandardBasicTypes.DOUBLE) );
registerFunction( "asin", new StandardSQLFunction("asin", StandardBasicTypes.DOUBLE) );
registerFunction( "atan", new StandardSQLFunction("atan", StandardBasicTypes.DOUBLE) );
registerFunction( "cos", new StandardSQLFunction("cos", StandardBasicTypes.DOUBLE) );
registerFunction( "cot", new StandardSQLFunction("cot", StandardBasicTypes.DOUBLE) );
registerFunction( "exp", new StandardSQLFunction("exp", StandardBasicTypes.DOUBLE) );
registerFunction( "log", new StandardSQLFunction( "log", StandardBasicTypes.DOUBLE) );
registerFunction( "log10", new StandardSQLFunction("log10", StandardBasicTypes.DOUBLE) );
registerFunction( "sin", new StandardSQLFunction("sin", StandardBasicTypes.DOUBLE) );
registerFunction( "sqrt", new StandardSQLFunction("sqrt", StandardBasicTypes.DOUBLE) );
registerFunction( "tan", new StandardSQLFunction("tan", StandardBasicTypes.DOUBLE) );
registerFunction( "pi", new NoArgSQLFunction("pi", StandardBasicTypes.DOUBLE) );
registerFunction( "square", new StandardSQLFunction("square") );
registerFunction( "rand", new StandardSQLFunction("rand", StandardBasicTypes.FLOAT) );
registerFunction("radians", new StandardSQLFunction("radians", StandardBasicTypes. DOUBLE) );
registerFunction("degrees", new StandardSQLFunction("degrees", StandardBasicTypes. DOUBLE) );
registerFunction( "round", new StandardSQLFunction("round") );
registerFunction( "ceiling", new StandardSQLFunction("ceiling") );
registerFunction( "floor", new StandardSQLFunction("floor") );
registerFunction( "isnull", new StandardSQLFunction("isnull") );
registerFunction( "concat", new VarArgsSQLFunction( StandardBasicTypes.STRING, "( ","+",")" ) );
registerFunction( "length", new StandardSQLFunction( "len", StandardBasicTypes.INTEGER ) );
registerFunction( "trim", new SQLFunctionTemplate( StandardBasicTypes.STRING, "ltrim( rtrim(?1))") );
registerFunction( "locate", new CharIndexFunction() );
getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, NO_BATCH);
}
}
You'll have to add
dialect = org.hibernate.dialect.SqlServer2008Dialect
in the ̀Datasource.groovy` config file and make this class available to your classpath
WARNING : in the blog post, the base version for Hibernate is 3.6.4 and the code above use a SQLServer2005Dialect class that doesn't exist in the 3.3.1 GA version. I've modified the code above to take this into account (I used SQLServerDialect) while assuming that these class were compatibles. I didn't check this.

Related

Emails not being sent via PHPMailer

I took over the web development for a company and they've got a feedback form on a couple pages (colorado, prescott, flagstaff and wyoming) that submits form info to an email as a spam filter using PHPmailer. However, they're not receiving emails the way they hoped. I can get emails through all addAddress emails except for the one using $recipeint. Searched and tried everything I could think of including explode and foreach other codes. Can't get this to work. A little help would be great.
$colorado = array( "ColoradoSprings", "Denver", "Boulder" );
$colorado = array_map( 'strtolower', $colorado );
$wyoming = array( "Cheyenne", "Buffalo" );
$wyoming = array_map( 'strtolower', $wyoming );
if ( in_array( $_POST[ 'location' ], $colorado ) || in_array( $_POST[ 'location' ], $wyoming ) )
$recipient = "person1#mail.com, person2#mail.com";
switch ( $_POST[ 'location' ] ) {
case "prescott":
$recipient = "person3#mail.com, person4#mail.com";
break;
case "flagstaff":
$recipient = "person3#mail.com, person4#mail.com";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.mail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "senderemail#mail.com";
$mail->Password = "password";
$mail->setFrom( 'allmail#mail.com', 'Everyone' );
$mail->addReplyTo( $_POST[ "Email" ], $_POST[ "Name" ] );
$mail->addAddress( 'allmail#mail.com', 'Everyone' );
$mail->addAddress( 'random#mail.com', 'John Doe' );
$mail->addAddress( $recipient, 'Others' );
This is strange code, or incomplete. As it stands it will only attempt to send email if location == 'flagstaff', so you probably need to pay some attention to your conditions.
addAddress only accepts a single address; if you want to send more, call it multiple times in a loop. For example:
$recipients = ["person1#mail.com", "person2#mail.com"];
foreach ($recipients as $recipient) {
$mail->addAddress($recipient);
}

BinarySecurityToken modified with XML escape characters using Apache WSS4J / CXF

I'm using the action based approach to ws-security, as the WSDL I am interfacing to does not contain a security policy. Code for the interceptor and security is below.
I'm getting an error back from the server indicating my binary security token is not base 64 encoded. That was a head scratcher, since it appears to be, and indicates it is in the type description. Then I noticed some characters were being replaced with XML escape characters. If I brute force send a message where these characters are reverted, the server responds, so I suspect they are not gracefully converting these back upon receive.
How do I stop the header from getting escaped?
Here's the code I'm using to configure my client:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean( );
factory.setServiceClass( Operations.class );
factory.setAddress( serviceUrl );
Map< String, Object > properties = Maps.newHashMap( );
properties.put( "mtom-enabled", "false" );
factory.setProperties( properties );
outProps.put( "cryptoProperties", sig_props );
outProps.put( WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE + " " + WSHandlerConstants.ENCRYPT );
outProps.put( WSHandlerConstants.USER, apiKeyPairAlias );
outProps.put( WSHandlerConstants.SIG_PROP_REF_ID, "cryptoProperties" );
outProps.put( WSHandlerConstants.ENC_PROP_REF_ID, "cryptoProperties" );
outProps.put( WSHandlerConstants.SIG_KEY_ID, "DirectReference" );
outProps.put( WSHandlerConstants.ENC_KEY_ID, "DirectReference" );
outProps.put( WSHandlerConstants.SIGNATURE_USER, apiKeyPairAlias );
outProps.put( WSHandlerConstants.ENCRYPTION_USER, apiKeyPairAlias );
outProps.put( WSHandlerConstants.PW_CALLBACK_REF, new ClientPasswordHandler( ) );
outProps.put( WSHandlerConstants.STORE_BYTES_IN_ATTACHMENT, "true" );
outProps.put( WSHandlerConstants.USE_SINGLE_CERTIFICATE, "false" );
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor( outProps );
factory.getOutInterceptors( ).add( wssOut );
Map< String, Object > inProps = Maps.newHashMap( );
inProps.put( WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE + " " + WSHandlerConstants.ENCRYPT );
inProps.put( "cryptoProperties", sig_props );
outProps.put( WSHandlerConstants.SIG_PROP_REF_ID, "cryptoProperties" );
outProps.put( WSHandlerConstants.ENC_PROP_REF_ID, "cryptoProperties" );
outProps.put( WSHandlerConstants.PW_CALLBACK_REF, new ClientPasswordHandler( ) );
WSS4JInInterceptor wssIn = new WSS4JInInterceptor( inProps );
factory.getInInterceptors( ).add( wssIn );

Populating 2D Array with songs and rating

So I am trying to write a 2D array using Java that stores the names of songs and its rating in each of its slots. I have tried doing this with nested for-each loops but it doesn't work. Is there a way to store the songs that works?
public class Jukebox
{
String[][] songList = new String[2][3];
public Jukebox()
{
for ( String[] row : songList)
{
for ( String column : row)
{
songList[0][0] = new Jukebox( "Jet Airliner" );
songList[0][1] = new MySong( "Slide", 4 );
songList[0][2] = new MySong( "Tom Sawyer", 3 );
songList[0][3] = new MySong( "Purple Rain", 2 );
songList[1][0] = new MySong( "Sing a Song", 1 );
songList[1][1] = new MySong( "Baba O'Riley", 5 );
songList[1][2] = new MySong( "Jumper", 4 );
songList[1][3] = new MySong( "Car Wash", 3 );
songList[2][0] = new MySong( "Kung Fu Fighting", 2 );
songList[2][1] = new MySong( "Right as Rain", 4 );
songList[2][2] = new MySong( "Beat It", 5 );
songList[2][3] = new MySong( "Bust a Move", 4 );
}
}
}
}
First, what problem do you need to solve? Wouldn't the use of a HashMap be more desirable? Suppose you have 5 songs:
Map<String, Integer> songs = new HashMap<>();
//populate the map as
songs.put("Slide", 4);
songs.put("Purple rain", 2);
songs.put("Jumper", 3);
//iterate through the map as
for (Map.Entry<String, Integer>entry : songs.entrySet()) {
System.out.println("Song: " + entry.getKey() + " rating: " + entry.getValue());
}
Hope this helps you.

Dapper-Dot-Net Example Code

This is code cut right from the Dapper examples:
var p = new DynamicParameters();
p.Add("#a", 11);
p.Add("#b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("#c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure);
int b = p.Get("#b");
int c = p.Get("#c");
Anyone: In the above code from the example provided, I get an error, "can't resolve .Execute"--referring to cnn.Execute. I look at the connection object and there is no method for Execute. Dapper obviously works well, so what am I doing wrong?
I believe this should get you fixed up:
using( var connection = new SqlConnection( connectionString ) )
{
try
{
var p = new DynamicParameters();
p.Add( "a", 11 );
p.Add( "b", dbType: DbType.Int32, direction: ParameterDirection.Output );
p.Add( "c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue );
connection.Open();
connection.Execute( "MyDamnStoredProc", p, commandType: CommandType.StoredProcedure );
int b = p.Get<int>( "b" );
int c = p.Get<int>( "c" );
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
}
}
Notes:
the params don't need the # symbol; dapper will handle that for you.
be sure to use named parameters; see the updated connection.Execute method that specifies the commandType: parameter. You do this so the optional parameters can be omitted from the method call.
"can't resolve .Execute"
That would be cause your extension method is missing, did you using Dapper; at the top of your file.
See also: http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic3

Qooxdoo form elements and getSelection()

Here's my code:
var sb = new qx.ui.form.SelectBox();
sb.add( new qx.ui.form.ListItem("English") );
sb.add( new qx.ui.form.ListItem("Nederlands") );
sb.add( new qx.ui.form.ListItem("Deutsch") );
sb.add( new qx.ui.form.ListItem("français") );
sb.add( new qx.ui.form.ListItem("Српски") );
How do I use setSelection() to select "Deutsch", and what if the items are numeric values? Can I also set values for these labels or is SelectBox() limited to labels?
For example:
value: en, label: English
value: de, label: Deutsch
etc.
Take a look at the example code below.
You can specify a model with each ListItem for storing additional information. It can act as value property on form items for example. See http://demo.qooxdoo.org/1.0.x/apiviewer/#qx.ui.form.ListItem
var selectBox = new qx.ui.form.SelectBox();
selectBox.add( new qx.ui.form.ListItem("English", null, "en" ));
selectBox.add( new qx.ui.form.ListItem("Nederlands", null, "nl" ));
var defaultItem = new qx.ui.form.ListItem("Deutsch", null, "de" );
selectBox.add(defaultItem );
selectBox.add( new qx.ui.form.ListItem("français", null, "fr"));
selectBox.add( new qx.ui.form.ListItem("Српски", null, "ru"));
selectBox.setSelection([defaultItem]);
selectBox.addListener("changeSelection", function(e) {
//Read model data from listitem
this.debug("changeSelection: " + e.getData()[0].getModel());
});
Maybe this example will be useful for you too:
var sb = new qx.ui.form.SelectBox();
var a = ["English", "Nederlands", "Deutsch", "Français", "Српски"];
var model = new qx.data.Array(a);
var controller = new qx.data.controller.List(model, sb);
controller.setSelection(model.slice(0,3));
At last line model.slice(0,3) returns subarray of model with three elements: from "English" to "Deutsch". And last element in this subarray will be "selected" by default.
See "Data Binding" in qooxdoo's manual for details.

Resources