Pages

Monday, April 19, 2010

Using block to invoke client Javascript code in Tapestry 5

In my previous post  I've been able to create a self updating tapestry zone.But its only halfway of what i want to do. Although I already give 2 button to start and stop the timer, i want it to be more automatic.Simply said, the server need to send a response that will trigger the stopTimer js function when the crawling process is finished.

To do this I'm using 2 block. The first block contain the regular content that will be returned from the server. While the second block contains the content and an embedded javascript that will invoke the stopTimer function in the client browser. This is the changes I made to my previous post.

The Code

Template Code :
        <t:zone t:id="infoZone" t:update="show">   
        </t:zone><br/>
       
        <t:block id="info">
            Updating Message ....
            <p t:type="OutputRaw" t:value="${message}">
                Text Output
            </p>
        </t:block>

        <t:block id="infoWithScript">
            Update Stopped
            <p t:type="OutputRaw" t:value="${message}">
                Text Output
            </p>
            <script type="text/javascript">
                stopTimer();
            </script>
        </t:block>

    <a t:type="actionlink" t:id="crawl" href="#">Start Crawl</a> &nbsp;

In the template code I'm adding the two block named "info" and "infoWithScript". The block will not be rendered by default. And also I added another actionlink that will change the state in the server and start the crawling process.

Changes in java code :
    @Inject
    private Block _info;
   
    @Inject
    private Block _infoWithScript;

    Object onActionFromRefreshZone() {
        if(crawler.getCrawlStatus() == Crawler.CRAWL_STARTED) {
            crawler.setCrawlStatus(Crawler.CRAWL_CRAWLING);
            crawler.updateData(URL);
        }
       
        if(crawler.getCrawlStatus() == Crawler.CRAWL_END) {
            crawler.setCrawlStatus(Crawler.CRAWL_IDLE);
            return _infoWithScript;
        }
        return _info;
    }

    void onActionFromCrawl() {
        crawler.setCrawlStatus(Crawler.CRAWL_STARTED);
    }

    public String getMessage() {
        return ProgressNotifier.getMessage();
    }

In the java code we add the two block that will be return by onActionFromRefrezhZone. This method will check if the crawl status in our services class is changed to started, then it will execute update data (the method that will invoke the real process and add messages to the ProgressNotifier).

At the end of the updateData method, the crawl status will be set to CRAWL_END. So the onActionFromRefreshZone will return the block that contain the embedded js to turn off the timer. And return the status to the original CRAWL_IDLE state.

Nothing changes in the client js script.

~FD

Friday, April 16, 2010

Ajax Zone with Interval in Tapestry 5

I'm planning to create a zone that will keep requesting data from the server at certain interval. Seems simple enough to do in Tapestry 5 right ?

But because of my lack of understanding about Ajax and Javascript concept, I've venture to many place to make it. ZoneUpdate, Progressive Display is a few things that i look a bit deep. Trying to understand their inner work, just to find out in which part could I add this simple code.

After a day struggling, i found out that my original idea work just fine. Yeah when its about web, ajax and javascript, I SUCKS ! BIG TIME !

So here's the code to make it work, so no other newbie would get lost like I Do.


The Code

I'm using the sample code from Zone Component in Tapestry 5.
Here's the template page :

<body>
<h1> Crawl Ajax </h1>
<h2> Timer Zone </h2>
   
    <div style="margin-left: 50px">
        <t:zone t:id="time2zone">
            time2:  ${time2}
        </t:zone><br/>

        <a t:type="actionlink" t:id="refreshZone" href="#"
            t:zone="time2zone">Refresh time2 </a>
            <br/><br/>

    </div>
       
    <input type="button" onclick="startTimer()" value="Start Timer" /> <br/>   
    <input type="button" onclick="stopTimer()" value="Stop Timer" /> <br/>   

</body>

Here's the java class code :

    @InjectComponent
    private Zone _time2Zone;

    // The code
   
    void onActionFromRefreshPage() {
        // Nothing to do - the page will call getTime1() and getTime2() as it renders.
    }

    // Isn't called if the link is clicked before the DOM is fully loaded. See
    // https://issues.apache.org/jira/browse/TAP5-1 .
    Object onActionFromRefreshZone() {
        // Here we can do whatever updates we want, then return the content we want rendered.
        return _time2Zone.getBody();
    }

    public Date getTime2() {
        return new Date();
    }
   
    Object onChangeOfTimerZone() {
        return _time2Zone.getBody();
    }

And finally, Here's the Java Script code :

<script type="text/javascript">

var timerId;
var linkId='refreshZone';

function updateMyZone() {
    alert("Update Zone!" + linkId);
    var actionLink = $(linkId);
    Tapestry.findZoneManager( actionLink ).updateFromURL( actionLink.href );
}

function startTimer() {
    alert('yohoo');
    timerId = window.setInterval('updateMyZone()', 2000);
}

function stopTimer() {
    clearInterval (timerId);
}
</script>

As you can see i only add 2 input button to start and stop the timer. The button will invoke the javascript function that uses setInterval that will invoke updateMyZone every 2 second. The updateMyZone will emulate the actionLink behaviour to update the zone.

Bleah its 10+ line of code and it took me the whole day. *still angry and ashamed with my self*

~FD

It happened again !

Okay, some reminder for the future. I don't know how many simple tapestry project I've made, but definitely more than a few. Yet i keep falling into the same hole. I keep missing some minor detail when creating a new project. And this time I'm facing a weird problem.

My project run fine in my development environment (eclipse WTP, tomcat, windows). But when ever i deploy it to my linux server. The page didn't show , it keep getting exception :

Page xxx did not generate any markup when rendered. This could be because its template file could not be located, or because a render phase method in the page prevented rendering.
 First I thought it was Case Sensitive problem. The name of the template (.tml) didn't match up to the class page. Although tapestry is case insensitive in many other things unfortunately this is something that T5 couldn't control. To bad that wasn't the problem :(

And then when i try to rebuild my project, clean, rebuild the class, and refreshing the project tree on eclipse explorer. You need to refresh the tree to make sure new/old/deleted file in the project will be the same with the wtp tomcat temp folder. The temp folder usually located in :
<your_workspace>/.metadata/.plugins/org.eclipse.wst.server.core/tmpX
 After rebuilding I found out that my local project also has the same problem, so it isn't about case sensitive. It turn out the source of my problem is this :

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
while the correct one is :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
Tapestry 5 could be very strict about this stuff, and no IDE support for Tapestry 5 certainly didn't help :( There are times when i build the html page from scratch just to test simple case, i forgot to ade the xmlns and Tapestry would just sprout some error.

It's a great framework no doubt. Years in front of its competitor. But it has its own perks *meh* Hope this can be a gentle reminder for me in the future.

~FD

Tuesday, March 23, 2010

One foot at the entrace

If you remember my september break down and what i think about my future, finally I've arrived to that point. Its a few days sooner than i expected but now I'm in front of the gate into a new world. I'm glad how it turn out. But first i need to finish few things first :)

Here's my check list for this week :
  • clean up my room and computer 
  • backup my work to dvd
  • start jogging, and what i can get with 5 days of regular road work
  • Finish my tax
  • create my first apps
  • commemorate today's event with another post on my blog :D

Monday, February 15, 2010

Akar dari frustasi

Hm.. sepertinya gw udah menemukan sedikit pencerahan, kenapa belakangan ini gw begitu mudah frustasi.

1. Sifat gw yang ingin melakukan banyak hal sendirian, setelah berhasil baru membicarakannya ke orang-orang. Ini merupakan proses pembelajaran bagi diri gw, kalau berhasil tapi dikasih tahu orang, rasanya pengetahuannya gak lengket. Makanya lebih suka untuk coba sendiri.

Jeleknya, disaat mentok, coba belok dikit mentok lagi, balik arah masih tetep mentok. Jadinya frustasi berlebihan. Padahal kalau tanya sama rekan, bisa diberikan jalan. Jadinya buang waktu berkutat ingin mengetahui semua hal kecil yang gak penting yang bikin sesuatu itu berjalan.

Agak susah dirubah sih, karena ini sifatnya kepribadian dan cara belajar. Tapi setidaknya ada hal2 di kantor yang membuat mau tidak mau, bertanya ke orang lain :D

2. Terasing dari sang khalik. Karena lingkungan kerja yang bisa di bilang agak kebarat-baratan. Atau setidaknya berakar dari ilmu barat, dimana people dianggap sebagai resource, yang diukur dari kemampuan teknikal skill-nya, kecepatan belajar, pengalaman, dst. Sebagai one of the unit of production. Tanpa diperhatikan hal spiritualnya. Hanya atribut2 teknis yang masuk kedalam KPI dan PE.

Walaupun tetap solat. Tetapi ketika melihat value diri di pekerjaan, bagaimana dinilai, bagaimana hal2 yang memberikan value kepada diri kita. Tetapi lama kelamaan, sifat berserah diri, kesabaran, dan kepemilikan waktu. Mulai tergeser oleh, deadline, harus bisa dan pendakian jenjang karir.

Manusia itu ringkih, lemah dan mudah frustasi. Hanya ketika mempunyai tempat bersandar lah mereka menjadi kuat, pantang menyerah, dan percaya apapun yang akan terjadi pasti akan terjadi. Disitulah peran penting satu kata yang bernama iman.

3. Tidur ! Gw baru baca artikel yang bilang, salah satu efek jelek dari kurang istirahat adalah MUDAH FRUSTASI ! Jadi gaya hidup ini yang tidak membedakan waktu kantor dan waktu dirumah. Bergadang dan kecanduan berlebihan terhadap internet semenjak ada Speedy. Secara langsung berkontribusi besar untuk membuat gw lebih mudah frustasi :D

Good newsnya setelah mengetahui hal-hal ini, gw bisa sedikit berbuat sesuatu. Setidaknya mencoba tidur lebih teratur dengan istirahat yang cukup. Tanpa menyalakan komputer demi download film di malam hari :))

~FD

Friday, February 12, 2010

Reverse proxy in apache 2 karmic koala

It turn out to enable reverse proxy in apache 2 is very simple. All you have to do is enable 2 module using this command :

> sudo a2enmod proxy
> sudo a2enmod proxy_http

modify the virtual host for your sample, the default is in /etc/apache2/sites-enabled/000-default

add this 2 lines for each of your context
ProxyPass /itsme/ http://www.mysite.net:8080/itsme/
ProxyPassReverse /itsme/ http://www.mysite.net:8080/itsme/

restart the apache server using
> sudo /etc/init.d/apache2 restart

and its done :D

~FD

Monday, February 8, 2010

Adam 2009

Well, i guess i found a little answer, of what i was feeling all this time. I guess for me, live was not all about collecting money to be rich, or chasing some career climbing the corporate ladder. For me it was all about connection, seeing the people that matter for you the most. Day to day, live in simplicity, enjoying each other company and honesty.

And yes, I've just watched the movie in the title.

~FD