1 package com.atlassian.plugin.web.descriptors;
2
3 import com.atlassian.plugin.PluginParseException;
4 import com.atlassian.plugin.elements.ResourceDescriptor;
5 import com.atlassian.plugin.hostcontainer.HostContainer;
6 import com.atlassian.plugin.module.ModuleFactory;
7 import com.atlassian.plugin.web.model.EmbeddedTemplateWebPanel;
8 import com.atlassian.plugin.web.model.ResourceTemplateWebPanel;
9 import com.atlassian.plugin.web.model.WebPanel;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Predicate;
12 import com.google.common.base.Supplier;
13 import com.google.common.collect.Iterables;
14 import org.apache.commons.lang.StringUtils;
15
16 import java.util.Iterator;
17
18
19
20
21
22
23
24
25
26
27 class WebPanelSupplierFactory
28 {
29 private final WebPanelModuleDescriptor webPanelModuleDescriptor;
30 private final HostContainer hostContainer;
31 private final ModuleFactory moduleFactory;
32
33 public WebPanelSupplierFactory(
34 WebPanelModuleDescriptor webPanelModuleDescriptor,
35 HostContainer hostContainer, ModuleFactory moduleFactory)
36 {
37 this.webPanelModuleDescriptor = webPanelModuleDescriptor;
38 this.hostContainer = hostContainer;
39 this.moduleFactory = moduleFactory;
40 }
41
42 public Supplier<WebPanel> build(final String moduleClassName)
43 {
44 if (moduleClassName != null)
45 {
46
47 return new Supplier<WebPanel>()
48 {
49 public WebPanel get()
50 {
51 return moduleFactory.createModule(moduleClassName, webPanelModuleDescriptor);
52 }
53 };
54 }
55
56 final ResourceDescriptor resource = getRequiredViewResource();
57 final String filename = resource.getLocation();
58 if (StringUtils.isNotEmpty(filename))
59 {
60
61 return new Supplier<WebPanel>()
62 {
63 public WebPanel get()
64 {
65 final ResourceTemplateWebPanel panel = hostContainer.create(ResourceTemplateWebPanel.class);
66 panel.setResourceFilename(filename);
67 panel.setResourceType(getRequiredResourceType(resource));
68 panel.setPlugin(webPanelModuleDescriptor.getPlugin());
69 return panel;
70 }
71 };
72 }
73
74
75
76 final String body = Preconditions.checkNotNull(resource.getContent());
77 return new Supplier<WebPanel>()
78 {
79 public WebPanel get()
80 {
81 final EmbeddedTemplateWebPanel panel = hostContainer.create(EmbeddedTemplateWebPanel.class);
82 panel.setTemplateBody(body);
83 panel.setResourceType(getRequiredResourceType(resource));
84 panel.setPlugin(webPanelModuleDescriptor.getPlugin());
85 return panel;
86 }
87 };
88 }
89
90
91
92
93
94
95 private ResourceDescriptor getRequiredViewResource() throws PluginParseException
96 {
97 final Iterable<ResourceDescriptor> resources = Iterables.filter(webPanelModuleDescriptor.getResourceDescriptors(), new Predicate<ResourceDescriptor>()
98 {
99 public boolean apply(final ResourceDescriptor input)
100 {
101 return "view".equals(input.getName());
102 }
103 });
104 final Iterator<ResourceDescriptor> iterator = resources.iterator();
105 if (!iterator.hasNext())
106 {
107 throw new PluginParseException("Required resource with name 'view' does not exist.");
108 }
109 else
110 {
111 return iterator.next();
112 }
113 }
114
115 private String getRequiredResourceType(final ResourceDescriptor resource)
116 {
117 final String type = resource.getType();
118 if (StringUtils.isEmpty(type))
119 {
120 throw new PluginParseException("Resource element is lacking a type attribute.");
121 }
122 else
123 {
124 return type;
125 }
126 }
127 }