Tuesday, April 28, 2009

DAO Links

DAO (Data Access Object) from sun

p4j very useful link

DataAccessObject

The DataAccessObject is the primary object of this pattern. The DataAccessObject abstracts the underlying data access implementation for the BusinessObject to enable transparent access to the data source. The BusinessObject also delegates data load and store operations to the DataAccessObject.

DataSource

This represents a data source implementation. A data source could be a database such as an RDBMS, OODBMS, XML repository, flat file system, and so forth. A data source can also be another system (legacy/mainframe), service (B2B service or credit card bureau), or some kind of repository (LDAP).

TransferObject

This represents a Transfer Object used as a data carrier. The DataAccessObject may use a Transfer Object to return data to the client. The DataAccessObject may also receive the data from the client in a Transfer Object to update the data in the data source.


Here we go for some useful links....

Issues in calling DAO from Sateless session bean

From JavaWorld

Core J2EE Patterns - Session Facade


Don't repeat the DAO!

jpa-ejb killed dao

JPA/EJB3 killed the DAO

Regarding to the abstract of the DAO-Pattern: "Access to data varies depending on the source of the data. Access to persistent storage, such as to a database, varies greatly depending on the type of storage (relational databases, object-oriented databases, flat files, and so forth) and the vendor implementation." the DAO tries to decouple the business logic from the proprietary resource. The solution to the problem is the following: "...The DataAccessObject is the primary object of this pattern. The DataAccessObject abstracts the underlying data access implementation for the BusinessObject to enable transparent access to the data source. The BusinessObject also delegates data load and store operations to the DataAccessObject..." [Core J2EE Patterns].

In the practise the DAO-Pattern was often realized by the following items:

  • DAO-Interface (provided a datasource-neutral interface)
  • DAO-Implementation (=access to the datasource implementation)
  • DAO-Factory (the creation of the implementation)
  • Optional: ServiceLocator (location of resources in JNDI)
The main problem in J2EE was the insufficient power of CMP 2.0. The limited query capabilities, no access to native SQL and lack of dynamic queries forced the developers to access the database using plain-JDBC. It was a very good idea to encapsulate the database access behind a replaceable and mockable implementation.
In EJB 3/Java EE 5 environment there is no need to use the low level JDBC to access the database any more. Actually you can use generic, but powerful Query Lanaguae, as well as Native SQL to fetch not only the persistent objects, but also data transfer objects and even primitive data types as well. It is even possible to execute update and delete statements. The JPA comes already with the EntityManager which provides already generic data access functionality. The usage cannot be simpler. The EntityManager will be just injected to the bean-class:

@Stateless
public class CustomerMgrBean implements CustomerMgr{

@PersistenceContext
private EntityManager em;

It's just one liner. The DAO pattern is actually no more interesting for general data access, but is still needed to access data from stored procedures, flat files etc. However the bean above can be considered as a "DAO", but very streamlined one...
You will find some examples in p4j5 - feel free to participate.



Faking css position: fixed in Internet Explorer 6

Useful link for IE6 css hack

Try at last

Another How to fix div position using javascript in IE 6.0

This is a trick that I have been doing for some time, but now that Internet Explorer 7 is out and it supports position fixed in css; I think this trick will been even more useful now that it will allow web developers to take better advantage of css and at the same time not leave out the Internet Explorer 6 user base. I am posting this here because I sent this to a friend a couple days ago and thought others might be able to use it as well.



The trick is really quite basic. For simplicity I am just putting this example inline, but you can put it where you want.


First your declare your intended style for IE7 (and basically every other browser):



<style type="text/css">
#fixed_div {
position: fixed;
top: 0px; /* tweak this according to placement */
left: 0px; /* tweak this according to placement */
/* add additional styling, etc. */
}
</style>

Second add in your Javascript function which is going to be doing your movement for IE6:



<script type="text/javascript">

function move_box() {
var offset = 0; // set offset (likely equal to your css top)
var element = document.getElementById('fixed_div');

element.style.top = (document.documentElement.scrollTop + offset) + 'px';
}
</script>

Then declare the element itself:



<div id="fixed_div">I am fixed, even in IE6</div>

Lastly, implement your fix for IE6 only:



<!--[if lt IE 7]>

<style type="text/css">
#fixed_div {
position: absolute;
top: 0px; /* tweak this according to placement */
left: 0px; /* tweak this according to placement */
}
</style>
<script type="text/javascript">
window.setInterval(move_box, 100);
</script>
<![endif]-->

I usually put this last part right before the end of the body (I guess you could just put the whole script there). This is because you don't want the window.setInterval to fire before your fixed div is declared. You could use the body onload but I try to stay away from that because I know it is popular for other uses on more advanced pages and I don't want to run the risk of interfering.


Here is the whole script put together:




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
. . .
<style type="text/css">
#fixed_div {
position: fixed;
top: 0px; /* tweak this according to placement */
left: 0px; /* tweak this according to placement */
/* add additional styling, etc. */
}
</style>
<script type="text/javascript">

function move_box() {
var offset = 0; // set offset (likely equal to your css top)
var element = document.getElementById('fixed_div');

element.style.top = (document.documentElement.scrollTop + offset) + 'px';
}
</script>
. . .
</head>
<body>
. . .
<div id="fixed_div">I am fixed, even in IE6</div>
. . .

<!--[if lt IE 7]>
<style type="text/css">
#fixed_div {
position: absolute;
top: 0px; /* tweak this according to placement */
left: 0px; /* tweak this according to placement */
}
</style>
<script type="text/javascript">
window.setInterval(move_box, 100);
</script>
<![endif]-->
</body>

</html>

NOTE: This will only work properly on pages that have a valid doctype definition. If you don't use one [you should start or] you need to change document.documentElement.scrollTop to document.body.scrollTop in the move_box function.


You can also modify this script to apply to multiple elements by passing arguments to the function or something similar. If you are doing multiple elements on a single page, you may want to consider setting up some kind of Javascript array that tracks what elements need to be corrected, this could save you from having multiple window.setInterval instances running.

http://www.finefrog.com/_code/59.html

http://www.howtocreate.co.uk/fixedPosition.html

Wednesday, April 22, 2009

Fluid Layout Template - Fixed Buttons Div

Reference : http://www.howtocreate.co.uk/fixedPosition.html

http://en.allexperts.com/q/Javascript-1520/position-fixed.htm

http://www.jtricks.com/javascript/navigation/fixed_menu.html

Put in head tag: 

Within body tag:





Deprecated



This fix is deprecated and is no longer supported (meaning that I will not help you to get it working with your pages). It was only written to fill the gap while waiting for Internet Explorer to implement fixed positioning. That has now happened in IE 7. Although this fix can make it work in IE 6 as well, that is counter productive for the future of the Web. IE 6 is a major problem to Web developers (IE 7 is a problem as well, but for now, let's overlook that, since it does at least implement fixed positioning), and the sooner it stops being used, the better.



Instead of using this hack, users of IE 6 should be encouraged to upgrade to IE 7. Users who cannot upgrade to IE 7 (because IE 7 is not being released for most Windows operating systems) should use a better browser, such as Opera or Firefox. They have been abandoned by Microsoft.



What is position: fixed;?


position: fixed; is an alternative to position: absolute; position: relative; and position: static;.


position: fixed; is basically the same as position: absolute; except that when the user scrolls the page, the element
does not scroll with it, it just says exactly where it was. There are many pages that want to use this in order to position
logos or menus.


What is wrong with position: fixed;?


Well, ... nothing. The problem is that the most popular browser - Internet Explorer for Windows - does not understand
it, and instead of reverting to position: absolute; which would be better than nothing, it reverts to position: static; as
specified by the CSS standard. This has the same effect as having no positioning at all. Note that IE 7 from beta 2 upwards
does support position: fixed; (if you use a document type declaration that triggers strict mode) so I will exclude IE 7 from this fix.


As a result, serveral people write scripts that use setInterval to reposition an absolutely positioned element every few
miliseconds, or (ignoring Netscape 4) when the onscroll event is detected. This produces a slightly jerky effect. It would
be better if the position: fixed; style could be applied in browsers that supported it, and browsers that didn't could use
position: absolute; and JavaScript. Some authors use the > CSS selector to isolate Internet Explorer and leave the element
positioned absolutely in that browser, without the scrolling effect.


div#fixme { position: absolute; left: 0px; top: 0px; }
body > div#fixme { position: fixed; }

It produces a reasonably nice effect, but it is even better when coupled with a JavaScript that checks if the position is
'absolute' using the currentStyle property, and then repositioning the element when the onscroll event is detected.




F
i
x

M
e




F
i
x

M
e

T
o
o








Friday, April 17, 2009

Send mail using javamail and gmail

This is my fully tested code. You need javamail 1.4 api to run this. If u find any kind of exception check ur system firewall that is blocking to send mail.

Add mail.jar and activation.jar in ur buildpath and run the code:


import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SimpleSSLMail {

  private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  private static final int SMTP_HOST_PORT = 465;
  private static final String SMTP_AUTH_USER = "xxxxx@gmail.com";
  private static final String SMTP_AUTH_PWD = "xxxx";

  public static void main(String[] args) throws Exception{
  new SimpleSSLMail().test();
  }

  public void test() throws Exception{
  Properties props = new Properties();

  props.put("mail.transport.protocol", "smtps");
  props.put("mail.smtps.host", SMTP_HOST_NAME);
  props.put("mail.smtps.auth", "true");
  // props.put("mail.smtps.quitwait", "false");

  Session mailSession = Session.getDefaultInstance(props);
   
  mailSession.setDebug(true);
   
  Transport transport = mailSession.getTransport();

  MimeMessage message = new MimeMessage(mailSession);
  message.setSubject("Hello Attatched world");
   
  //message.setContent("This is a test", "text/plain");
   
  // content
  MimeBodyPart mimeBodyPart = new MimeBodyPart();
   
  //mimeBodyPart.setText("Link goes here:");  
  String strLink = " Link goes here : http://www.google.com";
  mimeBodyPart.setText(strLink);
   
  // multipart is the main content holder
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(mimeBodyPart);
   
  // Part two is attachment
  File f = new File("F:\\angel.JPG");
  if(f.exists() && f.isFile())
  {
  System.out.println("attatching file......."+f.getName());
  mimeBodyPart = new MimeBodyPart();
  DataSource source = new FileDataSource(f);  
  DataHandler handler = new DataHandler(source);
  mimeBodyPart.setDataHandler(handler);
   
  mimeBodyPart.setFileName(f.getName());
   
  multipart.addBodyPart(mimeBodyPart);
   
  }
   
  // add multipart data to message
  message.setContent(multipart);
   
   
  message.addRecipient(Message.RecipientType.TO,
  new InternetAddress("xxxx@yahoo.com"));

  transport.connect
  (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

   
  transport.sendMessage(message,
  message.getRecipients(Message.RecipientType.TO));
  transport.close();
  }
}