JPQL Single-Point Association

Single-Point Association relationship like Many-To-One and One-To-One eager loads associated table. It causes N+1 Select Problem which will produce performance problem on the application. 1 Select from main query and N Selects for each associated table. N selects are unnecessary when data can be retrieved by a single select statement.

If there are thousands of records, the N+1 select statement could result to 1 Huge SELECT Statement

To fix it, change fetch type to laze
@ManyToOne(cascade={CascadeType.PERSIST},fetch=FetchType.LAZY)

Or use left join fetch in the query to load associated objects eagerly
"select s from Student s left join fetch s.guide"

You can also use batch fetching to improve performance when querying related objects.

Check Leap Year

public boolean isLeapYear() {
    if ((year % 400 == 0 || year % 100 != 0) && (year % 4 == 0)) {
        return true;
    }
    return false;
}

JPQL: Custom TypedQuery

//returns custom type 
public List<TourScheduleWithClientCount> searchByDateWithCount(Date from, Date to) {

    String sql = "SELECT NEW com.package.TourScheduleWithClientCount(ts,COUNT(ta.clientId)) FROM TourSchedule ts LEFT JOIN ts.tourAppliedList ta WHERE "
              + (to != null ? "ts.fromDate <= :to AND " : "")
              + (from != null ? "ts.fromDate >= :from AND " : "") 
              + " ts.dFlg = 0 GROUP BY ts.id ORDER BY ts.fromDate DESC";
    Query query = em.createQuery(sql);
    if (from != null) {
      query.setParameter("from", from);
    }
    if (to != null) {
      query.setParameter("to", to);
    }
    return query.getResultList();
}

public class TourScheduleWithClientCount {

    public TourScheduleWithClientCount() {
    }

    public TourScheduleWithClientCount(TourSchedule tourSchedule, Long count) {
        super();
        this.schedule = tourSchedule;
        this.numberOfClients = count;
    }

    private TourSchedule schedule;
    private Long numberOfClients;

    public TourSchedule getSchedule() {
        return schedule;
    }

    public void setSchedule(TourSchedule schedule) {
        this.schedule = schedule;
    }

    public Long getNumberOfClients() {
        return numberOfClients;
    }

    public void setNumberOfClients(Long numberOfClients) {
        this.numberOfClients = numberOfClients;
    }
}

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#JOIN

Customizing Google Calendar Colors

<iframe src="/gcalendar-wrapper.php?src=[google calendar embed url here]" style="border: 0" width="100%" height="600" frameborder="0" scrolling="no"></iframe>

Go to https://calendar.google.com/calendar and get the embed code

Hover to your calendar and click the arrow down symbol. Then click Calendar Settings.
You can find the embed code in Calendar Details

Download the file here https://github.com/tolabl/tsuchiya_HP/blob/master/gcalendar-wrapper.php

Require All Application to use SSL

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());

        //require application to use SSL
        filters.Add(new RequireHttpsAttribute());
    }
}

After that, we need to update project url. Right click on project > properties > web and update Project Url like https://localhost:44331/

Content Delivery Network or CDN

CDN is a network of servers to deliver resources to its nearest users.

When a website is using CDN, any user trying to access its resources will pass through CDN network. The nearest server that has the requested resources will deliver it to the user. Faster load speed.

Since the request didn’t even reach to the server, there’s reduced load to the server making it available to more users.

Below is an example, www.example.com is hosted in x.x.x.x server

Without CDN

>tracert www.example.com

Tracing route to www.example.com [x.x.x.x]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.0.100
  2     1 ms     2 ms     1 ms  10.228.209.86
  3     1 ms     2 ms     2 ms  122.2.174.85.static.pldt.net [122.2.174.85]
  4    14 ms    14 ms    14 ms  210.213.130.186.static.pldt.net [210.213.130.186]
  5    13 ms    13 ms    13 ms  210.213.130.162.static.pldt.net [210.213.130.162]
  6    31 ms    31 ms    31 ms  210.14.2.102
  7    35 ms    35 ms    34 ms  106.187.0.65
  8    77 ms    34 ms    34 ms  hkmjbb001.int-gw.kddi.ne.jp [111.87.5.25]
  9    84 ms    83 ms   100 ms  obpjbb205.int-gw.kddi.ne.jp [118.155.199.193]
 10    80 ms    80 ms    80 ms  jc-osa301.int-gw.kddi.ne.jp [113.157.227.82]
 11    83 ms    83 ms    83 ms  113.157.231.42
 12    84 ms    84 ms    84 ms  oshrt1s-drt2-2.bb.sakura.ad.jp [157.17.146.42]
 13    84 ms    84 ms    83 ms  osnrt2b-hrt1s.bb.sakura.ad.jp [157.17.146.10]
 14    84 ms    84 ms    85 ms  osnrt3c-nrt2b.bb.sakura.ad.jp [157.17.148.2]
 15    80 ms    80 ms    80 ms  210.188.211.108
 16    80 ms    81 ms    81 ms  x.x.x.x

Trace complete.


It took 16 hops for my request to reach www.example.com original server

With CDN

>tracert www.example.com

Tracing route to www.example.com [cdn ip address here]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.0.100
  2     4 ms     2 ms     1 ms  10.228.209.86
  3     2 ms     1 ms     1 ms  122.2.174.89.static.pldt.net [122.2.174.89]
  4    15 ms    14 ms    14 ms  210.213.130.194.static.pldt.net [210.213.130.194]
  5    14 ms    14 ms    14 ms  210.213.130.109.static.pldt.net [210.213.130.109]
  6    14 ms    16 ms    14 ms  210.14.3.102
  7    14 ms    15 ms    14 ms  [cdn ip address here]

  Trace complete.


It only took 7 hops to reach www.example.com using CDN and no request was made to its original server. So faster content delivery and reduced load to the original server.

.htaccess redirect http to https

#check if https is off
RewriteCond %{HTTPS} off
#if the condition above is true, apply this RewriteRule 
#it redirects http to https with 301 or permanent redirect
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#the code below will redirect all domain's non-www to www
#[NC] for case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Note: Search engines treat non-www and www sites as different sites, so for SEO purposes, select either www or non-www version of your site and do permanent redirect to the other site to avoid duplicate content

Parsing WordPress RSS Feed

Tested in content types
Content-Type:application/rss+xml;
Content-Type:text/xml;

$url = 'https://domain.com/blog/?feed=rss2';
$feed1_xml = simplexml_load_file($url) or die("feed not loading");

To get the first post title
echo $feed1_xml->channel->item[0]->title;

To get the first post published date
echo $feed1_xml->channel->item[0]->pubDate

Returned date is in this format Wed, 20 Apr 2016 04:22:10 +0000
You can also convert it to ‘Y/m/d’
date('Y/m/d',strtotime($feed1_xml->channel->item[0]->pubDate));