Skip to Content

[In my previous blog | Ajaxify Your Portal : Restart Reminder (part 1)], I explained my approach to create a server restart reminder via AJAX  request and we saw the basis for the request part. In this blog, we will complete the application, create the datasource part(only a text file ๐Ÿ™‚  and extend the functionality of the portal application.

Lets start with the response part-the datasource. My datasource will simply be a text file that can be reached via URL, and its content will be the date time format (dd.MM.yyyy HH:mm:ss). Of course you can put whatever code you like for returning the results from  servlets, jsp’s, php’s etc.

For reaching the text file via http-URL, I created an alias in the HTTP provider service in Visual Administrator and mapped it to my folder. So my D: b will be reached via http://

:

/bb and I will put the text file there.

!https://weblogs.sdn.sap.com/weblogs/images/251782120/reminder_.jpg|height=261|alt=image|width=506|src=https://weblogs.sdn.sap.com/weblogs/images/251782120/reminder_.jpg|border=0!

Inside the application, this URL should be checked frequently; therefore I use the setInterval function of the javascript. So my component code will write a code like setInterval(“checkRestartProcedure()”, NO_OF_MILLISECONDS);  in javascript and rest of the process will continue within this javascript. It gets the restart time, and if needed warn the user with an alert. Within NO_OF_MILLISECONDS milliseconds another check will be made.

 

In order to increase the usability of application, I made some of the parameters dynamic and  I created six parameters that are read from the iview properties. They are the alert message, the minutes text message(and whether this info will be given or not), the frequency to check the existence of the restart time and the frequency of the alert to popup to the user and the URL of the AJAX call. In order to do this, I put them in my portal-app.xml and they become iview parameters. I read them in my component code and passed them to the javascript.

image

Operations javascript code :

     //defaults

     var AJAX_TIME_STR;

     var WARNING_FREQ_SEC = 900;

     var COOKIE_LAST_WARN_KEY = “bb-last-warn-db”;

     var AJAX_DATE;

     var LAST_WARNING_HOWMANYMINUTES;

     var MINZ_DESCR_TEXT = “dakika”;

     var SHOW_MINZ = false;

     var MSG = “”;

     var RECALL_AJAX = true;//if error turnoff flag

     function calcMin(str){

          return (parseInt(str)/(1000*60)).toFixed(0);

     }

     function convertToLatinDate(mydateStr){

          mydates = mydateStr.split(‘/’);

          return new Date(mydates[1]‘/’mydates[0]‘/’mydates[2]);

     }

               

     function warningProc(targetURL){

          //Get time from restart txt. pass to return function

          //add new date in order to prevent IE from caching

          if(RECALL_AJAX) ajaxify(targetURL“?”new Date(),”POST”,””,”returnFunction”,”handleError”);

          

     }

     

     function createAlertText(msg,showMinz,minzToRestart,minzDescrText){

          alertText = msg;

          minzExtraText = ‘ ‘minzToRestart ‘ ‘+minzDescrText;//21 minutes dakika

          if(showMinz){

               alertText+=minzExtraText;

          }

          return alertText;

     }

     function ifWillCloseThenWarnAndWrite(){

          var NOW = new Date();

          if(NOW < AJAX_DATE){

               //warned and create cookie

               var minzToRestart = calcMin(AJAX_DATE-NOW);

               msg = MSG;

               showMinz = SHOW_MINZ;

               minzDescrText = MINZ_DESCR_TEXT;

               alert(createAlertText(msg,showMinz,minzToRestart,minzDescrText));

               createCookie(COOKIE_LAST_WARN_KEY,NOW,WARNING_FREQ_SEC);

          }

          else{//else do nothing

          }

     }

     function returnFunction(request){

          AJAX_TIME_STR  = request.responseText;

          AJAX_DATE = convertToLatinDate(AJAX_TIME_STR);

          var lastwarn = readCookie(COOKIE_LAST_WARN_KEY);

          if(lastwarn==null){

               ifWillCloseThenWarnAndWrite();

          }

          else{

               LAST_WARNING_HOWMANYMINUTES = calcMin(new Date(readCookie(COOKIE_LAST_WARN_KEY))-new Date())*-1;

          }

     }

     function handleError(request){

          //on error do nothing

          RECALL_AJAX = false;//dont make another ajax request if there is an error like 404

     }

     

     function createCookie(name,value,secondz) {

     if (secondz) {

          var date = new Date();

          date.setTime(date.getTime()+(secondz*1000));

          var expires = “; expires=”+date.toGMTString();

     }

     else var expires = “”;

     document.cookie = name“=”valueexpires“; path=/”;

}

function readCookie(name) {

     var nameEQ = name + “=”;

     var ca = document.cookie.split(‘;’);

     for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==’ ‘) c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,””,-1);
}

Portal Component Code

package tr.name.bb.bbportalutilapp;

import com.sapportals.portal.prt.component.AbstractPortalComponent;

import com.sapportals.portal.prt.component.IPortalComponentContext;

import com.sapportals.portal.prt.component.IPortalComponentProfile;

import com.sapportals.portal.prt.component.IPortalComponentRequest;

import com.sapportals.portal.prt.component.IPortalComponentResponse;

import com.sapportals.portal.prt.resource.IResource;

public class ServerRestartReminder extends AbstractPortalComponent

{

    public void doContent(IPortalComponentRequest request, IPortalComponentResponse response)

    {

          IPortalComponentContext context = request.getComponentContext();

          IPortalComponentProfile profile = context.getProfile();     

          

          IResource js2Resource = request.getResource(IResource.SCRIPT, “scripts/BBAjax.js”);          

          response.include(request, js2Resource);     

          

          IResource jsResource = request.getResource(IResource.SCRIPT, “scripts/ServerRestart.js”);          

          response.include(request, jsResource);

          

          

          String targetURLStr = profile.getProperty(“RestartReminder_ServerRestartFileLocation”);

          String TARGET_URL = “‘”targetURLStr“‘”;//”‘/bb/serverrestart3.txt'”;

          

          String checkfileStr = profile.getProperty(“RestartReminder_CheckRestartFileIntervalInMS”);

          int CHECK_FILE = Integer.parseInt(checkfileStr);//10000;//ms her n saniyede bir dosyaya birsey yazilmis mi diye kontrol et.

          

          String warnPersonStr = profile.getProperty(“RestartReminder_WarnUserIntervalInMS”);

          int WARN_PERSON = Integer.parseInt(warnPersonStr);//2*1000;//ms // her n saniyede bir kullaniciyi uyar.

          String minzDescrText = profile.getProperty(“RestartReminder_MinutesDescriptionText”);

          String MINZ_DESCR_TEXT = “‘”minzDescrText“‘”;//dakika saniye

          

          String alertMsg = profile.getProperty(“RestartReminder_AlertMessageContent”);

          String MSG = “‘”alertMsg“‘”;//Mesaj icerigi.

          

          String showMinz = profile.getProperty(“RestartReminder_ShouldMinuteInfoShown”);

          String SHOW_MINZ = showMinz;//false

          

     

          StringBuffer sb = new StringBuffer(“”);

          sb.append(“”);

String s = sb.toString();

response.write(s);

}

}

For using the component I created an iview from my component, hide its tray etc. and for making the iview available to every page within portal(like I said, a daemon); I put it under my framework page.

image

For testing it, change the date and time in your text file and within adjusted frequencies; bang; you see the restart alert. I will be reminded even I have no activity after logging on the portal like clicking the links, buttons etc. That’s it..

!https://weblogs.sdn.sap.com/weblogs/images/251782120/restartreminder2.jpg|height=162|alt=image|width=319|src=https://weblogs.sdn.sap.com/weblogs/images/251782120/restartreminder2.jpg|border=0!

One last word about the XMLHttpRequest. When I tested my application in both Firefox and IE, I noticed that IE brings the old date time from my text file even if I updated it. That’s a caching issue and in order to prevent this I add a timestamp after the request URL via this code.

 


ajaxify(targetURL“?”new Date() …

 

This code creates a new URL when calling and prevents IE to cache the request and the response.

The power to use these AJAX requests, enriches the browser world, and people create imaginative applications and combine them. If you want to see the examples; just search google for ajax mashups and seehow far they go. May be in my next blogs, I will use these frameworks and applications to continue enriching the portal.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

    1. Former Member Post author
      Hi Michael,

      I organize the blogs so that in the first part, I wrote about how to make a generic AJAX request and relation with the reminder; and second part is about the implementation details of the portal component and iviews.

      (0) 

Leave a Reply