*/
private String createSelect(DFAgentDescription dfdTemplate) throws Exception {
StringBuffer select = new StringBuffer("SELECT dfagentdescr.aid FROM dfagentdescr");
List lAs = new ArrayList();
List lWhere = new ArrayList();
// Name
AID agentAID = dfdTemplate.getName();
if(agentAID != null){
lWhere.add(" dfagentdescr.aid = '"+agentAID.getName()+"'");
}
// Lease time
Date lease = dfdTemplate.getLeaseTime();
long currTime = System.currentTimeMillis();
lWhere.add(" (dfagentdescr.lease = '-1' OR dfagentdescr.lease > '"+currTime+"')");
// Languages
Iterator iter = dfdTemplate.getAllLanguages();
int i=0;
while(iter.hasNext()){
String tmp = LANGUAGE+i;
lAs.add(", language "+tmp);
lWhere.add(tmp+".language='"+(String)iter.next()+"'");
lWhere.add(tmp+".descrid=dfagentdescr.id");
i++;
}
// Ontologies
iter = dfdTemplate.getAllOntologies();
i = 0;
while(iter.hasNext()){
String tmp = ONTOLOGY+i;
lAs.add(", ontology "+tmp);
lWhere.add(tmp+".ontology='"+(String)iter.next()+"'");
lWhere.add(tmp+".descrid=dfagentdescr.id");
i++;
}
// Protocols
iter = dfdTemplate.getAllProtocols();
i = 0;
while(iter.hasNext()){
String tmp = PROTOCOL+i;
lAs.add(", protocol "+tmp);
lWhere.add(tmp+".protocol='"+(String)iter.next()+"'");
lWhere.add(tmp+".descrid=dfagentdescr.id");
i++;
}
// Services
iter = dfdTemplate.getAllServices();
i = 0;
while(iter.hasNext()){
ServiceDescription service = (ServiceDescription)iter.next();
String serviceName = service.getName();
String serviceType = service.getType();
String serviceOwner = service.getOwnership();
// Service name, type and ownership
String tmp = SERVICE+i;
lAs.add(", service "+tmp);
if(serviceName != null){
lWhere.add(tmp+".sname='"+serviceName+"'");
}
if(serviceType != null){
lWhere.add(tmp+".stype='"+serviceType+"'");
}
if(serviceOwner != null){
lWhere.add(tmp+".sownership='"+serviceOwner+"'");
}
lWhere.add(tmp+".descrid=dfagentdescr.id");
i++;
// Service languages
Iterator iterS = service.getAllLanguages();
int j = 0;
while(iterS.hasNext()){
String tmp1 = SERVICELANGUAGE+j;
lAs.add(", servicelanguage "+tmp1);
lWhere.add(tmp1+".language='"+(String)iterS.next()+"'");
lWhere.add(tmp1+".serviceid="+tmp+".id");
j++;
}
// Service ontologies
iterS = service.getAllOntologies();
j = 0;
while(iterS.hasNext()){
String tmp1 = SERVICEONTOLOGY+j;
lAs.add(", serviceontology "+tmp1);
lWhere.add(tmp1+".ontology='"+(String)iterS.next()+"'");
lWhere.add(tmp1+".serviceid="+tmp+".id");
j++;
}
// Service protocols
iterS = service.getAllProtocols();
j = 0;
while(iterS.hasNext()){
String tmp1 = SERVICEPROTOCOL+j;
lAs.add(", serviceprotocol "+tmp1);
lWhere.add(tmp1+".protocol='"+(String)iterS.next()+"'");
lWhere.add(tmp1+".serviceid="+tmp+".id");
j++;
}
// Service properties
iterS = service.getAllProperties();
j = 0;
while(iterS.hasNext()){
String tmp1 = SERVICEPROPERTY+j;
lAs.add(", serviceproperty "+tmp1);
Property prop = (Property) iterS.next();
if (prop.getName() != null)
lWhere.add(tmp1+".propkey='"+prop.getName()+"'");
Object value = prop.getValue();
if (value != null) {
if (needSerialization(value)) {
String hashStr = getHashValue(prop.getValue());
lWhere.add(tmp1+".propvalhash='"+ hashStr +"'");
}
else {
lWhere.add(tmp1+".propval_str='"+ value +"'");
}
}
lWhere.add(tmp1+".serviceid="+tmp+".id");
j++;
}
}
// Concatenate all the aliases
iter = lAs.iterator();
while (iter.hasNext()) {
select.append((String) iter.next());
}
// Concatenate all WHERE
if (lWhere.size() > 0) {
select.append(" WHERE ");
}
iter = lWhere.iterator();
i = 0;
while (iter.hasNext()) {
if(i > 0) {
select.append(" and ");
}