SBI Simple Code Example

Friday, April 2, 2010 Posted by Sanka Senevirathna
web-application-config.xml















































/WEB-INF/classes/META-INF/persistence.xml













web-security-config.xml
































flex-servlet.xml




















/*=_messageBroker

















web.xml






SBI template Configured by Sanka
SBI template Configured by Sanka




contextConfigLocation
/WEB-INF/config/*-config.xml


springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy



springSecurityFilterChain
/*





flex.messaging.HttpFlexSession



org.springframework.web.context.ContextLoaderListener



flex
org.springframework.web.servlet.DispatcherServlet
1





flex
/messagebroker/*




bin-debug/testApp.html





jdbc/catalogueDB
javax.sql.DataSource
Container








context.xml






















persistence.xml




com.abccompany.domain.Book







Services


package com.abccompany.service;

import java.util.List;

import com.abccompany.domain.Book;

public interface BookService {
List getListBooks();
}


////////


package com.abccompany.service;
import java.util.List;
import javax.annotation.Resource;
import com.abccompany.dao.BookDao;
import com.abccompany.domain.Book;

public class BookServiceImpl implements BookService {

@Resource(name="jpaBookDao")
BookDao bookDao;

@Override
public List getListBooks() {
return bookDao.getListBooks();
}

}





DAOs

package com.abccompany.dao;
import java.util.List;
import com.abccompany.domain.Book;

public interface BookDao {
List getListBooks();
}


////////////////////

package com.abccompany.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.abccompany.domain.Book;
@Repository
public class JpaBookDao implements BookDao {

@PersistenceContext
EntityManager em;

@Transactional(readOnly=true)
@Override
public List getListBooks() {
return(em.createNamedQuery("listBooks").getResultList());
}

}





Domain Objects(JPA Enities / SQL Tables)

package com.abccompany.domain;

import java.io.Serializable;
import java.lang.Float;
import java.lang.Long;
import java.lang.String;
import javax.persistence.*;

/**
* Entity implementation class for Entity: Book
*
*/
@Entity
@NamedQuery(name="listBooks", query="SELECT b FROM Book b")
public class Book implements Serializable {


@Id
@Column(name="Id")
private Long bookId;
private String title;
private Float price;
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
private static final long serialVersionUID = 1L;

public Book() {
super();
}
public Long getId() {
return this.bookId;
}
public void setId(Long bookId) {
this.bookId = bookId;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Float getPrice() {
return this.price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Integer getNbOfPage() {
return nbOfPage;
}
public void setNbOfPage(Integer nbOfPage) {
this.nbOfPage = nbOfPage;
}
public Boolean getIllustrations() {
return illustrations;
}
public void setIllustrations(Boolean illustrations) {
this.illustrations = illustrations;
}

}





Exception Translaters


package com.abccompany.flex.exception;

import org.springframework.flex.core.ExceptionTranslator;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.util.ClassUtils;

import flex.messaging.MessageException;

public class AccessException implements ExceptionTranslator {

//AccessDeniedException cannot use because BlazeDS handle it.
//JRException like exceptions can be handle.What have to do is throws new JRException
@Override
public boolean handles(Class clazz) {
return (ClassUtils.isAssignable(AccessDeniedException.class,clazz));
}

@Override
public MessageException translate(Throwable th) {
MessageException ex =new MessageException();
ex.setCode("Application.Service");
ex.setMessage("Access Denied");
ex.setRootCause(th);
return null;
}

}


////////////////

package com.abccompany.flex.exception;
import org.springframework.flex.core.ExceptionTranslator;
import org.springframework.util.ClassUtils;
import flex.messaging.MessageException;

public class GenaricNullPointerException implements ExceptionTranslator {

@Override
public boolean handles(Class clazz) {
return (ClassUtils.isAssignable(NullPointerException.class, clazz));
}

@Override
public MessageException translate(Throwable th) {
MessageException ex =new MessageException();
ex.setCode("Application.Service");
ex.setMessage("Empty Feild");
ex.setRootCause(th);
return ex;
}
}



/////////////////////



package com.abccompany.flex.exception;

import java.io.IOException;

import org.springframework.flex.core.ExceptionTranslator;
import org.springframework.util.ClassUtils;

import flex.messaging.MessageException;
import flex.messaging.messages.ErrorMessage;

public class GlobalGenericException implements ExceptionTranslator {

@Override
public boolean handles(Class clazz) {
return (ClassUtils.isAssignable(IOException.class, clazz));
}

@Override
public MessageException translate(Throwable th) {
MessageException ex =new MessageException();
ex.setCode("Application.Service");
ex.setMessage("Empty field found");
ex.setRootCause(th);
System.out.println("---------sanka's MessageException GlobalGenericException----------");
return ex;
}

}




Security

package com.abccompany.security;

import java.util.Map;

import org.springframework.flex.security3.AuthenticationResultUtils;

public class Security3Helper {

public Map getAuthentication() {
return AuthenticationResultUtils.getAuthenticationResult();
}
}
Labels: , , ,

Post a Comment