Home » ATG & Java, Tech Corner

Droplet & A Repository Query

19 May 2010 1 comment Posted By:Sam

How to write a droplet??
How to execute a SELECT query in ATG repository??

A droplet is an ATG concept which is implemented with the help of java. We can say a droplet is complete when the java class and a properties file of the java class are combined. The scope of a droplet is always global. The droplet class extends DynamoServlet. It is same as that of a servlet class in java and it contains a service( ) method also. The two parameters of the service method are of type DynamoHttpServletRequest and DynamoHttpServletResponse. And it throws ServletException and IOException.
Following the previous example of an Employee table (refer), here also we are using the same itemDescriptor (employeeItemDescriptor) and same repository xml, for sake of clarity. Here the droplet is used to get the list of all employees (same names should not get repeated) and display it in dropdown list. A simple select query(select * from employeeTB) is used instead of using (select distinct EName from employeeTB). So the example will address both questions.

Three key parts while addressing the questions are,
1. JSP file where the employee names (EName) are displayed.
2. The droplet class or the java class (employeeNamesDroplet.java).
3. The corresponding properties file of the class (employeeNamesDroplet.properties)

1.JSP File(test.jsp)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<%@ taglib uri="/dspCoreTaglib" prefix="core"%>
<dsp:importbean bean=" Folder Structure/employeeNamesDroplet "/>
<dsp:importbean bean="/atg/dynamo/droplet/ForEach" />
<dsp:page>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Sample Page </title>
</head>
<body>
<%-- page content --%>
<div>
	<div> Employee Name </div><div>
   <dsp:select bean="testFormHandler.eName" id="ddlENAME">
    <dsp:option value="-1" selected="selected" >Select</dsp:option>
    <%--Our droplet --%>
	<dsp:droplet name="folderStructure/employeeNamesDroplet">
        	  <dsp:oparam name="output">
     <%--ATG OUT OF THE BOX DROPLET --%>
      	<dsp:droplet name="/atg/dynamo/droplet/ForEach">
        	 <dsp:param name="array" param="enameslist"/>
	 <dsp:oparam name="empty">
 <dsp:option value="There are No Employee Names in DB"></dsp:option>
	 </dsp:oparam>
	 <dsp:oparam name="output">
<%--eid, employeename are property values from SampleRepositoryXML file --%>
           <dsp:getvalueof id="en" param="element.eid">
	    <dsp:option value="<%=en%>">
	    <dsp:valueof param="element.employeename">
	    </dsp:valueof>
	    </dsp:option>
	 </dsp:getvalueof>
	</dsp:oparam>
        	</dsp:droplet>
       </dsp:oparam>
      </dsp:droplet>
     </dsp:select>
   </div>
</body>
</dsp:page>
</html>

2.“employeeNamesDroplet”

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import atg.repository.Query;
import atg.repository.Repository;
import atg.repository.RepositoryException;
import atg.repository.RepositoryItem;
import atg.repository.RepositoryView;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import atg.servlet.DynamoServlet;

public class employeeNamesDroplet extends DynamoServlet
{
private static final String OUTPUT = "output";
private static final String EMPTY = "empty";
private static final String E_NAMES="enameslist";
private Repository repository;
private String ItemDescriptor;

public void service(DynamoHttpServletRequest DHSRequest,DynamoHttpServletResponse DHSResponse) throws ServletException, IOException
{
   List eNamesList = getNames();
   if (eNamesList.size() > 0)
  {
       	DHSRequest.setParameter(E_NAMES, eNamesList);
         	DHSRequest.serviceParameter(OUTPUT,DHSRequest,DHSResponse);
   } else {
	DHSRequest.serviceParameter(EMPTY,DHSRequest,DHSResponse);
	}
}

private List getNames(){
  List tempNames = new ArrayList();
  List eNames = new ArrayList();
  if(getRepository() != null){
	RepositoryView rView;
	Query query;
	try{
	rView = getRepository().getView(“employeeItemDescriptor”);
	query = (Query) rView.getQueryBuilder().createUnconstrainedQuery();
	RepositoryItem[] rItems = rView.executeQuery(query);
   for(int ri=0; ri < rItems.length; )
   {if(tempNames.contains((String)(rItems[ri].getPropertyValue("employeename")))= = false)
    { tempNames.add((String)(rItems[ri].getPropertyValue("employeename")));
      eNames.add((RepositoryItem)rItems[ri]);
      ri++;
    }else{
 ri++;}
   	 }
}
catch(RepositoryException re)
{
System.out.print(“..”+re);
}
catch(Exception ex)
{System.out.print(“..”+ex);
}
}
return (List)eNames;
}// end of getnames method

public Repository getRepository() {
return repository;
}
public void setRepository(Repository repository) {
this.repository = repository;
}

}// end of the class

3.Properties File of the Droplet(“employeeNamesDroplet.properties”)
$class=droplet. employeeNamesDroplet
$scope=global
Repository = Give the name of your repository here.

Related posts:

  1. ForEach Droplet & its usage in DSP
  2. How to Use RQL in JAVA,ATG
  3. ATG Tag Library:DSP Tags
  4. ATG Repository Concepts
  5. Understanding-ATG(The e-Commerce Framework)

One Comment »

  • Style Leaver said:

    you are actually excellent. The contents are masterpiece. you’ve done a excellent process on this subject!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>