Thursday, July 24, 2014

JPA - JTA or Resouce_Local

Before using JPA, we have to consider to use transaction type as JTA or Resource_Local because it will cause different coding and transaction management.


  • JTA - container-managered entity manager
  1. injected to app automatically by j2ee container
  2. close/rollback transaction automatically without extra coding
  3. transaction propagation supported by j2ee container
  4. multiple data resources are required, such as more than 1 DB, DB and JMS,etc  
  • Resource_Local - application-managed entity manager
  1. can be used in j2ee container, J2SE and web container
  2. need to create entity manager explicitly
  3. need to close entity manager explicitly
  4. NO transaction propagation
But I think we can rely on Spring framework to inject entity manager and support transaction propagation.


  • eliminate DAO or JPA DAO Pattern
There are some discussion on DAO tier since JPA is introduced. I prefer to adopt jpa dao pattern personally.

1. A generic DAO interface

public interface GenericDao <T, PK extends Serializable> {

    PK create(T newInstance);
    T read(PK id);
    void update(T transientObject);
}

2. implement the genericDAO
3. For particular entity, if any new behavior is required
public interface UserDAO extends GenericDao<Person,ID> {
// define new method
List<User> findByName(String name);
}


Reference URL:
http://employees.oneonta.edu/higgindm/sweng/Generic_DAO.htm

Tuesday, December 10, 2013

AngularJS Learning Notes 1 - Views and Routing

For normal html page, if open a URL, server will find out the individual html page to show. But for angularjs, it will dynamically render individual html page as a div for the tag "ng-view".


  • This is a sample folder structure:

/rest
     | - css
     |- img
     |- js
          | - app.js
          |- controllers.js
          |- directives.js
          |- filters.js
          |- services.js
     |- lib
          |- angular
                |- angular.js
     |- partials
          |- home.html
          |- page1.html
          |- page2.html


  • home.html to define ng-view
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>My App</title>
</head>
<body>
 
  <div id="container-main" ng-view> </div>

  <script src="lib/angular/angular.js"></script>
  <script src="js/app.js"></script>
  <script src="js/services.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/filters.js"></script>
  <script src="js/directives.js"></script>
</body>
</html>
  • app.js to define router
'use strict';
//define a module which will be used throughout the article.
var mymodule = angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']);
// configure the router
mymodule.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/home', {templateUrl: 'partials/home.html'});
    $routeProvider.when('/page1', {templateUrl: 'partials/page1.html', controller: 'MyCtrl1'});
    $routeProvider.when('/page2', {templateUrl: 'partials/page2.html'});
    $routeProvider.otherwise({redirectTo: '/home'});
  }]);

  • controllers.js
'use strict';
function MyCtrl1(){}
  • Testing
After deploy to tomcat, open the following URLs to test

http://localhost:8080/rest/#/home
http://localhost:8080/rest/#/page1
http://localhost:8080/rest/#/page2


Reference website:
http://docs.angularjs.org/tutorial/step_07

Monday, July 1, 2013

Portable Git reuses existing ssh key

1) put existing key to c:\Users\<win account>\.ssh
2) run git-bash.bat from portable git folder
3) run command 'exec ssh-agent bash'
4) run command 'ssh-add'

then ssh-add will load the key file from c:\Users\<win account>\.ssh\id_rsa

Wednesday, April 3, 2013

spring eclipse jetty maven setup

1) setup jetty in your web app project


                       <plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.10.v20130312</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>${project.artifactId}</contextPath>
<descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
<jettyEnvXml>${basedir}/src/test/resources/jetty-env.xml</jettyEnvXml>
</webApp>
<scanTargets>
<scanTarget>src/main/java</scanTarget>
<scanTarget>src/main/resources</scanTarget>
<scanTarget>src/main/webapp</scanTarget>
</scanTargets>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/test/resources</directory>
<includes>
<include>jetty*.xml</include>
</includes>
<excludes>
<exclude></exclude>
</excludes>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
</plugin>

2) setup jndi in jetty-env.xml


<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure id='wac' class="org.mortbay.jetty.plugin.JettyWebAppContext">

<New id="mysql" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref id="wac"/></Arg>
      <Arg>jdbc/abcd</Arg>
      <Arg>
        <New class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://localhost:3306/abcddb</Set>
<Set name="username">user1</Set>
<Set name="password">pwd123</Set>
</New>
      </Arg>
    </New>
 
</Configure>

and, in web.xml


<resource-ref>
<description>JNDI config to access MySQL Database</description>
<res-ref-name>jdbc/abcd</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

3) db config in spring


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/cache
         http://www.springframework.org/schema/cache/spring-cache.xsd">

<aop:aspectj-autoproxy />

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/abcd</value>
</property>
</bean>
<context:component-scan base-package="com.yourproject.dao" />
<!-- We tell Spring that we are using annotations -->
<context:annotation-config />

<!-- switches on the @transactional behavior -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="packagesToScan" value="com.yourproject.dao" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>

</beans>



Reference:
http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin



 

Thursday, March 21, 2013

jQuery Masonry Tips

jQuery Masonry : http://masonry.desandro.com/index.html

If call ajax and render each elements dynamically by javascrit, need to call 'appended' method. but sometimes the elements only to be shown on single column.
if need to show elements with multiple columns,
set width first
$("#div").masonry({columnWidth: 310});


 var box = renderSingleElement(item);
 var $singlebox = $(box);
 $("#div_single_element").append($singlebox).masonry( 'appended', $singlebox);

take note if set column width as 310, the width of single element should not be greater than 285, otherwise the elements still be in one column, not multiple columns, so take care to set the element width and column width.


expose URL to access local files on server


Sometimes, we need to access local files on server via HTTP URL. If the files are under /webapp/<project>, take tomcat as an example, it is very east to access them via relative path. If the files are outside of tomcat or other web app servers, how to resolve it?

1) create soft link at /var/www/html/<your root context>


ln -s <file location on server> imgupload


2) modify apache setting

<Directory />
Options FollowSymlinks
Allow from all
</Directory>

refer to http://httpd.apache.org/docs/2.2/mod/core.html

3) then on web page
<img src="/imgupload/<subfolder if have>/abc.jpg" />

Tuesday, March 5, 2013

use JSTL to render image blob

1) on DB level, use byte[] to present image

private byte[] image

2) on servlet level, encode byte[] with Base64

String imageBase64=Base64.encode(image);

3)

<img src="data:image/jpg;base64, ${base64Image} "  height="152" width="267"/>