Chomper Stomping jQuery/JavaScript/CSS 3/HTML 5, Java/PHP/Python/ActionScript, Git, Chrome/Firefox Extensions, Wordpress/Game/iPhone App Development and other random techie tidbits I've collected

24May/115

Creating ATG Droplets and serving a default oparam

java-logo

Creating your own ATG droplets is not difficult. Servicing a default open parameter (oparam) in an ATG droplet is surprisingly extremely easy.

ATG has these things called "droplets" that you use from within your "dsp" tag library. You can --nay-- you SHOULD make your own droplets. You should have as little Java code in your JSP pages as humanly possible.

To do this takes a few steps:

  1. Create the .java file

    Create it under /modules/base/src/java/com/yourlibrary/

  2. Extend "DynamoServlet"

    The inside of your Java file should look something like this (to begin):

    package com.yourlibrary;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import atg.nucleus.naming.ParameterName;
    import atg.repository.*;
    import atg.servlet.*;

    public class YourDroplet extends DynamoServlet{
    @Override
    public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
    throws ServletException, IOException
    {
    }
    }

  3. Create the .properties file

    Create it under /modules/base/config/yourlibrary/

  4. Define your properties


    $class=com.yourlibrary.YourDroplet
    $scope=global
    $description=For documentation purposes only

    global scope makes it so there is only 1 instance of the droplet per JVM. You can do session or request scope as well. This is all server admin magic voodoo. I can give you no recommendations here, that's a topic for it's own entire post

  5. Implement the droplet

    Create a jsp page somehwere (probably something like /modules/base/j2ee-apps/base/web-app.war) if you haven't already, and then add this code:


    <%@ taglib uri="/dspTaglib" prefix="dsp"%>
    <dsp:importbean bean="/yourlibrary/YourDroplet" />
    <dsp:page>
    <dsp:droplet name="YourDroplet">
    </dsp:droplet>
    </dsp:page>

OK, at this point you have the skeleton for your droplet created. Go ahead and burn incense, sacrafice a chicken and compile and run and see if it worked. If not, don't ask for help here, I'm just a poor Java hating front-end dev trying to document an insane procress. Go to StackOverflow.com instead.

Now, let's make it serve a purpose. Let's say you just want, like, the user name or something:

Your .java file


package com.yourlibrary;

import java.io.IOException;
import javax.servlet.ServletException;
import atg.nucleus.naming.ParameterName;
import atg.repository.*;
import atg.servlet.*;

public class YourDroplet extends DynamoServlet{
@Override
public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
throws ServletException, IOException
{
String username = null;

username = "do whatever you have to do to get the username and put that here";

request.setParameter("username", username);
request.serviceLocalParameter("ousername", request, response);
}
}

Your .jsp file


<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<dsp:importbean bean="/yourlibrary/YourDroplet" />
<dsp:page>
<dsp:droplet name="YourDroplet">
<dsp:oparam name="ousername">
<dsp:valueof param="username" />
</dsp:oparam>
</dsp:droplet>
</dsp:page>

Now, let's say that some of your users are admins, and in some places you want to have a special thing that happens when the user is an admin, but in others, just the normal thing happens no matter what. You can use a default for this. Here's how:

Your .java file


package com.yourlibrary;

import java.io.IOException;
import javax.servlet.ServletException;
import atg.nucleus.naming.ParameterName;
import atg.repository.*;
import atg.servlet.*;

public class YourDroplet extends DynamoServlet{
@Override
public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
throws ServletException, IOException
{
String username = null;
boolean handled = false;
boolean isadmin = false;

username = "/*do whatever you have to do to get the username and put that here*/";
isadmin = /*find out if they are admin and set this boolean]*/;

request.setParameter("username", username);
if(isadmin){
handled = request.serviceLocalParameter("oadmin", request, response);
}

if(!handled){
request.serviceLocalParameter("ouser", request, response);
}
}
}

Your .jsp file


<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<dsp:importbean bean="/yourlibrary/YourDroplet" />
<dsp:page>
<!--Special case for admin-->
<dsp:droplet name="YourDroplet">
<dsp:oparam name="oadmin">
Admin <dsp:valueof param="username" />
</dsp:oparam>
<dsp:oparam name="ouser">
<dsp:valueof param="username" />
</dsp:oparam>
</dsp:droplet>

<!--No special case for admin, admin falls back on ouser-->
<dsp:droplet name="YourDroplet">
<dsp:oparam name="ouser">
<dsp:valueof param="username" />
</dsp:oparam>
</dsp:droplet>
</dsp:page>

You can take this to any level you want. Here is a more complex basic example:

Your .java file


package com.yourlibrary;

import java.io.IOException;
import javax.servlet.ServletException;
import atg.nucleus.naming.ParameterName;
import atg.repository.*;
import atg.servlet.*;

public class YourDroplet extends DynamoServlet{
@Override
public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
throws ServletException, IOException
{
String username = null;
boolean handled = false;
boolean isadmin = false;
boolean ismoderator = false;

username = "/*do whatever you have to do to get the username and put that here*/";
isadmin = /*find out if they are admin and set this boolean]*/;
ismoderator = /*find out if they are mod and set this boolean]*/;

request.setParameter("username", username);
if(isadmin){
handled = request.serviceLocalParameter("oadmin", request, response);
}elseif(ismoderator){
handled = request.serviceLocalParameter("omoderator", request, response);
}else{
handled = request.serviceLocalParameter("ouser", request, response);
}

if(!handled){
request.serviceLocalParameter("default", request, response);
}
}
}

Your .jsp file


<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<dsp:importbean bean="/yourlibrary/YourDroplet" />
<dsp:page>
<!--Each special case is used-->
<dsp:droplet name="YourDroplet">
<dsp:oparam name="oadmin">
Admin <dsp:valueof param="username" />
</dsp:oparam>
<dsp:oparam name="omoderator">
Moderator <dsp:valueof param="username" />
</dsp:oparam>
<dsp:oparam name="ouser">
User <dsp:valueof param="username" />
</dsp:oparam>
</dsp:droplet>

<!--No special case for admin or moderator, they fall back on default-->
<dsp:droplet name="YourDroplet">
<dsp:oparam name="ouser">
User <dsp:valueof param="username" />, you are not permitted here. Begone!
</dsp:oparam>
<dsp:oparam name="default">
Welcome <dsp:valueof param="username" />. Access granted!
</dsp:oparam>
</dsp:droplet>
</dsp:page>

Comments (5) Trackbacks (0)
  1. Hey,

    Thank you! Your explanation helped me a lot!

  2. hey can you please tell me how a droplet can be created for accessing values from a user profile.Like for example we want to access the shipping address of a user in atg control center.

  3. Wow, I have absolutely no idea. Better ask on stackoverflow.com. I’m actually not doing ATG development anymore. Sorry :(

  4. Goutam,
    U have to use FormHandlers for user profiles.

  5. You have to use FormHandlers for user profile


Leave a comment

No trackbacks yet.