Wednesday, December 9, 2015

Geocoding address with Google Maps API

<script src="https://maps.googleapis.com/maps/api/js?sensor=false">
<script>
var geocoder;
function geocodeAddr(address) {
 if(geocoder == 'undefined' || geocoder == null) {
  geocoder = new google.maps.Geocoder();
 }
 geocoder.geocode( { 'address': address}, function(results, status) {
 if (status == google.maps.GeocoderStatus.OK) {
  alert('Latitude: ' + results[0].geometry.location.lat());
  alert('Longitude: ' + results[0].geometry.location.lng());
 } else {
   alert('Geocode was not successful for the following reason: ' + status);
 }
  });
}
</script>

<input type="button" onclick="geocodeAddr('Cameron Highlands Resort,By The Golf Course, 39000 Tanah Rata,Cameron Highlands, Pahang, Malaysia')" />

With the geocodeAddr function above, the latitude and longitude of the given address will be alerted.


Done!!

Saturday, October 24, 2015

getElementById with PrimeFaces

To get a HTML component in javascript, normally we use getElementById('componentId'),
But there are 2 constraints to get PrimeFaces component.

1. the id defined in component tag is not the actual element id in HTML.
to resolve this issue, we can use the javascript API provided by PrimeFaces to retrieve the actual HTML element id.
#{p:component('componentId')}

2. quote in the above API breaks the quote in getElementById
to resolve this problem, the quote in PF's API need to be replaced.

document.getElementById('#{p:component(&quot;componentId&quot;)}')


Done!!

Complex Native SQL to JPA entity

Assuming there is a ContactDetail entity.
This could be a database table or table view.


@Entity public class ContactDetail {     private String name;     private String address;     private String phoneNo;     private String mobileNo;     private String email;     // getter and setter
}

the direct way to get this entity is to execute the following sql.

select name, address, phone_no, mobile_no, email from contact_detail;

another way is to select those column from different tables. 
eg. 

select p.name as name, a.address as address, b.phoneNo as phoneNo, b.mobileNo as mobileNo, c.email as email from person p join address a on a.p_id = p.p_id join phone b on b.p_id = p.p_id join email c on c.p_id = p.p_id;

where the entity columns are actually come from different table. 
which could be a view. 
or SQL to return a table entity based on business requirement.

after gotten the correct SQL, we can proceed to query the JPA with the native SQL.

List<ContactDetail > customers = (List<ContactDetail>)em.createNativeQuery(YOUR_NATIVE_SQL, ContactDetail.class).getResultList();


Done!!

Tuesday, August 18, 2015

How to customize date display in PrimeFaces Calendar component

There is a scenario where the requirement wants the calendar component to enable certain date only.
eg, enable 1st day of the month only.

To achieve this requirement, attribute beforeShowDay need to be used.
Description for beforeShowDay in PrimeFaces tag document.
Callback to execute before displaying a date, used to customize date display.

Steps:
1. Write the callback function to be called by the Calendar component.
function beforeShowDayCallback(date){
  if (date.getDate() == 1) {
    return [true, ''];
  }
  return [false, ''];
}

2. Call the function in calendar component.
<p:calendar id="gstEffDateId"  beforeShowDay="beforeShowDayCallback"  />


Done!!

Tuesday, August 11, 2015

Synchronize Class List problem when integrating JBoss with eclipseLink

The problem when integrating eclipseLink with JBoss 7.1
the attribute below in persistence.xml is not functioning
<exclude-unlisted-classes>false</exclude-unlisted-classes>

the workaround is to list all the entity class in persistence.xml.
but this is troublesome to developers, every time entities changed. the developers need to re-sync the entity classes again.

To avoid this troublesome step, we need to add additional module into JBoss AS7 to handle this eclipseLink integration.

The official document from JBoss is here.
And steps below are the simplified version of the official document.

1. Download the precompile jar from my drive.
2. Extract and copy the jar to JBoss modules folder
3. add the following line into standalone.xml
    <system-properties>

        <property name="eclipselink.archive.factory"

            value="id.au.ringerc.as7.eclipselinkintegration.JBossArchiveFactoryImpl"/>

    </system-properties>

4. set persistence.xml to exclude classes and remove all synchronized classes.

<exclude-unlisted-classes>false</exclude-unlisted-classes>

p/s: the eclipseLink jar must be 2.4.2 or above


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...