Saturday, July 25, 2009
Sunday, March 22, 2009
Infamous Error 150 while creating a table in MySQL
I am working on a data migration and defining the schema. The application will eventually be implemented in Ruby on Rails, but instead of migrations, the client needed the sql dump. My CREATE TABLE statement was failing with errno 150, and SHOW ENGINE INNODB STATUS was not helpful. The Foreign Key section kept telling me :
Cannot resolve table name close to:
(id)) engine=innodb
I was completely zapped, both the related tables were INNODB tables, the columns were same type and size, indices were defined correctly. It was after sometime that I realized that the syntax for creating the foreign key was wrong. I was saying:
FOREIGN KEY (parent_id) REFERENCES parent.id
But it should be:
FOREIGN KEY (parent_id) REFERENCES parent(id)
Cannot resolve table name close to:
(id)) engine=innodb
I was completely zapped, both the related tables were INNODB tables, the columns were same type and size, indices were defined correctly. It was after sometime that I realized that the syntax for creating the foreign key was wrong. I was saying:
FOREIGN KEY (parent_id) REFERENCES parent.id
But it should be:
FOREIGN KEY (parent_id) REFERENCES parent(id)
Sunday, August 31, 2008
YouTube API does not work through Resin
I have run into a strange issue with the youtube api. I am able to upload videos through the browser and verify them in my youtube acocunt. However, when I try to retrieve the videos, I do not get them. I am able to retrieve correctly through a junit test case.
After a lot of digging on the forum, I found this post:
Different behaviour under Tomcat and Resin
The client api documentation does not suggest any supported environments or versions. GData Doc
After a lot of digging on the forum, I found this post:
Different behaviour under Tomcat and Resin
The client api documentation does not suggest any supported environments or versions. GData Doc
Tuesday, February 26, 2008
Sunday, October 14, 2007
JSF immediate attribute
Usage of JSF immediate attribute can lead to a variety of issues, so it is a handle with care piece will one learns the JSF life cycle, different phases and the bypassing through immediate. For more, check:
ClearInputComponents - Myfaces Wiki
ClearInputComponents - Myfaces Wiki
Tuesday, October 09, 2007
Enterprise Java Community: Building Custom JSF UI Components
A brilliant article on JSF : Enterprise Java Community: Building Custom JSF UI Components
Fixing Required messages in JSF
Another interesting phase listener for required messages Fixing Required messages in JSF
JSF 1.2 components already support requiredMessage attribute, so this may not be handy there.
Customizing JSF Required Field Messages per Component Instance is another flavour of the same.
JSF 1.2 components already support requiredMessage attribute, so this may not be handy there.
Customizing JSF Required Field Messages per Component Instance is another flavour of the same.
Designing and Implementing Web Application Interfaces
Wonderful use of phase listeners in JSF : Designing and Implementing Web Application Interfaces
It outlines how JSF can be enhanced to define custom error messages per component. Mst read for all those who are on JSF 1.1
It outlines how JSF can be enhanced to define custom error messages per component. Mst read for all those who are on JSF 1.1
Friday, October 05, 2007
JSF dropdown with BigInteger keys
We wanted to create a drop down with JSF which had keys as java.math.BigInteger and labels as strings. The drop down was to be pre selected with a value. If everything is string, string, thinds work well. However, the drop down value is not prepopulated if it is bound to a BigInteger. Eventually, we were able to solve this with the following code:
JSP
Managed Bean
DTO
JSP
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<%@ page contentType=\"text/html;charset=windows-1252\"%>
<%@ taglib uri=\"http://java.sun.com/jsf/html\" prefix=\"h\"%>
<%@ taglib uri=\"http://java.sun.com/jsf/core\" prefix=\"f\"%>
<f:view>
<html>
<head>
<meta http-equiv=\"Content-Type\"
content=\"text/html; charset=windows-1252\"/>
<title>ConversionTrial</title>
</head>
<body><h:form>
<h:selectOneMenu id=\"menu\" value=\"#{ConversionBean.dto.code}\">
<f:selectItems value=\"#{ConversionBean.valueList}\"/>
</h:selectOneMenu>
</h:form></body>
</html>
</f:view>
Managed Bean
package view;
import java.math.BigInteger;
import java.util.ArrayList;
import javax.faces.model.SelectItem;
public class ConversionBean {
private DTO dto;
private ArrayList valueList;
public ConversionBean() {
dto = new DTO(new BigInteger(\"10\"), \"Large\");
}
public ArrayList getValueList(){
valueList = new ArrayList();
valueList.add(new SelectItem(new BigInteger(\"0\"), \"Small\"));
valueList.add(new SelectItem(new BigInteger(\"5\"), \"Medium\"));
valueList.add(new SelectItem(new BigInteger(\"10\"), \"Large\"));
return valueList;
}
public DTO getDto(){
return this.dto;
}
}
DTO
package view;
import java.math.BigInteger;
public class DTO {
private BigInteger code;
private String val;
public DTO(BigInteger code, String val) {
this.code = code;
this.val = val;
}
public void setCode(BigInteger s){
System.out.println(\"setCode to \" + s);
this.code = s;
}
public BigInteger getCode(){
System.out.println(\"getCode returns \" + code);
return this.code;
}
}
Monday, September 24, 2007
Hackers and Painters
Something I have always believed in, but never had the right words to express. Brilliant description of the creativity and artistry involved in the making of software: Hackers and Painters
Labels: Technical
Thursday, September 20, 2007
Surprising tip
I was taken aback by the tip A Very Simple Way to Skip Part of Code Without Using Comment Symbols duly linked from http://java.sun.com. It is a surprise way of inserting return statements in the middle of the code to test only the functionality upto the point of return.
Commenting out code and testing, or creating appropriate test harnesses so that all cases can be covered is surprisingly not advocated. Nor is the need for modular methods and further breakdown of code logically.
What is next, Goto ?
Commenting out code and testing, or creating appropriate test harnesses so that all cases can be covered is surprisingly not advocated. Nor is the need for modular methods and further breakdown of code logically.
What is next, Goto ?
Tuesday, September 18, 2007
Sortable table using JSF and AJAX4JSF
I have written about AJAX and JSF in my previous post. Let us now look at a way to implement sortable tables using JSF and AJAX. We want to build a table which has the header as links. If a particular link is clicked, the table entries are sorted based on the header clicked. We want the entire functionality to be AJAX based, so that only part of the page is refreshed.
A normal JSF table looks like this:
The backing bean Bean.java has a method
We modify the table header from plain text to links which fire AJAX requests, using Ajax4JSF. We want the table body to be refreshed when the header is pressed, so we specify that in the reRender property of the commandLink.
We now add a Comparator to the back end code, which can help to sort the ArrayList of data to be presented in the table. The Comparator has different sorting criteria. New methods
A normal JSF table looks like this:
<h:datatable id="addressTableHeader" border="0" cellpadding="0" cellspacing="0">
<h:column>
<f:facet name="header">
<h:outputtext value="Post Code">
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputtext value="Street">
</f:facet>
</h:column>
</h:dataTable>
<h:datatable id="addressTableBody" var="tableData" value="#{Bean.addressList}">
<h:column>
<h:outputtext value="#{tabledata.postcode}">
</h:column>
<h:column>
<h:outputtext value="#{tabledata.street}">
</h:column>
</h:dataTable>
The backing bean Bean.java has a method
getAddressList()which wraps an ArrayList inside a ListDataModel and returns it.
We modify the table header from plain text to links which fire AJAX requests, using Ajax4JSF. We want the table body to be refreshed when the header is pressed, so we specify that in the reRender property of the commandLink.
<h:datatable id="addressTableHeader" border="0" cellpadding="0" cellspacing="0">
<h:column>
<f:facet name="header">
<a4j:commandlink action="#{Bean.postcodeSort}" ajaxsingle="true" rerender="addressTableBody" immediate="true">
<h:outputtext value="Post Code" styleclass="tariffpopupsearchheading">
</a4j:commandLink>
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<a4j:commandlink action="#{Bean.streetSort}" ajaxsingle="true" rerender="addressTableBody" immediate="true">
<h:outputtext value="Street" styleclass="tariffpopupsearchheading">
</a4j:commandLink>
</f:facet>
</h:column>
</h:dataTable>
We now add a Comparator to the back end code, which can help to sort the ArrayList of data to be presented in the table. The Comparator has different sorting criteria. New methods
postcodeSort()and
streetSort()are added to the Bean, which help to set the sorting criteria on click . The Bean's
getAddresslist()method is modified to pass the appropriate Comparator.
Wednesday, September 12, 2007
JSF and AJAX
The Ajax4jsf Framework is a very interesting and fruitful way of adding AJAX support to the JSF applications. I do not know much AJAX, but I was able to add AJAX support to my application easily.
An interesting link for re-rendering input text boxes. can be found here. I struggled a lot with this concept, but the link helped make things very clear.
An interesting link for re-rendering input text boxes. can be found here. I struggled a lot with this concept, but the link helped make things very clear.
Thursday, August 30, 2007
Embedded Tomcat
My previous article on Tweaking Tomcat did not mention the use of Embedded Tomcat, which was necessary to run Tomcat programmatically in the same JVM. More information on this feature can be found in the following links:
http://www.vsj.co.uk/articles/display.asp?id=319
http://www.onjava.com/pub/a/onjava/2002/04/03/tomcat.html
http://www.vsj.co.uk/articles/display.asp?id=319
http://www.onjava.com/pub/a/onjava/2002/04/03/tomcat.html
Wednesday, August 08, 2007
Including one JSF page in another
It seems there is some confusion on how to include one JSF page within another. Here are the steps to include inner.jsp into outer.jsp
1. In outer.jsp, include inner.jsp as a simple JSP include.
<%@ include file="inner.jsp" %>
2. In inner.jsp, include the JSF tag libraries
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
3. In inner.jsp, create the subview at the beginning of the page:
4. At the end of inner.jsp, close the subview tag
1. In outer.jsp, include inner.jsp as a simple JSP include.
<%@ include file="inner.jsp" %>
2. In inner.jsp, include the JSF tag libraries
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
3. In inner.jsp, create the subview at the beginning of the page:
4. At the end of inner.jsp, close the subview tag
Monday, August 14, 2006
FLWOR with Berkeley DB XML
FLWOR (pronounced Flower) stands for "for let while order by return" and is and XQuery expression. FLWOR enables advanced querying. More details about
FLWOR can be found at http://www.w3schools.com/xquery/xquery_flwor.asp
Sleepycat's Berkeley's XML Database, DB XML the native XML database supports XQuery. I have tried to create a small example here to demonstrate how to read XQuery with FLWOR expressions from a file, and run them against the database of Berkeley DBs dbxml container.(the one built in the 'gettingStarted' section) It is assumed that the xml sample data from the getting started kit has been loaded.
Set your classpath to include the Berkley DB jars under jar folder
(db.jar, dbxml.jar, dbexamples.jar)
The code can be found here.
1. query.txt file, which holds the query.
2. FLWOR.java file, which holds the code.
I am assuming you have already run the examples. So after that, please compile and
say the following at the command line at test folder:
java FLWOR -h -q query.txt
FLWOR can be found at http://www.w3schools.com/xquery/xquery_flwor.asp
Sleepycat's Berkeley's XML Database, DB XML the native XML database supports XQuery. I have tried to create a small example here to demonstrate how to read XQuery with FLWOR expressions from a file, and run them against the database of Berkeley DBs dbxml container.(the one built in the 'gettingStarted' section) It is assumed that the xml sample data from the getting started kit has been loaded.
Set your classpath to include the Berkley DB jars under jar folder
(db.jar, dbxml.jar, dbexamples.jar)
The code can be found here.
1. query.txt file, which holds the query.
for $doc in collection(\'simpleExampleData.dbxml\')
where $doc/product/item[text()=\"Lemon Grass\"]
return $doc/product
2. FLWOR.java file, which holds the code.
import java.io.*;
import com.sleepycat.dbxml.*;
import com.sleepycat.db.*;
import dbxml.gettingStarted.*;
public class FLWOR {
private static void usage() {
String usageMessage = \"\\nThis program performs queries against a DBXML container.\\n\";
usageMessage += \"You should run exampleLoadContainer before running this example.\\n\";
usageMessage += \"You are only required to pass this command the path location of the database\\n\";
usageMessage += \"environment that you specified when you loaded the examples data:\\n\";
usageMessage += \" and the query file which holds the query\\n\";
usageMessage += \"\\t-h <dbenv directory> -q <queryFile>\\n\";
usageMessage += \"For example:\\n\";
usageMessage += \"\\tjava ch.inform.bdb.FLWOR -h examplesEnvironment -q query.txt\\n\";
System.out.println(usageMessage);
System.exit( -1 );
}
//Utility function to clean up objects, exceptions or not,
// containers and environments must be closed.
private static void cleanup(myDbEnv env, XmlContainer openedContainer) {
try {
if (openedContainer != null)
openedContainer.close();
if (env != null)
env.cleanup();
} catch (Exception e) {
// ignore exceptions on close
}
}
public static void main(String args[])
throws Throwable {
String theContainer = null;
File path2DbEnv = null;
File queryFile = null;
for(int i = 0; i < args.length; ++i) {
if (args[i].startsWith(\"-\")) {
switch(args[i].charAt(1)) {
case \'h\':
path2DbEnv = new File(args[++i]);
break;
case \'q\':
queryFile = new File(args[++i]);
break;
default:
usage();
}//switch
}//if
}//for
if (path2DbEnv == null || ! path2DbEnv.isDirectory()) {
usage();
}
if (queryFile == null || queryFile.isDirectory()) {
System.out.println(\"queryFile is \" + queryFile);
usage();
}
//Stream to read file
BufferedReader fin;
String query = \"\";
try
{
fin =
new BufferedReader(new FileReader(queryFile));
String line;
while ((line = fin.readLine()) != null ) {
query += \"\\n\" + line;
}
// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println (\"Unable to read from file\");
System.exit(-1);
}
System.out.println(\"Query is \" + query);
myDbEnv env = null;
XmlTransaction txn = null;
XmlContainer openedContainer = null;
try {
env = new myDbEnv(path2DbEnv);
XmlManager theMgr = env.getManager();
String containerMark = \"\\\'\";
theContainer = query.substring(
query.indexOf(containerMark)+1,
query.lastIndexOf(containerMark));
System.out.println(\"Container is \" + theContainer);
//Open a non-transactional container
openedContainer =
theMgr.openContainer(theContainer);
XmlQueryContext resultsContext = theMgr.createQueryContext();
XmlResults results = theMgr.query(query.trim(),
resultsContext);
XmlValue value = results.next();
while (value != null) {
//Pull the value out of the document query result set.
System.out.println(value.asString());
}
} catch (Exception e) {
System.err.println(\"Error performing query against \" + theContainer);
System.err.println(\" Message: \" + e.getMessage());
throw e;
}
finally {
cleanup(env, openedContainer);
}
} //End main
}
I am assuming you have already run the examples. So after that, please compile and
say the following at the command line at test folder:
java FLWOR -h
Labels: Berkeley DB, Java, Technical