KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > util > OqlUtil


1 package com.genimen.djeneric.web.util;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Map.Entry;
6
7 import com.genimen.djeneric.repository.DjDomain;
8 import com.genimen.djeneric.repository.DjExtent;
9 import com.genimen.djeneric.repository.DjOql;
10 import com.genimen.djeneric.repository.exceptions.DjenericException;
11
12 public class OqlUtil
13 {
14   public static DjOql createOql(DjenericSessionManager sessionManager, DjExtent extent, Map JavaDoc parameters)
15       throws DjenericException
16   {
17     DjOql oql = sessionManager.getPrimarySession().createOql(extent);
18
19     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
20     Iterator JavaDoc it = parameters.entrySet().iterator();
21     boolean first = true;
22     while (it.hasNext())
23     {
24       Map.Entry JavaDoc entry = (Entry) it.next();
25       String JavaDoc name = entry.getKey().toString();
26       
27       // Skip properties starting with a '.'
28
if(name.startsWith(".")) continue;
29       
30       Object JavaDoc v = entry.getValue();
31       if (v == null) v = "";
32       String JavaDoc value;
33
34       if (v instanceof String JavaDoc) value = (String JavaDoc) v;
35       else if (v instanceof String JavaDoc[]) value = ((String JavaDoc[]) v)[0];
36       else throw new DjenericException("Unsupported type of parameter: " + v.getClass().getName());
37
38       if (!"nodeid".equals(name) && value.trim().length() > 0)
39       {
40         if (!first) sb.append("&&");
41         first = false;
42
43         if (oql.getExtent().getProperty(name).getTypeCode() == DjDomain.STRING_TYPE && value.indexOf('%') != -1)
44         {
45           sb.append(name);
46           sb.append(" like ");
47         }
48         else
49         {
50           sb.append(name);
51           sb.append(getOperator(value));
52         }
53         sb.append(":_");
54         sb.append(name);
55         oql.setParameter("_" + name, stripOperator(value));
56       }
57     }
58     oql.setWhereExpression(sb.toString());
59     return oql;
60   }
61
62   public static String JavaDoc stripOperator(String JavaDoc value)
63   {
64     String JavaDoc result = value;
65     value = value.trim();
66     if (value.startsWith(">=")) result = value.substring(2).trim();
67     else if (value.startsWith("<=")) result = value.substring(2).trim();
68     else if (value.startsWith("!=")) result = value.substring(2).trim();
69     else if (value.startsWith("<>")) result = value.substring(2).trim();
70     else if (value.startsWith(">")) result = value.substring(1).trim();
71     else if (value.startsWith("<")) result = value.substring(1).trim();
72     return result;
73   }
74
75   public static String JavaDoc getOperator(String JavaDoc value)
76   {
77     String JavaDoc result = "==";
78     value = value.trim();
79     if (value.startsWith(">=")) result = ">=";
80     else if (value.startsWith("<=")) result = "<=";
81     else if (value.startsWith("!=")) result = "!=";
82     else if (value.startsWith("<>")) result = "!=";
83     else if (value.startsWith(">")) result = ">";
84     else if (value.startsWith("<")) result = "<";
85     return result;
86   }
87
88 }
89
Popular Tags