View Javadoc
1   package org.melati.example.contacts;
2   
3   import org.melati.Melati;
4   import org.melati.template.ServletTemplateContext;
5   import org.melati.PoemContext;
6   import org.melati.servlet.Form;
7   import org.melati.servlet.PathInfoException;
8   import org.melati.poem.Column;
9   import org.melati.poem.Field;
10  
11  
12   /**
13    *  Example servlet to search contacts and display them.
14    *
15    **/
16  public class Search extends ContactsServlet {
17    private static final long serialVersionUID = 1L;
18  
19    protected String doTemplateRequest(Melati melati, ServletTemplateContext context)
20       throws Exception {
21  
22      ContactsDatabase db = (ContactsDatabase)melati.getDatabase();
23      String name = Form.getFieldNulled(melati.getServletTemplateContext(),
24      "field_name");
25      Integer category = Form.getIntegerField(melati.getServletTemplateContext(),
26                                                  "field_category");
27      String submit = Form.getFieldNulled(melati.getServletTemplateContext(),
28                                                "submit");
29      Column<String> nameColumn = db.getContactTable().getNameColumn();
30      Column<Integer> contactColumn = db.getContactCategoryTable().getContactColumn();
31      Column<Integer> categoryColumn = db.getContactCategoryTable().getCategoryColumn();
32      context.put("name",new Field<String>(name, nameColumn));
33      context.put("category", new Field<Integer>
34                 (category, categoryColumn));
35  
36      String where = "";
37      if (name != null) where += nameColumn.quotedName() + " = '" + name + "' ";
38      if (category != null) {
39        if (!where.equals("")) where += " AND ";
40        where += "exists (SELECT " + 
41                 db.getContactTable().troidColumn().quotedName() + // "id"
42            " FROM " +  db.getContactCategoryTable().quotedName() +   // "contactcategory " +
43                 " WHERE " + categoryColumn.quotedName() + " = " + category.toString() + 
44                 " AND "  + contactColumn.quotedName() + " =  " + 
45                     db.getContactTable().quotedName() + "." + db.getContactTable().troidColumn().quotedName() +")";
46      }
47      if (submit != null) {
48        context.put("results", db.getContactTable().selection(where));
49      }
50      // The file extension is added by the ServletTemplateEngine
51      return "org/melati/example/contacts/Search";
52    }
53    
54    protected PoemContext poemContext(Melati melati)
55    throws PathInfoException {
56      return poemContextWithLDB(melati,"contacts");
57    }
58  }