View Javadoc
1   package org.melati.example.contacts;
2   
3   import org.melati.Melati;
4   import org.melati.PoemContext;
5   import org.melati.poem.Column;
6   import org.melati.poem.Field;
7   import org.melati.servlet.Form;
8   import org.melati.servlet.PathInfoException;
9   import org.melati.template.ServletTemplateContext;
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 search = Form.getFieldNulled(melati.getServletTemplateContext(),
28          "search");
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 (search != null) {
48        context.put("results", db.getContactTable().selection(where));
49      } else {
50        System.err.println("search not clicked");
51      }
52      // The file extension is added by the ServletTemplateEngine
53      return "org/melati/example/contacts/Search";
54    }
55    
56    protected PoemContext poemContext(Melati melati)
57    throws PathInfoException {
58      return poemContextWithLDB(melati,"contacts");
59    }
60  }