Skip to main content

Posts

Showing posts from September, 2010

Gmail or Google Apps as Liferay SMTP Server

Good thing of this approach is you don't need restart compare to setting inside ROOT.xml or portal-ext.properties Go to  Control Panel -->  Server Administration --> Mail  and enter these values  Note : If you don't see emails coming, enable your pop of your gmail account.

JPA Performance settings Explained

Lets consider this simple Entity, Ignore @SecondaryTables and other annotations if you are not familiar with just focus on @Basic, @OneToOne, @OneToMany annotations and get ready to answer these questions. @Entity @SecondaryTables({     @SecondaryTable(name="Employee_Comments") }) public class Employee implements Serializable {     private static final long serialVersionUID = 1L;     @Id     private Long id;     @Column(name = "name")     private String name;     @Basic(fetch = FetchType.LAZY)     @Column(name = "comments", table = "Employee_Comments")     private String comments;     @OneToMany     private List tags = new ArrayList ();     @OneToOne(fetch=FetchType.LAZY)     private Address address; .. } What happens when we load the above entity using em.find or by exe...

Reading RSS Feed in Java (3 llines of code) Explained

Rome is a very simple Java API for reading RSS Feeds and is available under Apache License 2.0 Rome Home Page https://rome.dev.java.net/ If you are a maven user just add this dependency         <dependency>             <groupId>rome</groupId>             <artifactId>rome</artifactId>             <version>1.0RC2</version>         </dependency> if you are using ant   1. Add Rome jar from https://rome.dev.java.net/dist/rome-1.0.jar   2. Add JDOM jar from http://www.jdom.org/dist/binary/archive/jdom-1.0.zip And here is the code to pull feeds,  import java.net.URL; import java.util.List; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFe...

JPA 2 new feature @ElementCollection explained

@ElementCollection is new annotation introduced in JPA 2.0, This will help us get rid of One-Many and Many-One shitty syntax. Example 1: Stores list of Strings in an Entity @Entity public class Users implements Serializable {     private static final long serialVersionUID = 1L;     @Id     @GeneratedValue(strategy = GenerationType.AUTO)     private Long id;     @ElementCollection     private List<String> certifications = new ArrayList <String> ();     public Long getId() {         return id;     }     public void setId(Long id) {         this.id = id;     }     public List <String> getCertifications() {         return certifications;     }     pub...

Liferay DWR Session and Security made easy

In my previous article I discussed about using DWR inside Liferay Portlets, I wasn't happy with security this model provides. First lets have a look at what I proposed ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);         portletRequest.getPortletSession().setAttribute("THEME_DISPLAY", themeDisplay, PortletSession.APPLICATION_SCOPE); I was executing the above code inside my portlet's doView() and this puts themeDisplay or remoteUser object into portlet session's application scope, that means my DWR classes access the object like this     @RemoteMethod     public String echo(String msg, HttpSession session) {         ThemeDisplay themeDisplay = (ThemeDisplay)  session.getAttribute("THEME_DISPLAY");     } The problems what I see in the above technique is User can access DWR methods even after his portlet sess...

JPA 2.0 enums with custom values

Let's consider we have this enum public enum EmpType { FULL_TIME("e1"), PART_TIME("e2"); private String val; private EmpType(String val) { this.val = val; } @Override public String toString() { return val; } } And the entity @Entity @NamedQueries({ @NamedQuery(name="Employee.findByEmployeeType", query="SELECT e FROM Employee e WHERE e.employeeType = ?1") }) @Access(AccessType.FIELD) public class Employee implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Transient private EmpType empType; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public EmpType getEmpType() { return empType; } public void setEmpType(EmpType empType) { this.empType = empType; } @Access(AccessType.PROPERTY) @Column(name="employee_type") protected String getEmploy...

JPA Compound Primary Keys Explained

The whole purpose of this article is to explain JPA Compound Primary Keys in a simple way. Some Entities needs to have a primary key based on more than one field (eg Legacy DB), so we don't have much choice here. JPA supports two ways of configuring compound primary keys however we are going to focus on the one which I feel makes Entities and Queries easy to read and understand. Lets take an example of a PhoneDirectory Entity which stores Phone No, Ext and Employee name, many employees can have same phone no but with different ext so our candidate for primary key is Phone No and Ext. We need to follow just couple of steps write this entity Step 1: 1. Create PhoneId class as show below 2. This class should have equals(), hashcode() and should implement Serializable 3. And should be annotated with @Embeddable import java.io.Serializable; import javax.persistence.Embeddable; /** * * @author intesar */ @Embeddable public class PhoneId implements Serializab...

Why Java Web Application UI lags ?

I've worked on more than 2 Dozen Java Web Applications in different companies and with various teams and every time I felt these applications lagged in UI compare to PHP applications. Don't get me wrong I'm trying to understand and find solution to problems what I'm seeing and I could easily learn from your exposure and experience or may be we all can understand whats happening. Though most of the teams I've worked with were extremely talented and we wrote huge large scale applications and in fact we also had UI experts on these projects, then why couldn't we write great UI? I feel there are two reason's for this firstly these teams are dominated by Java guys who mostly like working on server side code and they really don't enjoy working on UI and they spend enormousness time fixing performance, and other server side issues which hardly are issues for some project for an example in one of my project we spend weeks trying to fix application performance...

JPA Lifecycle Callback Events

Typical Entity Life-cycle Persist   - Creating Entity for the first time. Merge   - Updating detached Entity Load     - Loading Entity from Database Delete   - Deleting Entity from Database JPA provides total of 6 Annotations for Life-Cycle Callback events. @PrePersist @PostPersist @PreUpdate @PostUpdate @PreRemove @PostLoad Following two ways to utilize the above annotations. Case 1 : Defining inside Entity class Any method can be annotated with one or all annotations, though they should follow these rules.  Method can have any name  No parameters  Return type of void  Method should not be final or static  No Checked exceptions should be thrown  Any annotation can be only used on one method Example Code Snippet - 1 @Entity public class Employee implements Serializable { ... @PrePersist @PostPersist @PreUpdate @PostUpdate @PreRemove @PostLoad public void updateDate() {    ...

JPA and Enums

There are different ways of configuring enums inside JPA entity which yields different results. Let's consider we have this enum public enum EmpType { FULL_TIME, PART_TIME; } And the entity @Entity public class Employee implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String name; @Enumerated private EmpType empType; .. } To use enum we have to annotate field with @Enumerated Employee e = new Employee(); e.setId(1L); e.setName("na"); e.setEmpType(EmpType.FULL_TIME); em.persist(e); the above code will save entity as follows ID EMPTYPE NAME 1 0 na This is because all the enum's in EmpType are indexed starting from zero, so if we change order or introduce a new enum in middle we lose the context in Employee table. Instead use this @Enumerated(EnumType.STRING) which save the above entity as ID EMPTYPE ...