PageRenderTime 44ms CodeModel.GetById 38ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

/sitebricks/src/main/java/com/google/sitebricks/rendering/control/EmbedWidget.java

http://github.com/dhanji/sitebricks
Java | 84 lines | 58 code | 18 blank | 8 comment | 1 complexity | 30f0c7a49fc56ed5871f3faa403968ab MD5 | raw file
 1package com.google.sitebricks.rendering.control;
 2
 3import com.google.inject.Inject;
 4import com.google.inject.Provider;
 5import com.google.sitebricks.Evaluator;
 6import com.google.sitebricks.Renderable;
 7import com.google.sitebricks.Respond;
 8import com.google.sitebricks.compiler.Parsing;
 9import com.google.sitebricks.headless.Request;
10import com.google.sitebricks.routing.PageBook;
11import net.jcip.annotations.Immutable;
12
13import java.io.IOException;
14import java.util.Collections;
15import java.util.Map;
16import java.util.Set;
17
18/**
19 * @author Dhanji R. Prasanna (dhanji@gmail.com)
20 */
21@Immutable
22class EmbedWidget implements Renderable {
23  private final Map<String, String> bindExpressions;
24  private final Map<String, ArgumentWidget> arguments;
25  private final Evaluator evaluator;
26  private final PageBook pageBook;
27  private final String targetPage;
28
29  private EmbeddedRespondFactory factory;
30  private Provider<Request> request;
31
32  public EmbedWidget(Map<String, ArgumentWidget> arguments, String expression,
33                     Evaluator evaluator, PageBook pageBook, String targetPage) {
34    this.arguments = arguments;
35
36    this.evaluator = evaluator;
37    this.pageBook = pageBook;
38    this.targetPage = targetPage.toLowerCase();
39
40    //parse expression list
41    this.bindExpressions = Parsing.toBindMap(expression);
42  }
43
44
45  public void render(Object bound, Respond respond) {
46    PageBook.Page page = pageBook.forName(targetPage);
47
48    //create an instance of the embedded page
49    Object pageObject = page.instantiate();
50
51    //bind parameters to it as necessary
52    for (Map.Entry<String, String> entry : bindExpressions.entrySet()) {
53      evaluator.write(entry.getKey(), pageObject, evaluator.evaluate(entry.getValue(), bound));
54    }
55
56    //chain to embedded page (widget), with arguments
57    EmbeddedRespond embed = factory.get(arguments);
58
59    Request req = request.get();
60    try {
61      page.doMethod(req.method(), pageObject, "", req);
62    } catch (IOException e) {
63      throw new RuntimeException(e);
64    }
65
66    page.widget().render(pageObject, embed);
67
68    //extract and write embedded response to enclosing page's respond
69    respond.writeToHead(embed.toHeadString()); //TODO only write @Require tags
70    respond.write(embed.toString());
71
72    embed.clear();
73  }
74
75  public <T extends Renderable> Set<T> collect(Class<T> clazz) {
76    return Collections.emptySet();
77  }
78
79  @Inject
80  public void init(EmbeddedRespondFactory factory, Provider<Request> request) {
81    this.factory = factory;
82    this.request = request;
83  }
84}